From 8f4d1a333fc85ba72b5ce0faddbf29cae8e927c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Mon, 9 Oct 2023 10:54:44 +0200 Subject: [PATCH] Runtime: Js.export --- lib/js_of_ocaml/js.ml | 9 ++++++--- lib/tests/test_fun_call.ml | 24 +++++++++++------------- runtime/wasm/runtime.js | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/js_of_ocaml/js.ml b/lib/js_of_ocaml/js.ml index 3349b04ec..481622854 100644 --- a/lib/js_of_ocaml/js.ml +++ b/lib/js_of_ocaml/js.ml @@ -838,9 +838,12 @@ let export_js (field : js_string t) x = Unsafe.set (Unsafe.pure_js_expr "jsoo_exports") field - (if String.equal (Js.to_string (typeof (Obj.magic x))) "function" - (* function with arity/length equal to zero are already wrapped *) - && Unsafe.get (Obj.magic x) (Js.string "length") > 0 + (if match Sys.backend_type with + | Other "wasm_of_ocaml" -> Obj.tag (Obj.repr x) = Obj.closure_tag + | _ -> + String.equal (Js.to_string (typeof (Obj.magic x))) "function" + (* function with arity/length equal to zero are already wrapped *) + && Unsafe.get (Obj.magic x) (Js.string "length") > 0 then Obj.magic (wrap_callback (Obj.magic x)) else x) diff --git a/lib/tests/test_fun_call.ml b/lib/tests/test_fun_call.ml index 70c109851..f36b0b871 100644 --- a/lib/tests/test_fun_call.ml +++ b/lib/tests/test_fun_call.ml @@ -409,16 +409,14 @@ let%expect_test _ = [%expect {| Result: other |}] -(*ZZZ - let%expect_test _ = - let open Js_of_ocaml in - let f = Js.wrap_callback (fun s -> print_endline s) in - Js.export "f" f; - let () = - Js.Unsafe.fun_call - (Js.Unsafe.pure_js_expr "jsoo_exports")##.f - [| Js.Unsafe.coerce (Js.string "hello") |] - in - (); - [%expect {| hello |}] -*) +let%expect_test _ = + let open Js_of_ocaml in + let f = Js.wrap_callback (fun s -> print_endline (Js.to_string s)) in + Js.export "f" f; + let () = + Js.Unsafe.fun_call + (Js.Unsafe.pure_js_expr "jsoo_exports")##.f + [| Js.Unsafe.coerce (Js.string "hello") |] + in + (); + [%expect {| hello |}] diff --git a/runtime/wasm/runtime.js b/runtime/wasm/runtime.js index 29bd1e5ea..4c1f1310d 100644 --- a/runtime/wasm/runtime.js +++ b/runtime/wasm/runtime.js @@ -376,5 +376,5 @@ event.error&&caml_handle_uncaught_exception(event.error)) } await _initialize(); -})(((joo_global_object,globalThis)=>(x)=>eval(x))(globalThis,globalThis), +})(((joo_global_object,jsoo_exports,globalThis)=>(x)=>eval(x))(globalThis,globalThis?.module?.exports||globalThis,globalThis), PRIMITIVES);