Skip to content
Closed
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
1 change: 1 addition & 0 deletions changes/recursive-record-proto-field.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix a record field renamed with `@as("__proto__")` (or a variant tag renamed with `@tag("__proto__")`) of a recursive value `let rec x = {...}`: it is now an own property of the value, as it is of a record literal, instead of being dropped, becoming the prototype, or throwing `Cyclic __proto__ value`. An optional field of a recursive value set to `None` is now left out, as it is of a record literal, instead of being an own property holding `undefined`.
11 changes: 11 additions & 0 deletions compiler/backend/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,17 @@ let define_own_property (e : t) (name : string) (value : t) : t =
];
]

let define_own_properties (e : t) (source : t) : t =
let object_ = js_global "Object" in
call ~info:Js_call_info.ml_full_call
(dot object_ "defineProperties")
[
e;
call ~info:Js_call_info.ml_full_call
(dot object_ "getOwnPropertyDescriptors")
[source];
]

let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
match (e0.expression_desc, e1.expression_desc) with
| ( (Null | Undefined _),
Expand Down
5 changes: 5 additions & 0 deletions compiler/backend/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ val define_own_property : t -> string -> t -> t
data property of [e], as a property of an object literal does, where an
assignment would run a setter that [e] inherits *)

val define_own_properties : t -> t -> t
(** [Object.defineProperties(e, Object.getOwnPropertyDescriptors(source))]:
copies the own properties of [source] to [e] as own data properties, where
[Object.assign] would run a setter that [e] inherits *)

val true_ : t

val false_ : t
Expand Down
103 changes: 73 additions & 30 deletions compiler/backend/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,40 @@ let spreads_in_place (parts : J.expression list) =
in
in_place

(* Sets field [i] of [obj], an object made by the compiler, as an own data
property, as an object literal would. Assigning the field makes one or
replaces the own property [obj] already has, except for [__proto__] when
[obj] has no own [__proto__]: the assignment runs the setter [obj]
inherits from [Object.prototype], which sets the prototype of [obj], or
does nothing. [Object.defineProperty] replaces an own [__proto__] too. *)
let init_field (fld_info : Lambda.set_field_dbg_info) obj i value =
match set_field_name fld_info with
| "__proto__" -> E.define_own_property obj "__proto__" value
| _ -> Js_of_lam_block.set_field fld_info obj (Int32.of_int i) value

(* Whether a block made with [tag_info] is an object with a [__proto__]
property, a field or a tag renamed with [@as] or [@tag], which
[Object.assign] would copy to another object through the [__proto__]
setter it inherits, not as an own property. The names are those
[Js_dump] prints. *)
let block_has_proto_key (tag_info : Lambda.tag_info) =
let is_proto name = name = "__proto__" in
match tag_info with
| Blk_record {fields; array = false} ->
Array.exists (fun (name, _) -> is_proto name) fields
| Blk_record {array = true} -> false
| Blk_record_inlined {fields; runtime} -> (
Array.exists (fun (name, _) -> is_proto name) fields
||
match runtime with
| Tagged {tag_name} -> tag_name = Some "__proto__"
| Untagged _ -> false)
| Blk_constructor {runtime = {tag_name}} -> tag_name = Some "__proto__"
| Blk_record_ext {fields} -> Array.exists is_proto fields
| Blk_module fields ->
List.exists (fun name -> is_proto (Ext_ident.convert name)) fields
| Blk_tuple | Blk_poly_var | Blk_module_export _ | Blk_extension -> false

