Update filename check in import
@@ -1565,31 +1565,32 @@ static DictuInterpretResult run(DictuVM *vm) {
ObjString *fileName = READ_STRING();
Value moduleVal;
+ char path[PATH_MAX];
+ if (!resolvePath(frame->closure->function->module->path->chars, fileName->chars, path)) {
+ RUNTIME_ERROR("Could not open file \"%s\".", fileName->chars);
+ }
+
+ ObjString *pathObj = copyString(vm, path, strlen(path));
+ push(vm, OBJ_VAL(pathObj));
+
// If we have imported this file already, skip.
- if (tableGet(&vm->modules, fileName, &moduleVal)) {
+ if (tableGet(&vm->modules, pathObj, &moduleVal)) {
+ pop(vm);
vm->lastModule = AS_MODULE(moduleVal);
push(vm, NIL_VAL);
DISPATCH();
}
- char path[PATH_MAX];
- if (!resolvePath(frame->closure->function->module->path->chars, fileName->chars, path)) {
- RUNTIME_ERROR("Could not open file \"%s\".", fileName->chars);
- }
-
char *source = readFile(vm, path);
if (source == NULL) {
RUNTIME_ERROR("Could not open file \"%s\".", fileName->chars);
}
- ObjString *pathObj = copyString(vm, path, strlen(path));
- push(vm, OBJ_VAL(pathObj));
ObjModule *module = newModule(vm, pathObj);
module->path = dirname(vm, path, strlen(path));
vm->lastModule = module;
pop(vm);
- tableSet(vm, &vm->modules, fileName, OBJ_VAL(module));
push(vm, OBJ_VAL(module));
ObjFunction *function = compile(vm, module, source);
pop(vm);
