Hi so I am looking at duktape, but I can't seem to find a simple example which does something like:
- pre compile some js.
- pass that js some input e.g. strings and numbers and run it.
- get the result of that js.
It seems the examples go from here is how to evaluate js to here is how to call C functions from js, maybe I missed it.
Well it seems after bashing over the API doc and trial and error I eventually got something like:
duk_context *ctx = duk_create_heap_default();
duk_eval_string(ctx, "(function helloWorld(a,b) { return a.includes(b); })");
duk_dump_function(ctx);
int i;
for(i = 0; i < 10; i++) {
duk_dup_top(ctx); // I don't know why but it seems needed
duk_load_function(ctx); /* [ ... bytecode ] -> [ ... function ] */
duk_push_string(ctx, "the");
duk_push_string(ctx, "th");
duk_call(ctx, 2);
duk_bool_t res = duk_get_boolean(ctx, -1);
if(res) {
printf("You got it!\n");
} else {
printf("yeah nah!\n");
}
duk_pop(ctx);
}
Although I notice that if the JS has an error I get a seg fault, maybe I am meant to check something?
Also is this the correct way to cache the JS?