Skip to content

Commit 24d581a

Browse files
committed
fix(composition): refuse changing a layer's prototype, and keep an Array subclass instance as itself
1 parent 9c74828 commit 24d581a

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎src/lib/helpers/composition.mjs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @Email: <[email protected]>
88
* -----
99
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
10-
* @Last modified time: 2026-10-12T14:17:16-07:00 (1791839836)
10+
* @Last modified time: 2026-10-12T15:32:42-07:00 (1791844362)
1111
* -----
1212
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
1313
*
@@ -31,7 +31,7 @@
3131
* target or be refused by it;
3232
* - an array is copied with everything it holds (its elements and holes, any other own member and
3333
* each member's descriptor, which `slice()` would drop), and gets a layer when named exports are
34-
* added (`addsMembers`);
34+
* added (`addsMembers`); an instance of an Array subclass is treated as a class instance;
3535
* - a class instance or built-in (a `Map`, a `Date`) keeps private fields and internal slots no copy
3636
* reproduces; nothing adopts from it, so it is used as-is unless named exports are added, and then
3737
* gets a layer;
@@ -346,6 +346,9 @@ function layerOver(underlying, members = [], hidden = []) {
346346
...new Set([...Reflect.ownKeys(underlying).filter((key) => !removed.has(key) && !added.has(key)), ...added.keys(), ...shellFixedKeys])
347347
],
348348
getPrototypeOf: () => Reflect.getPrototypeOf(underlying),
349+
// The layer reports the value's prototype, and changing it would change the module's value, so a
350+
// change is refused; setting the prototype it already has succeeds, as on an object (#585 review).
351+
setPrototypeOf: (_shell, prototype) => prototype === Reflect.getPrototypeOf(underlying),
349352
// The layer reports keys its shell does not hold (the value's own and the added ones), which a
350353
// non-extensible shell would forbid, so it stays extensible: sealing it is refused.
351354
preventExtensions: () => false,
@@ -362,8 +365,8 @@ function layerOver(underlying, members = [], hidden = []) {
362365
* Copy an array with everything it holds: its elements and holes, any other own member (`items.label`)
363366
* and each member's descriptor (a read-only element, a getter), so the copy answers as the array does
364367
* (#585 review). `slice()` keeps only the elements, as plain data.
365-
* @param {Array} value - The array.
366-
* @returns {Array} The copy, with the array's prototype.
368+
* @param {Array} value - A plain array (an Array subclass instance is never copied).
369+
* @returns {Array} The copy.
367370
* @private
368371
*
369372
* @example
@@ -374,7 +377,6 @@ function copyArray(value) {
374377
const { length, ...members } = Object.getOwnPropertyDescriptors(value);
375378
Object.defineProperties(copy, members);
376379
Object.defineProperty(copy, "length", length);
377-
if (Object.getPrototypeOf(copy) !== Object.getPrototypeOf(value)) Object.setPrototypeOf(copy, Object.getPrototypeOf(value));
378380
return copy;
379381
}
380382

@@ -408,6 +410,9 @@ export function copyForComposition(value, { addsMembers = false } = {}) {
408410
// A function default with named exports is the node itself (`api.x()` calls it, `api.x.extra` is the
409411
// named export): the exports go on a callable layer, so the module's own function is never written to.
410412
if (typeof value === "function") return addsMembers ? layerOver(value) : value;
413+
// An instance of an Array subclass may hold private fields no copy reproduces: it is kept as itself,
414+
// as a class instance is (#585 review).
415+
if (Array.isArray(value) && Object.getPrototypeOf(value) !== Array.prototype) return addsMembers ? layerOver(value) : value;
411416
if (Array.isArray(value)) return addsMembers ? layerOver(value) : copyArray(value);
412417
if (hasOwnClass(value)) return addsMembers ? layerOver(value) : value;
413418
const descriptors = Object.getOwnPropertyDescriptors(value);

‎tests/vitests/suites/helpers/composition.test.vitest.mjs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @Email: <[email protected]>
88
* -----
99
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
10-
* @Last modified time: 2026-10-12T14:57:32-07:00 (1791842252)
10+
* @Last modified time: 2026-10-12T15:33:28-07:00 (1791844408)
1111
* -----
1212
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
1313
*
@@ -24,6 +24,7 @@
2424
* | plain object | copy (descriptors kept) | copy (members replaceable) |
2525
* | user Proxy (object/callable) | layer | layer |
2626
* | array | copy (elements, holes, members, descriptors) | layer |
27+
* | Array subclass instance | as-is | layer |
2728
* | class instance, Map | as-is | layer |
2829
* | composition layer | fresh layer over the same value | fresh layer over the same value |
2930
*
@@ -242,6 +243,31 @@ describe("layer receivers and fixed keys", () => {
242243
expect(Object.getOwnPropertyDescriptor(layer, "length").writable).toBe(true);
243244
});
244245

246+
it("refuses changing a layer's prototype, which reports the value's own, and accepts the one it has", () => {
247+
const value = Object.assign(() => "tool", { label: "kit" });
248+
const layer = copyForComposition(value, { addsMembers: true });
249+
expect(Reflect.setPrototypeOf(layer, { other: true })).toBe(false);
250+
expect(Object.getPrototypeOf(layer)).toBe(Function.prototype);
251+
expect(Reflect.setPrototypeOf(layer, Function.prototype)).toBe(true);
252+
expect(Object.getPrototypeOf(value)).toBe(Function.prototype);
253+
});
254+
255+
it("keeps an Array subclass instance itself, private fields included, and layers it for added members", () => {
256+
class Items extends Array {
257+
#tag = "x";
258+
tag() {
259+
return this.#tag;
260+
}
261+
}
262+
const items = Items.from(["a", "b"]);
263+
const kept = copyForComposition(items);
264+
expect(kept.tag()).toBe("x");
265+
const layer = copyForComposition(items, { addsMembers: true });
266+
expect(layer.tag()).toBe("x");
267+
expect(layer.length).toBe(2);
268+
expect(Array.isArray(layer)).toBe(true);
269+
});
270+
245271
it("defines a new member with the attributes it leaves out set false, as an object does", () => {
246272
const layer = copyForComposition(
247273
Object.assign(() => "tool", { label: "kit" }),

0 commit comments

Comments
 (0)