Set arity in native compiler and bytecode modules

Sx_compiler.compile/compile_module now emit arity (local slot count) in
the bytecode dict. MCP sx_build_bytecode serializes arity into .sxbc.json.
sx-platform.js passes arity through to K.loadModule(). Without this, the
VM allocated only 16 local slots per module frame.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-30 14:31:55 +00:00
parent b274e428eb
commit 408eca1cb0
26 changed files with 39 additions and 32 deletions

View File

@@ -635,7 +635,11 @@ let rec handle_tool name args =
let consts = match Sx_runtime.get code (String "constants") with
| List l | ListRef { contents = l } -> String.concat "," (List.map const_to_json l)
| _ -> "" in
Printf.sprintf "{\"t\":\"code\",\"v\":{\"bytecode\":[%s],\"constants\":[%s]}}" bc consts
let arity = match Sx_runtime.get code (String "arity") with
| Number n -> int_of_float n | _ -> 0 in
let uvc = match Sx_runtime.get code (String "upvalue-count") with
| Number n -> int_of_float n | _ -> 0 in
Printf.sprintf "{\"t\":\"code\",\"v\":{\"arity\":%d,\"upvalue-count\":%d,\"bytecode\":[%s],\"constants\":[%s]}}" arity uvc bc consts
and json_escape s =
let buf = Buffer.create (String.length s + 2) in
Buffer.add_char buf '"';
@@ -666,8 +670,10 @@ let rec handle_tool name args =
let consts = match Sx_runtime.get code (String "constants") with
| List l | ListRef { contents = l } -> String.concat "," (List.map const_to_json l)
| _ -> "" in
let json = Printf.sprintf "{\"magic\":\"SXBC\",\"version\":1,\"hash\":\"%s\",\"module\":{\"bytecode\":[%s],\"constants\":[%s]}}"
hash bc consts in
let arity = match Sx_runtime.get code (String "arity") with
| Number n -> int_of_float n | _ -> 0 in
let json = Printf.sprintf "{\"magic\":\"SXBC\",\"version\":1,\"hash\":\"%s\",\"module\":{\"arity\":%d,\"bytecode\":[%s],\"constants\":[%s]}}"
hash arity bc consts in
let json_path = (String.sub src_path 0 (String.length src_path - 3)) ^ ".sxbc.json" in
Out_channel.with_open_text json_path (fun oc -> output_string oc json);
let kb = String.length json / 1024 in