Debian Codegraph @

core/libjsonnet.cpp

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 #include <cerrno>
18 #include <cstdlib>
19 #include <cstring>
20
21 #include <exception>
22 #include <fstream>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26
27 extern "C" {
28 #include "libjsonnet.h"
29 #include "libjsonnet_fmt.h"
30 }
31
32 #include "desugarer.h"
33 #include "formatter.h"
34 #include "json.h"
35 #include "parser.h"
36 #include "static_analysis.h"
37 #include "vm.h"
38 #include "pass.h"
39 #include "string_utils.h"
40
41 namespace {
42 using ::jsonnet::internal::Allocator;
43 using ::jsonnet::internal::AST;
44 using ::jsonnet::internal::FmtOpts;
45 using ::jsonnet::internal::Fodder;
46 using ::jsonnet::internal::jsonnet_lex;
47 using ::jsonnet::internal::jsonnet_string_escape;
48 using ::jsonnet::internal::RuntimeError;
49 using ::jsonnet::internal::StaticError;
50 using ::jsonnet::internal::Tokens;
51 using ::jsonnet::internal::VmExt;
52 using ::jsonnet::internal::VmNativeCallback;
53 using ::jsonnet::internal::VmNativeCallbackMap;
54 using ::jsonnet::internal::CompilerPass;
55 using ::jsonnet::internal::LiteralString;
56
57 // Used in fmtDebugDesugaring mode to ensure the AST can be pretty-printed.
58 class ReEscapeStrings : public CompilerPass {
59 using CompilerPass::visit;
60 public:
61 ReEscapeStrings(Allocator &alloc) : CompilerPass(alloc) {}
62 void visit(LiteralString *lit)
63 {
64 if (lit->tokenKind != LiteralString::RAW_DESUGARED)
65 return;
66
67 // TODO: Share code with formatter.cpp EnforceStringStyle.
68 unsigned num_single = 0, num_double = 0;
69 for (char32_t c : lit->value) {
70 if (c == '\'')
71 num_single++;
72 if (c == '"')
73 num_double++;
74 }
75 bool use_single = (num_double > 0) && (num_single == 0);
76
77 lit->value = jsonnet_string_escape(lit->value, use_single);
78 lit->tokenKind = use_single ? LiteralString::SINGLE : LiteralString::DOUBLE;
79 }
80 };
81 } // namespace
82
83 static void memory_panic(void)
84 {
85 fputs("FATAL ERROR: a memory allocation error occurred.\n", stderr);
86 abort();
87 }
88
89 static char *from_string(JsonnetVm *vm, const std::string &v)
90 {
91 char *r = jsonnet_realloc(vm, nullptr, v.length() + 1);
92 std::memcpy(r, v.c_str(), v.length() + 1);
93 return r;
94 }
95
96 static char *from_string_nonull(JsonnetVm *vm, const std::string &v, size_t *buflen)
97 {
98 char *r = jsonnet_realloc(vm, nullptr, v.length());
99 std::memcpy(r, v.data(), v.length());
100 *buflen = v.length();
101 return r;
102 }
103
104 static int default_import_callback(void *ctx, const char *dir, const char *file,
105 char **found_here_cptr, char **buf, size_t *buflen);
106
107 const char *jsonnet_json_extract_string(JsonnetVm *vm, const struct JsonnetJsonValue *v)
108 {
109 (void)vm;
110 if (v->kind != JsonnetJsonValue::STRING)
111 return nullptr;
112 return v->string.c_str();
113 }
114
115 int jsonnet_json_extract_number(struct JsonnetVm *vm, const struct JsonnetJsonValue *v, double *out)
116 {
117 (void)vm;
118 if (v->kind != JsonnetJsonValue::NUMBER)
119 return 0;
120 *out = v->number;
121 return 1;
122 }
123
124 int jsonnet_json_extract_bool(struct JsonnetVm *vm, const struct JsonnetJsonValue *v)
125 {
126 (void)vm;
127 if (v->kind != JsonnetJsonValue::BOOL)
128 return 2;
129 return v->number != 0;
130 }
131
132 int jsonnet_json_extract_null(struct JsonnetVm *vm, const struct JsonnetJsonValue *v)
133 {
134 (void)vm;
135 return v->kind == JsonnetJsonValue::NULL_KIND;
136 }
137
138 JsonnetJsonValue *jsonnet_json_make_string(JsonnetVm *vm, const char *v)
139 {
140 (void)vm;
141 JsonnetJsonValue *r = new JsonnetJsonValue();
142 r->kind = JsonnetJsonValue::STRING;
143 r->string = v;
144 return r;
145 }
146
147 JsonnetJsonValue *jsonnet_json_make_number(struct JsonnetVm *vm, double v)
148 {
149 (void)vm;
150 JsonnetJsonValue *r = new JsonnetJsonValue();
151 r->kind = JsonnetJsonValue::NUMBER;
152 r->number = v;
153 return r;
154 }
155
156 JsonnetJsonValue *jsonnet_json_make_bool(struct JsonnetVm *vm, int v)
157 {
158 (void)vm;
159 JsonnetJsonValue *r = new JsonnetJsonValue();
160 r->kind = JsonnetJsonValue::BOOL;
161 r->number = v != 0;
162 return r;
163 }
164
165 JsonnetJsonValue *jsonnet_json_make_null(struct JsonnetVm *vm)
166 {
167 (void)vm;
168 JsonnetJsonValue *r = new JsonnetJsonValue();
169 r->kind = JsonnetJsonValue::NULL_KIND;
170 return r;
171 }
172
173 JsonnetJsonValue *jsonnet_json_make_array(JsonnetVm *vm)
174 {
175 (void)vm;
176 JsonnetJsonValue *r = new JsonnetJsonValue();
177 r->kind = JsonnetJsonValue::ARRAY;
178 return r;
179 }
180
181 void jsonnet_json_array_append(JsonnetVm *vm, JsonnetJsonValue *arr, JsonnetJsonValue *v)
182 {
183 (void)vm;
184 assert(arr->kind == JsonnetJsonValue::ARRAY);
185 arr->elements.emplace_back(v);
186 }
187
188 JsonnetJsonValue *jsonnet_json_make_object(JsonnetVm *vm)
189 {
190 (void)vm;
191 JsonnetJsonValue *r = new JsonnetJsonValue();
192 r->kind = JsonnetJsonValue::OBJECT;
193 return r;
194 }
195
196 void jsonnet_json_object_append(JsonnetVm *vm, JsonnetJsonValue *obj, const char *f,
197 JsonnetJsonValue *v)
198 {
199 (void)vm;
200 assert(obj->kind == JsonnetJsonValue::OBJECT);
201 obj->fields[std::string(f)] = std::unique_ptr<JsonnetJsonValue>(v);
202 }
203
204 void jsonnet_json_destroy(JsonnetVm *vm, JsonnetJsonValue *v)
205 {
206 (void)vm;
207 delete v;
208 }
209
210 struct JsonnetVm {
211 double gcGrowthTrigger;
212 unsigned maxStack;
213 unsigned gcMinObjects;
214 unsigned maxTrace;
215 std::map<std::string, VmExt> ext;
216 std::map<std::string, VmExt> tla;
217 JsonnetImportCallback *importCallback;
218 VmNativeCallbackMap nativeCallbacks;
219 void *importCallbackContext;
220 bool stringOutput;
221 bool trailingNewline;
222 std::vector<std::string> jpaths;
223
224 FmtOpts fmtOpts;
225 bool fmtDebugDesugaring;
226
227 JsonnetVm(void)
228 : gcGrowthTrigger(2.0),
229 maxStack(500),
230 gcMinObjects(1000),
231 maxTrace(20),
232 importCallback(default_import_callback),
233 importCallbackContext(this),
234 stringOutput(false),
235 trailingNewline(true),
236 fmtDebugDesugaring(false)
237 {
238 jpaths.emplace_back("/usr/share/jsonnet-" + std::string(jsonnet_version()) + "/");
239 jpaths.emplace_back("/usr/local/share/jsonnet-" + std::string(jsonnet_version()) + "/");
240 }
241 };
242
243 enum ImportStatus { IMPORT_STATUS_OK, IMPORT_STATUS_FILE_NOT_FOUND, IMPORT_STATUS_IO_ERROR };
244
245 static enum ImportStatus try_path(const std::string &dir, const std::string &rel,
246 std::string &content, std::string &found_here,
247 std::string &err_msg)
248 {
249 std::string abs_path;
250 if (rel.length() == 0) {
251 err_msg = "the empty string is not a valid filename";
252 return IMPORT_STATUS_IO_ERROR;
253 }
254 // It is possible that rel is actually absolute.
255 if (rel[0] == '/') {
256 abs_path = rel;
257 } else {
258 abs_path = dir + rel;
259 }
260
261 if (abs_path[abs_path.length() - 1] == '/') {
262 err_msg = "attempted to import a directory";
263 return IMPORT_STATUS_IO_ERROR;
264 }
265
266 std::ifstream f;
267 f.open(abs_path.c_str());
268 if (!f.good())
269 return IMPORT_STATUS_FILE_NOT_FOUND;
270 try {
271 content.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
272 } catch (const std::ios_base::failure &io_err) {
273 err_msg = io_err.what();
274 return IMPORT_STATUS_IO_ERROR;
275 }
276 if (!f.good()) {
277 err_msg = strerror(errno);
278 return IMPORT_STATUS_IO_ERROR;
279 }
280
281 found_here = abs_path;
282
283 return IMPORT_STATUS_OK;
284 }
285
286 static int default_import_callback(void *ctx, const char *dir, const char *file,
287 char **found_here_cptr, char **buf, size_t *buflen)
288 {
289 auto *vm = static_cast<JsonnetVm *>(ctx);
290
291 std::string input, found_here, err_msg;
292
293 ImportStatus status = try_path(dir, file, input, found_here, err_msg);
294
295 std::vector<std::string> jpaths(vm->jpaths);
296
297 // If not found, try library search path.
298 while (status == IMPORT_STATUS_FILE_NOT_FOUND) {
299 if (jpaths.size() == 0) {
300 const char *err = "no match locally or in the Jsonnet library paths.";
301 *buf = from_string_nonull(vm, err, buflen);
302 return 1; // failure
303 }
304 status = try_path(jpaths.back(), file, input, found_here, err_msg);
305 jpaths.pop_back();
306 }
307
308 if (status == IMPORT_STATUS_IO_ERROR) {
309 *buf = from_string_nonull(vm, err_msg, buflen);
310 return 1; // failure
311 }
312
313 assert(status == IMPORT_STATUS_OK);
314 *found_here_cptr = from_string(vm, found_here);
315 *buf = from_string_nonull(vm, input, buflen);
316 return 0; // success
317 }
318
319 #define TRY try {
320 #define CATCH(func) \
321 } \
322 catch (const std::bad_alloc &) \
323 { \
324 memory_panic(); \
325 } \
326 catch (const std::exception &e) \
327 { \
328 std::cerr << "Something went wrong during " func ", please report this: " << e.what() \
329 << std::endl; \
330 abort(); \
331 }
332
333 const char *jsonnet_version(void)
334 {
335 return LIB_JSONNET_VERSION;
336 }
337
338 JsonnetVm *jsonnet_make(void)
339 {
340 TRY
341 return new JsonnetVm();
342 CATCH("jsonnet_make")
343 return nullptr;
344 }
345
346 void jsonnet_destroy(JsonnetVm *vm)
347 {
348 TRY
349 delete vm;
350 CATCH("jsonnet_destroy")
351 }
352
353 void jsonnet_max_stack(JsonnetVm *vm, unsigned v)
354 {
355 vm->maxStack = v;
356 }
357
358 void jsonnet_gc_min_objects(JsonnetVm *vm, unsigned v)
359 {
360 vm->gcMinObjects = v;
361 }
362
363 void jsonnet_gc_growth_trigger(JsonnetVm *vm, double v)
364 {
365 vm->gcGrowthTrigger = v;
366 }
367
368 void jsonnet_string_output(struct JsonnetVm *vm, int v)
369 {
370 vm->stringOutput = bool(v);
371 }
372
373 void jsonnet_set_trailing_newline(struct JsonnetVm *vm, int enable)
374 {
375 vm->trailingNewline = bool(enable);
376 }
377
378 void jsonnet_import_callback(struct JsonnetVm *vm, JsonnetImportCallback *cb, void *ctx)
379 {
380 vm->importCallback = cb;
381 vm->importCallbackContext = ctx;
382 }
383
384 void jsonnet_native_callback(struct JsonnetVm *vm, const char *name, JsonnetNativeCallback *cb,
385 void *ctx, const char *const *params)
386 {
387 std::vector<std::string> params2;
388 for (; *params != nullptr; params++)
389 params2.push_back(*params);
390 vm->nativeCallbacks[name] = VmNativeCallback{cb, ctx, params2};
391 }
392
393 void jsonnet_ext_var(JsonnetVm *vm, const char *key, const char *val)
394 {
395 vm->ext[key] = VmExt(val, false);
396 }
397
398 void jsonnet_ext_code(JsonnetVm *vm, const char *key, const char *val)
399 {
400 vm->ext[key] = VmExt(val, true);
401 }
402
403 void jsonnet_tla_var(JsonnetVm *vm, const char *key, const char *val)
404 {
405 vm->tla[key] = VmExt(val, false);
406 }
407
408 void jsonnet_tla_code(JsonnetVm *vm, const char *key, const char *val)
409 {
410 vm->tla[key] = VmExt(val, true);
411 }
412
413 void jsonnet_fmt_debug_desugaring(JsonnetVm *vm, int v)
414 {
415 vm->fmtDebugDesugaring = v;
416 }
417
418 void jsonnet_fmt_indent(JsonnetVm *vm, int v)
419 {
420 vm->fmtOpts.indent = v;
421 }
422
423 void jsonnet_fmt_max_blank_lines(JsonnetVm *vm, int v)
424 {
425 vm->fmtOpts.maxBlankLines = v;
426 }
427
428 void jsonnet_fmt_string(JsonnetVm *vm, int v)
429 {
430 if (v != 'd' && v != 's' && v != 'l')
431 v = 'l';
432 vm->fmtOpts.stringStyle = v;
433 }
434
435 void jsonnet_fmt_comment(JsonnetVm *vm, int v)
436 {
437 if (v != 'h' && v != 's' && v != 'l')
438 v = 'l';
439 vm->fmtOpts.commentStyle = v;
440 }
441
442 void jsonnet_fmt_pad_arrays(JsonnetVm *vm, int v)
443 {
444 vm->fmtOpts.padArrays = v;
445 }
446
447 void jsonnet_fmt_pad_objects(JsonnetVm *vm, int v)
448 {
449 vm->fmtOpts.padObjects = v;
450 }
451
452 void jsonnet_fmt_pretty_field_names(JsonnetVm *vm, int v)
453 {
454 vm->fmtOpts.prettyFieldNames = v;
455 }
456
457 void jsonnet_fmt_sort_imports(JsonnetVm *vm, int v)
458 {
459 vm->fmtOpts.sortImports = v;
460 }
461
462 void jsonnet_max_trace(JsonnetVm *vm, unsigned v)
463 {
464 vm->maxTrace = v;
465 }
466
467 void jsonnet_jpath_add(JsonnetVm *vm, const char *path_)
468 {
469 if (std::strlen(path_) == 0)
470 return;
471 std::string path = path_;
472 if (path[path.length() - 1] != '/')
473 path += '/';
474 vm->jpaths.emplace_back(path);
475 }
476
477 static char *jsonnet_fmt_snippet_aux(JsonnetVm *vm, const char *filename, const char *snippet,
478 int *error)
479 {
480 try {
481 Allocator alloc;
482 std::string json_str;
483 AST *expr;
484 std::map<std::string, std::string> files;
485 Tokens tokens = jsonnet_lex(filename, snippet);
486
487 expr = jsonnet_parse(&alloc, tokens);
488 Fodder final_fodder = tokens.front().fodder;
489
490 if (vm->fmtDebugDesugaring) {
491 jsonnet_desugar(&alloc, expr, &vm->tla);
492 ReEscapeStrings(alloc).expr(expr);
493 }
494
495 json_str = jsonnet_fmt(expr, final_fodder, vm->fmtOpts);
496
497 *error = false;
498 return from_string(vm, json_str);
499
500 } catch (StaticError &e) {
501 std::stringstream ss;
502 ss << "STATIC ERROR: " << e << std::endl;
503 *error = true;
504 return from_string(vm, ss.str());
505 }
506 }
507
508 char *jsonnet_fmt_file(JsonnetVm *vm, const char *filename, int *error)
509 {
510 TRY
511 std::ifstream f;
512 f.open(filename);
513 if (!f.good()) {
514 std::stringstream ss;
515 ss << "Opening input file: " << filename << ": " << strerror(errno);
516 *error = true;
517 return from_string(vm, ss.str());
518 }
519 std::string input;
520 input.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
521
522 return jsonnet_fmt_snippet_aux(vm, filename, input.c_str(), error);
523 CATCH("jsonnet_fmt_file")
524 return nullptr; // Never happens.
525 }
526
527 char *jsonnet_fmt_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error)
528 {
529 TRY
530 return jsonnet_fmt_snippet_aux(vm, filename, snippet, error);
531 CATCH("jsonnet_fmt_snippet")
532 return nullptr; // Never happens.
533 }
534
535 namespace {
536 enum EvalKind { REGULAR, MULTI, STREAM };
537 } // namespace
538
539 static char *jsonnet_evaluate_snippet_aux(JsonnetVm *vm, const char *filename, const char *snippet,
540 int *error, EvalKind kind)
541 {
542 try {
543 Allocator alloc;
544 AST *expr;
545 Tokens tokens = jsonnet_lex(filename, snippet);
546
547 expr = jsonnet_parse(&alloc, tokens);
548
549 jsonnet_desugar(&alloc, expr, &vm->tla);
550
551 unsigned max_stack = vm->maxStack;
552
553 // For the stdlib desugaring.
554 max_stack++;
555
556 // For the TLA desugaring.
557 max_stack++;
558
559 jsonnet_static_analysis(expr);
560 switch (kind) {
561 case REGULAR: {
562 std::string json_str = jsonnet_vm_execute(&alloc,
563 expr,
564 vm->ext,
565 max_stack,
566 vm->gcMinObjects,
567 vm->gcGrowthTrigger,
568 vm->nativeCallbacks,
569 vm->importCallback,
570 vm->importCallbackContext,
571 vm->stringOutput);
572 if (vm->trailingNewline) {
573 json_str += "\n";
574 }
575 *error = false;
576 return from_string(vm, json_str);
577 } break;
578
579 case MULTI: {
580 std::map<std::string, std::string> files =
581 jsonnet_vm_execute_multi(&alloc,
582 expr,
583 vm->ext,
584 max_stack,
585 vm->gcMinObjects,
586 vm->gcGrowthTrigger,
587 vm->nativeCallbacks,
588 vm->importCallback,
589 vm->importCallbackContext,
590 vm->stringOutput);
591 size_t sz = 1; // final sentinel
592 for (const auto &pair : files) {
593 sz += pair.first.length() + 1; // include sentinel
594 sz += pair.second.length() + 2; // Add a '\n' as well as sentinel
595 }
596 char *buf = (char *)::malloc(sz);
597 if (buf == nullptr)
598 memory_panic();
599 std::ptrdiff_t i = 0;
600 for (const auto &pair : files) {
601 memcpy(&buf[i], pair.first.c_str(), pair.first.length() + 1);
602 i += pair.first.length() + 1;
603 memcpy(&buf[i], pair.second.c_str(), pair.second.length());
604 i += pair.second.length();
605 if (vm->trailingNewline) {
606 buf[i] = '\n';
607 i++;
608 }
609 buf[i] = '\0';
610 i++;
611 }
612 buf[i] = '\0'; // final sentinel
613 *error = false;
614 return buf;
615 } break;
616
617 case STREAM: {
618 if (!vm->trailingNewline) {
619 *error = true;
620 return from_string(vm, "INTERNAL: trailing-newline is required for streamed output");
621 }
622 std::vector<std::string> documents =
623 jsonnet_vm_execute_stream(&alloc,
624 expr,
625 vm->ext,
626 max_stack,
627 vm->gcMinObjects,
628 vm->gcGrowthTrigger,
629 vm->nativeCallbacks,
630 vm->importCallback,
631 vm->importCallbackContext,
632 vm->stringOutput);
633 size_t sz = 1; // final sentinel
634 for (const auto &doc : documents) {
635 sz += doc.length() + 2; // Add a '\n' as well as sentinel
636 }
637 char *buf = (char *)::malloc(sz);
638 if (buf == nullptr)
639 memory_panic();
640 std::ptrdiff_t i = 0;
641 for (const auto &doc : documents) {
642 memcpy(&buf[i], doc.c_str(), doc.length());
643 i += doc.length();
644 buf[i] = '\n';
645 i++;
646 buf[i] = '\0';
647 i++;
648 }
649 buf[i] = '\0'; // final sentinel
650 *error = false;
651 return buf;
652 } break;
653
654 default:
655 fputs("INTERNAL ERROR: bad value of 'kind', probably memory corruption.\n", stderr);
656 abort();
657 }
658
659 } catch (StaticError &e) {
660 std::stringstream ss;
661 ss << "STATIC ERROR: " << e << std::endl;
662 *error = true;
663 return from_string(vm, ss.str());
664
665 } catch (RuntimeError &e) {
666 std::stringstream ss;
667 ss << "RUNTIME ERROR: " << e.msg << std::endl;
668 const long max_above = vm->maxTrace / 2;
669 const long max_below = vm->maxTrace - max_above;
670 const long sz = e.stackTrace.size();
671 for (long i = 0; i < sz; ++i) {
672 const auto &f = e.stackTrace[i];
673 if (vm->maxTrace > 0 && i >= max_above && i < sz - max_below) {
674 if (i == max_above)
675 ss << "\t..." << std::endl;
676 } else {
677 ss << "\t" << f.location << "\t" << f.name << std::endl;
678 }
679 }
680 *error = true;
681 return from_string(vm, ss.str());
682 }
683
684 return nullptr; // Quiet, compiler.
685 }
686
687 static char *jsonnet_evaluate_file_aux(JsonnetVm *vm, const char *filename, int *error,
688 EvalKind kind)
689 {
690 std::ifstream f;
691 f.open(filename);
692 if (!f.good()) {
693 std::stringstream ss;
694 ss << "Opening input file: " << filename << ": " << strerror(errno);
695 *error = true;
696 return from_string(vm, ss.str());
697 }
698 std::string input;
699 input.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
700
701 return jsonnet_evaluate_snippet_aux(vm, filename, input.c_str(), error, kind);
702 }
703
704 char *jsonnet_evaluate_file(JsonnetVm *vm, const char *filename, int *error)
705 {
706 TRY
707 return jsonnet_evaluate_file_aux(vm, filename, error, REGULAR);
708 CATCH("jsonnet_evaluate_file")
709 return nullptr; // Never happens.
710 }
711
712 char *jsonnet_evaluate_file_multi(JsonnetVm *vm, const char *filename, int *error)
713 {
714 TRY
715 return jsonnet_evaluate_file_aux(vm, filename, error, MULTI);
716 CATCH("jsonnet_evaluate_file_multi")
717 return nullptr; // Never happens.
718 }
719
720 char *jsonnet_evaluate_file_stream(JsonnetVm *vm, const char *filename, int *error)
721 {
722 TRY
723 return jsonnet_evaluate_file_aux(vm, filename, error, STREAM);
724 CATCH("jsonnet_evaluate_file_stream")
725 return nullptr; // Never happens.
726 }
727
728 char *jsonnet_evaluate_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error)
729 {
730 TRY
731 return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, REGULAR);
732 CATCH("jsonnet_evaluate_snippet")
733 return nullptr; // Never happens.
734 }
735
736 char *jsonnet_evaluate_snippet_multi(JsonnetVm *vm, const char *filename, const char *snippet,
737 int *error)
738 {
739 TRY
740 return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, MULTI);
741 CATCH("jsonnet_evaluate_snippet_multi")
742 return nullptr; // Never happens.
743 }
744
745 char *jsonnet_evaluate_snippet_stream(JsonnetVm *vm, const char *filename, const char *snippet,
746 int *error)
747 {
748 TRY
749 return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, STREAM);
750 CATCH("jsonnet_evaluate_snippet_stream")
751 return nullptr; // Never happens.
752 }
753
754 char *jsonnet_realloc(JsonnetVm *vm, char *str, size_t sz)
755 {
756 (void)vm;
757 if (str == nullptr) {
758 if (sz == 0)
759 return nullptr;
760 auto *r = static_cast<char *>(::malloc(sz));
761 if (r == nullptr)
762 memory_panic();
763 return r;
764 } else {
765 if (sz == 0) {
766 ::free(str);
767 return nullptr;
768 } else {
769 auto *r = static_cast<char *>(::realloc(str, sz));
770 if (r == nullptr)
771 memory_panic();
772 return r;
773 }
774 }
775 }