Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double translation: callback fixes #1819

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/tests/test_fun_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand Down
13 changes: 13 additions & 0 deletions lib/tests/test_fun_call_2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand Down
8 changes: 8 additions & 0 deletions runtime/js/jslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions runtime/js/stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading