Skip to content

Commit 7b206bf

Browse files
authored
* Support @new @variadic Signed-off-by: Yuta Sato <cannorin@users.noreply.github.com> * [variadic-new] stage everything Signed-off-by: Yuta Sato <cannorin@users.noreply.github.com> Co-authored-by: Yuta Sato <cannorin@users.noreply.github.com>
1 parent 4396ad4 commit 7b206bf

17 files changed

+381
-150
lines changed

Changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
**Compiler**
66

7+
- #5364 support `@new @variadic`
8+
79
**Syntax**
810

911
**Playground**

jscomp/core/lam_compile_external_call.ml

+26-12
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626

2727
module E = Js_exp_make
2828

29-
let splice_fn_apply fn args =
29+
let splice_apply fn args =
3030
E.runtime_call Js_runtime_modules.caml_splice_call "spliceApply"
3131
[ fn; E.array Immutable args ]
3232

33-
let splice_obj_fn_apply obj name args =
33+
let splice_new_apply fn args =
34+
E.runtime_call Js_runtime_modules.caml_splice_call "spliceNewApply"
35+
[ fn; E.array Immutable args ]
36+
37+
let splice_obj_apply obj name args =
3438
E.runtime_call Js_runtime_modules.caml_splice_call "spliceObjApply"
3539
[ obj; E.str name; E.array Immutable args ]
3640

