mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-02 07:43:42 +08:00
Untyped code that loops over a native array (`for (const ch of checks)` in zod, over arrays the compiled code built) paid for the loop before its first element: - Every check of whether a typed capsule is a generator compared its type name against "async-generator<" with strncmp, and native array type names also start with 'a'. Iterator creation made several such checks. The test now looks at the second byte before comparing. - scr_dyn_iterator called the builtin values() through the general call path for a native array; it now creates the native iterator directly, as it already did for untyped arrays. - Read sites of `length` and `next` call scr_js_get_probe_native, which answers a native array's `length` and a native iterator's `next`, where the generic read built the array's checked view or looked the method up by name. Other read sites keep scr_js_get_probe, so their receivers that are not ordinary objects still miss without a further test.
30 lines
900 B
JavaScript
30 lines
900 B
JavaScript
export function sumAll(values) {
|
|
let total = 0;
|
|
for (const value of values) total += value;
|
|
return total;
|
|
}
|
|
export function lengths(arrays) {
|
|
const out = [];
|
|
for (const array of arrays) out.push(array.length);
|
|
return out;
|
|
}
|
|
export function describe(values) {
|
|
const parts = [];
|
|
for (const value of values) parts.push(typeof value === "object" ? value.sku : value);
|
|
return parts.join("+");
|
|
}
|
|
export function stepper(values) {
|
|
const iterator = values[Symbol.iterator]();
|
|
const next = iterator.next;
|
|
const out = [];
|
|
for (let step = next.call(iterator); !step.done; step = next.call(iterator)) out.push(step.value);
|
|
out.push(typeof iterator.next, iterator.next === next);
|
|
return out;
|
|
}
|
|
export async function asyncKinds(generator, array) {
|
|
const out = [];
|
|
for await (const value of generator) out.push(value);
|
|
for (const value of array) out.push(value);
|
|
return out;
|
|
}
|