mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 14:18:42 +01:00
Clean up getting modules.
Thanks, Michel!
This commit is contained in:
@ -133,7 +133,7 @@ static void collectGarbage(WrenVM* vm)
|
|||||||
// already been freed.
|
// already been freed.
|
||||||
vm->bytesAllocated = 0;
|
vm->bytesAllocated = 0;
|
||||||
|
|
||||||
wrenMarkObj(vm, (Obj*)vm->modules);
|
wrenMarkObj(vm, (Obj*)vm->modules);
|
||||||
|
|
||||||
// Temporary roots.
|
// Temporary roots.
|
||||||
for (int i = 0; i < vm->numTempRoots; i++)
|
for (int i = 0; i < vm->numTempRoots; i++)
|
||||||
@ -142,7 +142,7 @@ static void collectGarbage(WrenVM* vm)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The current fiber.
|
// The current fiber.
|
||||||
wrenMarkObj(vm, (Obj*)vm->fiber);
|
wrenMarkObj(vm, (Obj*)vm->fiber);
|
||||||
|
|
||||||
// The method handles.
|
// The method handles.
|
||||||
for (WrenMethod* handle = vm->methodHandles;
|
for (WrenMethod* handle = vm->methodHandles;
|
||||||
@ -427,26 +427,29 @@ static inline void callFunction(ObjFiber* fiber, Obj* function, int numArgs)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Looks up the previously loaded module with [name].
|
||||||
|
//
|
||||||
|
// Returns `NULL` if no module with that name has been loaded.
|
||||||
|
static ObjModule* getModule(WrenVM* vm, Value name)
|
||||||
|
{
|
||||||
|
Value moduleValue = wrenMapGet(vm->modules, name);
|
||||||
|
return !IS_UNDEFINED(moduleValue) ? AS_MODULE(moduleValue) : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Looks up the core module in the module map.
|
// Looks up the core module in the module map.
|
||||||
static ObjModule* getCoreModule(WrenVM* vm)
|
static ObjModule* getCoreModule(WrenVM* vm)
|
||||||
{
|
{
|
||||||
Value moduleValue = wrenMapGet(vm->modules, NULL_VAL);
|
ObjModule* module = getModule(vm, NULL_VAL);
|
||||||
ASSERT(!IS_UNDEFINED(moduleValue), "Could not find core module.");
|
ASSERT(module != NULL, "Could not find core module.");
|
||||||
return AS_MODULE(moduleValue);
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ObjFiber* loadModule(WrenVM* vm, Value name, const char* source)
|
static ObjFiber* loadModule(WrenVM* vm, Value name, const char* source)
|
||||||
{
|
{
|
||||||
ObjModule* module;
|
ObjModule* module = getModule(vm, name);
|
||||||
Value moduleValue = wrenMapGet(vm->modules, name);
|
|
||||||
|
|
||||||
// See if the module has already been loaded.
|
// See if the module has already been loaded.
|
||||||
if (!IS_UNDEFINED(moduleValue))
|
if (module == NULL)
|
||||||
{
|
|
||||||
// Execute the new code in the context of the existing module.
|
|
||||||
module = AS_MODULE(moduleValue);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
module = wrenNewModule(vm, AS_STRING(name));
|
module = wrenNewModule(vm, AS_STRING(name));
|
||||||
|
|
||||||
@ -505,10 +508,8 @@ static Value importModule(WrenVM* vm, Value name)
|
|||||||
static bool importVariable(WrenVM* vm, Value moduleName, Value variableName,
|
static bool importVariable(WrenVM* vm, Value moduleName, Value variableName,
|
||||||
Value* result)
|
Value* result)
|
||||||
{
|
{
|
||||||
Value moduleValue = wrenMapGet(vm->modules, moduleName);
|
ObjModule* module = getModule(vm, moduleName);
|
||||||
ASSERT(!IS_UNDEFINED(moduleValue), "Should only look up loaded modules.");
|
ASSERT(module != NULL, "Should only look up loaded modules.");
|
||||||
|
|
||||||
ObjModule* module = AS_MODULE(moduleValue);
|
|
||||||
|
|
||||||
ObjString* variable = AS_STRING(variableName);
|
ObjString* variable = AS_STRING(variableName);
|
||||||
uint32_t variableEntry = wrenSymbolTableFind(&module->variableNames,
|
uint32_t variableEntry = wrenSymbolTableFind(&module->variableNames,
|
||||||
@ -1225,9 +1226,8 @@ WrenMethod* wrenGetMethod(WrenVM* vm, const char* module, const char* variable,
|
|||||||
Value moduleName = wrenStringFormat(vm, "$", module);
|
Value moduleName = wrenStringFormat(vm, "$", module);
|
||||||
wrenPushRoot(vm, AS_OBJ(moduleName));
|
wrenPushRoot(vm, AS_OBJ(moduleName));
|
||||||
|
|
||||||
Value moduleValue = wrenMapGet(vm->modules, moduleName);
|
ObjModule* moduleObj = getModule(vm, moduleName);
|
||||||
// TODO: Handle module not being found.
|
// TODO: Handle module not being found.
|
||||||
ObjModule* moduleObj = AS_MODULE(moduleValue);
|
|
||||||
|
|
||||||
int variableSlot = wrenSymbolTableFind(&moduleObj->variableNames,
|
int variableSlot = wrenSymbolTableFind(&moduleObj->variableNames,
|
||||||
variable, strlen(variable));
|
variable, strlen(variable));
|
||||||
|
|||||||
Reference in New Issue
Block a user