Scripting: Install callbacks in mScriptContextAttachCore instead of mCoreThread

This commit is contained in:
Vicki Pfau 2025-01-02 01:21:20 -08:00
parent eb5fc371ff
commit ee21e43bdb
2 changed files with 33 additions and 55 deletions

View File

@ -1343,6 +1343,24 @@ static uint8_t _readLuminance(struct GBALuminanceSource* luminance) {
} }
#endif #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) { void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core) {
struct mScriptValue* coreValue = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCoreAdapter)); struct mScriptValue* coreValue = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCoreAdapter));
struct mScriptCoreAdapter* adapter = calloc(1, sizeof(*adapter)); struct mScriptCoreAdapter* adapter = calloc(1, sizeof(*adapter));
@ -1375,6 +1393,18 @@ void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core
} }
#endif #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); _rebuildMemoryMap(context, adapter);
coreValue->value.opaque = adapter; coreValue->value.opaque = adapter;

View File

@ -198,42 +198,6 @@ void _coreShutdown(void* context) {
MutexUnlock(&thread->impl->stateMutex); 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) { static THREAD_ENTRY _mCoreThreadRun(void* context) {
struct mCoreThread* threadContext = context; struct mCoreThread* threadContext = context;
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
@ -279,7 +243,6 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
struct mScriptContext* scriptContext = threadContext->scriptContext; struct mScriptContext* scriptContext = threadContext->scriptContext;
if (scriptContext) { if (scriptContext) {
mScriptContextAttachCore(scriptContext, core); mScriptContextAttachCore(scriptContext, core);
_mCoreThreadAddCallbacks(threadContext);
} }
#endif #endif
@ -289,12 +252,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
} }
#ifdef ENABLE_SCRIPTING #ifdef ENABLE_SCRIPTING
// startCallback could add a script context // startCallback could add a script context
if (scriptContext != threadContext->scriptContext) { scriptContext = threadContext->scriptContext;
scriptContext = threadContext->scriptContext;
if (scriptContext) {
_mCoreThreadAddCallbacks(threadContext);
}
}
if (scriptContext) { if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "start", NULL); mScriptContextTriggerCallback(scriptContext, "start", NULL);
} }
@ -312,12 +270,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
#ifdef ENABLE_SCRIPTING #ifdef ENABLE_SCRIPTING
// resetCallback could add a script context // resetCallback could add a script context
if (scriptContext != threadContext->scriptContext) { scriptContext = threadContext->scriptContext;
scriptContext = threadContext->scriptContext;
if (scriptContext) {
_mCoreThreadAddCallbacks(threadContext);
}
}
if (scriptContext) { if (scriptContext) {
mScriptContextTriggerCallback(scriptContext, "reset", NULL); mScriptContextTriggerCallback(scriptContext, "reset", NULL);
} }
@ -372,12 +325,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
} }
} }
#ifdef ENABLE_SCRIPTING #ifdef ENABLE_SCRIPTING
if (scriptContext != threadContext->scriptContext) { scriptContext = threadContext->scriptContext;
scriptContext = threadContext->scriptContext;
if (scriptContext) {
_mCoreThreadAddCallbacks(threadContext);
}
}
#endif #endif
if (wasPaused && !(impl->requested & mTHREAD_REQ_PAUSE)) { if (wasPaused && !(impl->requested & mTHREAD_REQ_PAUSE)) {
break; break;