import
import(file) — loads and executes another .scr file (extension optional).
It does two things:
- Merges the imported file’s globals into the root (current) global scope,
so you can call its functions directly by name. - Returns an object holding the imported globals, so you can also reach
them namespaced through the return value (and tell them apart from any
colliding names).
var math = import("libs/helper")
print(math.factorial(5)) # via the returned object
print(factorial(5)) # via the merged root global — same value
Every var/fn at the top level of the imported file is exported: the
import’s namespace object has one key per global, and that same name is bound
directly in the importing script’s root scope. const exports as const and
ref exports as an alias into the module’s storage; names declared inside a
block or function are scoped there and never export.
# libs/helper.scr
fn factorial(n) {
if (n <= 1) { return 1 }
return n * factorial(n - 1)
}
var helper_name = "helper"
# main.scr
var h = import("libs/helper")
print(h.factorial(6)) # 720 (via object)
print(factorial(6)) # 720 (via root scope)
print(helper_name) # helper (imported global is available too)
print(keys(h)) # includes "factorial" and "helper_name" — plus the engine's builtin globals, which the namespace object also carries
Importing is not scoped to the block where import appears — the globals
always land in the root scope. Beware collisions: an imported name that
overlaps an existing restricted global (such as a builtin like double or an
already-imported name) raises a REASSIGN_RESTRICTED error.
Circular imports are detected and raise an error (Import cycle detected: <file>) instead of recursing; chained imports are also capped at a depth
of 64.
Imports resolve against the process working directory and any
engine-configured search paths — not relative to the importing file.