mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 14:18:42 +01:00
Relative imports!
This is a breaking change because existing imports in user Wren code that assume the path is relative to the entrypoint file will now likely fail. Also, stack trace output and host API calls that take a module string now need the resolved module string, not the short name that appears in the import.
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
void callRunTests(WrenVM* vm)
|
||||
{
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "main", "Call", 0);
|
||||
wrenGetVariable(vm, "test/api/call", "Call", 0);
|
||||
WrenHandle* callClass = wrenGetSlotHandle(vm, 0);
|
||||
|
||||
WrenHandle* noParams = wrenMakeCallHandle(vm, "noParams");
|
||||
|
||||
@ -4,23 +4,23 @@
|
||||
|
||||
static void beforeDefined(WrenVM* vm)
|
||||
{
|
||||
wrenGetVariable(vm, "main", "A", 0);
|
||||
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
|
||||
}
|
||||
|
||||
static void afterDefined(WrenVM* vm)
|
||||
{
|
||||
wrenGetVariable(vm, "main", "A", 0);
|
||||
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
|
||||
}
|
||||
|
||||
static void afterAssigned(WrenVM* vm)
|
||||
{
|
||||
wrenGetVariable(vm, "main", "A", 0);
|
||||
wrenGetVariable(vm, "test/api/get_variable", "A", 0);
|
||||
}
|
||||
|
||||
static void otherSlot(WrenVM* vm)
|
||||
{
|
||||
wrenEnsureSlots(vm, 3);
|
||||
wrenGetVariable(vm, "main", "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, "get_variable_module", "Variable", 0);
|
||||
wrenGetVariable(vm, "test/api/get_variable_module", "Variable", 0);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn getVariableBindMethod(const char* signature)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import "get_variable_module"
|
||||
import "./get_variable_module"
|
||||
|
||||
class GetVariable {
|
||||
foreign static beforeDefined()
|
||||
|
||||
@ -24,8 +24,8 @@ const char* testName;
|
||||
static WrenForeignMethodFn bindForeignMethod(
|
||||
WrenVM* vm, const char* module, const char* className,
|
||||
bool isStatic, const char* signature)
|
||||
{
|
||||
if (strcmp(module, "main") != 0) return NULL;
|
||||
{
|
||||
if (strncmp(module, "test/", 5) != 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 (strcmp(module, "main") != 0) return methods;
|
||||
if (strncmp(module, "test/", 5) != 0) return methods;
|
||||
|
||||
foreignClassBindClass(className, &methods);
|
||||
if (methods.allocate != NULL) return methods;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
void resetStackAfterCallAbortRunTests(WrenVM* vm)
|
||||
{
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "main", "Test", 0);
|
||||
wrenGetVariable(vm, "test/api/reset_stack_after_call_abort", "Test", 0);
|
||||
WrenHandle* testClass = wrenGetSlotHandle(vm, 0);
|
||||
|
||||
WrenHandle* abortFiber = wrenMakeCallHandle(vm, "abortFiber()");
|
||||
@ -25,4 +25,4 @@ void resetStackAfterCallAbortRunTests(WrenVM* vm)
|
||||
wrenReleaseHandle(vm, testClass);
|
||||
wrenReleaseHandle(vm, abortFiber);
|
||||
wrenReleaseHandle(vm, afterConstruct);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ void resetStackAfterForeignConstructBindClass(
|
||||
void resetStackAfterForeignConstructRunTests(WrenVM* vm)
|
||||
{
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "main", "Test", 0);
|
||||
wrenGetVariable(vm, "test/api/reset_stack_after_foreign_construct", "Test", 0);
|
||||
WrenHandle* testClass = wrenGetSlotHandle(vm, 0);
|
||||
|
||||
WrenHandle* callConstruct = wrenMakeCallHandle(vm, "callConstruct()");
|
||||
@ -41,4 +41,4 @@ void resetStackAfterForeignConstructRunTests(WrenVM* vm)
|
||||
wrenReleaseHandle(vm, testClass);
|
||||
wrenReleaseHandle(vm, callConstruct);
|
||||
wrenReleaseHandle(vm, afterConstruct);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var fiber = Fiber.new {
|
||||
System.print("fiber 1")
|
||||
|
||||
import "yield_from_import_module"
|
||||
import "./yield_from_import_module"
|
||||
|
||||
System.print("fiber 2")
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
class Foo {
|
||||
foreign someUnknownMethod // expect runtime error: Could not find foreign method 'someUnknownMethod' for class Foo in module 'main'.
|
||||
foreign someUnknownMethod // expect runtime error: Could not find foreign method 'someUnknownMethod' for class Foo in module 'test/language/foreign/unknown_method'.
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import "module" for Module, Other
|
||||
import "./module" for Module, Other
|
||||
|
||||
System.print(Module) // expect: before
|
||||
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
System.print("before") // expect: before
|
||||
import "module" for Module // expect runtime error: Could not compile module 'module'.
|
||||
import "./module" for Module // expect runtime error: Could not compile module 'test/language/module/compile_error/module'.
|
||||
|
||||
@ -3,7 +3,7 @@ System.print("start a")
|
||||
|
||||
var A = "a value"
|
||||
System.print("a defined %(A)")
|
||||
import "b" for B
|
||||
import "./b" for B
|
||||
System.print("a imported %(B)")
|
||||
|
||||
System.print("end a")
|
||||
|
||||
@ -3,7 +3,7 @@ System.print("start b")
|
||||
|
||||
var B = "b value"
|
||||
System.print("b defined %(B)")
|
||||
import "a" for A
|
||||
import "./a" for A
|
||||
System.print("b imported %(A)")
|
||||
|
||||
System.print("end b")
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import "a"
|
||||
import "./a"
|
||||
|
||||
// Shared module should only run once:
|
||||
// expect: start a
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import "module"
|
||||
import "./module"
|
||||
// expect: Bool
|
||||
// expect: Class
|
||||
// expect: Fiber
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var Module = "outer"
|
||||
|
||||
if (true) {
|
||||
import "module" for Module
|
||||
import "./module" for Module
|
||||
// expect: ran module
|
||||
|
||||
System.print(Module) // expect: from module
|
||||
|
||||
@ -1 +1 @@
|
||||
import "module" NoString // expect error
|
||||
import "./module" NoString // expect error
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
import "something" for Index
|
||||
import "./something/module" for Index
|
||||
|
||||
System.print(Index) // expect: index
|
||||
@ -1,4 +1,4 @@
|
||||
import "module" for Module1, Module2, Module3, Module4, Module5
|
||||
import "./module" for Module1, Module2, Module3, Module4, Module5
|
||||
|
||||
// Only execute module body once:
|
||||
// expect: ran module
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
var Collides
|
||||
import "module" for Collides // expect error
|
||||
import "./module" for Collides // expect error
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import
|
||||
|
||||
|
||||
"module"
|
||||
"./module"
|
||||
|
||||
import "module" for
|
||||
import "./module" for
|
||||
|
||||
A,
|
||||
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
import "module"
|
||||
import "./module"
|
||||
// expect: ran module
|
||||
|
||||
2
test/language/module/relative_import/module_3.wren
Normal file
2
test/language/module/relative_import/module_3.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// nontest
|
||||
System.print("module_3")
|
||||
@ -0,0 +1,8 @@
|
||||
import "./sub/module"
|
||||
import "./sub/././///dir/module"
|
||||
// expect: sub/module
|
||||
// expect: sub/module_2
|
||||
// expect: sub/dir/module
|
||||
// expect: sub/dir/module_2
|
||||
// expect: sub/module_3
|
||||
// expect: module_3
|
||||
3
test/language/module/relative_import/sub/dir/module.wren
Normal file
3
test/language/module/relative_import/sub/dir/module.wren
Normal file
@ -0,0 +1,3 @@
|
||||
// nontest
|
||||
System.print("sub/dir/module")
|
||||
import "./module_2"
|
||||
@ -0,0 +1,4 @@
|
||||
// nontest
|
||||
System.print("sub/dir/module_2")
|
||||
import "../module_3"
|
||||
import "../../module_3"
|
||||
3
test/language/module/relative_import/sub/module.wren
Normal file
3
test/language/module/relative_import/sub/module.wren
Normal file
@ -0,0 +1,3 @@
|
||||
// nontest
|
||||
System.print("sub/module")
|
||||
import "./module_2"
|
||||
2
test/language/module/relative_import/sub/module_2.wren
Normal file
2
test/language/module/relative_import/sub/module_2.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// nontest
|
||||
System.print("sub/module_2")
|
||||
2
test/language/module/relative_import/sub/module_3.wren
Normal file
2
test/language/module/relative_import/sub/module_3.wren
Normal file
@ -0,0 +1,2 @@
|
||||
// nontest
|
||||
System.print("sub/module_3")
|
||||
@ -1,5 +1,5 @@
|
||||
// nontest
|
||||
System.print("a")
|
||||
import "shared" for Shared
|
||||
import "./shared" for Shared
|
||||
var A = "a %(Shared)"
|
||||
System.print("a done")
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
// nontest
|
||||
System.print("b")
|
||||
import "shared" for Shared
|
||||
import "./shared" for Shared
|
||||
var B = "b %(Shared)"
|
||||
System.print("b done")
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import "a" for A
|
||||
import "b" for B
|
||||
import "./a" for A
|
||||
import "./b" for B
|
||||
|
||||
// Shared module should only run once:
|
||||
// expect: a
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import "module" for Module
|
||||
import "./module" for Module
|
||||
// expect: ran module
|
||||
|
||||
System.print(Module) // expect: from module
|
||||
|
||||
@ -1 +1 @@
|
||||
import "does_not_exist" for DoesNotExist // expect runtime error: Could not load module 'does_not_exist'.
|
||||
import "./does_not_exist" for DoesNotExist // expect runtime error: Could not load module 'test/language/module/does_not_exist'.
|
||||
|
||||
@ -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 'module'.
|
||||
import "./module" for DoesNotExist // expect runtime error: Could not find a variable named 'DoesNotExist' in module 'test/language/module/unknown_variable/module'.
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import "meta" for Meta
|
||||
|
||||
var variables = Meta.getModuleVariables("main")
|
||||
var variables = Meta.getModuleVariables("test/meta/get_module_variables")
|
||||
|
||||
// Includes implicitly imported core stuff.
|
||||
System.print(variables.contains("Object")) // expect: true
|
||||
|
||||
Reference in New Issue
Block a user