Skip to content

Commit e388851

Browse files
authored
fix(array): support sparse slots (#181)
1 parent 5105a02 commit e388851

3 files changed

Lines changed: 108 additions & 23 deletions

File tree

‎src/proxy/array.ts‎

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ export function proxifyArrayElements<T extends any[]>(
3333
rest[0] as number,
3434
...rest.slice(1).map(n => literalToAst(n)),
3535
);
36-
return deleted.map(n => proxify(n as any, mod));
36+
const result: any[] = [];
37+
result.length = deleted.length;
38+
for (let index = 0; index < deleted.length; index++) {
39+
if (deleted[index] != null) {
40+
result[index] = proxify(deleted[index], mod);
41+
}
42+
}
43+
return result;
3744
},
3845
toJSON() {
3946
return elements.map(n => proxify(n as any, mod));
@@ -50,11 +57,13 @@ export function proxifyArrayElements<T extends any[]>(
5057
const self = receiver as any[];
5158
if (key === "map") {
5259
return (callback: (value: any, index: number, array: any[]) => any) => {
53-
const results = [];
54-
let index = 0;
55-
for (const item of self) {
56-
results.push(callback(item, index, self));
57-
index++;
60+
const results: any[] = [];
61+
const length = elements.length;
62+
results.length = length;
63+
for (let index = 0; index < length; index++) {
64+
if (elements[index] != null) {
65+
results[index] = callback(proxify(elements[index], mod), index, self);
66+
}
5867
}
5968
return results;
6069
};
@@ -64,12 +73,15 @@ export function proxifyArrayElements<T extends any[]>(
6473
callback: (value: any, index: number, array: any[]) => boolean,
6574
) => {
6675
const results = [];
67-
let index = 0;
68-
for (const item of self) {
76+
const length = elements.length;
77+
for (let index = 0; index < length; index++) {
78+
if (elements[index] == null) {
79+
continue;
80+
}
81+
const item = proxify(elements[index], mod);
6982
if (callback(item, index, self)) {
7083
results.push(item);
7184
}
72-
index++;
7385
}
7486
return results;
7587
};
@@ -78,10 +90,11 @@ export function proxifyArrayElements<T extends any[]>(
7890
return (
7991
callback: (value: any, index: number, array: any[]) => void,
8092
) => {
81-
let index = 0;
82-
for (const item of self) {
83-
callback(item, index, self);
84-
index++;
93+
const length = elements.length;
94+
for (let index = 0; index < length; index++) {
95+
if (elements[index] != null) {
96+
callback(proxify(elements[index], mod), index, self);
97+
}
8598
}
8699
};
87100
}
@@ -95,24 +108,32 @@ export function proxifyArrayElements<T extends any[]>(
95108
) => any,
96109
...initialValue: [any?]
97110
) => {
98-
const array = [...self];
99-
if (array.length === 0 && initialValue.length === 0) {
100-
throw new TypeError("Reduce of empty array with no initial value");
101-
}
102-
103111
let accumulator: any;
104112
let startIndex = 0;
113+
const length = elements.length;
105114

106115
if (initialValue.length > 0) {
107116
accumulator = initialValue[0];
108117
}
109118
else {
110-
accumulator = array[0];
111-
startIndex = 1;
119+
while (startIndex < length && elements[startIndex] == null) {
120+
startIndex++;
121+
}
122+
if (startIndex === length) {
123+
throw new TypeError("Reduce of empty array with no initial value");
124+
}
125+
accumulator = proxify(elements[startIndex++], mod);
112126
}
113127

114-
for (let i = startIndex; i < array.length; i++) {
115-
accumulator = callback(accumulator, array[i], i, array);
128+
for (let index = startIndex; index < length; index++) {
129+
if (elements[index] != null) {
130+
accumulator = callback(
131+
accumulator,
132+
proxify(elements[index], mod),
133+
index,
134+
self,
135+
);
136+
}
116137
}
117138

118139
return accumulator;
@@ -222,7 +243,7 @@ export function proxifyArrayElements<T extends any[]>(
222243
}
223244

224245
const index = +key;
225-
if (!Number.isNaN(index) && index < elements.length) {
246+
if (!Number.isNaN(index) && index < elements.length && elements[index] != null) {
226247
return {
227248
value: proxify(elements[index], mod),
228249
writable: true,

‎src/proxy/proxify.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import { proxifyObject } from "./object";
1818
const _cache = new WeakMap<ASTNode, any>();
1919

2020
export function proxify<T>(node: ASTNode, mod?: ProxifiedModule): Proxified<T> {
21+
// Array holes (sparse slots) are parsed as `null`; treat them as `undefined`.
22+
if (node == null) {
23+
return undefined as any;
24+
}
25+
2126
if (LITERALS_TYPEOF.has(typeof node)) {
2227
return node as any;
2328
}

‎test/array.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,63 @@ describe("array", () => {
118118
expect(mod.exports.default.length).toBe(1);
119119
expect(await generate(mod)).toMatchInlineSnapshot(`"export default [1];"`);
120120
});
121+
122+
it("supports iterating sparse arrays", () => {
123+
const mod = parseModule<{ default: Array<number | undefined> }>(
124+
`export default [1, , 3]`,
125+
);
126+
127+
expect([...mod.exports.default]).toEqual([1, undefined, 3]);
128+
expect(Object.keys(mod.exports.default)).toEqual(["0", "2"]);
129+
expect(mod.exports.default.shift()).toBe(1);
130+
expect(mod.exports.default.shift()).toBeUndefined();
131+
expect(mod.exports.default.pop()).toBe(3);
132+
});
133+
134+
it("preserves holes in array callbacks and splice results", () => {
135+
const mod = parseModule<{ default: Array<number | undefined> }>(
136+
`export default [1, , 3]`,
137+
);
138+
const indexes: number[] = [];
139+
140+
const mapped = mod.exports.default.map((value, index) => {
141+
indexes.push(index);
142+
return value;
143+
});
144+
expect(indexes).toEqual([0, 2]);
145+
expect(mapped).toHaveLength(3);
146+
expect(Object.keys(mapped)).toEqual(["0", "2"]);
147+
expect(mod.exports.default.filter(Boolean)).toEqual([1, 3]);
148+
149+
indexes.length = 0;
150+
mod.exports.default.forEach((_, index) => indexes.push(index));
151+
expect(indexes).toEqual([0, 2]);
152+
expect(mod.exports.default.reduce((sum, value) => sum + value)).toBe(4);
153+
154+
const deleted = mod.exports.default.splice(0);
155+
expect([...deleted]).toEqual([1, undefined, 3]);
156+
expect(Object.keys(deleted)).toEqual(["0", "2"]);
157+
});
158+
159+
it("does not visit elements appended during callbacks", () => {
160+
for (const method of ["map", "filter", "forEach", "reduce"] as const) {
161+
const mod = parseModule<{ default: number[] }>(`export default [1, 2]`);
162+
const visited: number[] = [];
163+
const visit = (value: number) => {
164+
visited.push(value);
165+
if (value === 1)
166+
mod.exports.default.push(3);
167+
return value;
168+
};
169+
170+
if (method === "reduce")
171+
mod.exports.default.reduce((sum, value) => sum + visit(value), 0);
172+
else if (method === "filter")
173+
mod.exports.default.filter(visit);
174+
else
175+
mod.exports.default[method](visit);
176+
177+
expect(visited).toEqual([1, 2]);
178+
}
179+
});
121180
});

0 commit comments

Comments
 (0)