From 1c44857da47f4a9b0dae0ccb45d93209267e79c8 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 9 Sep 2025 19:09:37 -0400 Subject: [PATCH] core: Move register and unregister to C. --- core/core.js | 17 -------------- src/api.js.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/core/core.js b/core/core.js index 7a463106..69dbfc86 100644 --- a/core/core.js +++ b/core/core.js @@ -199,23 +199,6 @@ async function getProcessBlob(blobId, key, options) { let imports = { core: { broadcast: broadcast.bind(process), - register: function (eventName, handler) { - if (!process.eventHandlers[eventName]) { - process.eventHandlers[eventName] = []; - } - process.eventHandlers[eventName].push(handler); - }, - unregister: function (eventName, handler) { - if (process.eventHandlers[eventName]) { - let index = process.eventHandlers[eventName].indexOf(handler); - if (index != -1) { - process.eventHandlers[eventName].splice(index, 1); - } - if (process.eventHandlers[eventName].length == 0) { - delete process.eventHandlers[eventName]; - } - } - }, user: getUser(process, process), users: async function () { try { diff --git a/src/api.js.c b/src/api.js.c index 8b08b9f6..7ad62d97 100644 --- a/src/api.js.c +++ b/src/api.js.c @@ -147,12 +147,78 @@ static JSValue _tf_api_core_apps(JSContext* context, JSValueConst this_val, int return result; } +static JSValue _tf_api_core_register(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data) +{ + JSValue event_name = argv[0]; + JSValue handler = argv[1]; + JSValue process = data[0]; + JSValue event_handlers = JS_GetPropertyStr(context, process, "eventHandlers"); + JSAtom atom = JS_ValueToAtom(context, event_name); + JSValue array = JS_GetProperty(context, event_handlers, atom); + if (!JS_IsArray(context, array)) + { + JS_FreeValue(context, array); + array = JS_NewArray(context); + JS_SetProperty(context, event_handlers, atom, JS_DupValue(context, array)); + } + JS_SetPropertyUint32(context, array, tf_util_get_length(context, array), JS_DupValue(context, handler)); + JS_FreeValue(context, array); + JS_FreeAtom(context, atom); + JS_FreeValue(context, event_handlers); + return JS_UNDEFINED; +} + +static JSValue _tf_api_core_unregister(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data) +{ + JSValue event_name = argv[0]; + JSValue handler = argv[1]; + JSValue process = data[0]; + JSValue event_handlers = JS_GetPropertyStr(context, process, "eventHandlers"); + JSAtom atom = JS_ValueToAtom(context, event_name); + JSValue array = JS_GetProperty(context, event_handlers, atom); + + if (JS_IsArray(context, array)) + { + JSValue index_of = JS_GetPropertyStr(context, array, "indexOf"); + + JSValue index = JS_Call(context, index_of, array, 1, &handler); + int int_index = -1; + JS_ToInt32(context, &int_index, index); + if (int_index != -1) + { + JSValue splice = JS_GetPropertyStr(context, array, "splice"); + JSValue splice_args[] = + { + index, + JS_NewInt32(context, 1), + }; + JSValue result = JS_Call(context, splice, array, 2, splice_args); + JS_FreeValue(context, result); + JS_FreeValue(context, splice); + } + JS_FreeValue(context, index); + JS_FreeValue(context, index_of); + + if (tf_util_get_length(context, array) == 0) + { + JS_DeleteProperty(context, event_handlers, atom, 0); + } + } + + JS_FreeValue(context, array); + JS_FreeAtom(context, atom); + JS_FreeValue(context, event_handlers); + return JS_UNDEFINED; +} + static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue imports = argv[0]; JSValue process = argv[1]; JSValue core = JS_GetPropertyStr(context, imports, "core"); JS_SetPropertyStr(context, core, "apps", JS_NewCFunctionData(context, _tf_api_core_apps, 1, 0, 1, &process)); + JS_SetPropertyStr(context, core, "register", JS_NewCFunctionData(context, _tf_api_core_register, 2, 0, 1, &process)); + JS_SetPropertyStr(context, core, "unregister", JS_NewCFunctionData(context, _tf_api_core_unregister, 2, 0, 1, &process)); JS_FreeValue(context, core); return JS_UNDEFINED; }