From 7c294b8f496af574e683bfff9aee89a6b39b5ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 28 Jan 2025 12:55:54 +0100 Subject: [PATCH 1/3] Double translation: fix overapplied callback There is a hack in caml_call_gen functions to make Js.wrap_callback work even when the OCaml function is called with more arguments that it expects. This hack was missing in the double translation case. --- runtime/js/stdlib.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/runtime/js/stdlib.js b/runtime/js/stdlib.js index 20e34d59fd..07b46f38ed 100644 --- a/runtime/js/stdlib.js +++ b/runtime/js/stdlib.js @@ -141,10 +141,9 @@ var caml_call_gen_tuple = (function () { if (d === 0) { return f.apply(null, args); } else if (d < 0) { - return caml_call_gen_direct( - f.apply(null, args.slice(0, n)), - args.slice(n), - ); + var g = f(...args.slice(0, n)); + if (typeof g !== "function") return g; + return caml_call_gen_direct(g, args.slice(n)); } else { // FIXME: Restore the optimization of handling specially d = 1 or 2 var args_ = args.slice(); From 7fda874e5f977a7c8539770d86ed80f90fc39377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 28 Jan 2025 13:22:28 +0100 Subject: [PATCH 2/3] Double translation: fix function caml_js_function_arity --- runtime/js/jslib.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/runtime/js/jslib.js b/runtime/js/jslib.js index 97aeaa96e4..8b974912d4 100644 --- a/runtime/js/jslib.js +++ b/runtime/js/jslib.js @@ -443,6 +443,14 @@ function caml_js_function_arity(f) { //Provides: caml_js_function_arity //If: effects +//If: doubletranslate +function caml_js_function_arity(f) { + return f.l >= 0 ? f.l : (f.l = f.length); +} + +//Provides: caml_js_function_arity +//If: effects +//If: !doubletranslate function caml_js_function_arity(f) { // Functions have an additional continuation parameter. This should // not be visible when calling them from JavaScript From e949a7a56a99ec9d4944170367e9c9efb830c117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 28 Jan 2025 13:45:15 +0100 Subject: [PATCH 3/3] Add tests for Js.Unsafe.callback --- lib/tests/test_fun_call.ml | 13 +++++++++++++ lib/tests/test_fun_call_2.ml | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/tests/test_fun_call.ml b/lib/tests/test_fun_call.ml index 05c4cb9c3f..69b914db8b 100644 --- a/lib/tests/test_fun_call.ml +++ b/lib/tests/test_fun_call.ml @@ -184,6 +184,19 @@ let%expect_test "wrap_callback_strict" = got 1, 2, undefined, done Result: 0 |}] +(* Wrap callback unsafe *) +let%expect_test "over application, extra arguments are dropped" = + call_and_log (Js.Unsafe.callback cb3) {| (function(f){ return f(1,2,3,4) }) |}; + [%expect {| + got 1, 2, 3, done + Result: 0 |}] + +let%expect_test "partial application, extra arguments set to undefined" = + call_and_log (Js.Unsafe.callback cb3) {| (function(f){ return f(1,2) }) |}; + [%expect {| + got 1, 2, undefined, done + Result: 0 |}] + (* Wrap meth callback *) let%expect_test "over application, extra arguments are dropped" = diff --git a/lib/tests/test_fun_call_2.ml b/lib/tests/test_fun_call_2.ml index 00a48af3b9..99a5f8c91b 100644 --- a/lib/tests/test_fun_call_2.ml +++ b/lib/tests/test_fun_call_2.ml @@ -181,6 +181,19 @@ let%expect_test "wrap_callback_strict" = got 1, 2, undefined, done Result: 0 |}] +(* Wrap callback unsafe *) +let%expect_test "over application, extra arguments are dropped" = + call_and_log (Js.Unsafe.callback cb3) {| (function(f){ return f(1,2,3,4) }) |}; + [%expect {| + got 1, 2, 3, done + Result: 0 |}] + +let%expect_test "partial application, extra arguments set to undefined" = + call_and_log (Js.Unsafe.callback cb3) {| (function(f){ return f(1,2) }) |}; + [%expect {| + got 1, 2, undefined, done + Result: 0 |}] + (* Wrap meth callback *) let%expect_test "over application, extra arguments are dropped" =