Why
A record spread {...base, f: v} of a record with an optional field (or with 20 fields or more) is a JS object spread. It was emitted as one object literal even when an override compiles to statements (a try, or a switch that is not one expression). Those statements ran before base was evaluated and copied, so a mutation of the base inside the override showed up in the copy. The copy is now taken first and the overrides are assigned to it. The literal stays when the overrides are plain expressions, or when their statements only define pure values and nothing evaluated before them runs code. The Lambda for the overrides also set the last declared field first, and the literal was built from the reversed list. That order was visible when the copy was bound and then used, and it put fields assigned to the copy afterwards before the overrides: the last assignment of a field lost, and a value reading the copy named a variable that was never defined. The fields are now set in declaration order in the Lambda, followed by the later assignments in source order. A spread whose later assignments read the copy, or set a field twice, is compiled as a copy and assignments. The copy is left out when the result is discarded or an override always throws.
modifiedJS record spread with an override that needs statements
@inline(never) let id = x => x@inline(never) let trace = (_n: string, v) => vtype r = {a: int, b: int, c?: int}let f = (base: r) => { ...trace("base", base), a: try { trace("a", id(5)) } catch { | _ => 0 }, b: trace("b", 7),}Before
After
function f(base) {function f(base) { let newrecord = {...trace("base", base)}; let tmp; let tmp; try { try { tmp = trace("a", id(5)); tmp = trace("a", id(5)); } catch (exn) { } catch (exn) { tmp = 0; tmp = 0; } } return { ...trace("base", base), a: tmp, b: trace("b", 7) }; newrecord.a = tmp; newrecord.b = trace("b", 7); return newrecord; }}
modifiedJS field assigned after a record spread
type r = {mutable a: int, mutable b: int, c?: int}let readsCopy = (base: r) => { let x = {...base, a: 1} x.b = x.a + 1 x}let lastWins = (base: r) => { let x = {...base, a: 1} x.a = 2 x}Before
After
function readsCopy(base) {function readsCopy(base) { return { ...base, b: newrecord.a + 1 | 0, a: 1 }; let newrecord = {...base}; newrecord.a = 1; newrecord.b = newrecord.a + 1 | 0; return newrecord; }} function lastWins(base) {function lastWins(base) { return { ...base, a: 2, a: 1 }; let newrecord = {...base}; newrecord.a = 1; newrecord.a = 2; return newrecord; }}
modifiedJS record spread bound and then used
@inline(never) let id = x => x@inline(never) let trace = (_n: string, v) => vtype r = {mutable a: int, mutable b: int, c?: int}let k = (base: r) => { let x = {...base, a: trace("a", 1), b: trace("b", 2)} ignore(id(x.a)) x}Before
After
function k(base) {function k(base) { let newrecord = {...base}; let newrecord = {...base}; newrecord.b = trace("b", 2); newrecord.a = trace("a", 1); newrecord.a = trace("a", 1); newrecord.b = trace("b", 2); id(newrecord.a); id(newrecord.a); return newrecord; return newrecord;}}
Review
- Is each override compiled once? Yes, every form is built from the same compiled overrides. Whether a value reads the copy is decided on the Lambda, before anything is compiled.
- Which checked-in outputs change? Only the new test's.
Why
The copy and assignments of a record spread define a __proto__ field with Object.defineProperty, but only when the copy is returned right after its fields are set and no field is set twice. A field assigned twice (the override and a later x.p = v), a value that reads the copy, or code that uses the copy before it is returned left the spread to the generic code, which assigns newrecord.__proto__ = v: when the base has no own __proto__, the inherited setter drops an int, and the copy has no own property. These cases are now compiled as a copy and assignments too, with the code after the assignments compiled next.
modifiedJS record spread whose __proto__ field is set again
type r = {a: int, @as("__proto__") mutable p?: int}let f = (base: r) => { let x = {...base, p: 5} x.p = Some(5) x}Before
After
function f(base) {function f(base) { let newrecord = {...base}; let newrecord = {...base}; newrecord.__proto__ = 5; newrecord.__proto__ = 5; Object.defineProperty(newrecord, "__proto__", { value: 5, writable: true, enumerable: true, configurable: true }); Object.defineProperty(newrecord, "__proto__", { value: 5, writable: true, enumerable: true, configurable: true }); return newrecord; return newrecord;}}
modifiedJS record spread used before it is returned
type r = {a: int, @as("__proto__") mutable p?: int}let g = (base: r) => { let x = {...base, p: 5} Console.log(x.a) x}Before
After
function g(base) {function g(base) { let newrecord = {...base}; let newrecord = {...base}; newrecord.__proto__ = 5; Object.defineProperty(newrecord, "__proto__", { value: 5, writable: true, enumerable: true, configurable: true }); console.log(newrecord.a); console.log(newrecord.a); return newrecord; return newrecord;}}
Review
- Does the output change for other field names? No: they are still assigned with Js_of_lam_block.set_field, as the generic code does. Only record_spread_order_test's output changes.
- Is the copy left out when a value reads it? No: the copy is dropped for a discarded result or a throwing override only when no value reads it and it is returned right after its fields are set.