Skip to content

Commit d41591c

Browse files
committed
Completely remove record_repr from lambda.
1 parent 2d8f2d5 commit d41591c

10 files changed

+26
-52
lines changed

compiler/core/js_dump.ml

+9-14
Original file line numberDiff line numberDiff line change
@@ -759,24 +759,19 @@ and expression_desc cxt ~(level : int) f x : cxt =
759759
Ext_list.map_combine fields el (fun x ->
760760
Js_op.Lit (Ext_ident.convert x)) ))
761761
(*name convention of Record is slight different from modules*)
762-
| Caml_block (el, mutable_flag, _, Blk_record {fields; record_repr}) -> (
762+
| Caml_block (el, mutable_flag, _, Blk_record {fields}) ->
763763
if
764764
Array.length fields <> 0
765-
&& Ext_array.for_alli fields (fun i v -> string_of_int i = v)
765+
&& Ext_array.for_alli fields (fun i (v, _) -> string_of_int i = v)
766766
then expression_desc cxt ~level f (Array (el, mutable_flag))
767767
else
768-
match record_repr with
769-
| Record_regular ->
770-
expression_desc cxt ~level f
771-
(Object (None, Ext_list.combine_array fields el (fun i -> Js_op.Lit i)))
772-
| Record_optional ->
773-
let fields =
774-
Ext_list.array_list_filter_map fields el (fun f x ->
775-
match x.expression_desc with
776-
| Undefined _ -> None
777-
| _ -> Some (Js_op.Lit f, x))
778-
in
779-
expression_desc cxt ~level f (Object (None, fields)))
768+
let fields =
769+
Ext_list.array_list_filter_map fields el (fun (f, opt) x ->
770+
match x.expression_desc with
771+
| Undefined _ when opt -> None
772+
| _ -> Some (Js_op.Lit f, x))
773+
in
774+
expression_desc cxt ~level f (Object (None, fields))
780775
| Caml_block (el, _, _, Blk_poly_var _) -> (
781776
match el with
782777
| [tag; value] ->

compiler/core/js_pass_flatten_and_mark_dead.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ let subst_map (substitution : J.expression Hash_ident.t) =
207207
match Ext_list.nth_opt fields i with
208208
| None -> Printf.sprintf "%d" i
209209
| Some x -> x)
210-
| Blk_record {fields} ->
211-
Ext_array.get_or fields i (fun _ ->
212-
Printf.sprintf "%d" i)
210+
| Blk_record {fields} -> (
211+
try fst fields.(i) with _ -> Printf.sprintf "%d" i)
213212
| _ -> Printf.sprintf "%d" i)
214213
in
215214
(i + 1, E.var match_id :: e, (match_id, v') :: acc))

compiler/core/lam_compile.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ let compile output_prefix =
425425
S.exp
426426
(Js_of_lam_block.set_field
427427
(match tag_info with
428-
| Blk_record {fields = xs} -> Fld_record_set xs.(i)
428+
| Blk_record {fields = xs} -> Fld_record_set (fst xs.(i))
429429
| Blk_record_inlined xs ->
430430
Fld_record_inline_set (fst xs.fields.(i))
431431
| Blk_constructor p -> (

compiler/core/lam_convert.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ let lam_extension_id loc (head : Lam.t) =
3434
let lazy_block_info : Lam_tag_info.t =
3535
Blk_record
3636
{
37-
fields = [|Literals.lazy_done; Literals.lazy_val|];
37+
fields = [|(Literals.lazy_done, false); (Literals.lazy_val, false)|];
3838
mutable_flag = Mutable;
39-
record_repr = Record_regular;
4039
}
4140

4241
(** A conservative approach to avoid packing exceptions

compiler/core/lam_util.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ let field_flatten_get
198198
| Fld_record {name} ->
199199
let found = ref None in
200200
for i = 0 to Array.length fields - 1 do
201-
if fields.(i) = name then found := Ext_list.nth_opt ls i done;
201+
if fst(fields.(i)) = name then found := Ext_list.nth_opt ls i done;
202202
(match !found with
203203
| Some c -> Lam.const c
204204
| None -> lam())

compiler/ml/lambda.ml

+6-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
type loc_kind = Loc_FILE | Loc_LINE | Loc_MODULE | Loc_LOC | Loc_POS
1717

18-
type record_repr = Record_regular | Record_optional
19-
2018
type tag_info =
2119
| Blk_constructor of {
2220
name: string;
@@ -35,9 +33,8 @@ type tag_info =
3533
| Blk_tuple
3634
| Blk_poly_var of string
3735
| Blk_record of {
38-
fields: string array;
36+
fields: (string * bool (* optional *)) array;
3937
mutable_flag: Asttypes.mutable_flag;
40-
record_repr: record_repr;
4138
}
4239
| Blk_module of string list
4340
| Blk_module_export of Ident.t list
@@ -87,12 +84,13 @@ let find_name (attr : Parsetree.attribute) =
8784
Some s
8885
| _ -> None
8986

90-
let blk_record (fields : (label * _) array) mut record_repr =
87+
let blk_record (fields : (label * _) array) mut =
9188
let all_labels_info =
9289
Ext_array.map fields (fun (lbl, _) ->
93-
Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
90+
( Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name,
91+
lbl.lbl_optional ))
9492
in
95-
Blk_record {fields = all_labels_info; mutable_flag = mut; record_repr}
93+
Blk_record {fields = all_labels_info; mutable_flag = mut}
9694

9795
let blk_record_ext fields mutable_flag =
9896
let all_labels_info =
@@ -114,12 +112,7 @@ let blk_record_inlined fields name num_nonconst ~tag ~attrs mutable_flag =
114112
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; attrs}
115113

116114
let ref_tag_info : tag_info =
117-
Blk_record
118-
{
119-
fields = [|"contents"|];
120-
mutable_flag = Mutable;
121-
record_repr = Record_regular;
122-
}
115+
Blk_record {fields = [|("contents", false)|]; mutable_flag = Mutable}
123116

124117
type field_dbg_info =
125118
| Fld_record of {name: string; mutable_flag: Asttypes.mutable_flag}

compiler/ml/lambda.mli

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ open Asttypes
1919

2020
type loc_kind = Loc_FILE | Loc_LINE | Loc_MODULE | Loc_LOC | Loc_POS
2121

22-
type record_repr = Record_regular | Record_optional
23-
2422
type tag_info =
2523
| Blk_constructor of {
2624
name: string;
@@ -39,9 +37,8 @@ type tag_info =
3937
| Blk_tuple
4038
| Blk_poly_var of string
4139
| Blk_record of {
42-
fields: string array;
40+
fields: (string * bool (* optional *)) array;
4341
mutable_flag: mutable_flag;
44-
record_repr: record_repr;
4542
}
4643
| Blk_module of string list
4744
| Blk_module_export of Ident.t list
@@ -68,7 +65,6 @@ val mutable_flag_of_tag_info : tag_info -> mutable_flag
6865
val blk_record :
6966
(Types.label_description * Typedtree.record_label_definition) array ->
7067
mutable_flag ->
71-
record_repr ->
7268
tag_info
7369

7470
val blk_record_ext :

compiler/ml/printlambda.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ let print_taginfo ppf = function
9797
fprintf ppf "%s/%i" name num_nonconst
9898
| Blk_poly_var name -> fprintf ppf "`%s" name
9999
| Blk_record {fields = ss} ->
100-
fprintf ppf "[%s]" (String.concat ";" (Array.to_list ss))
100+
fprintf ppf "[%s]" (String.concat ";" (List.map fst (Array.to_list ss)))
101101
| Blk_module ss -> fprintf ppf "[%s]" (String.concat ";" ss)
102102
| Blk_some -> fprintf ppf "some"
103103
| Blk_some_not_nested -> fprintf ppf "some_not_nested"

compiler/ml/translcore.ml

+2-11
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,7 @@ and transl_record loc env fields repres opt_init_expr =
12061206
match repres with
12071207
| Record_float_unused -> assert false
12081208
| Record_regular ->
1209-
Lconst
1210-
(Const_block
1211-
( Lambda.blk_record fields mut
1212-
(if optional then Record_optional else Record_regular),
1213-
cl ))
1209+
Lconst (Const_block (Lambda.blk_record fields mut, cl))
12141210
| Record_inlined {tag; name; num_nonconsts; attrs} ->
12151211
Lconst
12161212
(Const_block
@@ -1226,12 +1222,7 @@ and transl_record loc env fields repres opt_init_expr =
12261222
with Not_constant -> (
12271223
match repres with
12281224
| Record_regular ->
1229-
Lprim
1230-
( Pmakeblock
1231-
(Lambda.blk_record fields mut
1232-
(if optional then Record_optional else Record_regular)),
1233-
ll,
1234-
loc )
1225+
Lprim (Pmakeblock (Lambda.blk_record fields mut), ll, loc)
12351226
| Record_float_unused -> assert false
12361227
| Record_inlined {tag; name; num_nonconsts; attrs} ->
12371228
Lprim

tests/tests/src/record_regression.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ newrecord$4.aa = undefined;
5555

5656
function setAA(ao) {
5757
return {
58-
aa: ao
58+
aa: ao,
59+
bb: undefined
5960
};
6061
}
6162

0 commit comments

Comments
 (0)