From ed829a477222f169e4ff0727db920e8029fd0c7d Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 4 May 2019 17:32:17 +0100 Subject: [PATCH] JS: Attribute external JS to the URI it came from in backtraces Signed-off-by: Daniel Silverstone --- content/handlers/html/html_script.c | 13 ++++++++----- content/handlers/javascript/duktape/dukky.c | 8 ++++++-- content/handlers/javascript/js.h | 2 +- content/handlers/javascript/none/none.c | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/content/handlers/html/html_script.c b/content/handlers/html/html_script.c index 6f62fb989..405f6d43c 100644 --- a/content/handlers/html/html_script.c +++ b/content/handlers/html/html_script.c @@ -42,7 +42,7 @@ #include "html/html.h" #include "html/html_internal.h" -typedef bool (script_handler_t)(struct jscontext *jscontext, const char *data, size_t size) ; +typedef bool (script_handler_t)(struct jscontext *jscontext, const char *data, size_t size, const char *name); static script_handler_t *select_script_handler(content_type ctype) @@ -91,10 +91,11 @@ nserror html_script_exec(html_content *c, bool allow_defer) CONTENT_STATUS_DONE) { /* external script is now available */ const char *data; - unsigned long size; + size_t size; data = content_get_source_data( s->data.handle, &size ); - script_handler(c->jscontext, data, size); + script_handler(c->jscontext, data, size, + nsurl_access(hlcache_handle_get_url(s->data.handle))); s->already_started = true; @@ -307,7 +308,8 @@ convert_script_sync_cb(hlcache_handle *script, const char *data; unsigned long size; data = content_get_source_data(s->data.handle, &size ); - script_handler(parent->jscontext, data, size); + script_handler(parent->jscontext, data, size, + nsurl_access(hlcache_handle_get_url(s->data.handle))); } /* continue parse */ @@ -531,7 +533,8 @@ exec_inline_script(html_content *c, dom_node *node, dom_string *mimetype) if (script_handler != NULL) { script_handler(c->jscontext, dom_string_data(script), - dom_string_byte_length(script)); + dom_string_byte_length(script), + "?inline script?"); } return DOM_HUBBUB_OK; } diff --git a/content/handlers/javascript/duktape/dukky.c b/content/handlers/javascript/duktape/dukky.c index e9e72650c..171736338 100644 --- a/content/handlers/javascript/duktape/dukky.c +++ b/content/handlers/javascript/duktape/dukky.c @@ -716,7 +716,7 @@ duk_int_t dukky_pcall(duk_context *ctx, duk_size_t argc, bool reset_timeout) } -bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) +bool js_exec(jscontext *ctx, const char *txt, size_t txtlen, const char *name) { assert(ctx); if (txt == NULL || txtlen == 0) return false; @@ -724,7 +724,11 @@ bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) NSLOG(dukky, DEEPDEBUG, "%zd bytes: %s", txtlen, txt); (void) nsu_getmonotonic_ms(&ctx->exec_start_time); - duk_push_string(CTX, "?unknown source?"); + if (name != NULL) { + duk_push_string(CTX, name); + } else { + duk_push_string(CTX, "?unknown source?"); + } if (duk_pcompile_lstring_filename(CTX, DUK_COMPILE_EVAL, txt, txtlen) != 0) { NSLOG(dukky, INFO, "Failed to compile JavaScript input"); goto handle_error; diff --git a/content/handlers/javascript/js.h b/content/handlers/javascript/js.h index 2929d0b5e..89a87afe3 100644 --- a/content/handlers/javascript/js.h +++ b/content/handlers/javascript/js.h @@ -67,7 +67,7 @@ void js_destroycontext(jscontext *ctx); jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv); /* execute some javascript in a context */ -bool js_exec(jscontext *ctx, const char *txt, size_t txtlen); +bool js_exec(jscontext *ctx, const char *txt, size_t txtlen, const char *name); /* fire an event at a dom node */ diff --git a/content/handlers/javascript/none/none.c b/content/handlers/javascript/none/none.c index 9a8b0a49d..e1c992ebf 100644 --- a/content/handlers/javascript/none/none.c +++ b/content/handlers/javascript/none/none.c @@ -51,7 +51,7 @@ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv) return NULL; } -bool js_exec(jscontext *ctx, const char *txt, size_t txtlen) +bool js_exec(jscontext *ctx, const char *txt, size_t txtlen, const char *name) { return true; }