@@ -253,7 +257,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
253257
if splice then
254258
let args, eff, dynamic = assemble_args_has_splice arg_types args in
255259
add_eff eff
256-
(if dynamic then splice_fn_apply fn args
260+
(if dynamic then splice_apply fn args
257261
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
258262
else
259263
let args, eff = assemble_args_no_splice arg_types args in
@@ -265,13 +269,13 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
265269
let args, eff, dynamic = assemble_args_has_splice arg_types args in
266270
(* TODO: fix in rest calling convention *)
267271
add_eff eff
268-
(if dynamic then splice_fn_apply fn args
272+
(if dynamic then splice_apply fn args
269273
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
270274
else
271275
let args, eff = assemble_args_no_splice arg_types args in
272276
(* TODO: fix in rest calling convention *)
273277
add_eff eff (E.call ~info:{ arity = Full; call_info = Call_na } fn args)
274-
| Js_new { external_module_name = module_name; name = fn; scopes } ->
278+
| Js_new { external_module_name = module_name; name = fn; splice; scopes } ->
275279
(* handle [@@new]*)
276280
(* This has some side effect, it will
277281
mark its identifier (If it has) as an object,
@@ -281,15 +285,25 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
281285
TODO: we should propagate this property
282286
as much as we can(in alias table)
283287
*)
284-
let args, eff = assemble_args_no_splice arg_types args in
285-
let fn = translate_scoped_module_val module_name fn scopes in
286-
add_eff eff
287-
((match cxt.continuation with
288+
let mark () =
289+
match cxt.continuation with
288290
| Declare (_, id) | Assign id ->
289291
(* Format.fprintf Format.err_formatter "%a@."Ident.print id; *)
290292
Ext_ident.make_js_object id
291-
| EffectCall _ | NeedValue _ -> ());
292-
E.new_ fn args)
293+
| EffectCall _ | NeedValue _ -> ()
294+
in
295+
if splice then
296+
let args, eff, dynamic = assemble_args_has_splice arg_types args in
297+
let fn = translate_scoped_module_val module_name fn scopes in
298+
add_eff eff
299+
(mark ();
300+
if dynamic then splice_new_apply fn args
301+
else E.new_ fn args)
302+
else
303+
let args, eff = assemble_args_no_splice arg_types args in
304+
let fn = translate_scoped_module_val module_name fn scopes in
305+
add_eff eff
306+
(mark (); E.new_ fn args)
293307
| Js_send { splice; name; js_send_scopes } -> (
294308
match args with
295309
| self :: args ->
@@ -300,7 +314,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
300314
let args, eff, dynamic = assemble_args_has_splice arg_types args in
301315
add_eff eff
302316
(let self = translate_scoped_access js_send_scopes self in
303-
if dynamic then splice_obj_fn_apply self name args
317+
if dynamic then splice_obj_apply self name args
304318
else
305319
E.call
306320
~info:{ arity = Full; call_info = Call_na }

jscomp/frontend/ast_external_process.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,12 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
800800
val_send = None;
801801
set_name = None;
802802
get_name = None;
803-
splice = false;
803+
splice;
804804
scopes;
805805
mk_obj = _;
806806
return_wrapper = _;
807807
} ->
808-
Js_new { name; external_module_name; scopes }
808+
Js_new { name; external_module_name; splice; scopes }
809809
| { new_name = Some _; _ } ->
810810
Bs_syntaxerr.err loc
811811
(Conflict_ffi_attribute "Attribute found that conflicts with %@new")

jscomp/frontend/external_ffi_types.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type external_spec =
7272
| Js_new of {
7373
name : string ;
7474
external_module_name : external_module_name option;
75+
splice : bool ;
7576
scopes : string list;
7677
}
7778
| Js_set of {
@@ -224,7 +225,7 @@ let check_ffi ?loc ffi : bool =
224225
->
225226
upgrade (is_package_relative_path external_module_name.bundle);
226227
check_external_module_name external_module_name
227-
| Js_new {external_module_name ; name; scopes = _}
228+
| Js_new {external_module_name ; name; splice = _; scopes = _}
228229
| Js_call {external_module_name ; name ; splice = _; scopes = _ }
229230
->
230231
Ext_option.iter external_module_name (fun external_module_name ->
@@ -266,10 +267,10 @@ let is_bs_primitive s =
266267

267268
let () = Oprint.map_primitive_name :=
268269

269-
# 272 "frontend/external_ffi_types.pp.ml"
270+
# 273 "frontend/external_ffi_types.pp.ml"
270271
String.escaped
271272

272-
# 275 "frontend/external_ffi_types.pp.ml"
273+
# 276 "frontend/external_ffi_types.pp.ml"
273274
(* TODO: better error message when version mismatch *)
274275
let from_string s : t =
275276
if is_bs_primitive s then

jscomp/frontend/external_ffi_types.mli

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type external_spec =
5858
| Js_new of {
5959
name : string;
6060
external_module_name : external_module_name option;
61+
splice : bool;
6162
scopes : string list;
6263
}
6364
| Js_set of { js_set_name : string; js_set_scopes : string list }

jscomp/frontend/external_ffi_types.pp.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type external_spec =
7171
| Js_new of {
7272
name : string ;
7373
external_module_name : external_module_name option;
74+
splice : bool ;
7475
scopes : string list;
7576
}
7677
| Js_set of {
@@ -223,7 +224,7 @@ let check_ffi ?loc ffi : bool =
223224
->
224225
upgrade (is_package_relative_path external_module_name.bundle);
225226
check_external_module_name external_module_name
226-
| Js_new {external_module_name ; name; scopes = _}
227+
| Js_new {external_module_name ; name; splice = _; scopes = _}
227228
| Js_call {external_module_name ; name ; splice = _; scopes = _ }
228229
->
229230
Ext_option.iter external_module_name (fun external_module_name ->

jscomp/main/builtin_cmi_datasets.ml

+17-17
Large diffs are not rendered by default.

jscomp/main/builtin_cmj_datasets.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* f9d475089045bd1c017bc432cb172197 *)
1+
(* 6315eecf577934fb71f2fee59c91a9c2 *)
22
let module_names : string array = Obj.magic (
33
"Js" (* 23 *),
44
"Arg" (* 217 *),
@@ -25,7 +25,7 @@ let module_names : string array = Obj.magic (
2525
"Buffer" (* 531 *),
2626
"Digest" (* 153 *),
2727
"Genlex" (* 44 *),
28-
"Js_exn" (* 950 *),
28+
"Js_exn" (* 957 *),
2929
"Js_int" (* 116 *),
3030
"Js_obj" (* 23 *),
3131
"Lexing" (* 807 *),
@@ -151,7 +151,7 @@ let module_data : string array = Obj.magic (
151151
(* 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",
152152
(* 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",
153153
(* 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",
154-
(* 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",
154+
(* 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",
155155
(* 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",
156156
(* Js_obj *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
157157
(* 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",

jscomp/runtime/caml_splice_call.ml

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ let spliceApply : obj -> obj -> obj = [%raw{|function(fn,args){
3838
return fn.apply(null,applied)
3939
}|}]
4040

41+
let spliceNewApply : obj -> obj -> obj = [%raw{|function (ctor,args){
42+
var i, argLen;
43+
argLen = args.length
44+
var applied = [null] // Function.prototype.bind.apply(fn, args) requires the first element in `args` to be `null`
45+
for(i = 0; i < argLen - 1; ++i){
46+
applied.push(args[i])
47+
}
48+
var lastOne = args[argLen - 1]
49+
for(i = 0; i < lastOne.length; ++i ){
50+
applied.push(lastOne[i])
51+
}
52+
var C = Function.prototype.bind.apply(ctor, applied)
53+
return new C()
54+
}|}]
55+
4156
let spliceObjApply : obj -> obj -> obj -> obj = [%raw{|function(obj,name,args){
4257
var i, argLen;
4358
argLen = args.length

jscomp/runtime/caml_splice_call.mli

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ type obj = Obj.t
2727

2828
val spliceApply : obj -> obj -> obj
2929

30+
val spliceNewApply : obj -> obj -> obj
31+
3032
val spliceObjApply : obj -> obj -> obj -> obj

jscomp/test/splice_test.js

+78-7
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,80 @@ dynamic([
6464
3
6565
]);
6666

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

69-
a$1.push(1, 2, 3, 4);
69+
eq("File \"splice_test.ml\", line 49, characters 5-12", a$1, [
70+
1,
71+
2,
72+
3,
73+
4
74+
]);
75+
76+
function dynamicNew(arr) {
77+
var a = Caml_splice_call.spliceNewApply(Array, [
78+
1,
79+
2,
80+
arr
81+
]);
82+
eq("File \"splice_test.ml\", line 53, characters 5-12", a, Caml_array.concat({
83+
hd: [
84+
1,
85+
2
86+
],
87+
tl: {
88+
hd: arr,
89+
tl: /* [] */0
90+
}
91+
}));
92+
}
93+
94+
dynamicNew([
95+
3,
96+
4
97+
]);
98+
99+
dynamicNew([]);
100+
101+
dynamicNew([
102+
1,
103+
3
104+
]);
105+
106+
class Foo {
107+
constructor(...names) {
108+
this.names = names;
109+
}
110+
}
111+
;
112+
113+
var f = new Foo("a", "b", "c");
114+
115+
eq("File \"splice_test.ml\", line 74, characters 5-12", f.names, [
116+
"a",
117+
"b",
118+
"c"
119+
]);
120+
121+
function dynamicFoo(arr) {
122+
var f = Caml_splice_call.spliceNewApply(Foo, [arr]);
123+
eq("File \"splice_test.ml\", line 78, characters 5-12", f.names, arr);
124+
}
125+
126+
dynamicFoo([]);
127+
128+
dynamicFoo(["a"]);
129+
130+
dynamicFoo([
131+
"a",
132+
"b",
133+
"c"
134+
]);
135+
136+
var a$2 = [];
137+
138+
a$2.push(1, 2, 3, 4);
70139

71-
eq("File \"splice_test.ml\", line 51, characters 7-14", a$1, [
140+
eq("File \"splice_test.ml\", line 95, characters 7-14", a$2, [
72141
1,
73142
2,
74143
3,
@@ -81,7 +150,7 @@ function dynamic$1(arr) {
81150
1,
82151
arr
83152
]);
84-
eq("File \"splice_test.ml\", line 56, characters 7-14", a, Caml_array.concat({
153+
eq("File \"splice_test.ml\", line 100, characters 7-14", a, Caml_array.concat({
85154
hd: [1],
86155
tl: {
87156
hd: arr,
@@ -115,11 +184,11 @@ function f1(c) {
115184
]);
116185
}
117186

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

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

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

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

@@ -129,6 +198,8 @@ exports.eq = eq;
129198
exports.Caml_splice_call = Caml_splice_call$1;
130199
exports.f00 = f00;
131200
exports.dynamic = dynamic;
201+
exports.dynamicNew = dynamicNew;
202+
exports.dynamicFoo = dynamicFoo;
132203
exports.Pipe = Pipe;
133204
exports.f1 = f1;
134205
/* Not a pure module */

jscomp/test/splice_test.ml

+44
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,50 @@ let dynamic arr =
3737
;; dynamic [||]
3838
;; dynamic [|1;1;3|]
3939

40+
(* Array constructor with a single parameter `x`
41+
just makes an array with its length set to `x`,
42+
so at least two parameters are needed
43+
*)
44+
external newArr : int -> int -> int array -> int array = "Array"
45+
[@@bs.splice] [@@bs.new]
46+
47+
let () =
48+
let a = newArr 1 2 [|3;4|] in
49+
eq __LOC__ a [|1;2;3;4|]
50+
51+
let dynamicNew arr =
52+
let a = newArr 1 2 arr in
53+
eq __LOC__ a (Array.concat [[|1; 2|]; arr])
54+
55+
;; dynamicNew [|3;4|]
56+
;; dynamicNew [||]
57+
;; dynamicNew [|1;3|]
58+
59+
[%%raw{|
60+
class Foo {
61+
constructor(...names) {
62+
this.names = names;
63+
}
64+
}
65+
|}]
66+
67+
type foo
68+
69+
external newFoo : string array -> foo = "Foo" [@@bs.splice] [@@bs.new]
70+
external fooNames : foo -> string array = "names" [@@get]
71+
72+
let () =
73+
let f = newFoo [|"a";"b";"c"|] in
74+
eq __LOC__ (fooNames f) [|"a";"b";"c"|]
75+
76+
let dynamicFoo arr =
77+
let f = newFoo arr in
78+
eq __LOC__ (fooNames f) arr
79+
80+
;; dynamicFoo [||]
81+
;; dynamicFoo [|"a"|]
82+
;; dynamicFoo [|"a";"b";"c"|]
83+
4084
module Pipe = struct
4185
external push : int array -> int -> int array -> unit =
4286
"push" [@@send] [@@bs.splice]

0 commit comments

Comments
 (0)