Fix chained IO suspensions in value_to_js callback wrapper

The resume callback in the value_to_js VmSuspended handler now catches
VmSuspended recursively, building a new suspension object and calling
_driveAsync for each iteration. Fixes repeat N times ... wait ... end
which produces N sequential suspensions.

Bounce works on repeated clicks. 4/4 regression tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-09 21:37:52 +00:00
parent cf10e9a2d6
commit 9982cd5926
3 changed files with 75 additions and 45 deletions

View File

@@ -109,22 +109,35 @@ let rec value_to_js (v : value) : Js.Unsafe.any =
value_to_js result
with
| Sx_vm.VmSuspended (request, vm) ->
(* Build {suspended, request, resume} and hand to _driveAsync *)
let obj = Js.Unsafe.obj [||] in
Js.Unsafe.set obj (Js.string "suspended") (Js.Unsafe.inject Js._true);
Js.Unsafe.set obj (Js.string "request") (value_to_js request);
Js.Unsafe.set obj (Js.string "resume") (Js.wrap_callback (fun result_js ->
let result = js_to_value result_js in
try value_to_js (Sx_vm.resume_vm vm result)
with Eval_error msg ->
ignore (Js.Unsafe.meth_call
(Js.Unsafe.get Js.Unsafe.global (Js.string "console"))
"error" [| Js.Unsafe.inject (Js.string ("[sx] resume: " ^ msg)) |]);
Js.Unsafe.inject Js.null));
(* Build {suspended, request, resume} and hand to _driveAsync.
The resume callback must also catch VmSuspended for chaining
(e.g. repeat 3 times ... wait ... end). *)
let rec make_suspension req v =
let obj = Js.Unsafe.obj [||] in
Js.Unsafe.set obj (Js.string "suspended") (Js.Unsafe.inject Js._true);
Js.Unsafe.set obj (Js.string "request") (value_to_js req);
Js.Unsafe.set obj (Js.string "resume") (Js.wrap_callback (fun result_js ->
let result = js_to_value result_js in
try value_to_js (Sx_vm.resume_vm v result)
with
| Sx_vm.VmSuspended (req2, vm2) ->
let s = make_suspension req2 vm2 in
let drive = Js.Unsafe.get Js.Unsafe.global (Js.string "_driveAsync") in
if not (Js.Unsafe.equals drive Js.undefined) then
ignore (Js.Unsafe.fun_call drive [| Js.Unsafe.inject s |]);
Js.Unsafe.inject s
| Eval_error msg ->
ignore (Js.Unsafe.meth_call
(Js.Unsafe.get Js.Unsafe.global (Js.string "console"))
"error" [| Js.Unsafe.inject (Js.string ("[sx] resume: " ^ msg)) |]);
Js.Unsafe.inject Js.null));
obj
in
let s = make_suspension request vm in
let drive = Js.Unsafe.get Js.Unsafe.global (Js.string "_driveAsync") in
if not (Js.Unsafe.equals drive Js.undefined) then
ignore (Js.Unsafe.fun_call drive [| Js.Unsafe.inject obj |]);
Js.Unsafe.inject obj
ignore (Js.Unsafe.fun_call drive [| Js.Unsafe.inject s |]);
Js.Unsafe.inject s
| Eval_error msg ->
let fn_info = Printf.sprintf " [callback %s handle=%d]" (type_of v) handle in
ignore (Js.Unsafe.meth_call

File diff suppressed because one or more lines are too long

View File

@@ -1792,7 +1792,7 @@
blake2_js_for_wasm_create: blake2_js_for_wasm_create};
}
(globalThis))
({"link":[["runtime-0db9b496",0],["prelude-d7e4b000",0],["stdlib-23ce0836",[]],["re-9a0de245",[2]],["sx-42ed14c8",[2,3]],["jsoo_runtime-f96b44a8",[2]],["js_of_ocaml-651f6707",[2,5]],["dune__exe__Sx_browser-910b7ef4",[2,4,6]],["std_exit-10fb8830",[2]],["start-f808dbe1",0]],"generated":(b=>{var
({"link":[["runtime-0db9b496",0],["prelude-d7e4b000",0],["stdlib-23ce0836",[]],["re-9a0de245",[2]],["sx-42ed14c8",[2,3]],["jsoo_runtime-f96b44a8",[2]],["js_of_ocaml-651f6707",[2,5]],["dune__exe__Sx_browser-df4c0015",[2,4,6]],["std_exit-10fb8830",[2]],["start-f808dbe1",0]],"generated":(b=>{var
c=b,a=b?.module?.export||b;return{"env":{"caml_ba_kind_of_typed_array":()=>{throw new
Error("caml_ba_kind_of_typed_array not implemented")},"caml_exn_with_js_backtrace":()=>{throw new
Error("caml_exn_with_js_backtrace not implemented")},"caml_int64_create_lo_mi_hi":()=>{throw new