feat: add module loading with String.import_ and embedder-provided loadModuleFn

Implement initial module system allowing Wren code to import other modules via a temporary `String.import_` method. The embedder must supply a `WrenLoadModuleFn` callback that returns source code for a given module name; the VM caches loaded modules and returns a fiber to execute the module body. Add `wrenImportModule` to the VM API, wire `loadModuleFn` into `WrenConfiguration`, and update the CLI interpreter to resolve imports relative to the entry script's directory. Include test cases for importing multiple variables, shared imports across modules, and verifying that variable bindings are independent across import sites.
This commit is contained in:
Bob Nystrom
2015-02-06 15:01:15 +00:00
parent 7cba07a322
commit 70191aa022
19 changed files with 311 additions and 2 deletions
@@ -0,0 +1,8 @@
// nontest
var Module1 = "from module one"
var Module2 = "from module two"
var Module3 = "from module three"
var Module4 = "from module four"
var Module5 = "from module five"
IO.print("ran module")
@@ -0,0 +1,14 @@
var Module1 = "module.wren".import_("Module1")
var Module2 = "module.wren".import_("Module2")
var Module3 = "module.wren".import_("Module3")
var Module4 = "module.wren".import_("Module4")
var Module5 = "module.wren".import_("Module5")
// Only execute module body once:
// expect: ran module
IO.print(Module1) // expect: from module one
IO.print(Module2) // expect: from module two
IO.print(Module3) // expect: from module three
IO.print(Module4) // expect: from module four
IO.print(Module5) // expect: from module five