mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-21 05:50:55 +08:00
Represent ordinary JavaScript arrays with hole, value, and explicit-undefined states; add sparse side storage and carry missing values through the frontend, native backends, runtime, and Node-facing consumers.
30 lines
975 B
TypeScript
30 lines
975 B
TypeScript
// Cross-feature contract: sparse storage, copying sort, and missing-value
|
|
// reads must agree when used together, including through an alias.
|
|
const source: (number | undefined)[] = [];
|
|
source[4] = 30;
|
|
source[1] = undefined;
|
|
source[3] = 10;
|
|
const alias = source;
|
|
function describe(label: string, values: (number | undefined)[]): void {
|
|
const present: string[] = [];
|
|
values.forEach((value, index) => {
|
|
present.push(`${index}:${value}`);
|
|
});
|
|
console.log(label, values.length, present.join(","));
|
|
}
|
|
describe("source", source);
|
|
|
|
const ordered = source.toSorted((left, right) => (left ?? 0) - (right ?? 0));
|
|
describe("copy", ordered);
|
|
console.log("same-alias", alias === source);
|
|
describe("alias", alias);
|
|
|
|
source.sort((left, right) => (left ?? 0) - (right ?? 0));
|
|
describe("sorted", alias);
|
|
|
|
source.length = 1;
|
|
source.length = 4;
|
|
describe("regrown", source);
|
|
const [first, second] = source;
|
|
console.log("read", first, second, source[8], source.pop(), source.length);
|