Skip to content

Commit 6a5b199

Browse files
authored
Merge pull request #4409 from BuckleScript/variant_as_obj
variant as objects
2 parents 2763942 + cee867e commit 6a5b199

File tree

448 files changed

+98197
-77786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

448 files changed

+98197
-77786
lines changed

jscomp/core/js_block_runtime.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ let tag_is_zero (tag : J.expression) =
4343
| Blk_class
4444
| Blk_array
4545
| Blk_record_ext _ -> false
46-
| Blk_extension_slot -> false
4746
| Blk_na _ -> not (tag_is_zero tag )
4847

4948
let needBlockRuntime (tag : J.expression) (tag_info : J.tag_info) =
@@ -62,7 +61,7 @@ let needBlockRuntime (tag : J.expression) (tag_info : J.tag_info) =
6261
| Blk_record_inlined _
6362
| Blk_constructor _ -> true
6463
| Blk_record_ext _
65-
| Blk_extension_slot -> false
64+
-> false
6665
(* converted to [Pcreate_extension] in the beginning*)
6766

6867
let option_id =

jscomp/core/js_dump.ml

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,10 @@ and expression_desc cxt ~(level:int) f x : cxt =
844844
| Caml_block(el,_, _, Blk_module fields) ->
845845
expression_desc cxt ~level f (Object (
846846
(Ext_list.map_combine fields el Ext_ident.convert)))
847+
(*name convention of Record is slight different from modules*)
847848
| Caml_block(el,_, _, Blk_record fields) ->
848-
expression_desc cxt ~level f (Object (
849-
(List.combine (Array.to_list fields) el )))
850-
(* name convention of Record is slight different from modules
851-
*)
849+
expression_desc cxt ~level f (Object ((Ext_list.combine_array fields el )))
850+
852851
| Caml_block(el,_,_, Blk_poly_var name) ->
853852
begin match el with
854853
| [hash;value] ->
@@ -870,9 +869,39 @@ and expression_desc cxt ~(level:int) f x : cxt =
870869
| _ -> assert false
871870
end
872871
| Caml_block(el,_, _, (Blk_extension | Blk_record_ext _ as ext )) ->
873-
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
872+
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
873+
| Caml_block(el,_,tag, (Blk_record_inlined p)) ->
874+
let objs =
875+
let tails =
876+
Ext_list.combine_array_append p.fields el
877+
(if !Js_config.debug then ["NAME",E.str p.name]
878+
else []
879+
) in
880+
if p.num_nonconst = 1 then tails
881+
else ("tag",
882+
if !Js_config.debug then tag else {tag with comment = Some p.name}) :: tails in
883+
if p.num_nonconst = 1 && not !Js_config.debug then
884+
pp_comment_option f (Some p.name);
885+
expression_desc cxt ~level f (Object objs)
886+
| Caml_block(el,_,tag, (Blk_constructor p)) ->
887+
let objs =
888+
let tails =
889+
Ext_list.mapi_append el (fun i e -> "_" ^ string_of_int i , e )
890+
(if !Js_config.debug then
891+
["NAME", E.str p.name]
892+
else []) in
893+
if p.num_nonconst = 1 then
894+
tails
895+
else
896+
("tag",
897+
if !Js_config.debug then tag else {tag with comment = Some p.name}) :: tails
898+
in
899+
if p.num_nonconst = 1 && not !Js_config.debug then
900+
pp_comment_option f (Some p.name);
901+
expression_desc cxt ~level f (Object objs)
874902
| Caml_block( el, mutable_flag, tag, tag_info)
875903
->
904+
pp_comment_option f (Lam_compile_util.comment_of_tag_info tag_info);
876905
(* Note that, if we ignore more than tag [0] we loose some information
877906
with regard tag
878907
@@ -937,7 +966,6 @@ and expression_desc cxt ~(level:int) f x : cxt =
937966
| Blk_class
938967
| Blk_array
939968
| Blk_record_ext _
940-
| Blk_extension_slot
941969
| Blk_na _
942970
->
943971
dbg_block_create f;
@@ -1007,7 +1035,7 @@ and expression_desc cxt ~(level:int) f x : cxt =
10071035

10081036
| Object lst ->
10091037
let action () =
1010-
if lst = [] then begin P.string f "{ }" ; cxt end else
1038+
if lst = [] then begin P.string f "{}" ; cxt end else
10111039
P.brace_vgroup f 1 (fun _ ->
10121040
property_name_and_value_list cxt f lst) in
10131041
if level > 1 then

jscomp/core/js_exp_make.ml

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ let raw_js_code ?comment info s : t =
131131
let array ?comment mt es : t =
132132
{expression_desc = Array (es,mt) ; comment}
133133

134-
let sep = " : "
135-
let merge_outer_comment comment (e : t ) =
136-
match e.comment with
137-
| None -> {e with comment = Some comment}
138-
| Some s -> { e with
139-
comment
140-
= Some (comment ^ sep ^ s)}
141-
142134
let some_comment = None
143135

144136
let optional_block e : J.expression =
@@ -178,29 +170,6 @@ let make_block ?comment
178170
(tag_info : J.tag_info)
179171
(es : t list)
180172
(mutable_flag : J.mutable_flag) : t =
181-
let comment =
182-
match comment with
183-
| None -> Lam_compile_util.comment_of_tag_info tag_info
184-
| _ -> comment in
185-
let es =
186-
match tag_info with
187-
| Blk_record_inlined {fields = des}
188-
->
189-
Ext_list.mapi es (fun i e -> merge_outer_comment des.(i) e)
190-
| Blk_record_ext _
191-
| Blk_record _
192-
| Blk_module _
193-
| Blk_module_export
194-
| Blk_tuple
195-
| Blk_array
196-
| Blk_extension_slot
197-
| Blk_extension
198-
| Blk_class
199-
| Blk_constructor _
200-
| Blk_poly_var _
201-
| Blk_na _
202-
-> es
203-
in
204173
{
205174
expression_desc = Caml_block( es, mutable_flag, tag,tag_info) ;
206175
comment
@@ -284,14 +253,15 @@ let dummy_obj ?comment (info : Lam_tag_info.t) : t =
284253
match info with
285254
| Blk_record _
286255
| Blk_module _
287-
->
288-
{comment ; expression_desc = Object ([])}
289256
| Blk_constructor _
290-
| Blk_tuple | Blk_array
291-
| Blk_poly_var _ | Blk_extension_slot
292-
| Blk_extension | Blk_na _
293257
| Blk_record_inlined _
258+
| Blk_poly_var _
259+
| Blk_extension
294260
| Blk_record_ext _
261+
->
262+
{comment ; expression_desc = Object []}
263+
| Blk_tuple | Blk_array
264+
| Blk_na _
295265
| Blk_class | Blk_module_export
296266
->
297267
{comment ; expression_desc = Array ([],Mutable)}
@@ -401,6 +371,9 @@ let record_access (e : t) (name : string) (pos : int32) =
401371
)
402372
| _ -> { expression_desc = Static_index (e, name, Some pos); comment = None}
403373

374+
(* The same as {!record_access} except tag*)
375+
let inline_record_access = record_access
376+
404377
let poly_var_tag_access (e : t) =
405378
match e.expression_desc with
406379
| Caml_block (l,_, _, _) when no_side_effect e

jscomp/core/js_exp_make.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ val record_access :
188188
Int32.t ->
189189
t
190190

191+
val inline_record_access :
192+
t ->
193+
string ->
194+
Int32.t ->
195+
t
196+
197+
191198
val extension_access :
192199
t ->
193200
string option ->

jscomp/core/js_of_lam_block.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@ let make_block mutable_flag (tag_info : Lam_tag_info.t) tag args =
4848
(* (E.int ?comment:(Lam_compile_util.comment_of_tag_info tag_info) tag *)
4949
(* :: args) *)
5050

51-
let field (field_info : Lam_compat.field_dbg_info) e i =
51+
let field (field_info : Lam_compat.field_dbg_info) e (i : int32) =
5252
match field_info with
5353
| Fld_na _
5454
| Fld_tuple
5555

56-
57-
| Fld_record_inline _
58-
| Fld_variant
5956
| Fld_array
6057
->
6158
E.array_index_by_int
@@ -68,6 +65,12 @@ let field (field_info : Lam_compat.field_dbg_info) e i =
6865
E.extension_access e (Some name) i
6966
| Fld_extension ->
7067
E.extension_access e None i
68+
| Fld_variant
69+
->
70+
E.inline_record_access e
71+
("_" ^ Int32.to_string i) i
72+
| Fld_record_inline {name}
73+
-> E.inline_record_access e name i
7174
| Fld_record {name}
7275
-> E.record_access e name i
7376
| Fld_module {name}
@@ -80,13 +83,10 @@ let set_field (field_info : Lam_compat.set_field_dbg_info) e i e0 =
8083
match field_info with
8184
| Fld_set_na
8285
-> E.assign_by_int e i e0
83-
| Fld_record_inline_set comment
84-
85-
-> (* see GPR#631*)
86-
E.assign_by_int ~comment e i e0
8786
| Fld_record_extension_set name
8887
->
8988
E.extension_assign e i name e0
89+
| Fld_record_inline_set name
9090
| Fld_record_set name ->
9191
E.record_assign e i name e0
9292

jscomp/core/js_pass_flatten_and_mark_dead.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ let subst_map () = object (self)
228228
(match tag_info with
229229
| Blk_module fields ->
230230
(match Ext_list.nth_opt fields i with
231-
| None -> Printf.sprintf "%03d" i
231+
| None -> Printf.sprintf "%d" i
232232
| Some x -> x )
233233
| Blk_record fields ->
234-
Ext_array.get_or fields i (fun _ -> Printf.sprintf "%03d" i)
235-
| _ -> Printf.sprintf "%03d" i
234+
Ext_array.get_or fields i (fun _ -> Printf.sprintf "%d" i)
235+
| _ -> Printf.sprintf "%d" i
236236
)) in
237237
(i + 1, E.var match_id :: e, (match_id, v') :: acc)) in
238238
let e =

jscomp/core/lam_compat.ml

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,48 +95,6 @@ let cmp_int (cmp : comparison) (a : int) b : bool =
9595
| Cge -> a >= b
9696

9797

98-
99-
type bigarray_kind = Lambda.bigarray_kind =
100-
Pbigarray_unknown
101-
| Pbigarray_float32 | Pbigarray_float64
102-
| Pbigarray_sint8 | Pbigarray_uint8
103-
| Pbigarray_sint16 | Pbigarray_uint16
104-
| Pbigarray_int32 | Pbigarray_int64
105-
| Pbigarray_caml_int | Pbigarray_native_int
106-
| Pbigarray_complex32 | Pbigarray_complex64
107-
108-
109-
(* let eq_bigarray_kind (p : bigarray_kind) (p1 : bigarray_kind) =
110-
match p with
111-
| Pbigarray_unknown -> p1 = Pbigarray_unknown
112-
| Pbigarray_float32 -> p1 = Pbigarray_float32
113-
| Pbigarray_float64 -> p1 = Pbigarray_float64
114-
| Pbigarray_sint8 -> p1 = Pbigarray_sint8
115-
| Pbigarray_uint8 -> p1 = Pbigarray_uint8
116-
| Pbigarray_sint16 -> p1 = Pbigarray_sint16
117-
| Pbigarray_uint16 -> p1 = Pbigarray_uint16
118-
| Pbigarray_int32 -> p1 = Pbigarray_int32
119-
| Pbigarray_int64 -> p1 = Pbigarray_int64
120-
| Pbigarray_caml_int -> p1 = Pbigarray_caml_int
121-
| Pbigarray_native_int -> p1 = Pbigarray_native_int
122-
| Pbigarray_complex32 -> p1 = Pbigarray_complex32
123-
| Pbigarray_complex64 -> p1 = Pbigarray_complex64 *)
124-
125-
126-
type bigarray_layout = Lambda.bigarray_layout =
127-
Pbigarray_unknown_layout
128-
| Pbigarray_c_layout
129-
| Pbigarray_fortran_layout
130-
131-
132-
133-
134-
(* let eq_bigarray_layout (p : bigarray_layout) (p1 : bigarray_layout) =
135-
match p with
136-
| Pbigarray_unknown_layout -> p1 = Pbigarray_unknown_layout
137-
| Pbigarray_c_layout -> p1 = Pbigarray_c_layout
138-
| Pbigarray_fortran_layout -> p1 = Pbigarray_fortran_layout *)
139-
14098
type compile_time_constant =
14199
| Big_endian
142100
| Ostype_unix
@@ -172,6 +130,7 @@ type field_dbg_info = Lambda.field_dbg_info =
172130
| Fld_extension
173131
| Fld_variant
174132
| Fld_array
133+
175134
let str_of_field_info (x : field_dbg_info) : string option =
176135
match x with
177136
| Fld_na s -> if s = "" then None else Some s

jscomp/core/lam_compat.mli

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,7 @@ type boxed_integer = Lambda.boxed_integer =
3232
type comparison = Lambda.comparison =
3333
Ceq | Cneq | Clt | Cgt | Cle | Cge
3434

35-
type bigarray_kind = Lambda.bigarray_kind =
36-
Pbigarray_unknown
37-
| Pbigarray_float32 | Pbigarray_float64
38-
| Pbigarray_sint8 | Pbigarray_uint8
39-
| Pbigarray_sint16 | Pbigarray_uint16
40-
| Pbigarray_int32 | Pbigarray_int64
41-
| Pbigarray_caml_int | Pbigarray_native_int
42-
| Pbigarray_complex32 | Pbigarray_complex64
43-
44-
45-
type bigarray_layout = Lambda.bigarray_layout =
46-
Pbigarray_unknown_layout
47-
| Pbigarray_c_layout
48-
| Pbigarray_fortran_layout
35+
4936

5037

5138

jscomp/core/lam_compile.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ and compile_recursive_let ~all_bindings
362362
Js_output.output_of_expression
363363
(Declare (Alias, id))
364364
result ~no_effects:(lazy (Lam_analysis.no_side_effects arg)), []
365-
| Lprim {primitive = Pmakeblock (0, tag_info, _) ; args = ls}
365+
| Lprim{primitive = Pmakeblock (_, _, _) ; args }
366+
when args_either_function_or_const args ->
367+
compile_lambda
368+
{cxt with continuation = Declare (Alias ,id)} arg, []
369+
(* case of lazy blocks, treat it as usual *)
370+
| Lprim {primitive = Pmakeblock (_, (Blk_record _ | Blk_constructor {num_nonconst = 1} | Blk_record_inlined {num_nonconst = 1} as tag_info) , _) ; args = ls}
366371
when Ext_list.for_all ls (fun x ->
367372
match x with
368373
| Lvar pid ->
@@ -385,7 +390,9 @@ and compile_recursive_let ~all_bindings
385390
(Js_of_lam_block.set_field
386391
(match tag_info with
387392
| Blk_record xs -> Fld_record_set xs.(i)
388-
| _ -> Fld_set_na) (E.var id) (Int32.of_int i)
393+
| Blk_record_inlined xs -> Fld_record_inline_set xs.fields.(i)
394+
| Blk_constructor _ -> Fld_record_inline_set ("_" ^ string_of_int i)
395+
| _ -> assert false) (E.var id) (Int32.of_int i)
389396
(match x with
390397
| Lvar lid -> E.var lid
391398
| Lconst x -> Lam_compile_const.translate x
@@ -394,8 +401,7 @@ and compile_recursive_let ~all_bindings
394401
))
395402
), []
396403

397-
| Lprim{primitive = Pmakeblock (_, tag_info, _) ; args }
398-
when not (args_either_function_or_const args) ->
404+
| Lprim{primitive = Pmakeblock (_, tag_info, _) ; } ->
399405

400406
(* Lconst should not appear here if we do [scc]
401407
optimization, since it's faked recursive value,

jscomp/core/lam_compile_primitive.ml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,8 @@ let translate loc
603603
(Blk_constructor {name = "Other"; num_nonconst = 1})
604604
[E.str "BS"] Immutable
605605
)
606-
| Pduprecord (Record_regular| Record_extension) ->
606+
| Pduprecord (Record_regular| Record_extension| Record_inlined _ ) ->
607607
Lam_dispatch_primitive.translate loc "caml_obj_dup" args
608-
| Pduprecord ((
609-
Record_inlined {tag = 0; num_nonconsts = 1}
610-
611-
)) ->
612-
(* _size is the length of all_lables*)
613-
(* TODO: In debug mode, need switch to *)
614-
Lam_dispatch_primitive.translate loc "caml_array_dup" args
615-
| Pduprecord (Record_inlined _)
616-
->
617-
Lam_dispatch_primitive.translate loc "caml_obj_dup" args
618608

619609
| Plazyforce
620610
(* FIXME: we don't inline lazy force or at least

0 commit comments

Comments
 (0)