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..65c635deda 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,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 -> [] + | _ -> [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 +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) @@ -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 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..eb9132181d --- /dev/null +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.mjs @@ -0,0 +1,267 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +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; +} + +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; + +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) +}); + +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 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 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 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 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 116, characters 14-21", false); + } else { + Test_utils.eq("File \"recursive_record_proto_field_test.res\", line 115, characters 9-16", [ + match.__proto__, + inlineComputed.__proto__ + ], [ + 4, + 5 + ]); + } + 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 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 128, characters 32-39", match.nextTagged === taggedFirst); + } else { + 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 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 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 146, characters 17-24", false); + } + 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 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 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 157, characters 14-21", false); + } + }); + Mocha.test("untagged inline records", () => { + 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 168, characters 20-27", false); + } else { + 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 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 176, characters 20-27", false); + } + 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); + }); +}); + +export { + id, + constant, + computed, + withObject, + withSelf, + inlineConstant, + inlineComputed, + taggedFirst, + optionalNone, + taggedPair, + Ext, + 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 new file mode 100644 index 0000000000..221a81f737 --- /dev/null +++ b/tests/compiler_tests/src/recursive_record_proto_field_test.res @@ -0,0 +1,198 @@ +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} + +@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)}) + +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__, () => { + 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) + }) + + 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) + } + }) + + 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)) + }) +})