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