Scripting: Add callback arguments

This commit is contained in:
Vicki Pfau 2023-01-02 01:04:50 -08:00
parent fc2b94f9f7
commit fcfab847fc
5 changed files with 14 additions and 11 deletions

View File

@ -96,7 +96,7 @@ void mScriptContextAttachSocket(struct mScriptContext* context);
void mScriptContextExportConstants(struct mScriptContext* context, const char* nspace, struct mScriptKVPair* constants);
void mScriptContextExportNamespace(struct mScriptContext* context, const char* nspace, struct mScriptKVPair* value);
void mScriptContextTriggerCallback(struct mScriptContext*, const char* callback);
void mScriptContextTriggerCallback(struct mScriptContext*, const char* callback, struct mScriptList* args);
uint32_t mScriptContextAddCallback(struct mScriptContext*, const char* callback, struct mScriptValue* value);
void mScriptContextRemoveCallback(struct mScriptContext*, uint32_t cbid);

View File

@ -651,7 +651,7 @@ static struct mScriptValue* _mScriptCoreAdapterGet(struct mScriptCoreAdapter* ad
static void _mScriptCoreAdapterReset(struct mScriptCoreAdapter* adapter) {
adapter->core->reset(adapter->core);
mScriptContextTriggerCallback(adapter->context, "reset");
mScriptContextTriggerCallback(adapter->context, "reset", NULL);
}
mSCRIPT_DECLARE_STRUCT(mScriptCoreAdapter);

View File

@ -194,7 +194,7 @@ void _script_ ## NAME(void* context) { \
if (!threadContext->scriptContext) { \
return; \
} \
mScriptContextTriggerCallback(threadContext->scriptContext, #NAME); \
mScriptContextTriggerCallback(threadContext->scriptContext, #NAME, NULL); \
}
ADD_CALLBACK(frame)
@ -283,7 +283,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
}
}
if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "start");
mScriptContextTriggerCallback(scriptContext, "start", NULL);
}
#endif
@ -304,7 +304,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
}
}
if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "reset");
mScriptContextTriggerCallback(scriptContext, "reset", NULL);
}
#endif
@ -404,7 +404,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
}
#ifdef ENABLE_SCRIPTING
if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "reset");
mScriptContextTriggerCallback(scriptContext, "reset", NULL);
}
#endif
}
@ -428,7 +428,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
}
#ifdef ENABLE_SCRIPTING
if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "shutdown");
mScriptContextTriggerCallback(scriptContext, "shutdown", NULL);
mScriptContextDetachCore(scriptContext);
}
#endif

View File

@ -211,7 +211,7 @@ void mScriptContextDisownWeakref(struct mScriptContext* context, uint32_t weakre
poolEntry->refs = mSCRIPT_VALUE_UNREF;
}
void mScriptContextTriggerCallback(struct mScriptContext* context, const char* callback) {
void mScriptContextTriggerCallback(struct mScriptContext* context, const char* callback, struct mScriptList* args) {
struct mScriptValue* list = HashTableLookup(&context->callbacks, callback);
if (!list) {
return;
@ -224,6 +224,9 @@ void mScriptContextTriggerCallback(struct mScriptContext* context, const char* c
continue;
}
mScriptFrameInit(&frame);
if (args) {
mScriptListCopy(&frame.arguments, args);
}
if (fn->type->base == mSCRIPT_TYPE_WRAPPER) {
fn = mScriptValueUnwrap(fn);
}

View File

@ -82,15 +82,15 @@ M_TEST_DEFINE(callbacks) {
TEST_VALUE(S32, "val", 0);
mScriptContextTriggerCallback(&context, "test");
mScriptContextTriggerCallback(&context, "test", NULL);
TEST_VALUE(S32, "val", 1);
mScriptContextTriggerCallback(&context, "test");
mScriptContextTriggerCallback(&context, "test", NULL);
TEST_VALUE(S32, "val", 2);
TEST_PROGRAM("callbacks:remove(id)");
mScriptContextTriggerCallback(&context, "test");
mScriptContextTriggerCallback(&context, "test", NULL);
TEST_VALUE(S32, "val", 2);
mScriptContextDeinit(&context);