Skip to content

Support @new @variadic #5364

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

Merged
merged 2 commits into from
Jun 10, 2022
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
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**Compiler**

- #5364 support `@new @variadic`

**Syntax**

**Playground**
Expand Down
38 changes: 26 additions & 12 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@

module E = Js_exp_make

let splice_fn_apply fn args =
let splice_apply fn args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceApply"
[ fn; E.array Immutable args ]

let splice_obj_fn_apply obj name args =
let splice_new_apply fn args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceNewApply"
[ fn; E.array Immutable args ]

let splice_obj_apply obj name args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceObjApply"
[ obj; E.str name; E.array Immutable args ]

Expand Down Expand Up @@ -253,7 +257,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
add_eff eff
(if dynamic then splice_fn_apply fn args
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
Expand All @@ -265,13 +269,13 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
let args, eff, dynamic = assemble_args_has_splice arg_types args in
(* TODO: fix in rest calling convention *)
add_eff eff
(if dynamic then splice_fn_apply fn args
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
(* TODO: fix in rest calling convention *)
add_eff eff (E.call ~info:{ arity = Full; call_info = Call_na } fn args)
| Js_new { external_module_name = module_name; name = fn; scopes } ->
| Js_new { external_module_name = module_name; name = fn; splice; scopes } ->
(* handle [@@new]*)
(* This has some side effect, it will
mark its identifier (If it has) as an object,
Expand All @@ -281,15 +285,25 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
TODO: we should propagate this property
as much as we can(in alias table)
*)
let args, eff = assemble_args_no_splice arg_types args in
let fn = translate_scoped_module_val module_name fn scopes in
add_eff eff
((match cxt.continuation with
let mark () =
match cxt.continuation with
| Declare (_, id) | Assign id ->
(* Format.fprintf Format.err_formatter "%a@."Ident.print id; *)
Ext_ident.make_js_object id
| EffectCall _ | NeedValue _ -> ());
E.new_ fn args)
| EffectCall _ | NeedValue _ -> ()
in
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
let fn = translate_scoped_module_val module_name fn scopes in
add_eff eff
(mark ();
if dynamic then splice_new_apply fn args
else E.new_ fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
let fn = translate_scoped_module_val module_name fn scopes in
add_eff eff
(mark (); E.new_ fn args)
| Js_send { splice; name; js_send_scopes } -> (
match args with
| self :: args ->
Expand All @@ -300,7 +314,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
let args, eff, dynamic = assemble_args_has_splice arg_types args in
add_eff eff
(let self = translate_scoped_access js_send_scopes self in
if dynamic then splice_obj_fn_apply self name args
if dynamic then splice_obj_apply self name args
else
E.call
~info:{ arity = Full; call_info = Call_na }
Expand Down
4 changes: 2 additions & 2 deletions jscomp/frontend/ast_external_process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -800,12 +800,12 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
val_send = None;
set_name = None;
get_name = None;
splice = false;
splice;
scopes;
mk_obj = _;
return_wrapper = _;
} ->
Js_new { name; external_module_name; scopes }
Js_new { name; external_module_name; splice; scopes }
| { new_name = Some _; _ } ->
Bs_syntaxerr.err loc
(Conflict_ffi_attribute "Attribute found that conflicts with %@new")
Expand Down
7 changes: 4 additions & 3 deletions jscomp/frontend/external_ffi_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type external_spec =
| Js_new of {
name : string ;
external_module_name : external_module_name option;
splice : bool ;
scopes : string list;
}
| Js_set of {
Expand Down Expand Up @@ -224,7 +225,7 @@ let check_ffi ?loc ffi : bool =
->
upgrade (is_package_relative_path external_module_name.bundle);
check_external_module_name external_module_name
| Js_new {external_module_name ; name; scopes = _}
| Js_new {external_module_name ; name; splice = _; scopes = _}
| Js_call {external_module_name ; name ; splice = _; scopes = _ }
->
Ext_option.iter external_module_name (fun external_module_name ->
Expand Down Expand Up @@ -266,10 +267,10 @@ let is_bs_primitive s =

let () = Oprint.map_primitive_name :=

# 272 "frontend/external_ffi_types.pp.ml"
# 273 "frontend/external_ffi_types.pp.ml"
String.escaped

# 275 "frontend/external_ffi_types.pp.ml"
# 276 "frontend/external_ffi_types.pp.ml"
(* TODO: better error message when version mismatch *)
let from_string s : t =
if is_bs_primitive s then
Expand Down
1 change: 1 addition & 0 deletions jscomp/frontend/external_ffi_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type external_spec =
| Js_new of {
name : string;
external_module_name : external_module_name option;
splice : bool;
scopes : string list;
}
| Js_set of { js_set_name : string; js_set_scopes : string list }
Expand Down
3 changes: 2 additions & 1 deletion jscomp/frontend/external_ffi_types.pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type external_spec =
| Js_new of {
name : string ;
external_module_name : external_module_name option;
splice : bool ;
scopes : string list;
}
| Js_set of {
Expand Down Expand Up @@ -223,7 +224,7 @@ let check_ffi ?loc ffi : bool =
->
upgrade (is_package_relative_path external_module_name.bundle);
check_external_module_name external_module_name
| Js_new {external_module_name ; name; scopes = _}
| Js_new {external_module_name ; name; splice = _; scopes = _}
| Js_call {external_module_name ; name ; splice = _; scopes = _ }
->
Ext_option.iter external_module_name (fun external_module_name ->
Expand Down
34 changes: 17 additions & 17 deletions jscomp/main/builtin_cmi_datasets.ml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions jscomp/main/builtin_cmj_datasets.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* f9d475089045bd1c017bc432cb172197 *)
(* 6315eecf577934fb71f2fee59c91a9c2 *)
let module_names : string array = Obj.magic (
"Js" (* 23 *),
"Arg" (* 217 *),
Expand All @@ -25,7 +25,7 @@ let module_names : string array = Obj.magic (
"Buffer" (* 531 *),
"Digest" (* 153 *),
"Genlex" (* 44 *),
"Js_exn" (* 950 *),
"Js_exn" (* 957 *),
"Js_int" (* 116 *),
"Js_obj" (* 23 *),
"Lexing" (* 807 *),
Expand Down Expand Up @@ -151,7 +151,7 @@ let module_data : string array = Obj.magic (
(* Buffer *)"\132\149\166\190\000\000\001\255\000\000\000\140\000\000\001\216\000\000\001\188\160\b\000\000P\000\176#nth\144\160\160B@@@\176#sub\144\160\160C@@@\176$blit\144\160\160E@@@\176%clear\144\160\160A@@\144\148\192A\160\176\001\004\007!b@@\151\176\162A\144(position\160\144\004\t\160\146\160\025_i\000\000\000\000\000@@\176\1924stdlib-406/buffer.mlv\001\007\212\001\007\226\192\004\002v\001\007\212\001\007\241@\192B@@A\176%reset\144\160\160A@@@\176&create\144\160\160A@@@\176&length\144\160\160A@@\144\148\192A\160\176\001\004\005!b@@\151\176\161A\160\004%A\160\144\004\b@\176\192\004 t\001\007\185\001\007\200\192\004!t\001\007\185\001\007\210@\192B@@@\176(add_char\144\160\160B@@@\176(contents\144\160\160A@@@\176(to_bytes\144\160\160A@@@\176(truncate\144\160\160B@@@\176)add_bytes\144\160\160B@@@\176*add_buffer\144\160\160B@@@\176*add_string\144\160\160B@@@\176,add_subbytes\144\160\160D@@@\176-add_substring\144\160\160D@@@\176.add_substitute\144\160\160C@@@\176/add_utf_8_uchar\144\160\160B@@@\1762add_utf_16be_uchar\144\160\160B@@@\1762add_utf_16le_uchar\144\160\160B@@@A",
(* Digest *)"\132\149\166\190\000\000\000\133\000\000\000*\000\000\000\135\000\000\000\127\160\b\000\000 \000\176%bytes\144\160\160A@@@\176%equal\144\160\160B@@@\176&string\144\160\160A@@@\176&to_hex\144\160\160A@@@\176'compare\144\160\160B@@@\176(from_hex\144\160\160A@@@\176(subbytes\144\160\160C@@@\176)substring\144\160\160C@@@A",
(* Genlex *)"\132\149\166\190\000\000\000\024\000\000\000\b\000\000\000\024\000\000\000\023\160\144\176*make_lexer\144\160\160A\160A@@@A",
(* Js_exn *)"\132\149\166\190\000\000\003\162\000\000\000\214\000\000\003\002\000\000\002\227\160\240\176*raiseError\144\160\160A@A\144\148\192A\160\176\001\003\246#str@@\151\176D\160\151\176\180%Error\160\160AA@\182%Error@@\160\144\004\015@\176\1920others/js_exn.mlt\001\007\140\001\007\160\192\004\002t\001\007\140\001\007\173@@\176\192\004\004t\001\007\140\001\007\142\192\004\005t\001\007\140\001\007\189@\192B@@@\176-raiseUriError\144\160\160A@A\144\148\192A\160\176\001\004\014#str@@\151\176D\160\151\176\180(URIError\160\004 @\182(URIError@@\160\144\004\014@\176\192\004\031\000Y\001\011\143\001\011\162\192\004 \000Y\001\011\143\001\011\180@@\176\192\004\"\000Y\001\011\143\001\011\145\192\004#\000Y\001\011\143\001\011\181@\192B@@@\176.raiseEvalError\144\160\160A@A\144\148\192A\160\176\001\003\250#str@@\151\176D\160\151\176\180)EvalError\160\004>@\182)EvalError@@\160\144\004\014@\176\192\004=z\001\b1\001\bE\192\004>z\001\b1\001\bV@@\176\192\004@z\001\b1\001\b3\192\004Az\001\b1\001\bk@\192B@@@\176.raiseTypeError\144\160\160A@A\144\148\192A\160\176\001\004\n#str@@\151\176D\160\151\176\180)TypeError\160\004\\@\182)TypeError@@\160\144\004\014@\176\192\004[\000S\001\n\249\001\011\012\192\004\\\000S\001\n\249\001\011\031@@\176\192\004^\000S\001\n\249\001\n\251\192\004_\000S\001\n\249\001\011 @\192B@@@\176/raiseRangeError\144\160\160A@A\144\148\192A\160\176\001\003\254#str@@\151\176D\160\151\176\180*RangeError\160\004z@\182*RangeError@@\160\144\004\014@\176\192\004y\000@\001\b\229\001\b\249\192\004z\000@\001\b\229\001\t\011@@\176\192\004|\000@\001\b\229\001\b\231\192\004}\000@\001\b\229\001\t!@\192B@@@\1760raiseSyntaxError\144\160\160A@A\144\148\192A\160\176\001\004\006#str@@\151\176D\160\151\176\180+SyntaxError\160\004\152@\182+SyntaxError@@\160\144\004\014@\176\192\004\151\000M\001\n\\\001\no\192\004\152\000M\001\n\\\001\n\132@@\176\192\004\154\000M\001\n\\\001\n^\192\004\155\000M\001\n\\\001\n\133@\192B@@@\1763raiseReferenceError\144\160\160A@A\144\148\192A\160\176\001\004\002#str@@\151\176D\160\151\176\180.ReferenceError\160\004\182@\182.ReferenceError@@\160\144\004\014@\176\192\004\181\000G\001\t\177\001\t\196\192\004\182\000G\001\t\177\001\t\220@@\176\192\004\184\000G\001\t\177\001\t\179\192\004\185\000G\001\t\177\001\t\221@\192B@@@A",
(* Js_exn *)"\132\149\166\190\000\000\003\169\000\000\000\214\000\000\003\t\000\000\002\234\160\240\176*raiseError\144\160\160A@A\144\148\192A\160\176\001\003\246#str@@\151\176D\160\151\176\180%Error\160\160AA@\198%Error@@@\160\144\004\015@\176\1920others/js_exn.mlt\001\007\140\001\007\160\192\004\002t\001\007\140\001\007\173@@\176\192\004\004t\001\007\140\001\007\142\192\004\005t\001\007\140\001\007\189@\192B@@@\176-raiseUriError\144\160\160A@A\144\148\192A\160\176\001\004\014#str@@\151\176D\160\151\176\180(URIError\160\004 @\198(URIError@@@\160\144\004\014@\176\192\004\031\000Y\001\011\143\001\011\162\192\004 \000Y\001\011\143\001\011\180@@\176\192\004\"\000Y\001\011\143\001\011\145\192\004#\000Y\001\011\143\001\011\181@\192B@@@\176.raiseEvalError\144\160\160A@A\144\148\192A\160\176\001\003\250#str@@\151\176D\160\151\176\180)EvalError\160\004>@\198)EvalError@@@\160\144\004\014@\176\192\004=z\001\b1\001\bE\192\004>z\001\b1\001\bV@@\176\192\004@z\001\b1\001\b3\192\004Az\001\b1\001\bk@\192B@@@\176.raiseTypeError\144\160\160A@A\144\148\192A\160\176\001\004\n#str@@\151\176D\160\151\176\180)TypeError\160\004\\@\198)TypeError@@@\160\144\004\014@\176\192\004[\000S\001\n\249\001\011\012\192\004\\\000S\001\n\249\001\011\031@@\176\192\004^\000S\001\n\249\001\n\251\192\004_\000S\001\n\249\001\011 @\192B@@@\176/raiseRangeError\144\160\160A@A\144\148\192A\160\176\001\003\254#str@@\151\176D\160\151\176\180*RangeError\160\004z@\198*RangeError@@@\160\144\004\014@\176\192\004y\000@\001\b\229\001\b\249\192\004z\000@\001\b\229\001\t\011@@\176\192\004|\000@\001\b\229\001\b\231\192\004}\000@\001\b\229\001\t!@\192B@@@\1760raiseSyntaxError\144\160\160A@A\144\148\192A\160\176\001\004\006#str@@\151\176D\160\151\176\180+SyntaxError\160\004\152@\198+SyntaxError@@@\160\144\004\014@\176\192\004\151\000M\001\n\\\001\no\192\004\152\000M\001\n\\\001\n\132@@\176\192\004\154\000M\001\n\\\001\n^\192\004\155\000M\001\n\\\001\n\133@\192B@@@\1763raiseReferenceError\144\160\160A@A\144\148\192A\160\176\001\004\002#str@@\151\176D\160\151\176\180.ReferenceError\160\004\182@\198.ReferenceError@@@\160\144\004\014@\176\192\004\181\000G\001\t\177\001\t\196\192\004\182\000G\001\t\177\001\t\220@@\176\192\004\184\000G\001\t\177\001\t\179\192\004\185\000G\001\t\177\001\t\221@\192B@@@A",
(* Js_int *)"\132\149\166\190\000\000\000`\000\000\000\028\000\000\000Z\000\000\000W\160\144\176%equal\144\160\160B@@\144\148\192B\160\176\001\003\242!x@\160\176\001\003\243!y@@\151\176\152@\160\144\004\n\160\144\004\t@\176\1920others/js_int.ml\001\000\168\001\023\219\001\023\242\192\004\002\001\000\168\001\023\219\001\023\247@\192B@@@A",
(* Js_obj *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
(* Lexing *)"\132\149\166\190\000\000\003\019\000\000\000\192\000\000\002\153\000\000\002v\160\b\000\000@\000\176&engine\144\160\160C@@@\176&lexeme\144\160\160A@@@\176(new_line\144\160\160A@@@\176*lexeme_end\144\160\160A@@\144\148\192A\160\176\001\004@&lexbuf@@\151\176\161C\160(pos_cnum@\160\151\176\161K\160*lex_curr_pA\160\144\004\015@\176\1924stdlib-406/lexing.ml\001\000\210\001\026\178\001\026\202\192\004\002\001\000\210\001\026\178\001\026\219@@\176\004\004\192\004\004\001\000\210\001\026\178\001\026\228@\192B@@@\176*new_engine\144\160\160C@@@\176*sub_lexeme\144\160\160C@@@\176+flush_input\144\160\160A@@@\176+from_string\144\160\160A@@@\176+lexeme_char\144\160\160B@@@\176,lexeme_end_p\144\160\160A@@\144\148\192A\160\176\001\004D&lexbuf@@\151\176\161K\160\0042A\160\144\004\b@\176\192\0041\001\000\213\001\027\021\001\027/\192\0042\001\000\213\001\027\021\001\027@@\192B@@@\176,lexeme_start\144\160\160A@@\144\148\192A\160\176\001\004>&lexbuf@@\151\176\161C\160\004M@\160\151\176\161J\160+lex_start_pA\160\144\004\014@\176\192\004L\001\000\209\001\026|\001\026\150\192\004M\001\000\209\001\026|\001\026\168@@\176\004\003\192\004O\001\000\209\001\026|\001\026\177@\192B@@@\176-from_function\144\160\160A@@@\176.lexeme_start_p\144\160\160A@@\144\148\192A\160\176\001\004B&lexbuf@@\151\176\161J\160\004\029A\160\144\004\b@\176\192\004h\001\000\212\001\026\230\001\027\002\192\004i\001\000\212\001\026\230\001\027\020@\192B@@@\176.sub_lexeme_opt\144\160\160C@@@\176/sub_lexeme_char\144\160\160B@@\144\148\192B\160\176\001\0045&lexbuf@\160\176\001\0046!i@@\151\176d\160\151\176\161A\160*lex_bufferA\160\144\004\015@\176\192\004\137\001\000\197\001\025z\001\025\163\192\004\138\001\000\197\001\025z\001\025\180@\160\144\004\017@\176\192\004\142\001\000\197\001\025z\001\025\153\192\004\143\001\000\197\001\025z\001\025\182@\192B@@@\1763sub_lexeme_char_opt\144\160\160B@@@A",
Expand Down
15 changes: 15 additions & 0 deletions jscomp/runtime/caml_splice_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ let spliceApply : obj -> obj -> obj = [%raw{|function(fn,args){
return fn.apply(null,applied)
}|}]

let spliceNewApply : obj -> obj -> obj = [%raw{|function (ctor,args){
var i, argLen;
argLen = args.length
var applied = [null] // Function.prototype.bind.apply(fn, args) requires the first element in `args` to be `null`
for(i = 0; i < argLen - 1; ++i){
applied.push(args[i])
}
var lastOne = args[argLen - 1]
for(i = 0; i < lastOne.length; ++i ){
applied.push(lastOne[i])
}
var C = Function.prototype.bind.apply(ctor, applied)
return new C()
}|}]

let spliceObjApply : obj -> obj -> obj -> obj = [%raw{|function(obj,name,args){
var i, argLen;
argLen = args.length
Expand Down
2 changes: 2 additions & 0 deletions jscomp/runtime/caml_splice_call.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ type obj = Obj.t

val spliceApply : obj -> obj -> obj

val spliceNewApply : obj -> obj -> obj

val spliceObjApply : obj -> obj -> obj -> obj
85 changes: 78 additions & 7 deletions jscomp/test/splice_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,80 @@ dynamic([
3
]);

var a$1 = [];
var a$1 = new Array(1, 2, 3, 4);

a$1.push(1, 2, 3, 4);
eq("File \"splice_test.ml\", line 49, characters 5-12", a$1, [
1,
2,
3,
4
]);

function dynamicNew(arr) {
var a = Caml_splice_call.spliceNewApply(Array, [
1,
2,
arr
]);
eq("File \"splice_test.ml\", line 53, characters 5-12", a, Caml_array.concat({
hd: [
1,
2
],
tl: {
hd: arr,
tl: /* [] */0
}
}));
}

dynamicNew([
3,
4
]);

dynamicNew([]);

dynamicNew([
1,
3
]);

class Foo {
constructor(...names) {
this.names = names;
}
}
;

var f = new Foo("a", "b", "c");

eq("File \"splice_test.ml\", line 74, characters 5-12", f.names, [
"a",
"b",
"c"
]);

function dynamicFoo(arr) {
var f = Caml_splice_call.spliceNewApply(Foo, [arr]);
eq("File \"splice_test.ml\", line 78, characters 5-12", f.names, arr);
}

dynamicFoo([]);

dynamicFoo(["a"]);

dynamicFoo([
"a",
"b",
"c"
]);

var a$2 = [];

a$2.push(1, 2, 3, 4);

eq("File \"splice_test.ml\", line 51, characters 7-14", a$1, [
eq("File \"splice_test.ml\", line 95, characters 7-14", a$2, [
1,
2,
3,
Expand All @@ -81,7 +150,7 @@ function dynamic$1(arr) {
1,
arr
]);
eq("File \"splice_test.ml\", line 56, characters 7-14", a, Caml_array.concat({
eq("File \"splice_test.ml\", line 100, characters 7-14", a, Caml_array.concat({
hd: [1],
tl: {
hd: arr,
Expand Down Expand Up @@ -115,11 +184,11 @@ function f1(c) {
]);
}

eq("File \"splice_test.ml\", line 67, characters 6-13", Math.max(1, 2, 3), 3);
eq("File \"splice_test.ml\", line 111, characters 6-13", Math.max(1, 2, 3), 3);

eq("File \"splice_test.ml\", line 68, characters 6-13", Math.max(1), 1);
eq("File \"splice_test.ml\", line 112, characters 6-13", Math.max(1), 1);

eq("File \"splice_test.ml\", line 69, characters 6-13", Math.max(1, 1, 2, 3, 4, 5, 2, 3), 5);
eq("File \"splice_test.ml\", line 113, characters 6-13", Math.max(1, 1, 2, 3, 4, 5, 2, 3), 5);

Mt.from_pair_suites("splice_test.ml", suites.contents);

Expand All @@ -129,6 +198,8 @@ exports.eq = eq;
exports.Caml_splice_call = Caml_splice_call$1;
exports.f00 = f00;
exports.dynamic = dynamic;
exports.dynamicNew = dynamicNew;
exports.dynamicFoo = dynamicFoo;
exports.Pipe = Pipe;
exports.f1 = f1;
/* Not a pure module */
44 changes: 44 additions & 0 deletions jscomp/test/splice_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,50 @@ let dynamic arr =
;; dynamic [||]
;; dynamic [|1;1;3|]

(* Array constructor with a single parameter `x`
just makes an array with its length set to `x`,
so at least two parameters are needed
*)
external newArr : int -> int -> int array -> int array = "Array"
[@@bs.splice] [@@bs.new]

let () =
let a = newArr 1 2 [|3;4|] in
eq __LOC__ a [|1;2;3;4|]

let dynamicNew arr =
let a = newArr 1 2 arr in
eq __LOC__ a (Array.concat [[|1; 2|]; arr])

;; dynamicNew [|3;4|]
;; dynamicNew [||]
;; dynamicNew [|1;3|]

[%%raw{|
class Foo {
constructor(...names) {
this.names = names;
}
}
|}]

type foo

external newFoo : string array -> foo = "Foo" [@@bs.splice] [@@bs.new]
external fooNames : foo -> string array = "names" [@@get]

let () =
let f = newFoo [|"a";"b";"c"|] in
eq __LOC__ (fooNames f) [|"a";"b";"c"|]

let dynamicFoo arr =
let f = newFoo arr in
eq __LOC__ (fooNames f) arr

;; dynamicFoo [||]
;; dynamicFoo [|"a"|]
;; dynamicFoo [|"a";"b";"c"|]

module Pipe = struct
external push : int array -> int -> int array -> unit =
"push" [@@send] [@@bs.splice]
Expand Down
Loading