core: Move register and unregister to C.
This commit is contained in:
66
src/api.js.c
66
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user