|
@@ -36,6 +36,8 @@ static const char *current_url;
|
|
|
static jmp_buf panic_env;
|
|
static jmp_buf panic_env;
|
|
|
static int panic_armed;
|
|
static int panic_armed;
|
|
|
|
|
|
|
|
|
|
+static void report_top(js_State *state);
|
|
|
|
|
+
|
|
|
static void report_cb(js_State *state, const char *message)
|
|
static void report_cb(js_State *state, const char *message)
|
|
|
{
|
|
{
|
|
|
(void)state;
|
|
(void)state;
|
|
@@ -45,9 +47,13 @@ static void report_cb(js_State *state, const char *message)
|
|
|
|
|
|
|
|
static void panic_cb(js_State *state)
|
|
static void panic_cb(js_State *state)
|
|
|
{
|
|
{
|
|
|
- (void)state;
|
|
|
|
|
if (panic_armed)
|
|
if (panic_armed)
|
|
|
longjmp(panic_env, 1);
|
|
longjmp(panic_env, 1);
|
|
|
|
|
+ if (js_isobject(state, -1))
|
|
|
|
|
+ report_top(state);
|
|
|
|
|
+ else if (host.report)
|
|
|
|
|
+ host.report(js_trystring(state, -1,
|
|
|
|
|
+ "uncaught JavaScript exception"));
|
|
|
if (host.report)
|
|
if (host.report)
|
|
|
host.report("uncaught JavaScript exception");
|
|
host.report("uncaught JavaScript exception");
|
|
|
}
|
|
}
|
|
@@ -170,6 +176,548 @@ static unsigned int node_name(unsigned int node, char *buf, unsigned int cap)
|
|
|
return i;
|
|
return i;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* ---- DOM events ------------------------------------------------------- *
|
|
|
|
|
+ *
|
|
|
|
|
+ * Listeners live in a fixed table, one entry per (target, type, callback)
|
|
|
|
|
+ * registration, and callbacks are retained through the MuJS registry. The
|
|
|
|
|
+ * dispatcher walks the parent chain for bubbling and runs one listener at a
|
|
|
|
|
+ * time so a handler can safely mutate the tree. `preventDefault` is tracked
|
|
|
|
|
+ * per dispatch in a small event-state table and mirrored onto the event
|
|
|
|
|
+ * object, so a submit handler can cancel the browser's default action. */
|
|
|
|
|
+
|
|
|
|
|
+#define JS_LISTENERS 64
|
|
|
|
|
+#define JS_EVENT_SLOTS 8
|
|
|
|
|
+#define JS_EVENT_TEMPLATES 16
|
|
|
|
|
+
|
|
|
|
|
+#define JS_TARGET_WINDOW 0xFFFFFFF0u
|
|
|
|
|
+#define JS_TARGET_DOCUMENT 0xFFFFFFF1u
|
|
|
|
|
+
|
|
|
|
|
+struct js_listener {
|
|
|
|
|
+ unsigned char used;
|
|
|
|
|
+ unsigned char capture;
|
|
|
|
|
+ unsigned int target;
|
|
|
|
|
+ char type[24];
|
|
|
|
|
+ char ref[32];
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct js_event_state {
|
|
|
|
|
+ int used;
|
|
|
|
|
+ int prevented;
|
|
|
|
|
+ int stopped;
|
|
|
|
|
+ int immediate;
|
|
|
|
|
+ char objref[32];
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct js_template {
|
|
|
|
|
+ int used;
|
|
|
|
|
+ char ref[32];
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static struct js_listener listeners[JS_LISTENERS];
|
|
|
|
|
+static struct js_event_state events[JS_EVENT_SLOTS];
|
|
|
|
|
+static struct js_template event_templates[JS_EVENT_TEMPLATES];
|
|
|
|
|
+
|
|
|
|
|
+static void push_event_target(js_State *state, unsigned int target)
|
|
|
|
|
+{
|
|
|
|
|
+ if (target == JS_TARGET_WINDOW)
|
|
|
|
|
+ js_pushglobal(state);
|
|
|
|
|
+ else if (target == JS_TARGET_DOCUMENT)
|
|
|
|
|
+ js_getglobal(state, "document");
|
|
|
|
|
+ else
|
|
|
|
|
+ push_node(state, target + 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void obj_set(js_State *state, int idx, const char *name)
|
|
|
|
|
+{
|
|
|
|
|
+ /* The value was just pushed, so a negative object index moved down. */
|
|
|
|
|
+ if (idx < 0)
|
|
|
|
|
+ idx--;
|
|
|
|
|
+ js_setproperty(state, idx, name);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void obj_set_string(js_State *state, int idx, const char *name,
|
|
|
|
|
+ const char *value)
|
|
|
|
|
+{
|
|
|
|
|
+ js_pushstring(state, value ? value : "");
|
|
|
|
|
+ obj_set(state, idx, name);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void obj_set_bool(js_State *state, int idx, const char *name, int value)
|
|
|
|
|
+{
|
|
|
|
|
+ js_pushboolean(state, value != 0);
|
|
|
|
|
+ obj_set(state, idx, name);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int event_slot(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot = (int)(uintptr_t)js_currentfunctiondata(state);
|
|
|
|
|
+
|
|
|
|
|
+ if (slot < 0 || slot >= JS_EVENT_SLOTS)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ return slot;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_preventdefault(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot = event_slot(state);
|
|
|
|
|
+
|
|
|
|
|
+ if (slot >= 0) {
|
|
|
|
|
+ struct js_event_state *ev = &events[slot];
|
|
|
|
|
+
|
|
|
|
|
+ ev->prevented = 1;
|
|
|
|
|
+ if (ev->objref[0]) {
|
|
|
|
|
+ js_getregistry(state, ev->objref);
|
|
|
|
|
+ if (js_isobject(state, -1)) {
|
|
|
|
|
+ js_pushboolean(state, 1);
|
|
|
|
|
+ js_setproperty(state, -2, "defaultPrevented");
|
|
|
|
|
+ }
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ js_pushundefined(state);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_stoppropagation(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot = event_slot(state);
|
|
|
|
|
+
|
|
|
|
|
+ if (slot >= 0)
|
|
|
|
|
+ events[slot].stopped = 1;
|
|
|
|
|
+ js_pushundefined(state);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_stopimmediate(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot = event_slot(state);
|
|
|
|
|
+
|
|
|
|
|
+ if (slot >= 0) {
|
|
|
|
|
+ events[slot].stopped = 1;
|
|
|
|
|
+ events[slot].immediate = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ js_pushundefined(state);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Event-object field helpers. Every dispatch owns a registry key for its
|
|
|
|
|
+ * event object so preventDefault can update the object without access to
|
|
|
|
|
+ * `this` (this MuJS has no this accessor). */
|
|
|
|
|
+static void ev_set_string(js_State *state, int slot, const char *name,
|
|
|
|
|
+ const char *value)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ js_pushstring(state, value ? value : "");
|
|
|
|
|
+ js_setproperty(state, -2, name);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ev_set_bool(js_State *state, int slot, const char *name, int value)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ js_pushboolean(state, value != 0);
|
|
|
|
|
+ js_setproperty(state, -2, name);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ev_set_number(js_State *state, int slot, const char *name,
|
|
|
|
|
+ double value)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ js_pushnumber(state, value);
|
|
|
|
|
+ js_setproperty(state, -2, name);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ev_set_target(js_State *state, int slot, const char *name,
|
|
|
|
|
+ unsigned int target)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ push_event_target(state, target);
|
|
|
|
|
+ js_setproperty(state, -2, name);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int ev_get_bool(js_State *state, int slot, const char *name, int def)
|
|
|
|
|
+{
|
|
|
|
|
+ int rv = def;
|
|
|
|
|
+
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ js_getproperty(state, -1, name);
|
|
|
|
|
+ if (js_isboolean(state, -1))
|
|
|
|
|
+ rv = js_toboolean(state, -1);
|
|
|
|
|
+ js_pop(state, 2);
|
|
|
|
|
+ return rv;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ev_get_string(js_State *state, int slot, const char *name,
|
|
|
|
|
+ char *out, int cap, const char *def)
|
|
|
|
|
+{
|
|
|
|
|
+ const char *s = def ? def : "";
|
|
|
|
|
+ int n = 0;
|
|
|
|
|
+
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ js_getproperty(state, -1, name);
|
|
|
|
|
+ if (js_isstring(state, -1))
|
|
|
|
|
+ s = js_tostring(state, -1);
|
|
|
|
|
+ while (s[n] && n + 1 < cap) {
|
|
|
|
|
+ out[n] = s[n];
|
|
|
|
|
+ n++;
|
|
|
|
|
+ }
|
|
|
|
|
+ out[n] = '\0';
|
|
|
|
|
+ js_pop(state, 2);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_bind_methods(js_State *state, int slot)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ def_func_data(state, "preventDefault", event_preventdefault, 0,
|
|
|
|
|
+ (unsigned int)slot);
|
|
|
|
|
+ def_func_data(state, "stopPropagation", event_stoppropagation, 0,
|
|
|
|
|
+ (unsigned int)slot);
|
|
|
|
|
+ def_func_data(state, "stopImmediatePropagation", event_stopimmediate, 0,
|
|
|
|
|
+ (unsigned int)slot);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* The event object is on top of the stack; move it into the registry and
|
|
|
|
|
+ * fill in the standard fields. */
|
|
|
|
|
+static int event_prepare(js_State *state, unsigned int target)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot;
|
|
|
|
|
+
|
|
|
|
|
+ for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
|
|
|
|
|
+ if (!events[slot].used)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (slot == JS_EVENT_SLOTS)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ events[slot].used = 1;
|
|
|
|
|
+ events[slot].prevented = 0;
|
|
|
|
|
+ events[slot].stopped = 0;
|
|
|
|
|
+ events[slot].immediate = 0;
|
|
|
|
|
+ snprintf(events[slot].objref, sizeof(events[slot].objref),
|
|
|
|
|
+ "jseventobj_%d", slot);
|
|
|
|
|
+ js_setregistry(state, events[slot].objref);
|
|
|
|
|
+
|
|
|
|
|
+ ev_set_bool(state, slot, "defaultPrevented", 0);
|
|
|
|
|
+ ev_set_bool(state, slot, "isTrusted", 0);
|
|
|
|
|
+ ev_set_number(state, slot, "timeStamp", (double)u_ticks_ms());
|
|
|
|
|
+ ev_set_target(state, slot, "target", target);
|
|
|
|
|
+ ev_set_target(state, slot, "currentTarget", target);
|
|
|
|
|
+ ev_set_number(state, slot, "eventPhase", 2);
|
|
|
|
|
+ event_bind_methods(state, slot);
|
|
|
|
|
+ return slot;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int listener_call(js_State *state, const struct js_listener *l,
|
|
|
|
|
+ int slot, unsigned int current_target)
|
|
|
|
|
+{
|
|
|
|
|
+ js_getregistry(state, l->ref);
|
|
|
|
|
+ if (!js_iscallable(state, -1)) {
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ push_event_target(state, current_target);
|
|
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
|
|
+ if (js_pcall(state, 1)) {
|
|
|
|
|
+ report_top(state);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void listener_phase(js_State *state, unsigned int target,
|
|
|
|
|
+ const char *type, int phase, int slot)
|
|
|
|
|
+{
|
|
|
|
|
+ int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < JS_LISTENERS; i++) {
|
|
|
|
|
+ struct js_listener *l = &listeners[i];
|
|
|
|
|
+
|
|
|
|
|
+ if (!l->used || l->target != target ||
|
|
|
|
|
+ strcmp(l->type, type) != 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (phase == 1 && !l->capture)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (phase == 2 && l->capture)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ ev_set_target(state, slot, "currentTarget", target);
|
|
|
|
|
+ listener_call(state, l, slot, target);
|
|
|
|
|
+ if (events[slot].immediate)
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* supplied_idx: 0 creates a fresh object, otherwise it is the argument
|
|
|
|
|
+ * index of an Event supplied by JavaScript. Leaves a boolean result. */
|
|
|
|
|
+static int dispatch_object(js_State *state, unsigned int target,
|
|
|
|
|
+ int supplied_idx, const char *fallback,
|
|
|
|
|
+ int def_bubbles, int def_cancelable)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int path[64];
|
|
|
|
|
+ char type[32];
|
|
|
|
|
+ int np = 0, slot, i, prevented;
|
|
|
|
|
+ struct br_node *nd;
|
|
|
|
|
+
|
|
|
|
|
+ if (supplied_idx)
|
|
|
|
|
+ js_copy(state, supplied_idx);
|
|
|
|
|
+ else
|
|
|
|
|
+ js_newobject(state);
|
|
|
|
|
+ slot = event_prepare(state, target);
|
|
|
|
|
+ if (slot < 0) {
|
|
|
|
|
+ js_pushboolean(state, 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ ev_get_string(state, slot, "type", type, sizeof(type), fallback);
|
|
|
|
|
+ if (!type[0]) {
|
|
|
|
|
+ js_unref(state, events[slot].objref);
|
|
|
|
|
+ events[slot].objref[0] = '\0';
|
|
|
|
|
+ events[slot].used = 0;
|
|
|
|
|
+ js_pushboolean(state, 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ ev_set_string(state, slot, "type", type);
|
|
|
|
|
+ ev_set_bool(state, slot, "bubbles",
|
|
|
|
|
+ ev_get_bool(state, slot, "bubbles", def_bubbles));
|
|
|
|
|
+ ev_set_bool(state, slot, "cancelable",
|
|
|
|
|
+ ev_get_bool(state, slot, "cancelable", def_cancelable));
|
|
|
|
|
+
|
|
|
|
|
+ path[np++] = target;
|
|
|
|
|
+ if (target != JS_TARGET_WINDOW && target != JS_TARGET_DOCUMENT &&
|
|
|
|
|
+ D && target < D->nnode) {
|
|
|
|
|
+ nd = &D->nodes[target];
|
|
|
|
|
+ while (nd->parent && nd->parent < D->nnode &&
|
|
|
|
|
+ np < (int)(sizeof(path) / sizeof(path[0]))) {
|
|
|
|
|
+ path[np++] = nd->parent;
|
|
|
|
|
+ nd = &D->nodes[nd->parent];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (target != JS_TARGET_WINDOW)
|
|
|
|
|
+ path[np++] = JS_TARGET_DOCUMENT;
|
|
|
|
|
+ if (np < (int)(sizeof(path) / sizeof(path[0])))
|
|
|
|
|
+ path[np++] = JS_TARGET_WINDOW;
|
|
|
|
|
+
|
|
|
|
|
+ /* Capture from the outermost ancestor down to the parent. */
|
|
|
|
|
+ for (i = np - 1; i > 0; i--) {
|
|
|
|
|
+ if (events[slot].stopped)
|
|
|
|
|
+ break;
|
|
|
|
|
+ listener_phase(state, path[i], type, 1, slot);
|
|
|
|
|
+ }
|
|
|
|
|
+ /* Target phase: capture and bubble listeners in registration order. */
|
|
|
|
|
+ if (!events[slot].stopped)
|
|
|
|
|
+ listener_phase(state, path[0], type, 0, slot);
|
|
|
|
|
+ /* Bubble only for events that ask for it; stopPropagation stops the
|
|
|
|
|
+ * remaining ancestors. */
|
|
|
|
|
+ if (ev_get_bool(state, slot, "bubbles", def_bubbles)) {
|
|
|
|
|
+ for (i = 1; i < np && !events[slot].stopped; i++)
|
|
|
|
|
+ listener_phase(state, path[i], type, 2, slot);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ prevented = events[slot].prevented;
|
|
|
|
|
+ js_unref(state, events[slot].objref);
|
|
|
|
|
+ events[slot].objref[0] = '\0';
|
|
|
|
|
+ events[slot].used = 0;
|
|
|
|
|
+ js_pushboolean(state, prevented ? 0 : 1);
|
|
|
|
|
+ return prevented ? 0 : 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void add_listener(js_State *state, unsigned int target, int remove)
|
|
|
|
|
+{
|
|
|
|
|
+ const char *type;
|
|
|
|
|
+ int i;
|
|
|
|
|
+
|
|
|
|
|
+ if (!js_iscallable(state, 2))
|
|
|
|
|
+ return;
|
|
|
|
|
+ type = js_tostring(state, 1);
|
|
|
|
|
+ if (!type[0])
|
|
|
|
|
+ return;
|
|
|
|
|
+ if (!remove) {
|
|
|
|
|
+ for (i = 0; i < JS_LISTENERS; i++)
|
|
|
|
|
+ if (!listeners[i].used)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (i == JS_LISTENERS)
|
|
|
|
|
+ return;
|
|
|
|
|
+ listeners[i].used = 1;
|
|
|
|
|
+ listeners[i].capture = js_isboolean(state, 3) &&
|
|
|
|
|
+ js_toboolean(state, 3);
|
|
|
|
|
+ listeners[i].target = target;
|
|
|
|
|
+ snprintf(listeners[i].type, sizeof(listeners[i].type), "%s",
|
|
|
|
|
+ type);
|
|
|
|
|
+ js_copy(state, 2);
|
|
|
|
|
+ snprintf(listeners[i].ref, sizeof(listeners[i].ref), "%s",
|
|
|
|
|
+ js_ref(state));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < JS_LISTENERS; i++) {
|
|
|
|
|
+ struct js_listener *l = &listeners[i];
|
|
|
|
|
+ int eq;
|
|
|
|
|
+
|
|
|
|
|
+ if (!l->used || l->target != target ||
|
|
|
|
|
+ strcmp(l->type, type) != 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ js_getregistry(state, l->ref);
|
|
|
|
|
+ js_copy(state, 2);
|
|
|
|
|
+ eq = js_equal(state);
|
|
|
|
|
+ js_pop(state, 2);
|
|
|
|
|
+ if (!eq)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ js_unref(state, l->ref);
|
|
|
|
|
+ l->used = 0;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void node_addlistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = (unsigned int)(uintptr_t)
|
|
|
|
|
+ js_currentfunctiondata(state) - 1;
|
|
|
|
|
+
|
|
|
|
|
+ add_listener(state, node, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void node_removelistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = (unsigned int)(uintptr_t)
|
|
|
|
|
+ js_currentfunctiondata(state) - 1;
|
|
|
|
|
+
|
|
|
|
|
+ add_listener(state, node, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void node_dispatchevent(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = (unsigned int)(uintptr_t)
|
|
|
|
|
+ js_currentfunctiondata(state) - 1;
|
|
|
|
|
+
|
|
|
|
|
+ if (!D || !node) {
|
|
|
|
|
+ js_pushboolean(state, 1);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (js_isobject(state, 1)) {
|
|
|
|
|
+ dispatch_object(state, node, 1, "event", 1, 0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ char type[32];
|
|
|
|
|
+ const char *s = js_tostring(state, 1);
|
|
|
|
|
+ unsigned int i = 0;
|
|
|
|
|
+
|
|
|
|
|
+ while (s[i] && i + 1 < sizeof(type)) {
|
|
|
|
|
+ type[i] = s[i];
|
|
|
|
|
+ i++;
|
|
|
|
|
+ }
|
|
|
|
|
+ type[i] = '\0';
|
|
|
|
|
+ dispatch_object(state, node, 0, type, 1, 0);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void win_addlistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ add_listener(state, JS_TARGET_WINDOW, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void win_removelistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ add_listener(state, JS_TARGET_WINDOW, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void doc_addlistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ add_listener(state, JS_TARGET_DOCUMENT, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void doc_removelistener(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ add_listener(state, JS_TARGET_DOCUMENT, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_init(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot = (int)(uintptr_t)js_currentfunctiondata(state);
|
|
|
|
|
+ const char *type;
|
|
|
|
|
+
|
|
|
|
|
+ if (slot < 0 || slot >= JS_EVENT_TEMPLATES ||
|
|
|
|
|
+ !event_templates[slot].used)
|
|
|
|
|
+ return;
|
|
|
|
|
+ type = js_tostring(state, 1);
|
|
|
|
|
+ js_getregistry(state, event_templates[slot].ref);
|
|
|
|
|
+ if (!js_isobject(state, -1)) {
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ obj_set_string(state, -1, "type", type);
|
|
|
|
|
+ obj_set_bool(state, -1, "bubbles", js_toboolean(state, 2));
|
|
|
|
|
+ obj_set_bool(state, -1, "cancelable", js_toboolean(state, 3));
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_build_object(js_State *state, const char *type,
|
|
|
|
|
+ int bubbles, int cancelable, int with_init)
|
|
|
|
|
+{
|
|
|
|
|
+ js_newobject(state);
|
|
|
|
|
+ js_pushstring(state, type ? type : "");
|
|
|
|
|
+ js_setproperty(state, -2, "type");
|
|
|
|
|
+ js_pushboolean(state, bubbles != 0);
|
|
|
|
|
+ js_setproperty(state, -2, "bubbles");
|
|
|
|
|
+ js_pushboolean(state, cancelable != 0);
|
|
|
|
|
+ js_setproperty(state, -2, "cancelable");
|
|
|
|
|
+ js_pushboolean(state, 0);
|
|
|
|
|
+ js_setproperty(state, -2, "defaultPrevented");
|
|
|
|
|
+ if (with_init) {
|
|
|
|
|
+ int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < JS_EVENT_TEMPLATES; i++)
|
|
|
|
|
+ if (!event_templates[i].used)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (i < JS_EVENT_TEMPLATES) {
|
|
|
|
|
+ event_templates[i].used = 1;
|
|
|
|
|
+ snprintf(event_templates[i].ref,
|
|
|
|
|
+ sizeof(event_templates[i].ref),
|
|
|
|
|
+ "jseventtmpl_%d", i);
|
|
|
|
|
+ js_copy(state, -1);
|
|
|
|
|
+ js_setregistry(state, event_templates[i].ref);
|
|
|
|
|
+ def_func_data(state, "initEvent", event_init, 3,
|
|
|
|
|
+ (unsigned int)i);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void event_call(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ const char *type = js_isdefined(state, 1) ? js_tostring(state, 1) : "";
|
|
|
|
|
+ int bubbles = 0, cancelable = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (js_isobject(state, 2)) {
|
|
|
|
|
+ js_getproperty(state, 2, "bubbles");
|
|
|
|
|
+ bubbles = js_toboolean(state, -1);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ js_getproperty(state, 2, "cancelable");
|
|
|
|
|
+ cancelable = js_toboolean(state, -1);
|
|
|
|
|
+ js_pop(state, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ event_build_object(state, type, bubbles, cancelable, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void doc_createevent(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ const char *type = js_tostring(state, 1);
|
|
|
|
|
+
|
|
|
|
|
+ event_build_object(state, type, 0, 0, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int js_browser_event(unsigned int node, const char *type, int bubbles,
|
|
|
|
|
+ int cancelable)
|
|
|
|
|
+{
|
|
|
|
|
+ int slot, rv;
|
|
|
|
|
+
|
|
|
|
|
+ if (!J || !D)
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
|
|
|
|
|
+ if (!events[slot].used)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (slot == JS_EVENT_SLOTS)
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ js_setlimit(J, JS_RUN_LIMIT, 0);
|
|
|
|
|
+ rv = dispatch_object(J, node, 0, type, bubbles, cancelable);
|
|
|
|
|
+ js_pop(J, 1);
|
|
|
|
|
+ return rv;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static unsigned int node_type_value(unsigned int node)
|
|
static unsigned int node_type_value(unsigned int node)
|
|
|
{
|
|
{
|
|
|
struct br_node *n;
|
|
struct br_node *n;
|
|
@@ -929,6 +1477,15 @@ static void node_matches(js_State *state)
|
|
|
js_tostring(state, 1)));
|
|
js_tostring(state, 1)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static void node_click(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = node_of(js_currentfunctiondata(state));
|
|
|
|
|
+
|
|
|
|
|
+ if (host.click)
|
|
|
|
|
+ host.click(node);
|
|
|
|
|
+ js_pushundefined(state);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static void node_getelementsbytagname(js_State *state)
|
|
static void node_getelementsbytagname(js_State *state)
|
|
|
{
|
|
{
|
|
|
unsigned int node = node_of(js_currentfunctiondata(state));
|
|
unsigned int node = node_of(js_currentfunctiondata(state));
|
|
@@ -1035,6 +1592,55 @@ static void node_push_text(js_State *state, unsigned int node)
|
|
|
free(buf);
|
|
free(buf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static unsigned int node_form_ancestor(unsigned int node)
|
|
|
|
|
+{
|
|
|
|
|
+ while (node && node < D->nnode) {
|
|
|
|
|
+ if (D->nodes[node].type == BR_NODE_ELEM &&
|
|
|
|
|
+ D->nodes[node].tag == BR_TAG_FORM)
|
|
|
|
|
+ return node;
|
|
|
|
|
+ node = D->nodes[node].parent;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void push_form_elements(js_State *state, unsigned int form)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int out[128];
|
|
|
|
|
+ int n = 0, i;
|
|
|
|
|
+
|
|
|
|
|
+ if (host.form_elements)
|
|
|
|
|
+ n = host.form_elements(form, out, (int)(sizeof(out) /
|
|
|
|
|
+ sizeof(out[0])));
|
|
|
|
|
+ js_newarray(state);
|
|
|
|
|
+ for (i = 0; i < n; i++) {
|
|
|
|
|
+ const char *name;
|
|
|
|
|
+
|
|
|
|
|
+ push_node(state, out[i] + 1);
|
|
|
|
|
+ js_setindex(state, -2, i);
|
|
|
|
|
+ name = br_find_attr(D, out[i], "name");
|
|
|
|
|
+ if (name && name[0]) {
|
|
|
|
|
+ push_node(state, out[i] + 1);
|
|
|
|
|
+ js_setproperty(state, -2, name);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void form_submit_js(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = node_of(js_currentfunctiondata(state));
|
|
|
|
|
+
|
|
|
|
|
+ if (host.form_submit)
|
|
|
|
|
+ host.form_submit(node);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void form_reset_js(js_State *state)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int node = node_of(js_currentfunctiondata(state));
|
|
|
|
|
+
|
|
|
|
|
+ if (host.form_reset)
|
|
|
|
|
+ host.form_reset(node);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/* Userdata property hooks: every element wrapper carries node + 1. */
|
|
/* Userdata property hooks: every element wrapper carries node + 1. */
|
|
|
static int node_has(js_State *state, void *p, const char *name)
|
|
static int node_has(js_State *state, void *p, const char *name)
|
|
|
{
|
|
{
|
|
@@ -1046,6 +1652,45 @@ static int node_has(js_State *state, void *p, const char *name)
|
|
|
return 0;
|
|
return 0;
|
|
|
n = &D->nodes[node];
|
|
n = &D->nodes[node];
|
|
|
|
|
|
|
|
|
|
+ if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM) {
|
|
|
|
|
+ if (!strcmp(name, "elements")) {
|
|
|
|
|
+ push_form_elements(state, node);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!strcmp(name, "length")) {
|
|
|
|
|
+ unsigned int out[128];
|
|
|
|
|
+ int count = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (host.form_elements)
|
|
|
|
|
+ count = host.form_elements(node, out,
|
|
|
|
|
+ (int)(sizeof(out) / sizeof(out[0])));
|
|
|
|
|
+ js_pushnumber(state, count);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!strcmp(name, "submit")) {
|
|
|
|
|
+ push_func_data(state, "submit", form_submit_js, 0,
|
|
|
|
|
+ node + 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!strcmp(name, "reset")) {
|
|
|
|
|
+ push_func_data(state, "reset", form_reset_js, 0,
|
|
|
|
|
+ node + 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!strcmp(name, "form")) {
|
|
|
|
|
+ unsigned int f;
|
|
|
|
|
+
|
|
|
|
|
+ if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM)
|
|
|
|
|
+ f = 0;
|
|
|
|
|
+ else
|
|
|
|
|
+ f = node_form_ancestor(node);
|
|
|
|
|
+ if (f)
|
|
|
|
|
+ push_node(state, f + 1);
|
|
|
|
|
+ else
|
|
|
|
|
+ js_pushnull(state);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
|
|
if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
|
|
|
node_push_text(state, node);
|
|
node_push_text(state, node);
|
|
|
return 1;
|
|
return 1;
|
|
@@ -1252,11 +1897,11 @@ static int node_has(js_State *state, void *p, const char *name)
|
|
|
if (!strcmp(name, "querySelectorAll"))
|
|
if (!strcmp(name, "querySelectorAll"))
|
|
|
{ push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
|
|
{ push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
|
|
|
if (!strcmp(name, "addEventListener"))
|
|
if (!strcmp(name, "addEventListener"))
|
|
|
- { push_func_data(state, "addEventListener", node_noop, 2, node + 1); return 1; }
|
|
|
|
|
|
|
+ { push_func_data(state, "addEventListener", node_addlistener, 2, node + 1); return 1; }
|
|
|
if (!strcmp(name, "removeEventListener"))
|
|
if (!strcmp(name, "removeEventListener"))
|
|
|
- { push_func_data(state, "removeEventListener", node_noop, 2, node + 1); return 1; }
|
|
|
|
|
|
|
+ { push_func_data(state, "removeEventListener", node_removelistener, 2, node + 1); return 1; }
|
|
|
if (!strcmp(name, "dispatchEvent"))
|
|
if (!strcmp(name, "dispatchEvent"))
|
|
|
- { push_func_data(state, "dispatchEvent", node_noop, 1, node + 1); return 1; }
|
|
|
|
|
|
|
+ { push_func_data(state, "dispatchEvent", node_dispatchevent, 1, node + 1); return 1; }
|
|
|
if (!strcmp(name, "getBoundingClientRect"))
|
|
if (!strcmp(name, "getBoundingClientRect"))
|
|
|
{ push_func_data(state, "getBoundingClientRect", node_getrect, 0, node + 1); return 1; }
|
|
{ push_func_data(state, "getBoundingClientRect", node_getrect, 0, node + 1); return 1; }
|
|
|
if (!strcmp(name, "getClientRects")) {
|
|
if (!strcmp(name, "getClientRects")) {
|
|
@@ -1279,8 +1924,11 @@ static int node_has(js_State *state, void *p, const char *name)
|
|
|
js_pushnumber(state, 0);
|
|
js_pushnumber(state, 0);
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
- if (!strcmp(name, "click") || !strcmp(name, "focus") ||
|
|
|
|
|
- !strcmp(name, "blur")) {
|
|
|
|
|
|
|
+ if (!strcmp(name, "click")) {
|
|
|
|
|
+ push_func_data(state, "click", node_click, 0, node + 1);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!strcmp(name, "focus") || !strcmp(name, "blur")) {
|
|
|
push_func_data(state, name, node_noop, 0, node + 1);
|
|
push_func_data(state, name, node_noop, 0, node + 1);
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
@@ -1537,11 +2185,6 @@ static void doc_queryselectorall(js_State *state)
|
|
|
push_query_array(state, 0, js_tostring(state, 1));
|
|
push_query_array(state, 0, js_tostring(state, 1));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void doc_noop(js_State *state)
|
|
|
|
|
-{
|
|
|
|
|
- (void)state;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
static void location_get(js_State *state)
|
|
static void location_get(js_State *state)
|
|
|
{
|
|
{
|
|
|
js_pushstring(state, current_url ? current_url : "");
|
|
js_pushstring(state, current_url ? current_url : "");
|
|
@@ -1956,8 +2599,9 @@ static void install_globals(js_State *state)
|
|
|
def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
|
|
def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
|
|
|
def_func(state, "querySelector", doc_queryselector, 1);
|
|
def_func(state, "querySelector", doc_queryselector, 1);
|
|
|
def_func(state, "querySelectorAll", doc_queryselectorall, 1);
|
|
def_func(state, "querySelectorAll", doc_queryselectorall, 1);
|
|
|
- def_func(state, "addEventListener", doc_noop, 2);
|
|
|
|
|
- def_func(state, "removeEventListener", doc_noop, 2);
|
|
|
|
|
|
|
+ def_func(state, "addEventListener", doc_addlistener, 2);
|
|
|
|
|
+ def_func(state, "removeEventListener", doc_removelistener, 2);
|
|
|
|
|
+ def_func(state, "createEvent", doc_createevent, 1);
|
|
|
|
|
|
|
|
h = br_find_tag(D, BR_TAG_HTML);
|
|
h = br_find_tag(D, BR_TAG_HTML);
|
|
|
if (!h && D->nnode > 1)
|
|
if (!h && D->nnode > 1)
|
|
@@ -1996,6 +2640,9 @@ static void install_globals(js_State *state)
|
|
|
js_newcfunction(state, js_alert, "alert", 1);
|
|
js_newcfunction(state, js_alert, "alert", 1);
|
|
|
js_defglobal(state, "alert", JS_DONTENUM);
|
|
js_defglobal(state, "alert", JS_DONTENUM);
|
|
|
|
|
|
|
|
|
|
+ js_newcfunction(state, event_call, "Event", 1);
|
|
|
|
|
+ js_defglobal(state, "Event", JS_DONTENUM);
|
|
|
|
|
+
|
|
|
push_location(state);
|
|
push_location(state);
|
|
|
js_defglobal(state, "location", JS_DONTENUM);
|
|
js_defglobal(state, "location", JS_DONTENUM);
|
|
|
|
|
|
|
@@ -2020,8 +2667,8 @@ static void install_globals(js_State *state)
|
|
|
|
|
|
|
|
def_global_func(state, "matchMedia", win_matchmedia, 1);
|
|
def_global_func(state, "matchMedia", win_matchmedia, 1);
|
|
|
def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
|
|
def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
|
|
|
- def_global_func(state, "addEventListener", win_noop, 2);
|
|
|
|
|
- def_global_func(state, "removeEventListener", win_noop, 2);
|
|
|
|
|
|
|
+ def_global_func(state, "addEventListener", win_addlistener, 2);
|
|
|
|
|
+ def_global_func(state, "removeEventListener", win_removelistener, 2);
|
|
|
def_global_func(state, "scrollTo", win_noop, 2);
|
|
def_global_func(state, "scrollTo", win_noop, 2);
|
|
|
def_global_func(state, "scrollBy", win_noop, 2);
|
|
def_global_func(state, "scrollBy", win_noop, 2);
|
|
|
def_global_func(state, "setTimeout", win_settimeout, 1);
|
|
def_global_func(state, "setTimeout", win_settimeout, 1);
|
|
@@ -2076,6 +2723,9 @@ int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
|
|
|
current_url = h->page_url;
|
|
current_url = h->page_url;
|
|
|
panic_armed = 0;
|
|
panic_armed = 0;
|
|
|
memset(timers, 0, sizeof(timers));
|
|
memset(timers, 0, sizeof(timers));
|
|
|
|
|
+ memset(listeners, 0, sizeof(listeners));
|
|
|
|
|
+ memset(events, 0, sizeof(events));
|
|
|
|
|
+ memset(event_templates, 0, sizeof(event_templates));
|
|
|
|
|
|
|
|
/* Classic scripts are sloppy by default in browsers; strict mode is
|
|
/* Classic scripts are sloppy by default in browsers; strict mode is
|
|
|
* opt-in via a "use strict" directive. */
|
|
* opt-in via a "use strict" directive. */
|