mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
An Array subclass's type maps to its element array type, so a field such as TreeNode.kids holds a Kids instance in a slot typed as an array of TreeNode. The collector visits such a slot as an array, which has a collector header when its element ops trace, but the class only got a header when its own class unit was on a cycle, and the collector read a header the instance did not have. An Array subclass now carries a header whenever an array of its element type does, and its collector descriptor visits its elements instead of naming no children. An element read of an array of Array subclass instances is a union with undefined, and 'instanceof' against the class answered false for it. A union now tests its array arms' class at run time.
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
// Instances of classes that extend Array read back from an array of them
|
|
// (an element read is `Row | undefined`) still answer instanceof for their
|
|
// own class and Array, false for another Array subclass, and plain arrays
|
|
// answer false.
|
|
class Row extends Array<number> {
|
|
sum(): number {
|
|
let s = 0;
|
|
for (const v of this) s += v;
|
|
return s;
|
|
}
|
|
}
|
|
class Column extends Array<number> {}
|
|
|
|
function rows(n: number): Row[] {
|
|
const out: Row[] = [];
|
|
for (let i = 0; i < n; i++) {
|
|
const row = new Row();
|
|
for (let j = 0; j <= i; j++) row.push(j);
|
|
out.push(row);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const lines: number[][] = [new Row(), new Column(), [1, 2]];
|
|
|
|
const table = rows(6);
|
|
console.log(table.length, table[5]!.sum(), table[5] instanceof Row, table[5] instanceof Column);
|
|
console.log(table[4] instanceof Row, table[4] instanceof Array);
|
|
console.log(table.at(-1) instanceof Row, table.at(10) instanceof Row);
|
|
const plain: number[][] = [[1, 2]];
|
|
console.log(plain[0] instanceof Row, plain[0] instanceof Array);
|
|
const kinds = lines.map((r) => (r instanceof Column ? "column" : r instanceof Row ? "row" : "plain"));
|
|
console.log(kinds.join(","));
|
|
let checks = 0;
|
|
for (let round = 0; round < 20; round++)
|
|
checks += rows(6).reduce((s, r) => s + (r instanceof Row ? r.sum() : 0), 0);
|
|
console.log(checks);
|