Skip to content

Commit

Permalink
update code to use wren 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby0x1 committed Apr 26, 2021
1 parent 9fb4c16 commit 8229047
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/cli/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/cli/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
14 changes: 8 additions & 6 deletions src/cli/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 8229047

Please sign in to comment.