From 39d967e94b4715fa5d43c5e1fcbcc95ad31f7562 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 31 Oct 2015 22:59:26 +0100 Subject: Support for event getters --- javascript/duktape/dukky.c | 126 +++++++++++++++++++++++++-------------------- javascript/duktape/dukky.h | 3 ++ 2 files changed, 72 insertions(+), 57 deletions(-) (limited to 'javascript') diff --git a/javascript/duktape/dukky.c b/javascript/duktape/dukky.c index b8108eac3..8d9b84a73 100644 --- a/javascript/duktape/dukky.c +++ b/javascript/duktape/dukky.c @@ -445,29 +445,15 @@ static void dukky_push_event(duk_context *ctx, dom_event *evt) /* ... event */ } -static void dukky_push_handler_code(duk_context *ctx, dom_event *evt) +static void dukky_push_handler_code_(duk_context *ctx, dom_string *name, + dom_event_target *et) { - dom_string *name, *onname, *val; - dom_element *ele; + dom_string *onname, *val; + dom_element *ele = (dom_element *)et; dom_exception exc; - exc = dom_event_get_type(evt, &name); - if (exc != DOM_NO_ERR) { - duk_push_lstring(ctx, "", 0); - return; - } - exc = dom_string_concat(corestring_dom_on, name, &onname); if (exc != DOM_NO_ERR) { - dom_string_unref(name); - duk_push_lstring(ctx, "", 0); - return; - } - dom_string_unref(name); - - exc = dom_event_get_target(evt, &ele); - if (exc != DOM_NO_ERR) { - dom_string_unref(onname); duk_push_lstring(ctx, "", 0); return; } @@ -475,49 +461,22 @@ static void dukky_push_handler_code(duk_context *ctx, dom_event *evt) exc = dom_element_get_attribute(ele, onname, &val); if (exc != DOM_NO_ERR) { dom_string_unref(onname); - dom_node_unref(ele); duk_push_lstring(ctx, "", 0); return; } - dom_node_unref(ele); + dom_string_unref(onname); duk_push_lstring(ctx, dom_string_data(val), dom_string_length(val)); dom_string_unref(val); } - -static void dukky_generic_event_handler(dom_event *evt, void *pw) +bool dukky_get_current_value_of_event_handler(duk_context *ctx, + dom_string *name, + dom_event_target *et) { - duk_context *ctx = (duk_context *)pw; - dom_string *name; - dom_exception exc; - dom_event_target *targ; - - LOG("WOOP WOOP, An event:"); - exc = dom_event_get_type(evt, &name); - if (exc != DOM_NO_ERR) { - LOG("Unable to find the event name"); - return; - } - LOG("Event's name is %*s", - dom_string_length(name), dom_string_data(name)); - - exc = dom_event_get_target(evt, &targ); - if (exc != DOM_NO_ERR) { - dom_string_unref(name); - LOG("Unable to find the event target"); - return; - } - - /* ... */ - if (dukky_push_node(ctx, (dom_node *)targ) == false) { - dom_string_unref(name); - dom_node_unref(targ); - LOG("Unable to push JS node representation?!"); - return; - } - dom_node_unref(targ); - /* ... node */ + /* Must be entered as: + * ... node(et) + */ duk_get_prop_string(ctx, -1, HANDLER_MAGIC); /* ... node handlers */ duk_push_lstring(ctx, dom_string_data(name), dom_string_length(name)); @@ -528,7 +487,7 @@ static void dukky_generic_event_handler(dom_event *evt, void *pw) /* ... node handlers undefined */ duk_pop_2(ctx); /* ... node */ - dukky_push_handler_code(ctx, evt); + dukky_push_handler_code_(ctx, name, et); /* ... node handlercode */ /** @todo This is entirely wrong, but it's hard to get right */ duk_push_string(ctx, "function (event) {"); @@ -544,21 +503,62 @@ static void dukky_generic_event_handler(dom_event *evt, void *pw) if (duk_pcompile(ctx, DUK_COMPILE_FUNCTION) != 0) { /* ... node err */ LOG("Unable to proceed with handler, could not compile"); - dom_string_unref(name); duk_pop_2(ctx); - return ; + return false; } /* ... node handler */ duk_insert(ctx, -2); /* ... handler node */ } else { /* ... node handlers handler */ - duk_insert(ctx, -2); + duk_insert(ctx, -3); /* ... handler node handlers */ duk_pop(ctx); /* ... handler node */ } + /* ... handler node */ + return true; +} + +static void dukky_generic_event_handler(dom_event *evt, void *pw) +{ + duk_context *ctx = (duk_context *)pw; + dom_string *name; + dom_exception exc; + dom_event_target *targ; + + LOG("WOOP WOOP, An event:"); + exc = dom_event_get_type(evt, &name); + if (exc != DOM_NO_ERR) { + LOG("Unable to find the event name"); + return; + } + LOG("Event's name is %*s", + dom_string_length(name), dom_string_data(name)); + + exc = dom_event_get_target(evt, &targ); + if (exc != DOM_NO_ERR) { + dom_string_unref(name); + LOG("Unable to find the event target"); + return; + } + + /* ... */ + if (dukky_push_node(ctx, (dom_node *)targ) == false) { + dom_string_unref(name); + dom_node_unref(targ); + LOG("Unable to push JS node representation?!"); + return; + } + /* ... node */ + if (dukky_get_current_value_of_event_handler( + ctx, name, (dom_event_target *)targ) == false) { + dom_node_unref(targ); + dom_string_unref(name); + return; + } /** @todo handle other kinds of event than the generic case */ + dom_node_unref(targ); dom_string_unref(name); /* ... handler node */ dukky_push_event(ctx, evt); @@ -570,7 +570,19 @@ static void dukky_generic_event_handler(dom_event *evt, void *pw) exc = dom_event_stop_immediate_propagation(evt); if (exc != DOM_NO_ERR) LOG("WORSE! could not stop propagation"); - duk_pop(ctx); + duk_get_prop_string(ctx, -1, "name"); + duk_get_prop_string(ctx, -2, "message"); + duk_get_prop_string(ctx, -3, "fileName"); + duk_get_prop_string(ctx, -4, "lineNumber"); + duk_get_prop_string(ctx, -5, "stack"); + /* ... err name message fileName lineNumber stack */ + LOG("Uncaught error in JS: %s: %s", duk_safe_to_string(ctx, -5), + duk_safe_to_string(ctx, -4)); + LOG(" was at: %s line %s", duk_safe_to_string(ctx, -3), + duk_safe_to_string(ctx, -2)); + LOG(" Stack trace: %s", duk_safe_to_string(ctx, -1)); + + duk_pop_n(ctx, 6); /* ... */ return; } diff --git a/javascript/duktape/dukky.h b/javascript/duktape/dukky.h index 4ed462ea5..d1fff36aa 100644 --- a/javascript/duktape/dukky.h +++ b/javascript/duktape/dukky.h @@ -31,5 +31,8 @@ void dukky_inject_not_ctr(duk_context *ctx, int idx, const char *name); void dukky_register_event_listener_for(duk_context *ctx, struct dom_element *ele, dom_string *name); +bool dukky_get_current_value_of_event_handler(duk_context *ctx, + dom_string *name, + dom_event_target *et); #endif -- cgit v1.2.3