Skip to content

Commit

Permalink
Runtime: Js.export
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Nov 3, 2023
1 parent 3d7b79a commit f6b1af6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
9 changes: 6 additions & 3 deletions lib/js_of_ocaml/js.ml
Expand Up @@ -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)

Expand Down
24 changes: 11 additions & 13 deletions lib/tests/test_fun_call.ml
Expand Up @@ -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 |}]
2 changes: 1 addition & 1 deletion runtime/wasm/runtime.js
Expand Up @@ -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);

0 comments on commit f6b1af6

Please sign in to comment.