Debian Codegraph @

include/libjsonnet.h

raw
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 LIB_JSONNET_H
18 #define LIB_JSONNET_H
19
20 #include <stddef.h>
21
22 /** \file This file is a library interface for evaluating Jsonnet. It is written in C++ but exposes
23 * a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example
24 * of using the library.
25 */
26
27 /** The version string of the Jsonnet interpreter.
28 *
29 * This is currently grepped out of this file by setup.py, Makefile, and CMakeLists.txt so be aware
30 * of that when making changes.
31 *
32 * If this isn't the same as jsonnet_version() then you've got a mismatched binary / header.
33 */
34 #define LIB_JSONNET_VERSION "v0.22.0"
35
36 /** Return the version string of the Jsonnet interpreter. Conforms to semantic versioning
37 * https://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between
38 * header and compiled library.
39 */
40 const char *jsonnet_version(void);
41
42 /** Jsonnet virtual machine context. */
43 struct JsonnetVm;
44
45 /** Create a new Jsonnet virtual machine. */
46 struct JsonnetVm *jsonnet_make(void);
47
48 /** Set the maximum stack depth. */
49 void jsonnet_max_stack(struct JsonnetVm *vm, unsigned v);
50
51 /** Set the number of objects required before a garbage collection cycle is allowed. */
52 void jsonnet_gc_min_objects(struct JsonnetVm *vm, unsigned v);
53
54 /** Run the garbage collector after this amount of growth in the number of objects. */
55 void jsonnet_gc_growth_trigger(struct JsonnetVm *vm, double v);
56
57 /** Expect a string as output and don't JSON encode it. */
58 void jsonnet_string_output(struct JsonnetVm *vm, int v);
59
60 /** Enable/disable trailing newline in manifested/string output. */
61 void jsonnet_set_trailing_newline(struct JsonnetVm *vm, int enable);
62
63 /** Callback used to load imports.
64 *
65 * The returned char* should be allocated with jsonnet_realloc. It will be cleaned up by
66 * libjsonnet when no-longer needed.
67 *
68 * \param ctx User pointer, given in jsonnet_import_callback.
69 * \param base The directory containing the code that did the import.
70 * \param rel The path imported by the code.
71 * \param found_here Set this byref param to path to the file, absolute or relative to the
72 * process's CWD. This is necessary so that imports from the content of the imported file can
73 * be resolved correctly. Allocate memory with jsonnet_realloc. Only use when *success = 1.
74 * \param success Set this byref param to 1 to indicate success and 0 for failure.
75 * \param buf Set this byref param to the content of the imported file, or an error message. Allocate memory with jsonnet_realloc. Do not include a null terminator byte.
76 * \param buflen Set this byref param to the length of the data returned in buf.
77 * \returns 0 to indicate success and 1 for failure. On success, the content is in *buf. On failure, an error message is in *buf.
78 */
79 typedef int JsonnetImportCallback(void *ctx, const char *base, const char *rel,
80 char **found_here, char **buf, size_t *buflen);
81
82 /** An opaque type which can only be utilized via the jsonnet_json_* family of functions.
83 */
84 struct JsonnetJsonValue;
85
86 /** If the value is a string, return it as UTF8 otherwise return NULL.
87 */
88 const char *jsonnet_json_extract_string(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
89
90 /** If the value is a number, return 1 and store the number in out, otherwise return 0.
91 */
92 int jsonnet_json_extract_number(struct JsonnetVm *vm, const struct JsonnetJsonValue *v,
93 double *out);
94
95 /** Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.
96 */
97 int jsonnet_json_extract_bool(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
98
99 /** Return 1 if the value is null, else 0.
100 */
101 int jsonnet_json_extract_null(struct JsonnetVm *vm, const struct JsonnetJsonValue *v);
102
103 /** Convert the given UTF8 string to a JsonnetJsonValue.
104 */
105 struct JsonnetJsonValue *jsonnet_json_make_string(struct JsonnetVm *vm, const char *v);
106
107 /** Convert the given double to a JsonnetJsonValue.
108 */
109 struct JsonnetJsonValue *jsonnet_json_make_number(struct JsonnetVm *vm, double v);
110
111 /** Convert the given bool (1 or 0) to a JsonnetJsonValue.
112 */
113 struct JsonnetJsonValue *jsonnet_json_make_bool(struct JsonnetVm *vm, int v);
114
115 /** Make a JsonnetJsonValue representing null.
116 */
117 struct JsonnetJsonValue *jsonnet_json_make_null(struct JsonnetVm *vm);
118
119 /** Make a JsonnetJsonValue representing an array.
120 *
121 * Assign elements with jsonnet_json_array_append.
122 */
123 struct JsonnetJsonValue *jsonnet_json_make_array(struct JsonnetVm *vm);
124
125 /** Add v to the end of the array.
126 */
127 void jsonnet_json_array_append(struct JsonnetVm *vm, struct JsonnetJsonValue *arr,
128 struct JsonnetJsonValue *v);
129
130 /** Make a JsonnetJsonValue representing an object with the given number of fields.
131 *
132 * Every index of the array must have a unique value assigned with jsonnet_json_array_element.
133 */
134 struct JsonnetJsonValue *jsonnet_json_make_object(struct JsonnetVm *vm);
135
136 /** Add the field f to the object, bound to v.
137 *
138 * This replaces any previous binding of the field.
139 */
140 void jsonnet_json_object_append(struct JsonnetVm *vm, struct JsonnetJsonValue *obj, const char *f,
141 struct JsonnetJsonValue *v);
142
143 /** Clean up a JSON subtree.
144 *
145 * This is useful if you want to abort with an error mid-way through building a complex value.
146 */
147 void jsonnet_json_destroy(struct JsonnetVm *vm, struct JsonnetJsonValue *v);
148
149 /** Callback to provide native extensions to Jsonnet.
150 *
151 * The returned JsonnetJsonValue* should be allocated with jsonnet_realloc. It will be cleaned up
152 * along with the objects rooted at argv by libjsonnet when no-longer needed. Return a string upon
153 * failure, which will appear in Jsonnet as an error. The argv pointer is an array whose size
154 * matches the array of parameters supplied when the native callback was originally registered.
155 *
156 * \param ctx User pointer, given in jsonnet_native_callback.
157 * \param argv Array of arguments from Jsonnet code.
158 * \param success Set this byref param to 1 to indicate success and 0 for failure.
159 * \returns The content of the imported file, or an error message.
160 */
161 typedef struct JsonnetJsonValue *JsonnetNativeCallback(void *ctx,
162 const struct JsonnetJsonValue *const *argv,
163 int *success);
164
165 /** Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will
166 * only return NULL if sz was zero.
167 *
168 * \param buf If NULL, allocate a new buffer. If an previously allocated buffer, resize it.
169 * \param sz The size of the buffer to return. If zero, frees the buffer.
170 * \returns The new buffer.
171 */
172 char *jsonnet_realloc(struct JsonnetVm *vm, char *buf, size_t sz);
173
174 /** Override the callback used to locate imports.
175 */
176 void jsonnet_import_callback(struct JsonnetVm *vm, JsonnetImportCallback *cb, void *ctx);
177
178 /** Register a native extension.
179 *
180 * This will appear in Jsonnet as a function type and can be accessed from std.nativeExt("foo").
181 *
182 * DO NOT register native callbacks with side-effects! Jsonnet is a lazy functional language and
183 * will call your function when you least expect it, more times than you expect, or not at all.
184 *
185 * \param vm The vm.
186 * \param name The name of the function as visible to Jsonnet code, e.g. "foo".
187 * \param cb The PURE function that implements the behavior you want.
188 * \param ctx User pointer, stash non-global state you need here.
189 * \param params NULL-terminated array of the names of the params. Must be valid identifiers.
190 */
191 void jsonnet_native_callback(struct JsonnetVm *vm, const char *name, JsonnetNativeCallback *cb,
192 void *ctx, const char *const *params);
193
194 /** Bind a Jsonnet external var to the given string.
195 *
196 * Argument values are copied so memory should be managed by caller.
197 */
198 void jsonnet_ext_var(struct JsonnetVm *vm, const char *key, const char *val);
199
200 /** Bind a Jsonnet external var to the given code.
201 *
202 * Argument values are copied so memory should be managed by caller.
203 */
204 void jsonnet_ext_code(struct JsonnetVm *vm, const char *key, const char *val);
205
206 /** Bind a string top-level argument for a top-level parameter.
207 *
208 * Argument values are copied so memory should be managed by caller.
209 */
210 void jsonnet_tla_var(struct JsonnetVm *vm, const char *key, const char *val);
211
212 /** Bind a code top-level argument for a top-level parameter.
213 *
214 * Argument values are copied so memory should be managed by caller.
215 */
216 void jsonnet_tla_code(struct JsonnetVm *vm, const char *key, const char *val);
217
218 /** Set the number of lines of stack trace to display (0 for all of them). */
219 void jsonnet_max_trace(struct JsonnetVm *vm, unsigned v);
220
221 /** Add to the default import callback's library search path.
222 *
223 * The search order is last to first, so more recently appended paths take precedence.
224 * Note this is not an isolation mechanism or a way to restrict access to the file
225 * system. Imports can directly refer to absolute paths, or traverse 'up' the
226 * filesystem with '..' paths. They are not limited to paths below the specified
227 * search paths.
228 */
229 void jsonnet_jpath_add(struct JsonnetVm *vm, const char *v);
230
231 /** Evaluate a file containing Jsonnet code, return a JSON string.
232 *
233 * The returned string should be cleaned up with jsonnet_realloc.
234 *
235 * \param filename Path to a file containing Jsonnet code.
236 * \param error Return by reference whether or not there was an error.
237 * \returns Either JSON or the error message.
238 */
239 char *jsonnet_evaluate_file(struct JsonnetVm *vm, const char *filename, int *error);
240
241 /** Evaluate a string containing Jsonnet code, return a JSON string.
242 *
243 * The returned string should be cleaned up with jsonnet_realloc.
244 *
245 * \param filename Path to a file (used in error messages).
246 * \param snippet Jsonnet code to execute.
247 * \param error Return by reference whether or not there was an error.
248 * \returns Either JSON or the error message.
249 */
250 char *jsonnet_evaluate_snippet(struct JsonnetVm *vm, const char *filename, const char *snippet,
251 int *error);
252
253 /** Evaluate a file containing Jsonnet code, return a number of named JSON files.
254 *
255 * The returned character buffer contains an even number of strings, the filename and JSON for each
256 * JSON file interleaved. It should be cleaned up with jsonnet_realloc.
257 *
258 * \param filename Path to a file containing Jsonnet code.
259 * \param error Return by reference whether or not there was an error.
260 * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
261 */
262 char *jsonnet_evaluate_file_multi(struct JsonnetVm *vm, const char *filename, int *error);
263
264 /** Evaluate a string containing Jsonnet code, return a number of named JSON files.
265 *
266 * The returned character buffer contains an even number of strings, the filename and JSON for each
267 * JSON file interleaved. It should be cleaned up with jsonnet_realloc.
268 *
269 * \param filename Path to a file containing Jsonnet code.
270 * \param snippet Jsonnet code to execute.
271 * \param error Return by reference whether or not there was an error.
272 * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
273 */
274 char *jsonnet_evaluate_snippet_multi(struct JsonnetVm *vm, const char *filename,
275 const char *snippet, int *error);
276
277 /** Evaluate a file containing Jsonnet code, return a number of JSON files.
278 *
279 * The returned character buffer contains several strings. It should be cleaned up with
280 * jsonnet_realloc.
281 *
282 * \param filename Path to a file containing Jsonnet code.
283 * \param error Return by reference whether or not there was an error.
284 * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
285 */
286 char *jsonnet_evaluate_file_stream(struct JsonnetVm *vm, const char *filename, int *error);
287
288 /** Evaluate a string containing Jsonnet code, return a number of JSON files.
289 *
290 * The returned character buffer contains several strings. It should be cleaned up with
291 * jsonnet_realloc.
292 *
293 * \param filename Path to a file containing Jsonnet code.
294 * \param snippet Jsonnet code to execute.
295 * \param error Return by reference whether or not there was an error.
296 * \returns Either the error, or a sequence of strings separated by \0, terminated with \0\0.
297 */
298 char *jsonnet_evaluate_snippet_stream(struct JsonnetVm *vm, const char *filename,
299 const char *snippet, int *error);
300
301 /** Complement of \see jsonnet_vm_make. */
302 void jsonnet_destroy(struct JsonnetVm *vm);
303
304 #endif // LIB_JSONNET_H