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.
modifiedJS recursive record with a __proto__ field
@inline(never) let id = x => xtype rec t = {next: t, @as("__proto__") p: int}let rec n = {next: n, p: 1}let rec m = {next: m, p: id(2)}
js
modifiedJS recursive variant with a __proto__ tag
@tag("__proto__")type rec tagged = First({nextTagged: tagged}) | Second({value: int})let rec taggedFirst = First({nextTagged: taggedFirst})
js
modifiedJS recursive record with optional fields set to None
type rec r = {next: r, @as("__proto__") p?: int, q?: int}let rec x = {next: x, p: ?None, q: ?None}
js
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.keysof the value matches the literal's. - 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.
- Why not change updateDummy in the runtime? Recursive modules use it too, and every other recursive value keeps the call it has.
- 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.