Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative module imports #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/cli/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ static const char* resolveModule(WrenVM* vm, const char* importer,
if (pathType(module) == PATH_TYPE_SIMPLE) return module;

// Get the directory containing the importing module.
Path* path = pathNew(importer);
Path* path;
if (strcmp(importer, "repl") == 0)
{
path = pathNew(rootDirectory);
pathAppendChar(path, '/');
}
else
{
path = pathNew(importer);
}
pathDirName(path);

// Add the relative import path.
Expand Down Expand Up @@ -357,8 +366,15 @@ WrenInterpretResult runFile(const char* path)

WrenInterpretResult runRepl()
{
// This cast is safe since we don't try to free the string later.
rootDirectory = (char*)".";
char buffer[PATH_MAX * 4];
size_t length = sizeof(buffer);
if (uv_cwd(buffer, &length) != 0)
{
fprintf(stderr, "Could not get current working directory.\n");
exit(70); // EX_SOFTWARE.
}
rootDirectory = buffer;

initVM();

printf("\\\\/\"-\n");
Expand Down
5 changes: 5 additions & 0 deletions test/module/relative_different_level/a/foo.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Import module relative to the current module.
import "../bar" for Bar
// expect: ran bar module

System.print(Bar) // expect: from bar
3 changes: 3 additions & 0 deletions test/module/relative_different_level/bar.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// nontest
var Bar = "from bar"
System.print("ran bar module")
3 changes: 3 additions & 0 deletions test/module/relative_same_level/bar.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// nontest
var Bar = "from bar"
System.print("ran bar module")
5 changes: 5 additions & 0 deletions test/module/relative_same_level/foo.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Import module relative to the current module.
import "./bar" for Bar
// expect: ran bar module

System.print(Bar) // expect: from bar