I managed to emulate Duktape's exception model, where JS errors can be thrown directly from C code. Here's a test function:
static int
js_foo(int num_args, bool is_ctor)
{
const char* str;
int num;
printf("foo() called with %d args\n", num_args);
str = jsal_require_string(1);
num = jsal_require_int(2);
printf("%s %d\n", str, num);
return 0;
}
jsal_init();
jsal_push_global_object();
jsal_push_function(js_foo, "foo", 0);
jsal_set_named_property(-2, "foo");
jsal_pop(1);
jsal_push_eval("foo('pigs!', '812');");
jsal_uninit();
Output:
foo() called with 2 args
JSAL exception thrown from unguarded C code!
-> TypeError: '812' is not a number
The second printf in foo() is not reached because jsal_require_int(2) throws an error first. This will make it possible to just plug in JSAL calls in place of Duktape ones with very little refactoring.