Modules & Imports
Using the import function, you can load a native module or another Pyle script and get back a module map.
Native modules
Section titled “Native modules”let os = import("os")let math = import("math")let json = import("json")
print(os.platform())printf("sqrt(144) = {}", math.sqrt(144))Native modules registered with the VM are found before the filesystem is searched. Which modules are available depends on the build:
| Layer | Modules |
|---|---|
Core (libpyle) |
os, path, fs, math, encoding, io, color, proc (when process support is compiled in) |
CLI interpreter (pyle) |
json, http, crypto, worker |
| External | raylib (requires a separate build) |
A library-only embedder does not get json, http, or crypto unless it registers them.
Script imports
Section titled “Script imports”let helpers = import("lib/helpers")let config = import("config.pyl")The .pyl extension is optional. Full and relative paths are accepted.
let cfg = import("examples/raylib/suika/cfg")let absolute = import("C:/projects/mylib.pyl")Imported scripts are evaluated once. Their top-level declarations become the module map.
fn hello(name) => "hello, " + namelet version = "1.0"let greet = import("lib/greet")
print(greet.hello("Ada"))print(greet.version)Import paths
Section titled “Import paths”The VM starts with ./ on the search path. The CLI interpreter also adds the std/ directory next to the executable and ./std/, which is how import("worker") finds std/worker.pyl.
add_import_path appends a directory to that same search path; it does not replace it.
add_import_path("lib")add_import_path("vendor")Import names may include subdirectories and .. segments. The resolver tries the name as-is, then relative to the importing script for ./ names, then relative to each search directory:
let helpers = import("lib/helpers")let shared = import("../shared/utils")Caching
Section titled “Caching”Modules are cached by name. A second import of the same module returns the same map.
let a = import("config")let b = import("config")print(a == b) # true, same map objectModule globals
Section titled “Module globals”Each module has its own global namespace. Functions and closures defined in a module resolve that module’s globals, not the importer’s.
let count = 0fn next() { count += 1 return count}let counter = import("counter")print(counter.next()) # 1print(counter.next()) # 2Cycles
Section titled “Cycles”If module A imports B and B imports A, B receives A’s partially initialized state. Avoid cycles when the initialization order matters.
Declaration files
Section titled “Declaration files”Files ending in .d.pyl describe native APIs for editors. They are never executed, and the interpreter refuses to import or run one.
std/<module>.d.pylfor native modules such ashttp,os,json,math,color, andcrypto.std/core/<type>.d.pylfor built-in types such asstring,array,map,bytes,coro, andfuture.std/core/builtin.d.pyldeclares process-global natives and is loaded automatically by the language server.
Declaration syntax:
fn name(param: type): ret { }let NAME: typestruct Name(field: type) { }