From a59349af8a8f852e920b9c09a28e3f44009932ca Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 11 May 2022 23:26:03 -0700 Subject: [PATCH] Scripting: Fix some bugs with context globals --- src/core/scripting.c | 2 -- src/script/context.c | 7 +++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/scripting.c b/src/core/scripting.c index 7044583b2..52fa9a76b 100644 --- a/src/core/scripting.c +++ b/src/core/scripting.c @@ -334,7 +334,6 @@ void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core coreValue->value.opaque = adapter; coreValue->flags = mSCRIPT_VALUE_FLAG_FREE_BUFFER; mScriptContextSetGlobal(context, "emu", coreValue); - mScriptValueDeref(coreValue); } void mScriptContextDetachCore(struct mScriptContext* context) { @@ -367,7 +366,6 @@ void mScriptContextAttachLogger(struct mScriptContext* context, struct mLogger* struct mScriptValue* value = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mLogger)); value->value.opaque = logger; mScriptContextSetGlobal(context, "console", value); - mScriptValueDeref(value); } void mScriptContextDetachLogger(struct mScriptContext* context) { diff --git a/src/script/context.c b/src/script/context.c index 93d414360..ba552be41 100644 --- a/src/script/context.c +++ b/src/script/context.c @@ -24,6 +24,11 @@ static void _engineContextDestroy(void* ctx) { context->destroy(context); } +static void _engineAddGlobal(const char* key, void* value, void* user) { + struct mScriptEngineContext* context = user; + context->setGlobal(context, key, value); +} + static void _contextAddGlobal(const char* key, void* value, void* user) { UNUSED(key); struct mScriptEngineContext* context = value; @@ -99,6 +104,7 @@ struct mScriptEngineContext* mScriptContextRegisterEngine(struct mScriptContext* struct mScriptEngineContext* ectx = engine->create(engine, context); if (ectx) { HashTableInsert(&context->engines, engine->name, ectx); + HashTableEnumerate(&context->rootScope, _engineAddGlobal, ectx); } return ectx; } @@ -116,6 +122,7 @@ void mScriptContextSetGlobal(struct mScriptContext* context, const char* key, st mScriptContextClearWeakref(context, oldValue->value.u32); } uint32_t weakref = mScriptContextSetWeakref(context, value); + mScriptValueDeref(value); value = mScriptValueAlloc(mSCRIPT_TYPE_MS_WEAKREF); value->value.u32 = weakref; HashTableInsert(&context->rootScope, key, value);