Get logical imports in "wren_modules" working.

There's a lot of changes here and surely some rough edges to iron out.
Also, I need to update the docs. But I want to get closer to landing
this so I can build on it.
This commit is contained in:
Bob Nystrom
2018-07-15 20:09:41 -07:00
parent 8210452970
commit c367fc3bfc
31 changed files with 665 additions and 190 deletions

View File

@ -6,7 +6,7 @@
void callRunTests(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "test/api/call", "Call", 0);
wrenGetVariable(vm, "./test/api/call", "Call", 0);
WrenHandle* callClass = wrenGetSlotHandle(vm, 0);
WrenHandle* noParams = wrenMakeCallHandle(vm, "noParams");

View File

@ -4,23 +4,23 @@
static void beforeDefined(WrenVM* vm)
{
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
wrenGetVariable(vm, "./test/api/get_variable", "A", 0);
}
static void afterDefined(WrenVM* vm)
{
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
wrenGetVariable(vm, "./test/api/get_variable", "A", 0);
}
static void afterAssigned(WrenVM* vm)
{
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
wrenGetVariable(vm, "./test/api/get_variable", "A", 0);
}
static void otherSlot(WrenVM* vm)
{
wrenEnsureSlots(vm, 3);
wrenGetVariable(vm, "test/api/get_variable", "B", 2);
wrenGetVariable(vm, "./test/api/get_variable", "B", 2);
// Move it into return position.
const char* string = wrenGetSlotString(vm, 2);
@ -29,7 +29,7 @@ static void otherSlot(WrenVM* vm)
static void otherModule(WrenVM* vm)
{
wrenGetVariable(vm, "test/api/get_variable_module", "Variable", 0);
wrenGetVariable(vm, "./test/api/get_variable_module", "Variable", 0);
}
WrenForeignMethodFn getVariableBindMethod(const char* signature)

View File

@ -25,7 +25,7 @@ static WrenForeignMethodFn bindForeignMethod(
WrenVM* vm, const char* module, const char* className,
bool isStatic, const char* signature)
{
if (strncmp(module, "test/", 5) != 0) return NULL;
if (strncmp(module, "./test/", 7) != 0) return NULL;
// For convenience, concatenate all of the method qualifiers into a single
// signature string.
@ -78,7 +78,7 @@ static WrenForeignClassMethods bindForeignClass(
WrenVM* vm, const char* module, const char* className)
{
WrenForeignClassMethods methods = { NULL, NULL };
if (strncmp(module, "test/", 5) != 0) return methods;
if (strncmp(module, "./test/", 7) != 0) return methods;
foreignClassBindClass(className, &methods);
if (methods.allocate != NULL) return methods;

View File

@ -6,7 +6,7 @@
void resetStackAfterCallAbortRunTests(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "test/api/reset_stack_after_call_abort", "Test", 0);
wrenGetVariable(vm, "./test/api/reset_stack_after_call_abort", "Test", 0);
WrenHandle* testClass = wrenGetSlotHandle(vm, 0);
WrenHandle* abortFiber = wrenMakeCallHandle(vm, "abortFiber()");

View File

@ -22,7 +22,8 @@ void resetStackAfterForeignConstructBindClass(
void resetStackAfterForeignConstructRunTests(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "test/api/reset_stack_after_foreign_construct", "Test", 0);
wrenGetVariable(vm,
"./test/api/reset_stack_after_foreign_construct", "Test", 0);
WrenHandle* testClass = wrenGetSlotHandle(vm, 0);
WrenHandle* callConstruct = wrenMakeCallHandle(vm, "callConstruct()");

View File

@ -1,3 +1,3 @@
class Foo {
foreign someUnknownMethod // expect runtime error: Could not find foreign method 'someUnknownMethod' for class Foo in module 'test/language/foreign/unknown_method'.
foreign someUnknownMethod // expect runtime error: Could not find foreign method 'someUnknownMethod' for class Foo in module './test/language/foreign/unknown_method'.
}

View File

@ -1,2 +1,2 @@
System.print("before") // expect: before
import "./module" for Module // expect runtime error: Could not compile module 'test/language/module/compile_error/module'.
import "./module" for Module // expect runtime error: Could not compile module './test/language/module/compile_error/module'.

View File

@ -0,0 +1,6 @@
// Import a module from within a named package.
import "foo/within_foo" for Foo
// expect: ran foo module
// expect: ran bar module
System.print(Foo) // expect: from foo

View File

@ -0,0 +1,5 @@
// nontest
var Bar = "from bar"
System.print("ran bar module")
import "foo/within_foo"

View File

@ -0,0 +1,5 @@
// nontest
var Foo = "from foo"
System.print("ran foo module")
import "bar/within_bar"

View File

@ -0,0 +1,5 @@
// Import a module whose name is the same as the package.
import "foo" for Module
// expect: ran module
System.print(Module) // expect: from module

View File

@ -0,0 +1,3 @@
// nontest
var Module = "from module"
System.print("ran module")

View File

@ -1 +1 @@
import "./does_not_exist" for DoesNotExist // expect runtime error: Could not load module 'test/language/module/does_not_exist'.
import "./does_not_exist" for DoesNotExist // expect runtime error: Could not load module './test/language/module/does_not_exist'.

View File

@ -1,3 +1,3 @@
// Should execute the module:
// expect: ran module
import "./module" for DoesNotExist // expect runtime error: Could not find a variable named 'DoesNotExist' in module 'test/language/module/unknown_variable/module'.
import "./module" for DoesNotExist // expect runtime error: Could not find a variable named 'DoesNotExist' in module './test/language/module/unknown_variable/module'.

View File

@ -0,0 +1,3 @@
// Stops as soon as it finds a wren_modules directory, regardless of whether or
// not it contains the desired module.
import "foo" // expect runtime error: Could not load module 'foo'.

View File

@ -0,0 +1,2 @@
// nontest
System.print("ran foo module")

View File

@ -0,0 +1,3 @@
// Walk up parent directories from the root script to find "wren_modules".
import "foo"
// expect: ran foo module

View File

@ -0,0 +1,2 @@
// nontest
System.print("ran foo module")

View File

@ -1,6 +1,6 @@
import "meta" for Meta
var variables = Meta.getModuleVariables("test/meta/get_module_variables")
var variables = Meta.getModuleVariables("./test/meta/get_module_variables")
// Includes implicitly imported core stuff.
System.print(variables.contains("Object")) // expect: true

View File

@ -9,40 +9,39 @@
static void expectNormalize(const char* input, const char* expected)
{
Path* path = pathNew(input);
Path* result = pathNormalize(path);
pathNormalize(path);
if (strcmp(result->chars, expected) != 0)
if (strcmp(path->chars, expected) != 0)
{
printf("FAIL %-30s Want %s\n", input, expected);
printf(" Got %s\n\n", result->chars);
printf(" Got %s\n\n", path->chars);
fail();
}
else
{
#if SHOW_PASSES
printf("PASS %-30s -> %s\n", input, result->chars);
printf("PASS %-30s -> %s\n", input, path->chars);
#endif
pass();
}
pathFree(path);
pathFree(result);
}
static void testNormalize()
{
// simple cases
// Simple cases.
expectNormalize("", ".");
expectNormalize(".", ".");
expectNormalize("..", "..");
expectNormalize("a", "a");
expectNormalize("/", "/");
// collapses redundant separators
// Collapses redundant separators.
expectNormalize("a/b/c", "a/b/c");
expectNormalize("a//b///c////d", "a/b/c/d");
// eliminates "." parts
// Eliminates "." parts, except one at the beginning.
expectNormalize("./", ".");
expectNormalize("/.", "/");
expectNormalize("/./", "/");
@ -50,10 +49,10 @@ static void testNormalize()
expectNormalize("a/./b", "a/b");
expectNormalize("a/.b/c", "a/.b/c");
expectNormalize("a/././b/./c", "a/b/c");
expectNormalize("././a", "a");
expectNormalize("././a", "./a");
expectNormalize("a/./.", "a");
// eliminates ".." parts
// Eliminates ".." parts.
expectNormalize("..", "..");
expectNormalize("../", "..");
expectNormalize("../../..", "../../..");
@ -68,7 +67,7 @@ static void testNormalize()
expectNormalize("a/b/c/../../d/e/..", "a/d");
expectNormalize("a/b/../../../../c", "../../c");
// does not walk before root on absolute paths
// Does not walk before root on absolute paths.
expectNormalize("..", "..");
expectNormalize("../", "..");
expectNormalize("/..", "/");
@ -84,7 +83,7 @@ static void testNormalize()
expectNormalize("a/b/../../../../c", "../../c");
expectNormalize("a/b/c/../../..d/./.e/f././", "a/..d/.e/f.");
// removes trailing separators
// Removes trailing separators.
expectNormalize("./", ".");
expectNormalize(".//", ".");
expectNormalize("a/", "a");
@ -94,7 +93,7 @@ static void testNormalize()
expectNormalize("foo/bar/baz", "foo/bar/baz");
expectNormalize("foo", "foo");
expectNormalize("foo/bar/", "foo/bar");
expectNormalize("./foo/././bar/././", "foo/bar");
expectNormalize("./foo/././bar/././", "./foo/bar");
}
void testPath()