diff --git a/src/cli/modules.c b/src/cli/modules.c index 481c284d..2c3fd9cd 100644 --- a/src/cli/modules.c +++ b/src/cli/modules.c @@ -239,15 +239,26 @@ static WrenForeignMethodFn findMethod(ClassRegistry* clas, return NULL; } -char* readBuiltInModule(const char* name) +void loadModuleComplete(WrenVM* vm, const char* name, struct WrenLoadModuleResult result) { + if (result.source == NULL) return; + + free((void*)result.source); +} + +WrenLoadModuleResult loadBuiltInModule(const char* name) +{ + WrenLoadModuleResult result = {0}; ModuleRegistry* module = findModule(name); - if (module == NULL) return NULL; + if (module == NULL) return result; size_t length = strlen(*module->source); char* copy = (char*)malloc(length + 1); memcpy(copy, *module->source, length + 1); - return copy; + + result.onComplete = loadModuleComplete; + result.source = copy; + return result; } WrenForeignMethodFn bindBuiltInForeignMethod( diff --git a/src/cli/modules.h b/src/cli/modules.h index 1db5fed3..3150f382 100644 --- a/src/cli/modules.h +++ b/src/cli/modules.h @@ -7,7 +7,8 @@ #include "wren.h" // Returns the source for built-in module [name]. -char* readBuiltInModule(const char* module); +WrenLoadModuleResult loadBuiltInModule(const char* module); +void loadModuleComplete(WrenVM* vm, const char* name, struct WrenLoadModuleResult result); // Looks up a foreign method in a built-in module. // diff --git a/src/cli/vm.c b/src/cli/vm.c index 7052d7d4..48d2b1ee 100644 --- a/src/cli/vm.c +++ b/src/cli/vm.c @@ -164,15 +164,16 @@ static const char* resolveModule(WrenVM* vm, const char* importer, // // Returns it if found, or NULL if the module could not be found. Exits if the // module was found but could not be read. -static char* readModule(WrenVM* vm, const char* module) +static WrenLoadModuleResult loadModule(WrenVM* vm, const char* module) { + WrenLoadModuleResult result = {0}; Path* filePath; if (pathType(module) == PATH_TYPE_SIMPLE) { // If there is no "wren_modules" directory, then the only logical imports // we can handle are built-in ones. Let the VM try to handle it. findModulesDirectory(); - if (wrenModulesDirectory == NULL) return readBuiltInModule(module); + if (wrenModulesDirectory == NULL) return loadBuiltInModule(module); // TODO: Should we explicitly check for the existence of the module's base // directory inside "wren_modules" here? @@ -194,15 +195,16 @@ static char* readModule(WrenVM* vm, const char* module) // Add a ".wren" file extension. pathAppendString(filePath, ".wren"); - char* source = readFile(filePath->chars); + result.onComplete = loadModuleComplete; + result.source = readFile(filePath->chars); pathFree(filePath); // If we didn't find it, it may be a module built into the CLI or VM, so keep // going. - if (source != NULL) return source; + if (result.source != NULL) return result; // Otherwise, see if it's a built-in module. - return readBuiltInModule(module); + return loadBuiltInModule(module); } // Binds foreign methods declared in either built in modules, or the injected @@ -271,7 +273,7 @@ static void initVM() config.bindForeignMethodFn = bindForeignMethod; config.bindForeignClassFn = bindForeignClass; config.resolveModuleFn = resolveModule; - config.loadModuleFn = readModule; + config.loadModuleFn = loadModule; config.writeFn = write; config.errorFn = reportError;