Skip to content

Commit 2209c13

Browse files
committed
builtins/typedarray: add subarray
test262: 57.19% (+0.01) | πŸ§ͺ 50372 | 🀠 28809 (+7) | ❌ 6967 (+8) | πŸ’€ 13579 (-15) | πŸ—οΈ 31 | πŸ’₯ 263 | ⏰ 6 | πŸ“ 717
1 parent acdc08f commit 2209c13

File tree

5 files changed

+117
-40
lines changed

5 files changed

+117
-40
lines changed

β€Žcompiler/builtins/typedarray.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ export const __${name}_prototype_at = (_this: ${name}, index: number) => {
128128
129129
export const __${name}_prototype_slice = (_this: ${name}, start: number, end: number) => {
130130
const len: i32 = _this.length;
131-
if (Porffor.type(end) == Porffor.TYPES.undefined) end = len;
132-
133131
start |= 0;
134132
end |= 0;
135133
@@ -160,7 +158,6 @@ export const __${name}_prototype_slice = (_this: ${name}, start: number, end: nu
160158
161159
export const __${name}_prototype_set = (_this: ${name}, array: any, offset: number) => {
162160
const len: i32 = _this.length;
163-
if (Porffor.type(offset) == Porffor.TYPES.undefined) offset = 0;
164161
165162
offset |= 0;
166163
if (Porffor.fastOr(offset < 0, offset > len)) throw new RangeError('Offset out of bounds');
@@ -179,6 +176,32 @@ export const __${name}_prototype_set = (_this: ${name}, array: any, offset: numb
179176
}
180177
};
181178
179+
export const __${name}_prototype_subarray = (_this: ${name}, start: number, end: any) => {
180+
const len: i32 = _this.length;
181+
if (Porffor.type(end) == Porffor.TYPES.undefined) end = len;
182+
183+
start |= 0;
184+
end |= 0;
185+
186+
if (start < 0) {
187+
start = len + start;
188+
if (start < 0) start = 0;
189+
}
190+
if (start > len) start = len;
191+
if (end < 0) {
192+
end = len + end;
193+
if (end < 0) end = 0;
194+
}
195+
if (end > len) end = len;
196+
197+
const out: ${name} = Porffor.allocateBytes(12);
198+
Porffor.wasm.i32.store(out, end - start, 0, 0);
199+
Porffor.wasm.i32.store(out, Porffor.wasm.i32.load(_this, 0, 4) + start * ${name}.BYTES_PER_ELEMENT, 0, 4);
200+
Porffor.wasm.i32.store(out, Porffor.wasm.i32.load(_this, 0, 8) + start * ${name}.BYTES_PER_ELEMENT, 0, 8);
201+
202+
return out;
203+
};
204+
182205
${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}
183206
`;
184207

0 commit comments

Comments
Β (0)