1#include <stdlib.h>2#include <stdio.h>34#include "libjsonnet.h"56typedef struct JsonnetJsonValue* val_t;7typedef struct JsonnetVm* vm_t;89typedef struct native_ctx_t {10 vm_t vm11} native_ctx_t;1213val_t native_add(void* nctx, const struct JsonnetJsonValue* const* argv, int* success) {14 native_ctx_t* ctx = nctx;15 double a;16 double b;17 jsonnet_json_extract_number(ctx->vm, argv[0], &a);18 jsonnet_json_extract_number(ctx->vm, argv[1], &b);19 *success = 1;20 return jsonnet_json_make_number(ctx->vm, a + b);21}2223int main(int argc, const char **argv)24{25 int error;26 char *output;27 struct JsonnetVm *vm;28 if (argc != 2) {29 fprintf(stderr, "libjsonnet_test_file <file>\n");30 return EXIT_FAILURE;31 }32 vm = jsonnet_make();3334 native_ctx_t* native_ctx = malloc(sizeof(native_ctx_t));35 native_ctx->vm = vm;36 const char* params[3] = {"a", "b", NULL};37 jsonnet_native_callback(vm, "nativeAdd", native_add, native_ctx, params);3839 output = jsonnet_evaluate_file(vm, argv[1], &error);40 if (error) {41 fprintf(stderr, "%s", output);42 jsonnet_realloc(vm, output, 0);43 jsonnet_destroy(vm);44 return EXIT_FAILURE;45 }46 printf("%s", output);47 jsonnet_realloc(vm, output, 0);48 free(native_ctx);49 jsonnet_destroy(vm);50 return EXIT_SUCCESS;51}