Add scope-collected/scope-clear-collected!/scope-emitted primitives

Register hashtable-based scope accessors that bypass the CEK special form
dispatch, for use by adapter-html.sx and shell templates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 02:50:23 +00:00
parent 49fd4a51d6
commit ce9c5d3a08
3 changed files with 20 additions and 3 deletions

View File

@@ -1141,6 +1141,8 @@ PRIMITIVES_JS_MODULES: dict[str, str] = {
PRIMITIVES["scope-emit!"] = scopeEmit;
PRIMITIVES["scope-peek"] = scopePeek;
PRIMITIVES["scope-emitted"] = sxEmitted;
PRIMITIVES["scope-collected"] = sxCollected;
PRIMITIVES["scope-clear-collected!"] = sxClearCollected;
// ---- VM stack primitives ----
// The VM spec (vm.sx) requires these array-like operations.

View File

@@ -224,6 +224,23 @@ let () = Sx_primitives.register "scope-emitted" (fun args ->
(match stack with List items :: _ -> List items | _ -> List [])
| _ -> List [])
let () = Sx_primitives.register "scope-collected" (fun args ->
match args with
| [String name] ->
let stack = try Hashtbl.find _scope_stacks name with Not_found -> [] in
(match stack with List items :: _ -> List items | _ -> List [])
| _ -> List [])
let () = Sx_primitives.register "scope-clear-collected!" (fun args ->
match args with
| [String name] ->
let stack = try Hashtbl.find _scope_stacks name with Not_found -> [] in
(match stack with
| _ :: rest -> Hashtbl.replace _scope_stacks name (List [] :: rest)
| [] -> Hashtbl.replace _scope_stacks name [List []]);
Nil
| _ -> Nil)
let () = Sx_primitives.register "provide-push!" (fun args ->
match Sx_primitives.get_primitive "scope-push!" with
| NativeFn (_, fn) -> fn args | _ -> Nil)