core: Move register and unregister to C.

This commit is contained in:
2025-09-09 19:09:37 -04:00
parent bca4440867
commit 1c44857da4
2 changed files with 66 additions and 17 deletions

View File

@@ -199,23 +199,6 @@ async function getProcessBlob(blobId, key, options) {
let imports = { let imports = {
core: { core: {
broadcast: broadcast.bind(process), 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), user: getUser(process, process),
users: async function () { users: async function () {
try { try {

View File

@@ -147,12 +147,78 @@ static JSValue _tf_api_core_apps(JSContext* context, JSValueConst this_val, int
return result; 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) static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
JSValue imports = argv[0]; JSValue imports = argv[0];
JSValue process = argv[1]; JSValue process = argv[1];
JSValue core = JS_GetPropertyStr(context, imports, "core"); 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, "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); JS_FreeValue(context, core);
return JS_UNDEFINED; return JS_UNDEFINED;
} }