Support for event getters

This commit is contained in:
Daniel Silverstone 2015-10-31 22:59:26 +01:00
parent 47794b32ff
commit 39d967e94b
2 changed files with 76 additions and 61 deletions

View File

@ -445,29 +445,15 @@ static void dukky_push_event(duk_context *ctx, dom_event *evt)
/* ... event */ /* ... 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_string *onname, *val;
dom_element *ele; dom_element *ele = (dom_element *)et;
dom_exception exc; 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); exc = dom_string_concat(corestring_dom_on, name, &onname);
if (exc != DOM_NO_ERR) { 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); duk_push_lstring(ctx, "", 0);
return; return;
} }
@ -475,16 +461,64 @@ static void dukky_push_handler_code(duk_context *ctx, dom_event *evt)
exc = dom_element_get_attribute(ele, onname, &val); exc = dom_element_get_attribute(ele, onname, &val);
if (exc != DOM_NO_ERR) { if (exc != DOM_NO_ERR) {
dom_string_unref(onname); dom_string_unref(onname);
dom_node_unref(ele);
duk_push_lstring(ctx, "", 0); duk_push_lstring(ctx, "", 0);
return; return;
} }
dom_node_unref(ele);
dom_string_unref(onname); dom_string_unref(onname);
duk_push_lstring(ctx, dom_string_data(val), dom_string_length(val)); duk_push_lstring(ctx, dom_string_data(val), dom_string_length(val));
dom_string_unref(val); dom_string_unref(val);
} }
bool dukky_get_current_value_of_event_handler(duk_context *ctx,
dom_string *name,
dom_event_target *et)
{
/* 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));
/* ... node handlers name */
duk_get_prop(ctx, -2);
/* ... node handlers handler? */
if (duk_is_undefined(ctx, -1)) {
/* ... node handlers undefined */
duk_pop_2(ctx);
/* ... node */
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) {");
/* ... node handlercode prefix */
duk_insert(ctx, -2);
/* ... node prefix handlercode */
duk_push_string(ctx, "}");
/* ... node prefix handlercode suffix */
duk_concat(ctx, 3);
/* ... node fullhandlersrc */
duk_push_string(ctx, "internal raw uncompiled handler");
/* ... node fullhandlersrc filename */
if (duk_pcompile(ctx, DUK_COMPILE_FUNCTION) != 0) {
/* ... node err */
LOG("Unable to proceed with handler, could not compile");
duk_pop_2(ctx);
return false;
}
/* ... node handler */
duk_insert(ctx, -2);
/* ... handler node */
} else {
/* ... node handlers handler */
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) static void dukky_generic_event_handler(dom_event *evt, void *pw)
{ {
@ -516,49 +550,15 @@ static void dukky_generic_event_handler(dom_event *evt, void *pw)
LOG("Unable to push JS node representation?!"); LOG("Unable to push JS node representation?!");
return; return;
} }
dom_node_unref(targ);
/* ... node */ /* ... node */
duk_get_prop_string(ctx, -1, HANDLER_MAGIC); if (dukky_get_current_value_of_event_handler(
/* ... node handlers */ ctx, name, (dom_event_target *)targ) == false) {
duk_push_lstring(ctx, dom_string_data(name), dom_string_length(name)); dom_node_unref(targ);
/* ... node handlers name */ dom_string_unref(name);
duk_get_prop(ctx, -2); return;
/* ... node handlers handler? */
if (duk_is_undefined(ctx, -1)) {
/* ... node handlers undefined */
duk_pop_2(ctx);
/* ... node */
dukky_push_handler_code(ctx, evt);
/* ... node handlercode */
/** @todo This is entirely wrong, but it's hard to get right */
duk_push_string(ctx, "function (event) {");
/* ... node handlercode prefix */
duk_insert(ctx, -2);
/* ... node prefix handlercode */
duk_push_string(ctx, "}");
/* ... node prefix handlercode suffix */
duk_concat(ctx, 3);
/* ... node fullhandlersrc */
duk_push_string(ctx, "internal raw uncompiled handler");
/* ... node fullhandlersrc filename */
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 ;
}
/* ... node handler */
duk_insert(ctx, -2);
/* ... handler node */
} else {
/* ... node handlers handler */
duk_insert(ctx, -2);
/* ... handler node handlers */
duk_pop(ctx);
/* ... handler node */
} }
/** @todo handle other kinds of event than the generic case */ /** @todo handle other kinds of event than the generic case */
dom_node_unref(targ);
dom_string_unref(name); dom_string_unref(name);
/* ... handler node */ /* ... handler node */
dukky_push_event(ctx, evt); 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); exc = dom_event_stop_immediate_propagation(evt);
if (exc != DOM_NO_ERR) if (exc != DOM_NO_ERR)
LOG("WORSE! could not stop propagation"); 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; return;
} }

View File

@ -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, void dukky_register_event_listener_for(duk_context *ctx,
struct dom_element *ele, struct dom_element *ele,
dom_string *name); dom_string *name);
bool dukky_get_current_value_of_event_handler(duk_context *ctx,
dom_string *name,
dom_event_target *et);
#endif #endif