Skip to content

Commit

Permalink
test: extend bindings tests to include native methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed May 9, 2023
1 parent 2e3471b commit 96690c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions bindings/c/libjsonnet_test_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
5 changes: 5 additions & 0 deletions bindings/test.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
a: 1,
b: "hello",
c: std.native("nativeAdd")(2, 3),
}

0 comments on commit 96690c7

Please sign in to comment.