From 909f8bac07a15dbea0db3bd3d406e81a45721765 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 11 Mar 2024 18:07:51 +0100 Subject: [PATCH] [mujs] More properties for MessageEvent --- src/ecmascript/mujs.c | 2 + src/ecmascript/mujs/message.c | 192 +++++++++++++++++++++++++++++++++- src/ecmascript/mujs/message.h | 1 + 3 files changed, 194 insertions(+), 1 deletion(-) diff --git a/src/ecmascript/mujs.c b/src/ecmascript/mujs.c index 0836718d..715d33a3 100644 --- a/src/ecmascript/mujs.c +++ b/src/ecmascript/mujs.c @@ -34,6 +34,7 @@ #include "ecmascript/mujs/localstorage.h" #include "ecmascript/mujs/location.h" #include "ecmascript/mujs/mapa.h" +#include "ecmascript/mujs/message.h" #include "ecmascript/mujs/navigator.h" #include "ecmascript/mujs/screen.h" #include "ecmascript/mujs/unibar.h" @@ -139,6 +140,7 @@ mujs_get_interpreter(struct ecmascript_interpreter *interpreter) mjs_xhr_init(J); mjs_event_init(J); mjs_keyboardEvent_init(J); + mjs_messageEvent_init(J); return J; #if 0 diff --git a/src/ecmascript/mujs/message.c b/src/ecmascript/mujs/message.c index 8be0b723..900ea02d 100644 --- a/src/ecmascript/mujs/message.c +++ b/src/ecmascript/mujs/message.c @@ -19,11 +19,25 @@ static void mjs_messageEvent_get_property_lastEventId(js_State *J); static void mjs_messageEvent_get_property_origin(js_State *J); static void mjs_messageEvent_get_property_source(js_State *J); +static void mjs_messageEvent_get_property_bubbles(js_State *J); +static void mjs_messageEvent_get_property_cancelable(js_State *J); +static void mjs_messageEvent_get_property_composed(js_State *J); +static void mjs_messageEvent_get_property_defaultPrevented(js_State *J); +static void mjs_messageEvent_get_property_type(js_State *J); + +static void mjs_messageEvent_preventDefault(js_State *J); + struct message_event { char *data; char *lastEventId; char *origin; char *source; + + char *type_; + unsigned int bubbles:1; + unsigned int cancelable:1; + unsigned int composed:1; + unsigned int defaultPrevented:1; }; static @@ -39,6 +53,7 @@ void mjs_messageEvent_finalizer(js_State *J, void *val) mem_free_if(event->lastEventId); mem_free_if(event->origin); mem_free_if(event->source); + mem_free_if(event->type_); mem_free(event); } } @@ -69,14 +84,64 @@ mjs_push_messageEvent(js_State *J, char *data, char *origin, char *source) js_newobject(J); { js_newuserdata(J, "event", event, mjs_messageEvent_finalizer); - + addmethod(J, "preventDefault", mjs_messageEvent_preventDefault, 0); + addproperty(J, "bubbles", mjs_messageEvent_get_property_bubbles, NULL); + addproperty(J, "cancelable", mjs_messageEvent_get_property_cancelable, NULL); + addproperty(J, "composed", mjs_messageEvent_get_property_composed, NULL); addproperty(J, "data", mjs_messageEvent_get_property_data, NULL); + addproperty(J, "defaultPrevented", mjs_messageEvent_get_property_defaultPrevented, NULL); addproperty(J, "lastEventId", mjs_messageEvent_get_property_lastEventId, NULL); addproperty(J, "origin", mjs_messageEvent_get_property_origin, NULL); addproperty(J, "source", mjs_messageEvent_get_property_source, NULL); + addproperty(J, "type", mjs_messageEvent_get_property_type, NULL); } } +static void +mjs_messageEvent_get_property_bubbles(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + js_pushboolean(J, event->bubbles); +} + +static void +mjs_messageEvent_get_property_cancelable(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + js_pushboolean(J, event->cancelable); +} + +static void +mjs_messageEvent_get_property_composed(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + js_pushboolean(J, event->composed); +} + static void mjs_messageEvent_get_property_data(js_State *J) { @@ -92,6 +157,21 @@ mjs_messageEvent_get_property_data(js_State *J) js_pushstring(J, event->data); } +static void +mjs_messageEvent_get_property_defaultPrevented(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + js_pushboolean(J, event->defaultPrevented); +} + static void mjs_messageEvent_get_property_lastEventId(js_State *J) { @@ -136,3 +216,113 @@ mjs_messageEvent_get_property_source(js_State *J) } js_pushstring(J, event->source); } + +static void +mjs_messageEvent_get_property_type(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + js_pushstring(J, event->type_ ?: ""); +} + +static void +mjs_messageEvent_preventDefault(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event"); + + if (!event) { + js_pushnull(J); + return; + } + if (event->cancelable) { + event->defaultPrevented = 1; + } + js_pushundefined(J); +} + +static void +mjs_messageEvent_fun(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushundefined(J); +} + +static void +mjs_messageEvent_constructor(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct message_event *event = (struct message_event *)mem_calloc(1, sizeof(*event)); + + if (!event) { + return; + } + event->type_ = null_or_stracpy(js_tostring(J, 1)); + + js_getproperty(J, 2, "bubbles"); + event->bubbles = js_toboolean(J, -1); + js_pop(J, 1); + js_getproperty(J, 2, "cancelable"); + event->cancelable = js_toboolean(J, -1); + js_pop(J, 1); + js_getproperty(J, 2, "composed"); + event->composed = js_toboolean(J, -1); + js_pop(J, 1); + + if (js_hasproperty(J, 2, "data")) { + event->data = null_or_stracpy(js_tostring(J, -1)); + js_pop(J, 1); + } + + if (js_hasproperty(J, 2, "lastEventId")) { + event->lastEventId = null_or_stracpy(js_tostring(J, -1)); + js_pop(J, 1); + } + + if (js_hasproperty(J, 2, "origin")) { + event->origin = null_or_stracpy(js_tostring(J, -1)); + js_pop(J, 1); + } + + if (js_hasproperty(J, 2, "source")) { + event->source = null_or_stracpy(js_tostring(J, -1)); + js_pop(J, 1); + } + + js_newobject(J); + { + js_newuserdata(J, "event", event, mjs_messageEvent_finalizer); + addmethod(J, "preventDefault", mjs_messageEvent_preventDefault, 0); + addproperty(J, "bubbles", mjs_messageEvent_get_property_bubbles, NULL); + addproperty(J, "cancelable", mjs_messageEvent_get_property_cancelable, NULL); + addproperty(J, "composed", mjs_messageEvent_get_property_composed, NULL); + addproperty(J, "data", mjs_messageEvent_get_property_data, NULL); + addproperty(J, "defaultPrevented", mjs_messageEvent_get_property_defaultPrevented, NULL); + addproperty(J, "lastEventId", mjs_messageEvent_get_property_lastEventId, NULL); + addproperty(J, "origin", mjs_messageEvent_get_property_origin, NULL); + addproperty(J, "source", mjs_messageEvent_get_property_source, NULL); + addproperty(J, "type", mjs_messageEvent_get_property_type, NULL); + } +} + +int +mjs_messageEvent_init(js_State *J) +{ + js_pushglobal(J); + js_newcconstructor(J, mjs_messageEvent_fun, mjs_messageEvent_constructor, "MessageEvent", 0); + js_defglobal(J, "MessageEvent", JS_DONTENUM); + return 0; +} diff --git a/src/ecmascript/mujs/message.h b/src/ecmascript/mujs/message.h index 6de0d384..0946b3b3 100644 --- a/src/ecmascript/mujs/message.h +++ b/src/ecmascript/mujs/message.h @@ -7,6 +7,7 @@ extern "C" { #endif +int mjs_messageEvent_init(js_State *J); void mjs_push_messageEvent(js_State *J, char *data, char *origin, char *source); #ifdef __cplusplus