From 96690c796f43009b1aea12add816b62a233d1dc6 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Tue, 9 May 2023 14:45:33 +0200 Subject: [PATCH] test: extend bindings tests to include native methods --- bindings/c/libjsonnet_test_file.c | 24 ++++++++++++++++++++++++ bindings/test.jsonnet | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 bindings/test.jsonnet diff --git a/bindings/c/libjsonnet_test_file.c b/bindings/c/libjsonnet_test_file.c index 084603dd..26d174ef 100644 --- a/bindings/c/libjsonnet_test_file.c +++ b/bindings/c/libjsonnet_test_file.c @@ -16,6 +16,23 @@ limitations under the License. #include "libjsonnet.h" +typedef struct JsonnetJsonValue* val_t; +typedef struct JsonnetVm* vm_t; + +typedef struct native_ctx_t { + vm_t vm +} native_ctx_t; + +val_t native_add(void* nctx, const struct JsonnetJsonValue* const* argv, int* success) { + native_ctx_t* ctx = nctx; + double a; + double b; + jsonnet_json_extract_number(ctx->vm, argv[0], &a); + jsonnet_json_extract_number(ctx->vm, argv[1], &b); + *success = 1; + return jsonnet_json_make_number(ctx->vm, a + b); +} + int main(int argc, const char **argv) { int error; @@ -26,6 +43,12 @@ int main(int argc, const char **argv) return EXIT_FAILURE; } vm = jsonnet_make(); + + native_ctx_t* native_ctx = malloc(sizeof(native_ctx_t)); + native_ctx->vm = vm; + const char* params[3] = {"a", "b", NULL}; + jsonnet_native_callback(vm, "nativeAdd", native_add, native_ctx, params); + output = jsonnet_evaluate_file(vm, argv[1], &error); if (error) { fprintf(stderr, "%s", output); @@ -35,6 +58,7 @@ int main(int argc, const char **argv) } printf("%s", output); jsonnet_realloc(vm, output, 0); + free(native_ctx); jsonnet_destroy(vm); return EXIT_SUCCESS; } diff --git a/bindings/test.jsonnet b/bindings/test.jsonnet new file mode 100644 index 00000000..0f6303f0 --- /dev/null +++ b/bindings/test.jsonnet @@ -0,0 +1,5 @@ +{ + a: 1, + b: "hello", + c: std.native("nativeAdd")(2, 3), +}