From ee21e43bdb022cc9cd649aa1d85e4041b130b1b4 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 2 Jan 2025 01:21:20 -0800 Subject: [PATCH] Scripting: Install callbacks in mScriptContextAttachCore instead of mCoreThread --- src/core/scripting.c | 30 +++++++++++++++++++++++ src/core/thread.c | 58 +++----------------------------------------- 2 files changed, 33 insertions(+), 55 deletions(-) diff --git a/src/core/scripting.c b/src/core/scripting.c index b36a91412..20659f0bc 100644 --- a/src/core/scripting.c +++ b/src/core/scripting.c @@ -1343,6 +1343,24 @@ static uint8_t _readLuminance(struct GBALuminanceSource* luminance) { } #endif +#define CALLBACK(NAME) _mScriptCoreCallback ## NAME +#define DEFINE_CALLBACK(NAME) \ + void CALLBACK(NAME) (void* context) { \ + struct mScriptContext* scriptContext = context; \ + if (!scriptContext) { \ + return; \ + } \ + mScriptContextTriggerCallback(scriptContext, #NAME, NULL); \ + } + +DEFINE_CALLBACK(frame) +DEFINE_CALLBACK(crashed) +DEFINE_CALLBACK(sleep) +DEFINE_CALLBACK(stop) +DEFINE_CALLBACK(keysRead) +DEFINE_CALLBACK(savedataUpdated) +DEFINE_CALLBACK(alarm) + void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core) { struct mScriptValue* coreValue = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCoreAdapter)); struct mScriptCoreAdapter* adapter = calloc(1, sizeof(*adapter)); @@ -1375,6 +1393,18 @@ void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core } #endif + struct mCoreCallbacks callbacks = { + .videoFrameEnded = CALLBACK(frame), + .coreCrashed = CALLBACK(crashed), + .sleep = CALLBACK(sleep), + .shutdown = CALLBACK(stop), + .keysRead = CALLBACK(keysRead), + .savedataUpdated = CALLBACK(savedataUpdated), + .alarm = CALLBACK(alarm), + .context = context + }; + core->addCoreCallbacks(core, &callbacks); + _rebuildMemoryMap(context, adapter); coreValue->value.opaque = adapter; diff --git a/src/core/thread.c b/src/core/thread.c index 2b16a03c7..988ce0848 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -198,42 +198,6 @@ void _coreShutdown(void* context) { MutexUnlock(&thread->impl->stateMutex); } -#ifdef ENABLE_SCRIPTING -#define ADD_CALLBACK(NAME) \ -void _script_ ## NAME(void* context) { \ - struct mCoreThread* threadContext = context; \ - if (!threadContext->scriptContext) { \ - return; \ - } \ - mScriptContextTriggerCallback(threadContext->scriptContext, #NAME, NULL); \ -} - -ADD_CALLBACK(frame) -ADD_CALLBACK(crashed) -ADD_CALLBACK(sleep) -ADD_CALLBACK(stop) -ADD_CALLBACK(keysRead) -ADD_CALLBACK(savedataUpdated) -ADD_CALLBACK(alarm) - -#undef ADD_CALLBACK -#define SCRIPT(NAME) _script_ ## NAME - -static void _mCoreThreadAddCallbacks(struct mCoreThread* threadContext) { - struct mCoreCallbacks callbacks = { - .videoFrameEnded = SCRIPT(frame), - .coreCrashed = SCRIPT(crashed), - .sleep = SCRIPT(sleep), - .shutdown = SCRIPT(stop), - .keysRead = SCRIPT(keysRead), - .savedataUpdated = SCRIPT(savedataUpdated), - .alarm = SCRIPT(alarm), - .context = threadContext - }; - threadContext->core->addCoreCallbacks(threadContext->core, &callbacks); -} -#endif - static THREAD_ENTRY _mCoreThreadRun(void* context) { struct mCoreThread* threadContext = context; #ifdef USE_PTHREADS @@ -279,7 +243,6 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { struct mScriptContext* scriptContext = threadContext->scriptContext; if (scriptContext) { mScriptContextAttachCore(scriptContext, core); - _mCoreThreadAddCallbacks(threadContext); } #endif @@ -289,12 +252,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { } #ifdef ENABLE_SCRIPTING // startCallback could add a script context - if (scriptContext != threadContext->scriptContext) { - scriptContext = threadContext->scriptContext; - if (scriptContext) { - _mCoreThreadAddCallbacks(threadContext); - } - } + scriptContext = threadContext->scriptContext; if (scriptContext) { mScriptContextTriggerCallback(scriptContext, "start", NULL); } @@ -312,12 +270,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { #ifdef ENABLE_SCRIPTING // resetCallback could add a script context - if (scriptContext != threadContext->scriptContext) { - scriptContext = threadContext->scriptContext; - if (scriptContext) { - _mCoreThreadAddCallbacks(threadContext); - } - } + scriptContext = threadContext->scriptContext; if (scriptContext) { mScriptContextTriggerCallback(scriptContext, "reset", NULL); } @@ -372,12 +325,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { } } #ifdef ENABLE_SCRIPTING - if (scriptContext != threadContext->scriptContext) { - scriptContext = threadContext->scriptContext; - if (scriptContext) { - _mCoreThreadAddCallbacks(threadContext); - } - } + scriptContext = threadContext->scriptContext; #endif if (wasPaused && !(impl->requested & mTHREAD_REQ_PAUSE)) { break;