From 3c49de2d2fcc82518121929513c536cd44d7b0f7 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Thu, 8 Dec 2016 14:20:55 +0200 Subject: [PATCH] Use duk_inspect_callstack_entry() in logging extra --- extras/logging/duk_logging.c | 40 ++++++++++-------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/extras/logging/duk_logging.c b/extras/logging/duk_logging.c index f585d15a..6ef2e8da 100644 --- a/extras/logging/duk_logging.c +++ b/extras/logging/duk_logging.c @@ -46,17 +46,17 @@ static duk_ret_t duk__logger_constructor(duk_context *ctx) { * issue now. */ - /* With the logging framework being an extra now, we need - * Duktape.act() which is stashed to make sure it's available - * here. This is a rather slow approach but it only affects - * logger initialization. - */ - - duk_push_global_stash(ctx); - duk_get_prop_string(ctx, -1, "\xff" "logger:getName"); - duk_call(ctx, 0 /*nargs*/); - duk_replace(ctx, 0); - /* leave stash on top intentionally, no need to pop */ + duk_inspect_callstack_entry(ctx, -2); + if (duk_is_object(ctx, -1)) { + if (duk_get_prop_string(ctx, -1, "function")) { + if (duk_get_prop_string(ctx, -1, "fileName")) { + if (duk_is_string(ctx, -1)) { + duk_replace(ctx, 0); + } + } + } + } + /* Leave values on stack on purpose, ignored below. */ /* Stripping the filename might be a good idea * ("/foo/bar/quux.js" -> logger name "quux"), @@ -372,24 +372,6 @@ void duk_logging_init(duk_context *ctx, duk_uint_t flags) { * setting Duktape.Logger will now fail. */ - /* Stash some values so that we can look them up later even if they - * have changed since init. Add a getName() helper into the stash - * for determining logger names based on the calling function - * (uses Duktape.act() now). - */ - duk_push_global_stash(ctx); - duk_dup(ctx, -3); - duk_put_prop_string(ctx, -2, "\xff" "logger:constructor"); - duk_eval_string(ctx, - "(function(){" - "var A=Duktape.act;" /* ensure doesn't change after init */ - "return function getName(){" - "try{return A(-4).function.fileName;}catch(e){}" /* undefined -> inherit "anon" */ - "};" - "})()"); - duk_put_prop_string(ctx, -2, "\xff" "logger:getName"); - duk_pop(ctx); - /* [ ... func Duktape.Logger Duktape.Logger.prototype ] */ duk_call(ctx, 2);