Diagnostic: enhanced Not callable error with VM state context

Shows pending_cek, reuse_stack count, and frames count in the error.
Also transfers reuse_stack from _active_vm at VmSuspended catch sites.

Finding: the Not callable: nil happens during cek_resume (pending_cek=false,
kont=12 frames). The CEK continuation tries to call a letrec function that
is nil because letrec bindings are in VM local SLOTS, not in the CEK env.
The VM→CEK boundary crossing during suspension loses the local slot values.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 15:49:04 +00:00
parent b9c9216409
commit 112eed50d0
5 changed files with 319 additions and 240 deletions

View File

@@ -109,6 +109,14 @@ let rec value_to_js (v : value) : Js.Unsafe.any =
value_to_js result
with
| Sx_vm.VmSuspended (request, vm) ->
(* Transfer reuse_stack from the active VM to the suspension VM.
call_closure_reuse saves caller frames on _active_vm AFTER the
inner VmSuspended propagates, so the suspension VM doesn't have them. *)
(match !Sx_vm._active_vm with
| Some active when active.Sx_vm.reuse_stack <> [] ->
vm.Sx_vm.reuse_stack <- vm.Sx_vm.reuse_stack @ active.Sx_vm.reuse_stack;
active.Sx_vm.reuse_stack <- []
| _ -> ());
(* Build {suspended, request, resume} and hand to _driveAsync.
The resume callback must also catch VmSuspended for chaining
(e.g. repeat 3 times ... wait ... end). *)
@@ -124,9 +132,14 @@ let rec value_to_js (v : value) : Js.Unsafe.any =
(* Return suspension object — the JS driveAsync caller handles scheduling *)
Js.Unsafe.inject (make_suspension req2 vm2)
| Eval_error msg ->
(* Enhanced error: show pending_cek kont and reuse_stack info *)
let extra = Printf.sprintf " [vm: pending_cek=%b reuse=%d frames=%d]"
(v.Sx_vm.pending_cek <> None)
(List.length v.Sx_vm.reuse_stack)
(List.length v.Sx_vm.frames) in
ignore (Js.Unsafe.meth_call
(Js.Unsafe.get Js.Unsafe.global (Js.string "console"))
"error" [| Js.Unsafe.inject (Js.string ("[sx] resume: " ^ msg)) |]);
"error" [| Js.Unsafe.inject (Js.string ("[sx] resume: " ^ msg ^ extra)) |]);
Js.Unsafe.inject Js.null));
obj
in
@@ -549,6 +562,12 @@ let api_call_fn fn_js args_js =
return_via_side_channel (value_to_js (call_sx_fn fn args))
with
| Sx_vm.VmSuspended (request, vm) ->
(* Transfer reuse_stack from active VM *)
(match !Sx_vm._active_vm with
| Some active when active.Sx_vm.reuse_stack <> [] ->
vm.Sx_vm.reuse_stack <- vm.Sx_vm.reuse_stack @ active.Sx_vm.reuse_stack;
active.Sx_vm.reuse_stack <- []
| _ -> ());
sync_vm_to_env ();
Js.Unsafe.inject (make_js_callFn_suspension request vm)
| Eval_error msg ->