From 00b108f200b75ea48525c5e5a661f2175aab7168 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 10 Mar 2024 16:30:26 +0100 Subject: [PATCH] [quickjs] event placeholder --- src/ecmascript/quickjs.c | 2 + src/ecmascript/quickjs/Makefile | 2 +- src/ecmascript/quickjs/event.c | 163 +++++++++++++++++++++++++++++ src/ecmascript/quickjs/event.h | 17 +++ src/ecmascript/quickjs/meson.build | 2 +- 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/ecmascript/quickjs/event.c create mode 100644 src/ecmascript/quickjs/event.h diff --git a/src/ecmascript/quickjs.c b/src/ecmascript/quickjs.c index 91a2752a0..bcab6e31d 100644 --- a/src/ecmascript/quickjs.c +++ b/src/ecmascript/quickjs.c @@ -28,6 +28,7 @@ #include "ecmascript/quickjs/console.h" #include "ecmascript/quickjs/document.h" #include "ecmascript/quickjs/element.h" +#include "ecmascript/quickjs/event.h" #include "ecmascript/quickjs/heartbeat.h" #include "ecmascript/quickjs/history.h" #include "ecmascript/quickjs/localstorage.h" @@ -174,6 +175,7 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter) js_localstorage_init(ctx); js_element_init(ctx); js_xhr_init(ctx); + js_event_init(ctx); interpreter->document_obj = js_document_init(ctx); diff --git a/src/ecmascript/quickjs/Makefile b/src/ecmascript/quickjs/Makefile index 03bc54359..ffcc014fc 100644 --- a/src/ecmascript/quickjs/Makefile +++ b/src/ecmascript/quickjs/Makefile @@ -1,7 +1,7 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config -OBJS = attr.o attributes.o collection.o console.o css.o document.o element.o form.o forms.o heartbeat.o history.o implementation.o input.o \ +OBJS = attr.o attributes.o collection.o console.o css.o document.o element.o event.o form.o forms.o heartbeat.o history.o implementation.o input.o \ keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o screen.o style.o unibar.o window.o xhr.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/quickjs/event.c b/src/ecmascript/quickjs/event.c new file mode 100644 index 000000000..677a6fd35 --- /dev/null +++ b/src/ecmascript/quickjs/event.c @@ -0,0 +1,163 @@ +/* The QuickJS Event object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" +#include "ecmascript/quickjs/event.h" +#include "intl/charsets.h" +#include "terminal/event.h" + +#define countof(x) (sizeof(x) / sizeof((x)[0])) + +static JSClassID js_event_class_id; + +static JSValue js_event_get_property_type(JSContext *ctx, JSValueConst this_val); + +struct eljs_event { + char *type_; +}; + +static +void js_event_finalizer(JSRuntime *rt, JSValue val) +{ + REF_JS(val); + + struct eljs_event *event = (struct eljs_event *)JS_GetOpaque(val, js_event_class_id); + + if (event) { + mem_free_if(event->type_); + mem_free(event); + } +} + +static JSClassDef js_event_class = { + "Event", + js_event_finalizer +}; + +static const JSCFunctionListEntry js_event_proto_funcs[] = { + JS_CGETSET_DEF("type", js_event_get_property_type, NULL) +}; + +static JSValue +js_event_get_property_type(JSContext *ctx, JSValueConst this_val) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + struct eljs_event *event = (struct eljs_event *)(JS_GetOpaque(this_val, js_event_class_id)); + + if (!event) { + return JS_NULL; + } + if (!event->type_) { + JSValue r = JS_NewString(ctx, ""); + RETURN_JS(r); + } + JSValue r = JS_NewString(ctx, event->type_); + + RETURN_JS(r); +} + +static JSValue +js_event_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(new_target); + + JSValue obj = JS_NewObjectClass(ctx, js_event_class_id); + REF_JS(obj); + + if (JS_IsException(obj)) { + return obj; + } + struct eljs_event *event = (struct eljs_event *)mem_calloc(1, sizeof(*event)); + + if (!event) { + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; + } + + if (argc > 0) { + const char *str; + size_t len; + + str = JS_ToCStringLen(ctx, &len, argv[0]); + + if (str) { + event->type_ = memacpy(str, len); + JS_FreeCString(ctx, str); + } + } + JS_SetOpaque(obj, event); + + return obj; +} + + +static void +JS_NewGlobalCConstructor2(JSContext *ctx, JSValue func_obj, const char *name, JSValueConst proto) +{ + REF_JS(func_obj); + REF_JS(proto); + + JSValue global_object = JS_GetGlobalObject(ctx); + + JS_DefinePropertyValueStr(ctx, global_object, name, + JS_DupValue(ctx, func_obj), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + JS_SetConstructor(ctx, func_obj, proto); + JS_FreeValue(ctx, func_obj); + JS_FreeValue(ctx, global_object); +} + +static JSValueConst +JS_NewGlobalCConstructor(JSContext *ctx, const char *name, JSCFunction *func, int length, JSValueConst proto) +{ + JSValue func_obj; + func_obj = JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_constructor_or_func, 0); + REF_JS(func_obj); + REF_JS(proto); + + JS_NewGlobalCConstructor2(ctx, func_obj, name, proto); + + return func_obj; +} + +int +js_event_init(JSContext *ctx) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JSValue proto, obj; + + /* Event class */ + JS_NewClassID(&js_event_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_event_class_id, &js_event_class); + proto = JS_NewObject(ctx); + REF_JS(proto); + + JS_SetPropertyFunctionList(ctx, proto, js_event_proto_funcs, countof(js_event_proto_funcs)); + JS_SetClassProto(ctx, js_event_class_id, proto); + + /* Event object */ + obj = JS_NewGlobalCConstructor(ctx, "Event", js_event_constructor, 1, proto); + REF_JS(obj); + +// JS_SetPropertyFunctionList(ctx, obj, js_event_class_funcs, countof(js_event_class_funcs)); + + return 0; +} diff --git a/src/ecmascript/quickjs/event.h b/src/ecmascript/quickjs/event.h new file mode 100644 index 000000000..7e3c30540 --- /dev/null +++ b/src/ecmascript/quickjs/event.h @@ -0,0 +1,17 @@ +#ifndef EL__ECMASCRIPT_QUICKJS_EVENT_H +#define EL__ECMASCRIPT_QUICKJS_EVENT_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int js_event_init(JSContext *ctx); +//JSValue get_keyboardEvent(JSContext *ctx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ecmascript/quickjs/meson.build b/src/ecmascript/quickjs/meson.build index babc9624f..9dff8f282 100644 --- a/src/ecmascript/quickjs/meson.build +++ b/src/ecmascript/quickjs/meson.build @@ -1,3 +1,3 @@ -srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'document.c', 'element.c', 'form.c', 'forms.c', 'heartbeat.c', 'history.c', +srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'document.c', 'element.c', 'event.c', 'form.c', 'forms.c', 'heartbeat.c', 'history.c', 'implementation.c', 'input.c', 'keyboard.c', 'localstorage.c', 'location.c', 'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'style.c', 'unibar.c', 'window.c', 'xhr.c')