I would like to print the function call stack in the fatal handler of the duktape:
void duktape_fatal(void *d, const char *m)
{
int line, pc, level;
const char *fnName = NULL;
printf("JSE fatal: %s!\n", (m ? m : ""));
level = -1;
while (1)
{
duk_inspect_callstack_entry(ctx, level);
if (duk_is_undefined(ctx, -1))
{
duk_pop(ctx);
break;
}
duk_get_prop_string(ctx, -1, "function");
fnName = duk_to_string(ctx, -1);
duk_pop(ctx);
duk_get_prop_string(ctx, -1, "lineNumber");
line = duk_to_int(ctx, -1);
duk_pop(ctx);
duk_get_prop_string(ctx, -3, "pc");
pc = duk_to_int(ctx, -1);
duk_pop(ctx);
duk_pop(ctx);
printf("Trace %i,%i: %s\n", pc, line, fnName);
level--;
}
while (1)
{
;
}
}
And this is an example JS script that triggers the fatal error:
function test3() {
var b = 1;
print(a);
}
function test2() {
test3();
}
function test() {
test2();
}
test();
In this example, the fatal function will print only the fault lineNumber, but not the call stack. Any help is appreciated.