Coverage Report

Created: 2025-11-09 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jsonnet/core/vm.h
Line
Count
Source
1
/*
2
Copyright 2015 Google Inc. All rights reserved.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
    http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
#ifndef JSONNET_VM_H
18
#define JSONNET_VM_H
19
20
#include <libjsonnet.h>
21
#include <cstdint>
22
23
#include "ast.h"
24
25
namespace jsonnet::internal {
26
27
/** A single line of a stack trace from a runtime error.
28
 */
29
struct TraceFrame {
30
    LocationRange location;
31
    std::string name;
32
    TraceFrame(const LocationRange &location, const std::string &name = "")
33
780k
        : location(location), name(name)
34
780k
    {
35
780k
    }
36
};
37
38
/** Exception that is thrown by the interpreter when it reaches an error construct, or divide by
39
 * zero, array bounds error, dynamic type error, etc.
40
 */
41
struct RuntimeError {
42
    std::vector<TraceFrame> stackTrace;
43
    std::string msg;
44
    RuntimeError(const std::vector<TraceFrame> stack_trace, const std::string &msg)
45
7.71k
        : stackTrace(stack_trace), msg(msg)
46
7.71k
    {
47
7.71k
    }
48
};
49
50
/** Holds native callback and context. */
51
struct VmNativeCallback {
52
    JsonnetNativeCallback *cb;
53
    void *ctx;
54
    std::vector<std::string> params;
55
};
56
57
typedef std::map<std::string, VmNativeCallback> VmNativeCallbackMap;
58
59
/** Stores external values / code. */
60
struct VmExt {
61
    std::string data;
62
    bool isCode;
63
0
    VmExt() : isCode(false) {}
64
0
    VmExt(const std::string &data, bool is_code) : data(data), isCode(is_code) {}
65
};
66
67
/** Execute the program and return the value as a JSON string.
68
 *
69
 * \param alloc The allocator used to create the ast.
70
 * \param ast The program to execute.
71
 * \param ext The external vars / code.
72
 * \param max_stack Recursion beyond this level gives an error.
73
 * \param gc_min_objects The garbage collector does not run when the heap is this small.
74
 * \param gc_growth_trigger Growth since last garbage collection cycle to trigger a new cycle.
75
 * \param import_callback A callback to handle imports
76
 * \param import_callback_ctx Context param for the import callback.
77
 * \param string_output Whether to expect a string and output it without JSON encoding
78
 * \throws RuntimeError reports runtime errors in the program.
79
 * \returns The JSON result in string form.
80
 */
81
std::string jsonnet_vm_execute(Allocator *alloc, const AST *ast,
82
                               const std::map<std::string, VmExt> &ext, unsigned max_stack,
83
                               double gc_min_objects, double gc_growth_trigger,
84
                               const VmNativeCallbackMap &natives,
85
                               JsonnetImportCallback *import_callback, void *import_callback_ctx,
86
                               bool string_output);
87
88
/** Execute the program and return the value as a number of named JSON files.
89
 *
90
 * This assumes the given program yields an object whose keys are filenames.
91
 *
92
 * \param alloc The allocator used to create the ast.
93
 * \param ast The program to execute.
94
 * \param ext The external vars / code.
95
 * \param tla The top-level arguments (strings or code).
96
 * \param max_stack Recursion beyond this level gives an error.
97
 * \param gc_min_objects The garbage collector does not run when the heap is this small.
98
 * \param gc_growth_trigger Growth since last garbage collection cycle to trigger a new cycle.
99
 * \param import_callback A callback to handle imports
100
 * \param import_callback_ctx Context param for the import callback.
101
 * \param string_output Whether to expect a string and output it without JSON encoding
102
 * \throws RuntimeError reports runtime errors in the program.
103
 * \returns A mapping from filename to the JSON strings for that file.
104
 */
105
std::map<std::string, std::string> jsonnet_vm_execute_multi(
106
    Allocator *alloc, const AST *ast, const std::map<std::string, VmExt> &ext, unsigned max_stack,
107
    double gc_min_objects, double gc_growth_trigger, const VmNativeCallbackMap &natives,
108
    JsonnetImportCallback *import_callback, void *import_callback_ctx, bool string_output);
109
110
/** Execute the program and return the value as a stream of JSON files.
111
 *
112
 * This assumes the given program yields an array whose elements are individual
113
 * JSON files.
114
 *
115
 * \param alloc The allocator used to create the ast.
116
 * \param ast The program to execute.
117
 * \param ext The external vars / code.
118
 * \param tla The top-level arguments (strings or code).
119
 * \param max_stack Recursion beyond this level gives an error.
120
 * \param gc_min_objects The garbage collector does not run when the heap is this small.
121
 * \param gc_growth_trigger Growth since last garbage collection cycle to trigger a new cycle.
122
 * \param import_callback A callback to handle imports
123
 * \param import_callback_ctx Context param for the import callback.
124
 * \param string_output Whether to expect a string and output it without JSON encoding
125
 * \throws RuntimeError reports runtime errors in the program.
126
 * \returns A mapping from filename to the JSON strings for that file.
127
 */
128
std::vector<std::string> jsonnet_vm_execute_stream(
129
    Allocator *alloc, const AST *ast, const std::map<std::string, VmExt> &ext, unsigned max_stack,
130
    double gc_min_objects, double gc_growth_trigger, const VmNativeCallbackMap &natives,
131
    JsonnetImportCallback *import_callback, void *import_callback_ctx, bool string_output);
132
133
}  // namespace jsonnet::internal
134
135
#endif