let compile output_prefix =
(* When compiling a read from another module, a nested source path like
Other.A.B.make reaches this point as nested module-field reads:
Expand Down Expand Up @@ -733,27 +767,41 @@ let compile output_prefix =
*)
( Js_output.make
(S.define_variable ~kind:Variable id (E.dummy_obj tag_info)
:: List.mapi
(fun i (x : Lambda.t) ->
S.exp
(Js_of_lam_block.set_field
(match tag_info with
| Blk_record {fields = xs} -> Fld_record_set (fst xs.(i))
:: List.concat
(List.mapi
(fun i (x : Lambda.t) ->
let (fld_info : Lambda.set_field_dbg_info), optional =
match tag_info with
| Blk_record {fields = xs; array} ->
let name, optional = xs.(i) in
(Fld_record_set name, optional && not array)
| Blk_record_inlined xs ->
Fld_record_inline_set (fst xs.fields.(i))
| Blk_constructor p -> (
let name, optional = xs.fields.(i) in
(Fld_record_inline_set name, optional)
| Blk_constructor p ->
let is_cons = p.name = Literals.cons in
match (is_cons, i) with
| true, 0 -> Fld_record_inline_set Literals.hd
| true, 1 -> Fld_record_inline_set Literals.tl
| _, _ -> Fld_record_inline_set ("_" ^ string_of_int i))
| _ -> assert false)
(E.var id) (Int32.of_int i)
(match x with
let name =
match (is_cons, i) with
| true, 0 -> Literals.hd
| true, 1 -> Literals.tl
| _, _ -> "_" ^ string_of_int i
in
(Fld_record_inline_set name, false)
| _ -> assert false
in
let value =
match x with
| Lvar lid -> E.var lid
| Lconst x -> Lam_compile_const.translate x
| _ -> assert false)))
ls),
| _ -> assert false
in
match value.expression_desc with
(* An optional field of an object record set to [None] is
left out, as [Js_dump] leaves it out of an object
literal; array-backed records keep every slot. *)
| Undefined _ when optional -> []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve undefined slots in recursive array records

This omission also applies to array-backed records, although Js_dump preserves every array element, including optional None values (compiler/backend/js_dump.ml:929–930). For example, type rec t = {@as("0") next: t, @as("1") value?: int} with let rec x = {next: x, value: ?None} now initializes only x[0], leaving the array length at 1 instead of 2. Optional fields before other elements instead become holes, changing array iteration and own-key behavior. Restrict the omission to object-backed records and retain explicit undefined assignments for array slots.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. For type rec t = {@as("0") next: t, @as("1") value?: int}, let rec x = {next: x, value: ?None} omitted x[1] = undefined, leaving x.length === 1 instead of 2, and a leading ?None left index 0 as a hole.

Fixed in db553ff (after rebasing onto master): compile_recursive_let now omits optional Undefined fields only for object-backed records (array = false), so array-backed records assign every slot as Js_dump does for array literals. Regression tests in recursive_record_proto_field_test.res (arrayOptionalNone, arrayLeadingNone, arrayOptionalComputed) check Array.length and Object.hasOwn for both trailing and leading ?None slots. No other checked-in outputs change.

| _ -> [S.exp (init_field fld_info (E.var id) i value)])
ls)),
[] )
| Lprim {primitive = Pmakeblock tag_info} -> (
(* Lconst should not appear here if we do [scc]
Expand All @@ -769,8 +817,11 @@ let compile output_prefix =
(List.append b
[
S.exp
(E.runtime_call Primitive_modules.object_ "updateDummy"
[E.var id; v]);
(if block_has_proto_key tag_info then
E.define_own_properties (E.var id) v
else
E.runtime_call Primitive_modules.object_ "updateDummy"
[E.var id; v]);
]),
[S.define_variable ~kind:Variable id (E.dummy_obj tag_info)] )
| _ -> assert false)
Expand Down Expand Up @@ -2240,23 +2291,15 @@ let compile output_prefix =
the base before the copy. Copy first, then set each field. *)
let copy = E.var id in
(* Each override is an own property of the copy, as it is of the
literal. Assigning a field makes one, except for [__proto__]
when [init] has no own [__proto__]: the assignment runs the
setter the copy inherits from [Object.prototype], which sets
the prototype of the copy, or does nothing. *)
let set_field fld_info i value =
match set_field_name fld_info with
| "__proto__" -> E.define_own_property copy "__proto__" value
| _ ->
Js_of_lam_block.set_field fld_info copy (Int32.of_int i) value
in
literal, also when [init] has no own [__proto__] *)
let assignments =
List.concat_map
(fun (i, fld_info, set_loc, (output : Js_output.t), value) ->
output.block
@ [
S.exp
(with_source_loc set_loc (set_field fld_info i value));
(with_source_loc set_loc
(init_field fld_info copy i value));
])
overrides
in
Expand Down
Loading
Loading