Transparent lazy module loading — code loads like data

When the VM or CEK hits an undefined symbol, it checks a symbol→library
index (built from manifest exports at boot), loads the library that
exports it, and returns the value. Execution continues as if the module
was always loaded. No import statements, no load-library! calls, no
Suspense boundaries — just call the function.

This is the same mechanism as IO suspension for data fetching. The
programmer doesn't distinguish between calling a local function and
calling one that needs its module fetched first. The runtime treats
code as just another resource.

Implementation:
- _symbol_resolve_hook in sx_types.ml — called by env_get_id (CEK path)
  and vm_global_get (VM path) when a symbol isn't found
- Symbol→library index built from manifest exports in sx-platform.js
- __resolve-symbol native calls __sxLoadLibrary, module loads, symbol
  appears in globals, execution resumes
- compile-modules.js extracts export lists into module-manifest.json
- Playground page demonstrates: (freeze-scope) triggers freeze.sxbc
  download transparently on first use

2650/2650 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 22:14:19 +00:00
parent f4f8715d06
commit 2f3e727a6f
11 changed files with 961 additions and 67 deletions

View File

@@ -204,20 +204,26 @@ let vm_global_get vm_val frame_val name =
| None ->
(* Walk closure env chain *)
let f = unwrap_frame frame_val in
let not_found () =
(* Try evaluator's primitive table *)
try prim_call n [] with _ ->
(* Try symbol resolve hook — transparent lazy module loading *)
match !_symbol_resolve_hook with
| Some hook ->
(match hook n with
| Some v -> v
| None -> raise (Eval_error ("VM undefined: " ^ n)))
| None -> raise (Eval_error ("VM undefined: " ^ n))
in
(match f.vf_closure.vm_closure_env with
| Some env ->
let id = intern n in
let rec find_env e =
match Hashtbl.find_opt e.bindings id with
| Some v -> v
| None -> (match e.parent with Some p -> find_env p | None ->
(* Try evaluator's primitive table as last resort *)
(try prim_call n [] with _ ->
raise (Eval_error ("VM undefined: " ^ n))))
| None -> (match e.parent with Some p -> find_env p | None -> not_found ())
in find_env env
| None ->
(try prim_call n [] with _ ->
raise (Eval_error ("VM undefined: " ^ n))))
| None -> not_found ())
let vm_global_set vm_val frame_val name v =
let m = unwrap_vm vm_val in