git.delta.rocks / jrsonnet / refs/commits / 96690c796f43

difftreelog

test extend bindings tests to include native methods

Yaroslav Bolyukin2023-05-09parent: #2e3471b.patch.diff
in: master

2 files changed

modifiedbindings/c/libjsonnet_test_file.cdiffbeforeafterboth
1616
17#include "libjsonnet.h"17#include "libjsonnet.h"
18
19typedef struct JsonnetJsonValue* val_t;
20typedef struct JsonnetVm* vm_t;
21
22typedef struct native_ctx_t {
23 vm_t vm
24} native_ctx_t;
25
26val_t native_add(void* nctx, const struct JsonnetJsonValue* const* argv, int* success) {
27 native_ctx_t* ctx = nctx;
28 double a;
29 double b;
30 jsonnet_json_extract_number(ctx->vm, argv[0], &a);
31 jsonnet_json_extract_number(ctx->vm, argv[1], &b);
32 *success = 1;
33 return jsonnet_json_make_number(ctx->vm, a + b);
34}
1835
19int main(int argc, const char **argv)36int main(int argc, const char **argv)
20{37{
27 }44 }
28 vm = jsonnet_make();45 vm = jsonnet_make();
46
47 native_ctx_t* native_ctx = malloc(sizeof(native_ctx_t));
48 native_ctx->vm = vm;
49 const char* params[3] = {"a", "b", NULL};
50 jsonnet_native_callback(vm, "nativeAdd", native_add, native_ctx, params);
51
29 output = jsonnet_evaluate_file(vm, argv[1], &error);52 output = jsonnet_evaluate_file(vm, argv[1], &error);
30 if (error) {53 if (error) {
35 }58 }
36 printf("%s", output);59 printf("%s", output);
37 jsonnet_realloc(vm, output, 0);60 jsonnet_realloc(vm, output, 0);
61 free(native_ctx);
38 jsonnet_destroy(vm);62 jsonnet_destroy(vm);
39 return EXIT_SUCCESS;63 return EXIT_SUCCESS;
40}64}
addedbindings/test.jsonnetdiffbeforeafterboth

no changes