Support context dump safely in duktape

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
This commit is contained in:
Daniel Silverstone 2019-05-06 15:09:39 +01:00
parent d719bdcee3
commit 378383ea3b
2 changed files with 24 additions and 2 deletions

View File

@ -669,6 +669,8 @@ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv)
duk_pop(CTX);
/* ... */
dukky_log_stack_frame(CTX, "New compartment created");
return (jsobject *)ctx;
}
@ -698,8 +700,8 @@ static void dukky_dump_error(duk_context *ctx)
{
/* stack is ..., errobj */
duk_idx_t stacktop = duk_get_top(ctx);
if (!duk_is_error(ctx, stacktop - 1)) {
NSLOG(dukky, INFO, "Uncaught non-Error derived error in JS: %s", duk_safe_to_string(ctx, stacktop - 1));
if (!duk_is_error(ctx, -1)) {
NSLOG(dukky, INFO, "Uncaught non-Error derived error in JS: %s", duk_safe_to_string(ctx, -1));
} else {
#define GETTER(what) \
if (duk_has_prop_string(ctx, stacktop - 1, what)) { \
@ -761,6 +763,23 @@ void dukky_push_generics(duk_context *ctx, const char *generic)
/* ..., generic */
}
static duk_int_t dukky_push_context_dump(duk_context *ctx, void *udata)
{
duk_push_context_dump(ctx);
return 1;
}
void dukky_log_stack_frame(duk_context *ctx, const char * reason)
{
if (duk_safe_call(ctx, dukky_push_context_dump, NULL, 0, 1) != 0) {
duk_pop(ctx);
duk_push_string(ctx, "[???]");
}
NSLOG(dukky, DEEPDEBUG, "%s, stack is: %s", reason, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
}
/* exported interface documented in js.h */
bool
js_exec(jscontext *ctx, const uint8_t *txt, size_t txtlen, const char *name)

View File

@ -54,4 +54,7 @@ duk_int_t dukky_pcall(duk_context *ctx, duk_size_t argc, bool reset_timeout);
/* Push a generics function onto the stack */
void dukky_push_generics(duk_context *ctx, const char *generic);
/* Log the current stack frame if possible */
void dukky_log_stack_frame(duk_context *ctx, const char * reason);
#endif