From 6ef9688bd22d97212d9c28910dde9ec13e64a673 Mon Sep 17 00:00:00 2001 From: giles Date: Tue, 24 Mar 2026 16:29:52 +0000 Subject: [PATCH] Fix primitive? lookup + replace coercion; remove debug output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit primitive? in make_server_env was checking env bindings only (NativeFn), missing all 132 primitives in the Sx_primitives hashtable. Now checks both primitives table and env. get-primitive similarly fixed. replace primitive now coerces SxExpr/Thunk/RawHTML/etc to strings instead of crashing with "replace: 3 string args" — fixes aser JIT DISABLED. Co-Authored-By: Claude Opus 4.6 (1M context) --- hosts/ocaml/bin/sx_server.ml | 11 +++++++---- hosts/ocaml/lib/sx_primitives.ml | 11 ++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/hosts/ocaml/bin/sx_server.ml b/hosts/ocaml/bin/sx_server.ml index 6fa25ec..cbdd77f 100644 --- a/hosts/ocaml/bin/sx_server.ml +++ b/hosts/ocaml/bin/sx_server.ml @@ -593,14 +593,17 @@ let make_server_env () = bind "primitive?" (fun args -> match args with | [String name] -> - (* Check if name is bound in the env as a NativeFn *) - (try match env_get env name with NativeFn _ -> Bool true | _ -> Bool false - with _ -> Bool false) + (* Check both the primitives table and the env *) + Bool (Sx_primitives.is_primitive name || + (try (match env_get env name with NativeFn _ -> true | _ -> false) + with _ -> false)) | _ -> Bool false); bind "get-primitive" (fun args -> match args with | [String name] -> - (try env_get env name with _ -> Nil) + (* Check primitives table first, then env *) + (try Sx_primitives.get_primitive name + with _ -> try env_get env name with _ -> Nil) | _ -> Nil); (* Character classification — platform primitives for spec/parser.sx *) diff --git a/hosts/ocaml/lib/sx_primitives.ml b/hosts/ocaml/lib/sx_primitives.ml index f821212..3eb3c52 100644 --- a/hosts/ocaml/lib/sx_primitives.ml +++ b/hosts/ocaml/lib/sx_primitives.ml @@ -283,8 +283,17 @@ let () = String (String.concat sep (List.map to_string items)) | _ -> raise (Eval_error "join: 2 args")); register "replace" (fun args -> + let to_str = function + | String s -> s | SxExpr s -> s | RawHTML s -> s + | Keyword k -> k | Symbol s -> s + | Nil -> "" | Bool true -> "true" | Bool false -> "false" + | Number n -> if Float.is_integer n then string_of_int (int_of_float n) else Printf.sprintf "%g" n + | Thunk _ as t -> (match !_sx_trampoline_fn t with String s -> s | v -> to_string v) + | v -> to_string v + in match args with - | [String s; String old_s; String new_s] -> + | [s; old_s; new_s] -> + let s = to_str s and old_s = to_str old_s and new_s = to_str new_s in let ol = String.length old_s in if ol = 0 then String s else begin