From 6e4590d5ee3658e55f414b2eace28a87c7475a8f Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 28 Nov 2026 13:49:12 -0500 Subject: [PATCH 1/3] Define a __proto__ field of a recursive value as its own property Why: A recursive value `let rec x = {...}` is made as an empty object whose fields are set afterwards: assigned one by one when they are variables or constants, or copied from the record with Object.assign. For a field renamed with @as("__proto__"), or a tag renamed with @tag("__proto__"), both run the __proto__ setter the object inherits from Object.prototype, where the record literal prints ["__proto__"]: v and makes an own property. An int was dropped, an object became the prototype, and the record itself threw "Cyclic __proto__ value". The field is now defined with Object.defineProperty, as a record spread's copy already does, and a record with such a key is copied with Object.defineProperties. An optional field set to None is left out, as it is of a record literal, so that it does not become an own property holding undefined. JS modified (recursive record with a __proto__ field): Example: @inline(never) let id = x => x type rec t = {next: t, @as("__proto__") p: int} let rec n = {next: n, p: 1} let rec m = {next: m, p: id(2)} Before (js): let n = {}; n.next = n; n.__proto__ = 1; let m = {}; Primitive_object.updateDummy(m, { next: m, ["__proto__"]: id(2) }); After (js): let n = {}; n.next = n; Object.defineProperty(n, "__proto__", { value: 1, writable: true, enumerable: true, configurable: true }); let m = {}; Object.defineProperties(m, Object.getOwnPropertyDescriptors({ next: m, ["__proto__"]: id(2) })); JS modified (recursive variant with a __proto__ tag): Example: @tag("__proto__") type rec tagged = First({nextTagged: tagged}) | Second({value: int}) let rec taggedFirst = First({nextTagged: taggedFirst}) Before (js): let taggedFirst = {}; Primitive_object.updateDummy(taggedFirst, { ["__proto__"]: "First", nextTagged: taggedFirst }); After (js): let taggedFirst = {}; Object.defineProperties(taggedFirst, Object.getOwnPropertyDescriptors({ ["__proto__"]: "First", nextTagged: taggedFirst })); JS modified (recursive record with optional fields set to None): Example: type rec r = {next: r, @as("__proto__") p?: int, q?: int} let rec x = {next: x, p: ?None, q: ?None} Before (js): let x = {}; x.next = x; x.__proto__ = undefined; x.q = undefined; After (js): let x = {}; x.next = x; Review: Does the output change for other recursive values? No: only a block whose keys, as Js_dump prints them, include __proto__ is copied with Object.defineProperties, and only a field named __proto__ is defined with Object.defineProperty. Other values still use updateDummy and assignments, and no checked-in output changes. An optional field set to None is no longer assigned undefined, whatever its name, so `Object.keys` of the value matches the literal's. Review: Is Object.defineProperties the same as Object.assign otherwise? For the copied literal, yes: its properties are writable, enumerable and configurable data properties, defined in the same order, and the empty object has no own properties they would replace. Review: Why not change updateDummy in the runtime? Recursive modules use it too, and every other recursive value keeps the call it has. Review: Why skip a None field in the caller and not in init_field? The record spread's copy calls init_field too, and there an override set to None must replace the field the copy took from the spread record. Signed-off-by: Rob --- changes/recursive-record-proto-field.fix.md | 1 + compiler/backend/js_exp_make.ml | 11 ++ compiler/backend/js_exp_make.mli | 5 + compiler/backend/lam_compile.ml | 102 ++++++++---- .../src/recursive_record_proto_field_test.mjs | 152 ++++++++++++++++++ .../src/recursive_record_proto_field_test.res | 104 ++++++++++++ 6 files changed, 345 insertions(+), 30 deletions(-) create mode 100644 changes/recursive-record-proto-field.fix.md create mode 100644 tests/compiler_tests/src/recursive_record_proto_field_test.mjs create mode 100644 tests/compiler_tests/src/recursive_record_proto_field_test.res diff --git a/changes/recursive-record-proto-field.fix.md b/changes/recursive-record-proto-field.fix.md new file mode 100644 index 0000000000..a4037f0e70 --- /dev/null +++ b/changes/recursive-record-proto-field.fix.md @@ -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`. diff --git a/compiler/backend/js_exp_make.ml b/compiler/backend/js_exp_make.ml index 5e734c9d54..ffaaffb710 100644 --- a/compiler/backend/js_exp_make.ml +++ b/compiler/backend/js_exp_make.ml @@ -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 _), diff --git a/compiler/backend/js_exp_make.mli b/compiler/backend/js_exp_make.mli index e0c891f6dd..daaf0fc89b 100644 --- a/compiler/backend/js_exp_make.mli +++ b/compiler/backend/js_exp_make.mli @@ -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 diff --git a/compiler/backend/lam_compile.ml b/compiler/backend/lam_compile.ml index 6fe20f7a1d..2a7aecb7fe 100644 --- a/compiler/backend/lam_compile.ml +++ b/compiler/backend/lam_compile.ml @@ -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: @@ -733,27 +767,40 @@ 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} -> + let name, optional = xs.(i) in + (Fld_record_set name, optional) | 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 set to [None] is left out, as + [Js_dump] leaves it out of a record literal *) + | Undefined _ when optional -> [] + | _ -> [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] @@ -769,8 +816,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) @@ -2240,23 +2290,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 diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.mjs b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs new file mode 100644 index 0000000000..b72f6d341b --- /dev/null +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs @@ -0,0 +1,152 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; + +function id(x) { + return x; +} + +let constant = {}; + +constant.next = constant; + +Object.defineProperty(constant, "__proto__", { + value: 1, + writable: true, + enumerable: true, + configurable: true +}); + +let computed = {}; + +Object.defineProperties(computed, Object.getOwnPropertyDescriptors({ + next: computed, + ["__proto__"]: id(2) +})); + +let withObject = {}; + +Object.defineProperties(withObject, Object.getOwnPropertyDescriptors({ + self: withObject, + ["__proto__"]: { + x: 3 + } +})); + +let withSelf = {}; + +withSelf.cyclic = withSelf; + +Object.defineProperty(withSelf, "__proto__", { + value: withSelf, + writable: true, + enumerable: true, + configurable: true +}); + +let inlineConstant = {}; + +inlineConstant.nextInline = inlineConstant; + +Object.defineProperty(inlineConstant, "__proto__", { + value: 4, + writable: true, + enumerable: true, + configurable: true +}); + +let inlineComputed = {}; + +Object.defineProperties(inlineComputed, Object.getOwnPropertyDescriptors({ + TAG: "Inline", + nextInline: inlineComputed, + ["__proto__"]: id(5) +})); + +let taggedFirst = {}; + +Object.defineProperties(taggedFirst, Object.getOwnPropertyDescriptors({ + ["__proto__"]: "First", + nextTagged: taggedFirst +})); + +let optionalNone = {}; + +optionalNone.nextOptional = optionalNone; + +function isOrdinary(x) { + return Object.getPrototypeOf(x) === Object.prototype; +} + +Mocha.describe("Recursive_record_proto_field_test", () => { + Mocha.test("a field set from a constant", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 53, characters 7-14", Object.hasOwn(constant, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 54, characters 7-14", id(constant).__proto__, 1); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 55, characters 7-14", Object.getPrototypeOf(constant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 56, characters 7-14", id(constant).next === constant); + }); + Mocha.test("a computed field", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 60, characters 7-14", Object.hasOwn(computed, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 61, characters 7-14", computed.__proto__, 2); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 62, characters 7-14", Object.getPrototypeOf(computed) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 63, characters 7-14", id(computed).next === computed); + }); + Mocha.test("an object field does not become the prototype", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 67, characters 7-14", Object.hasOwn(withObject, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 68, characters 7-14", withObject.__proto__.x, 3); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 69, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); + }); + Mocha.test("a field holding the record itself", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 73, characters 7-14", Object.hasOwn(withSelf, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 74, characters 7-14", id(withSelf).__proto__ === withSelf); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 75, characters 7-14", Object.getPrototypeOf(withSelf) === Object.prototype); + }); + Mocha.test("an inline record", () => { + let match = id(inlineConstant); + if (typeof match !== "object" || typeof inlineComputed !== "object") { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 82, characters 14-21", false); + } else { + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 81, characters 9-16", [ + match.__proto__, + inlineComputed.__proto__ + ], [ + 4, + 5 + ]); + } + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 84, characters 7-14", Object.hasOwn(inlineConstant, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 85, characters 7-14", Object.hasOwn(inlineComputed, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 86, characters 7-14", Object.getPrototypeOf(inlineConstant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 87, characters 7-14", Object.getPrototypeOf(inlineComputed) === Object.prototype); + }); + Mocha.test("a tag named __proto__", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 91, characters 7-14", Object.hasOwn(taggedFirst, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 92, characters 7-14", Object.getPrototypeOf(taggedFirst) === Object.prototype); + let match = id(taggedFirst); + if (match.__proto__ === "First") { + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 94, characters 32-39", match.nextTagged === taggedFirst); + } else { + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 95, characters 22-29", false); + } + }); + Mocha.test("an optional field set to None", () => { + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 100, characters 7-14", Object.keys(optionalNone), ["nextOptional"]); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 101, characters 7-14", Object.getPrototypeOf(optionalNone) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 102, characters 7-14", id(optionalNone).nextOptional === optionalNone); + }); +}); + +export { + id, + constant, + computed, + withObject, + withSelf, + inlineConstant, + inlineComputed, + taggedFirst, + optionalNone, + isOrdinary, +} +/* computed Not a pure module */ diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.res b/tests/compiler_tests/src/recursive_record_proto_field_test.res new file mode 100644 index 0000000000..6676d88929 --- /dev/null +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.res @@ -0,0 +1,104 @@ +open Mocha +open Test_utils + +/*** +A field renamed to `__proto__` of a recursive value is an own property, as it +is of a record literal. The value is made as an empty object whose fields are +set afterwards, where an assignment, or `Object.assign`, runs the +`__proto__` setter the object inherits from `Object.prototype`. +*/ + +@inline(never) let id = x => x + +@val external hasOwn: ('a, string) => bool = "Object.hasOwn" +@val external getPrototypeOf: 'a => 'b = "Object.getPrototypeOf" +@val external objectPrototype: 'a = "Object.prototype" + +type rec intProto = {next: intProto, @as("__proto__") p: int} + +// Its fields are variables or constants: they are assigned one by one +let rec constant = {next: constant, p: 1} + +// The fields are computed: the record is copied to the empty object +let rec computed = {next: computed, p: id(2)} + +type rec objectProto = {self: objectProto, @as("__proto__") q: {"x": int}} + +let rec withObject = {self: withObject, q: {"x": 3}} + +type rec selfProto = {cyclic: selfProto, @as("__proto__") r: selfProto} + +let rec withSelf = {cyclic: withSelf, r: withSelf} + +type rec inline = Inline({nextInline: inline, @as("__proto__") pInline: int}) | Empty + +let rec inlineConstant = Inline({nextInline: inlineConstant, pInline: 4}) + +let rec inlineComputed = Inline({nextInline: inlineComputed, pInline: id(5)}) + +@tag("__proto__") +type rec tagged = First({nextTagged: tagged}) | Second({value: int}) + +let rec taggedFirst = First({nextTagged: taggedFirst}) + +type rec optionalProto = {nextOptional: optionalProto, @as("__proto__") pOptional?: int} + +// An optional field set to None is left out, as it is of a record literal +let rec optionalNone = {nextOptional: optionalNone, pOptional: ?None} + +let isOrdinary = x => getPrototypeOf(x) === objectPrototype + +describe(__MODULE__, () => { + test("a field set from a constant", () => { + ok(__LOC__, hasOwn(constant, "__proto__")) + eq(__LOC__, id(constant).p, 1) + ok(__LOC__, isOrdinary(constant)) + ok(__LOC__, id(constant).next === constant) + }) + + test("a computed field", () => { + ok(__LOC__, hasOwn(computed, "__proto__")) + eq(__LOC__, computed.p, 2) + ok(__LOC__, isOrdinary(computed)) + ok(__LOC__, id(computed).next === computed) + }) + + test("an object field does not become the prototype", () => { + ok(__LOC__, hasOwn(withObject, "__proto__")) + eq(__LOC__, withObject.q["x"], 3) + ok(__LOC__, isOrdinary(withObject)) + }) + + test("a field holding the record itself", () => { + ok(__LOC__, hasOwn(withSelf, "__proto__")) + ok(__LOC__, id(withSelf).r === withSelf) + ok(__LOC__, isOrdinary(withSelf)) + }) + + test("an inline record", () => { + switch (id(inlineConstant), inlineComputed) { + | (Inline({pInline: constant}), Inline({pInline: computed})) => + eq(__LOC__, (constant, computed), (4, 5)) + | _ => ok(__LOC__, false) + } + ok(__LOC__, hasOwn(inlineConstant, "__proto__")) + ok(__LOC__, hasOwn(inlineComputed, "__proto__")) + ok(__LOC__, isOrdinary(inlineConstant)) + ok(__LOC__, isOrdinary(inlineComputed)) + }) + + test("a tag named __proto__", () => { + ok(__LOC__, hasOwn(taggedFirst, "__proto__")) + ok(__LOC__, isOrdinary(taggedFirst)) + switch id(taggedFirst) { + | First({nextTagged}) => ok(__LOC__, nextTagged === taggedFirst) + | Second(_) => ok(__LOC__, false) + } + }) + + test("an optional field set to None", () => { + eq(__LOC__, Object.keysToArray(Obj.magic(optionalNone)), ["nextOptional"]) + ok(__LOC__, isOrdinary(optionalNone)) + ok(__LOC__, id(optionalNone).nextOptional === optionalNone) + }) +}) From ed43f7aa348121edda165b2540144557fd9fc06e Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 28 Nov 2026 23:13:17 -0500 Subject: [PATCH 2/3] Test __proto__ keys on recursive constructors, extensions and untagged records Add cases to `recursive_record_proto_field_test.res` covering the remaining reachable branches of `block_has_proto_key`: a tuple-payload constructor with `@tag("__proto__")`, an extensible variant inline record with `@as("__proto__")`, and `@unboxed` untagged inline records with and without `@as("__proto__")`. Signed-off-by: Rob --- .../src/recursive_record_proto_field_test.mjs | 133 ++++++++++++++---- .../src/recursive_record_proto_field_test.res | 65 +++++++++ 2 files changed, 171 insertions(+), 27 deletions(-) diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.mjs b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs index b72f6d341b..bba16cfad0 100644 --- a/tests/compiler_tests/src/recursive_record_proto_field_test.mjs +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs @@ -2,6 +2,8 @@ import * as Mocha from "mocha"; import * as Test_utils from "./test_utils.mjs"; +import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.mjs"; +import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.mjs"; function id(x) { return x; @@ -75,39 +77,71 @@ let optionalNone = {}; optionalNone.nextOptional = optionalNone; +let taggedPair = {}; + +Object.defineProperties(taggedPair, Object.getOwnPropertyDescriptors({ + ["__proto__"]: "Pair", + _0: taggedPair, + _1: id(6) +})); + +let Ext = /* @__PURE__ */Primitive_exceptions.create("Recursive_record_proto_field_test.Ext"); + +let extValue = {}; + +Object.defineProperties(extValue, Object.getOwnPropertyDescriptors({ + RE_EXN_ID: Ext, + nextExt: extValue, + ["__proto__"]: id(7) +})); + +let untaggedWithProto = {}; + +Object.defineProperties(untaggedWithProto, Object.getOwnPropertyDescriptors({ + nextU: untaggedWithProto, + ["__proto__"]: id(8) +})); + +let untaggedWithoutProto = {}; + +Primitive_object.updateDummy(untaggedWithoutProto, { + nextP: untaggedWithoutProto, + value: id(9) +}); + function isOrdinary(x) { return Object.getPrototypeOf(x) === Object.prototype; } Mocha.describe("Recursive_record_proto_field_test", () => { Mocha.test("a field set from a constant", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 53, characters 7-14", Object.hasOwn(constant, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 54, characters 7-14", id(constant).__proto__, 1); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 55, characters 7-14", Object.getPrototypeOf(constant) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 56, characters 7-14", id(constant).next === constant); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 77, characters 7-14", Object.hasOwn(constant, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 78, characters 7-14", id(constant).__proto__, 1); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 79, characters 7-14", Object.getPrototypeOf(constant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 80, characters 7-14", id(constant).next === constant); }); Mocha.test("a computed field", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 60, characters 7-14", Object.hasOwn(computed, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 61, characters 7-14", computed.__proto__, 2); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 62, characters 7-14", Object.getPrototypeOf(computed) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 63, characters 7-14", id(computed).next === computed); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 84, characters 7-14", Object.hasOwn(computed, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 85, characters 7-14", computed.__proto__, 2); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 86, characters 7-14", Object.getPrototypeOf(computed) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 87, characters 7-14", id(computed).next === computed); }); Mocha.test("an object field does not become the prototype", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 67, characters 7-14", Object.hasOwn(withObject, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 68, characters 7-14", withObject.__proto__.x, 3); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 69, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 91, characters 7-14", Object.hasOwn(withObject, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 92, characters 7-14", withObject.__proto__.x, 3); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 93, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); }); Mocha.test("a field holding the record itself", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 73, characters 7-14", Object.hasOwn(withSelf, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 74, characters 7-14", id(withSelf).__proto__ === withSelf); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 75, characters 7-14", Object.getPrototypeOf(withSelf) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 97, characters 7-14", Object.hasOwn(withSelf, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 98, characters 7-14", id(withSelf).__proto__ === withSelf); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 99, characters 7-14", Object.getPrototypeOf(withSelf) === Object.prototype); }); Mocha.test("an inline record", () => { let match = id(inlineConstant); if (typeof match !== "object" || typeof inlineComputed !== "object") { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 82, characters 14-21", false); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 106, characters 14-21", false); } else { - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 81, characters 9-16", [ + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 105, characters 9-16", [ match.__proto__, inlineComputed.__proto__ ], [ @@ -115,25 +149,65 @@ Mocha.describe("Recursive_record_proto_field_test", () => { 5 ]); } - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 84, characters 7-14", Object.hasOwn(inlineConstant, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 85, characters 7-14", Object.hasOwn(inlineComputed, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 86, characters 7-14", Object.getPrototypeOf(inlineConstant) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 87, characters 7-14", Object.getPrototypeOf(inlineComputed) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 108, characters 7-14", Object.hasOwn(inlineConstant, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 109, characters 7-14", Object.hasOwn(inlineComputed, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 110, characters 7-14", Object.getPrototypeOf(inlineConstant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 111, characters 7-14", Object.getPrototypeOf(inlineComputed) === Object.prototype); }); Mocha.test("a tag named __proto__", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 91, characters 7-14", Object.hasOwn(taggedFirst, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 92, characters 7-14", Object.getPrototypeOf(taggedFirst) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 115, characters 7-14", Object.hasOwn(taggedFirst, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 116, characters 7-14", Object.getPrototypeOf(taggedFirst) === Object.prototype); let match = id(taggedFirst); if (match.__proto__ === "First") { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 94, characters 32-39", match.nextTagged === taggedFirst); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 118, characters 32-39", match.nextTagged === taggedFirst); } else { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 95, characters 22-29", false); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 119, characters 22-29", false); } }); Mocha.test("an optional field set to None", () => { - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 100, characters 7-14", Object.keys(optionalNone), ["nextOptional"]); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 101, characters 7-14", Object.getPrototypeOf(optionalNone) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 102, characters 7-14", id(optionalNone).nextOptional === optionalNone); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 124, characters 7-14", Object.keys(optionalNone), ["nextOptional"]); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 125, characters 7-14", Object.getPrototypeOf(optionalNone) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 126, characters 7-14", id(optionalNone).nextOptional === optionalNone); + }); + Mocha.test("a constructor with a tag named __proto__", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 130, characters 7-14", Object.hasOwn(taggedPair, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 131, characters 7-14", Object.getPrototypeOf(taggedPair) === Object.prototype); + let match = id(taggedPair); + if (typeof match !== "object") { + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 136, characters 17-24", false); + } + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 134, characters 9-16", match._0 === taggedPair); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 135, characters 9-16", match._1, 6); + }); + Mocha.test("an extension inline record", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 141, characters 7-14", Object.hasOwn(extValue, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 142, characters 7-14", Object.getPrototypeOf(extValue) === Object.prototype); + let match = id(extValue); + if (match.RE_EXN_ID === Ext) { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 145, characters 9-16", match.nextExt === extValue); + return Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 146, characters 9-16", match.__proto__, 7); + } else { + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 147, characters 14-21", false); + } + }); + Mocha.test("untagged inline records", () => { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 152, characters 7-14", Object.hasOwn(untaggedWithProto, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 153, characters 7-14", Object.getPrototypeOf(untaggedWithProto) === Object.prototype); + let match = id(untaggedWithProto); + if (typeof match === "string") { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 158, characters 20-27", false); + } else { + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 156, characters 9-16", match.nextU === untaggedWithProto); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 157, characters 9-16", match.__proto__, 8); + } + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 160, characters 7-14", !Object.hasOwn(untaggedWithoutProto, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 161, characters 7-14", Object.getPrototypeOf(untaggedWithoutProto) === Object.prototype); + let match$1 = id(untaggedWithoutProto); + if (typeof match$1 === "string") { + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 166, characters 20-27", false); + } + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 164, characters 9-16", match$1.nextP === untaggedWithoutProto); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 165, characters 9-16", match$1.value, 9); }); }); @@ -147,6 +221,11 @@ export { inlineComputed, taggedFirst, optionalNone, + taggedPair, + Ext, + extValue, + untaggedWithProto, + untaggedWithoutProto, isOrdinary, } /* computed Not a pure module */ diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.res b/tests/compiler_tests/src/recursive_record_proto_field_test.res index 6676d88929..dfc38ed459 100644 --- a/tests/compiler_tests/src/recursive_record_proto_field_test.res +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.res @@ -46,6 +46,30 @@ type rec optionalProto = {nextOptional: optionalProto, @as("__proto__") pOptiona // An optional field set to None is left out, as it is of a record literal let rec optionalNone = {nextOptional: optionalNone, pOptional: ?None} +@tag("__proto__") +type rec taggedCtor = Pair(taggedCtor, int) | Stop + +let rec taggedPair = Pair(taggedPair, id(6)) + +type ext = .. +type ext += Ext({nextExt: ext, @as("__proto__") pExt: int}) + +let rec extValue = Ext({nextExt: extValue, pExt: id(7)}) + +@unboxed +type rec untaggedProto = + | UProto({nextU: untaggedProto, @as("__proto__") pU: int}) + | UStr(string) + +let rec untaggedWithProto = UProto({nextU: untaggedWithProto, pU: id(8)}) + +@unboxed +type rec untaggedPlain = + | UPlain({nextP: untaggedPlain, value: int}) + | PStr(string) + +let rec untaggedWithoutProto = UPlain({nextP: untaggedWithoutProto, value: id(9)}) + let isOrdinary = x => getPrototypeOf(x) === objectPrototype describe(__MODULE__, () => { @@ -101,4 +125,45 @@ describe(__MODULE__, () => { ok(__LOC__, isOrdinary(optionalNone)) ok(__LOC__, id(optionalNone).nextOptional === optionalNone) }) + + test("a constructor with a tag named __proto__", () => { + ok(__LOC__, hasOwn(taggedPair, "__proto__")) + ok(__LOC__, isOrdinary(taggedPair)) + switch id(taggedPair) { + | Pair(next, v) => + ok(__LOC__, next === taggedPair) + eq(__LOC__, v, 6) + | Stop => ok(__LOC__, false) + } + }) + + test("an extension inline record", () => { + ok(__LOC__, hasOwn(extValue, "__proto__")) + ok(__LOC__, isOrdinary(extValue)) + switch id(extValue) { + | Ext({nextExt, pExt}) => + ok(__LOC__, nextExt === extValue) + eq(__LOC__, pExt, 7) + | _ => ok(__LOC__, false) + } + }) + + test("untagged inline records", () => { + ok(__LOC__, hasOwn(untaggedWithProto, "__proto__")) + ok(__LOC__, isOrdinary(untaggedWithProto)) + switch id(untaggedWithProto) { + | UProto({nextU, pU}) => + ok(__LOC__, nextU === untaggedWithProto) + eq(__LOC__, pU, 8) + | UStr(_) => ok(__LOC__, false) + } + ok(__LOC__, !hasOwn(untaggedWithoutProto, "__proto__")) + ok(__LOC__, isOrdinary(untaggedWithoutProto)) + switch id(untaggedWithoutProto) { + | UPlain({nextP, value}) => + ok(__LOC__, nextP === untaggedWithoutProto) + eq(__LOC__, value, 9) + | PStr(_) => ok(__LOC__, false) + } + }) }) From d5f4943ff0b1dd5f28d8be5f9b221d1e7e6e1db6 Mon Sep 17 00:00:00 2001 From: Rob Date: Sun, 29 Nov 2026 12:54:39 -0500 Subject: [PATCH 3/3] Preserve undefined slots in recursive array-backed records Why: Codex review of 58df2f349 found that omitting optional fields set to `None` on the recursive-record field-assignment path also applied to array-backed records (`Blk_record {array = true}`), leaving trailing slots missing from the array's length and leading slots as sparse holes, unlike `Js_dump` which keeps every element in array literals. Only object-backed records (`array = false`) now omit optional `None` fields. JS modified (recursive array-backed record with optional None fields): Example: type rec arrayOptional = {@as("0") nextArr: arrayOptional, @as("1") valueArr?: int} let rec arrayOptionalNone = {nextArr: arrayOptionalNone, valueArr: ?None} Before (js): let arrayOptionalNone = []; arrayOptionalNone[0] = arrayOptionalNone; After (js): let arrayOptionalNone = []; arrayOptionalNone[0] = arrayOptionalNone; arrayOptionalNone[1] = undefined; Review: Which checked-in outputs change? Only `recursive_record_proto_field_test.mjs` for the new array-record cases. Signed-off-by: Rob --- compiler/backend/lam_compile.ml | 9 +- .../src/recursive_record_proto_field_test.mjs | 130 +++++++++++------- .../src/recursive_record_proto_field_test.res | 29 ++++ 3 files changed, 117 insertions(+), 51 deletions(-) diff --git a/compiler/backend/lam_compile.ml b/compiler/backend/lam_compile.ml index 2a7aecb7fe..65c635deda 100644 --- a/compiler/backend/lam_compile.ml +++ b/compiler/backend/lam_compile.ml @@ -772,9 +772,9 @@ let compile output_prefix = (fun i (x : Lambda.t) -> let (fld_info : Lambda.set_field_dbg_info), optional = match tag_info with - | Blk_record {fields = xs} -> + | Blk_record {fields = xs; array} -> let name, optional = xs.(i) in - (Fld_record_set name, optional) + (Fld_record_set name, optional && not array) | Blk_record_inlined xs -> let name, optional = xs.fields.(i) in (Fld_record_inline_set name, optional) @@ -796,8 +796,9 @@ let compile output_prefix = | _ -> assert false in match value.expression_desc with - (* An optional field set to [None] is left out, as - [Js_dump] leaves it out of a record literal *) + (* 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 -> [] | _ -> [S.exp (init_field fld_info (E.var id) i value)]) ls)), diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.mjs b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs index bba16cfad0..eb9132181d 100644 --- a/tests/compiler_tests/src/recursive_record_proto_field_test.mjs +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs @@ -109,39 +109,58 @@ Primitive_object.updateDummy(untaggedWithoutProto, { value: id(9) }); +let arrayOptionalNone = []; + +arrayOptionalNone[0] = arrayOptionalNone; + +arrayOptionalNone[1] = undefined; + +let arrayOptionalComputed = []; + +Primitive_object.updateDummy(arrayOptionalComputed, [ + arrayOptionalComputed, + id(10) +]); + +let arrayLeadingNone = []; + +arrayLeadingNone[0] = undefined; + +arrayLeadingNone[1] = arrayLeadingNone; + function isOrdinary(x) { return Object.getPrototypeOf(x) === Object.prototype; } Mocha.describe("Recursive_record_proto_field_test", () => { Mocha.test("a field set from a constant", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 77, characters 7-14", Object.hasOwn(constant, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 78, characters 7-14", id(constant).__proto__, 1); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 79, characters 7-14", Object.getPrototypeOf(constant) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 80, characters 7-14", id(constant).next === constant); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 87, characters 7-14", Object.hasOwn(constant, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 88, characters 7-14", id(constant).__proto__, 1); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 89, characters 7-14", Object.getPrototypeOf(constant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 90, characters 7-14", id(constant).next === constant); }); Mocha.test("a computed field", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 84, characters 7-14", Object.hasOwn(computed, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 85, characters 7-14", computed.__proto__, 2); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 86, characters 7-14", Object.getPrototypeOf(computed) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 87, characters 7-14", id(computed).next === computed); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 94, characters 7-14", Object.hasOwn(computed, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 95, characters 7-14", computed.__proto__, 2); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 96, characters 7-14", Object.getPrototypeOf(computed) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 97, characters 7-14", id(computed).next === computed); }); Mocha.test("an object field does not become the prototype", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 91, characters 7-14", Object.hasOwn(withObject, "__proto__")); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 92, characters 7-14", withObject.__proto__.x, 3); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 93, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 101, characters 7-14", Object.hasOwn(withObject, "__proto__")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 102, characters 7-14", withObject.__proto__.x, 3); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 103, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); }); Mocha.test("a field holding the record itself", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 97, characters 7-14", Object.hasOwn(withSelf, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 98, characters 7-14", id(withSelf).__proto__ === withSelf); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 99, characters 7-14", Object.getPrototypeOf(withSelf) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 107, characters 7-14", Object.hasOwn(withSelf, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 108, characters 7-14", id(withSelf).__proto__ === withSelf); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 109, characters 7-14", Object.getPrototypeOf(withSelf) === Object.prototype); }); Mocha.test("an inline record", () => { let match = id(inlineConstant); if (typeof match !== "object" || typeof inlineComputed !== "object") { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 106, characters 14-21", false); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 116, characters 14-21", false); } else { - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 105, characters 9-16", [ + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 115, characters 9-16", [ match.__proto__, inlineComputed.__proto__ ], [ @@ -149,65 +168,79 @@ Mocha.describe("Recursive_record_proto_field_test", () => { 5 ]); } - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 108, characters 7-14", Object.hasOwn(inlineConstant, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 109, characters 7-14", Object.hasOwn(inlineComputed, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 110, characters 7-14", Object.getPrototypeOf(inlineConstant) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 111, characters 7-14", Object.getPrototypeOf(inlineComputed) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 118, characters 7-14", Object.hasOwn(inlineConstant, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 119, characters 7-14", Object.hasOwn(inlineComputed, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 120, characters 7-14", Object.getPrototypeOf(inlineConstant) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 121, characters 7-14", Object.getPrototypeOf(inlineComputed) === Object.prototype); }); Mocha.test("a tag named __proto__", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 115, characters 7-14", Object.hasOwn(taggedFirst, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 116, characters 7-14", Object.getPrototypeOf(taggedFirst) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 125, characters 7-14", Object.hasOwn(taggedFirst, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 126, characters 7-14", Object.getPrototypeOf(taggedFirst) === Object.prototype); let match = id(taggedFirst); if (match.__proto__ === "First") { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 118, characters 32-39", match.nextTagged === taggedFirst); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 128, characters 32-39", match.nextTagged === taggedFirst); } else { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 119, characters 22-29", false); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 129, characters 22-29", false); } }); Mocha.test("an optional field set to None", () => { - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 124, characters 7-14", Object.keys(optionalNone), ["nextOptional"]); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 125, characters 7-14", Object.getPrototypeOf(optionalNone) === Object.prototype); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 126, characters 7-14", id(optionalNone).nextOptional === optionalNone); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 134, characters 7-14", Object.keys(optionalNone), ["nextOptional"]); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 135, characters 7-14", Object.getPrototypeOf(optionalNone) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 136, characters 7-14", id(optionalNone).nextOptional === optionalNone); }); Mocha.test("a constructor with a tag named __proto__", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 130, characters 7-14", Object.hasOwn(taggedPair, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 131, characters 7-14", Object.getPrototypeOf(taggedPair) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 140, characters 7-14", Object.hasOwn(taggedPair, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 141, characters 7-14", Object.getPrototypeOf(taggedPair) === Object.prototype); let match = id(taggedPair); if (typeof match !== "object") { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 136, characters 17-24", false); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 146, characters 17-24", false); } - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 134, characters 9-16", match._0 === taggedPair); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 135, characters 9-16", match._1, 6); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 144, characters 9-16", match._0 === taggedPair); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 145, characters 9-16", match._1, 6); }); Mocha.test("an extension inline record", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 141, characters 7-14", Object.hasOwn(extValue, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 142, characters 7-14", Object.getPrototypeOf(extValue) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 151, characters 7-14", Object.hasOwn(extValue, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 152, characters 7-14", Object.getPrototypeOf(extValue) === Object.prototype); let match = id(extValue); if (match.RE_EXN_ID === Ext) { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 145, characters 9-16", match.nextExt === extValue); - return Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 146, characters 9-16", match.__proto__, 7); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 155, characters 9-16", match.nextExt === extValue); + return Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 156, characters 9-16", match.__proto__, 7); } else { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 147, characters 14-21", false); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 157, characters 14-21", false); } }); Mocha.test("untagged inline records", () => { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 152, characters 7-14", Object.hasOwn(untaggedWithProto, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 153, characters 7-14", Object.getPrototypeOf(untaggedWithProto) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 162, characters 7-14", Object.hasOwn(untaggedWithProto, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 163, characters 7-14", Object.getPrototypeOf(untaggedWithProto) === Object.prototype); let match = id(untaggedWithProto); if (typeof match === "string") { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 158, characters 20-27", false); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 168, characters 20-27", false); } else { - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 156, characters 9-16", match.nextU === untaggedWithProto); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 157, characters 9-16", match.__proto__, 8); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 166, characters 9-16", match.nextU === untaggedWithProto); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 167, characters 9-16", match.__proto__, 8); } - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 160, characters 7-14", !Object.hasOwn(untaggedWithoutProto, "__proto__")); - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 161, characters 7-14", Object.getPrototypeOf(untaggedWithoutProto) === Object.prototype); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 170, characters 7-14", !Object.hasOwn(untaggedWithoutProto, "__proto__")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 171, characters 7-14", Object.getPrototypeOf(untaggedWithoutProto) === Object.prototype); let match$1 = id(untaggedWithoutProto); if (typeof match$1 === "string") { - return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 166, characters 20-27", false); + return Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 176, characters 20-27", false); } - Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 164, characters 9-16", match$1.nextP === untaggedWithoutProto); - Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 165, characters 9-16", match$1.value, 9); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 174, characters 9-16", match$1.nextP === untaggedWithoutProto); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 175, characters 9-16", match$1.value, 9); + }); + Mocha.test("array-backed recursive records keep optional None slots", () => { + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 182, characters 7-14", arrayOptionalNone.length, 2); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 183, characters 7-14", Object.hasOwn(arrayOptionalNone, "0")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 184, characters 7-14", Object.hasOwn(arrayOptionalNone, "1")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 185, characters 7-14", id(arrayOptionalNone)[0] === arrayOptionalNone); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 186, characters 7-14", id(arrayOptionalNone)[1], undefined); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 189, characters 7-14", arrayLeadingNone.length, 2); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 190, characters 7-14", Object.hasOwn(arrayLeadingNone, "0")); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 191, characters 7-14", Object.hasOwn(arrayLeadingNone, "1")); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 192, characters 7-14", id(arrayLeadingNone)[0], undefined); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 193, characters 7-14", id(arrayLeadingNone)[1] === arrayLeadingNone); + Test_utils.ok("File \"recursive_record_proto_field_test.res\", line 195, characters 7-14", id(arrayOptionalComputed)[0] === arrayOptionalComputed); + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 196, characters 7-14", id(arrayOptionalComputed)[1], 10); }); }); @@ -226,6 +259,9 @@ export { extValue, untaggedWithProto, untaggedWithoutProto, + arrayOptionalNone, + arrayOptionalComputed, + arrayLeadingNone, isOrdinary, } /* computed Not a pure module */ diff --git a/tests/compiler_tests/src/recursive_record_proto_field_test.res b/tests/compiler_tests/src/recursive_record_proto_field_test.res index dfc38ed459..221a81f737 100644 --- a/tests/compiler_tests/src/recursive_record_proto_field_test.res +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.res @@ -70,6 +70,16 @@ type rec untaggedPlain = let rec untaggedWithoutProto = UPlain({nextP: untaggedWithoutProto, value: id(9)}) +type rec arrayOptional = {@as("0") nextArr: arrayOptional, @as("1") valueArr?: int} + +let rec arrayOptionalNone = {nextArr: arrayOptionalNone, valueArr: ?None} + +let rec arrayOptionalComputed = {nextArr: arrayOptionalComputed, valueArr: ?Some(id(10))} + +type rec arrayLeadingOptional = {@as("0") firstArr?: int, @as("1") restArr: arrayLeadingOptional} + +let rec arrayLeadingNone = {firstArr: ?None, restArr: arrayLeadingNone} + let isOrdinary = x => getPrototypeOf(x) === objectPrototype describe(__MODULE__, () => { @@ -166,4 +176,23 @@ describe(__MODULE__, () => { | PStr(_) => ok(__LOC__, false) } }) + + test("array-backed recursive records keep optional None slots", () => { + let arr: array = Obj.magic(arrayOptionalNone) + eq(__LOC__, Array.length(arr), 2) + ok(__LOC__, hasOwn(arrayOptionalNone, "0")) + ok(__LOC__, hasOwn(arrayOptionalNone, "1")) + ok(__LOC__, id(arrayOptionalNone).nextArr === arrayOptionalNone) + eq(__LOC__, id(arrayOptionalNone).valueArr, None) + + let leading: array = Obj.magic(arrayLeadingNone) + eq(__LOC__, Array.length(leading), 2) + ok(__LOC__, hasOwn(arrayLeadingNone, "0")) + ok(__LOC__, hasOwn(arrayLeadingNone, "1")) + eq(__LOC__, id(arrayLeadingNone).firstArr, None) + ok(__LOC__, id(arrayLeadingNone).restArr === arrayLeadingNone) + + ok(__LOC__, id(arrayOptionalComputed).nextArr === arrayOptionalComputed) + eq(__LOC__, id(arrayOptionalComputed).valueArr, Some(10)) + }) })