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