diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 36c939d4c0..7555b6b470 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -1135,6 +1135,23 @@ void ScriptEngine::Tick() JSContext* jobCtx; while (true) { + // TODO: this is a bit of a hack, perhaps the PluginScope class should be replaced by a better way of tracking context + // Lookup the plugin from the context with a slow linear search + JSContext* ctx = JS_GetPendingJobContext(_runtime); + std::shared_ptr plugin; + if (ctx != nullptr) + { + for (const auto& p : _plugins) + { + if (p->GetContext() == ctx) + { + plugin = p; + break; + } + } + } + ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, false); + if (JS_ExecutePendingJob(_runtime, &jobCtx) == 0) break; } diff --git a/src/thirdparty/quickjs-ng/quickjs-amalgam.c b/src/thirdparty/quickjs-ng/quickjs-amalgam.c index 6f9fdd51f3..9b7d70673c 100644 --- a/src/thirdparty/quickjs-ng/quickjs-amalgam.c +++ b/src/thirdparty/quickjs-ng/quickjs-amalgam.c @@ -12344,6 +12344,14 @@ bool JS_IsJobPending(JSRuntime *rt) return !list_empty(&rt->job_list); } +JSContext *JS_GetPendingJobContext(JSRuntime *rt) +{ + if (JS_IsJobPending(rt)) { + return list_entry(rt->job_list.next, JSJobEntry, link)->ctx; + } + return NULL; +} + /* return < 0 if exception, 0 if no job pending, 1 if a job was executed successfully. the context of the job is stored in '*pctx' */ int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx) diff --git a/src/thirdparty/quickjs-ng/quickjs.h b/src/thirdparty/quickjs-ng/quickjs.h index 63f0b0995f..1117ea0269 100644 --- a/src/thirdparty/quickjs-ng/quickjs.h +++ b/src/thirdparty/quickjs-ng/quickjs.h @@ -1193,6 +1193,7 @@ JS_EXTERN int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func, int argc, JSValueConst *argv); JS_EXTERN bool JS_IsJobPending(JSRuntime *rt); +JS_EXTERN JSContext *JS_GetPendingJobContext(JSRuntime *rt); JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx); /* Structure to retrieve (de)serialized SharedArrayBuffer objects. */