From fed7286aba256b5e40283e2e4fe1544aebfdba14 Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Sun, 7 Jun 2020 13:17:24 +0200 Subject: [PATCH 1/2] Support relative module imports --- src/cli/vm.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/cli/vm.c b/src/cli/vm.c index 7052d7d4..1937c5bc 100644 --- a/src/cli/vm.c +++ b/src/cli/vm.c @@ -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. @@ -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"); From 9707748c9b78e968275cd07526bb3da89af423bc Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Mon, 8 Jun 2020 01:43:54 +0200 Subject: [PATCH 2/2] Add tests for relative imports --- test/module/relative_different_level/a/foo.wren | 5 +++++ test/module/relative_different_level/bar.wren | 3 +++ test/module/relative_same_level/bar.wren | 3 +++ test/module/relative_same_level/foo.wren | 5 +++++ 4 files changed, 16 insertions(+) create mode 100644 test/module/relative_different_level/a/foo.wren create mode 100644 test/module/relative_different_level/bar.wren create mode 100644 test/module/relative_same_level/bar.wren create mode 100644 test/module/relative_same_level/foo.wren diff --git a/test/module/relative_different_level/a/foo.wren b/test/module/relative_different_level/a/foo.wren new file mode 100644 index 00000000..5d401aea --- /dev/null +++ b/test/module/relative_different_level/a/foo.wren @@ -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 diff --git a/test/module/relative_different_level/bar.wren b/test/module/relative_different_level/bar.wren new file mode 100644 index 00000000..739827c7 --- /dev/null +++ b/test/module/relative_different_level/bar.wren @@ -0,0 +1,3 @@ +// nontest +var Bar = "from bar" +System.print("ran bar module") diff --git a/test/module/relative_same_level/bar.wren b/test/module/relative_same_level/bar.wren new file mode 100644 index 00000000..739827c7 --- /dev/null +++ b/test/module/relative_same_level/bar.wren @@ -0,0 +1,3 @@ +// nontest +var Bar = "from bar" +System.print("ran bar module") diff --git a/test/module/relative_same_level/foo.wren b/test/module/relative_same_level/foo.wren new file mode 100644 index 00000000..238d2e83 --- /dev/null +++ b/test/module/relative_same_level/foo.wren @@ -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