Scripting: Fix some bugs with context globals

This commit is contained in:
Vicki Pfau 2022-05-11 23:26:03 -07:00
parent e8e9a3e3c3
commit a59349af8a
2 changed files with 7 additions and 2 deletions

View File

@ -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) {

View File

@ -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);