Skip to content

Commit 76ebdd1

Browse files
proggeramlugRalph Küpper
andauthored
fix(runtime): numeric-key computed calls must read the element, not dispatch by name (#6328) (#6344)
`await Promise.all([...])` silently evaporated — no output, exit code 0 — when the input promises were settled through executor-captured resolvers invoked from a loop: const ps = Array.from({length: 100}, () => new Promise(r => resolvers.push(r))); const all = Promise.all(ps); for (let i = 0; i < 100; i++) resolvers[i](i); const r = await all; // never resumes The promise machinery was not at fault. `Promise.all` is not even required to reproduce: the defect is that `arr[i](x)` — a call whose callee is a computed member with a *numeric* key — was a silent no-op inside an `async` function. Codegen's `try_lower_index_get_call` bails out to the array element-call lowering only when `is_numeric_expr` proves the index numeric; otherwise the call goes to `js_native_call_method_value` (the #321 dynamic-key `this`-binding path). Inside an async function it can never prove it: the async-to-generator transform turns every body local into a boxed mutable capture typed `Any`, so the loop counter reads back untyped. `js_native_call_method_value` then ran the key through `js_to_property_key`, which canonicalizes the index to the string "3", and handed that to the dispatch tower — which resolves by METHOD NAME (own-field scan + prototype/class-id chain). An Array's ELEMENT storage is none of those, so the lookup missed, `undefined` came back, and the call vanished. The loop resolved nothing; `await all` waited on a promise nobody would ever settle; the event loop drained and the process exited 0. The 8-vs-9 threshold in the report is the loop unroller: at a low trip count the indices const-fold to literals, which codegen *can* prove numeric. Fix: per spec `obj[k](...)` is Get(obj, k) then Call — the property read wins. For a numeric key, resolve the element/own value through `js_object_get_index_polymorphic` first and invoke it when the key names something. Fall through to the name tower when it names nothing, so a numerically-named vtable method (`class C { 3() {} }`) keeps its dispatch, and `this` stays bound either way. Fixture asserts exit code as well as stdout — the failure mode is a silent exit 0. Co-authored-by: Ralph Küpper <ralph@skelpo.com>
1 parent 108bdf2 commit 76ebdd1

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

‎crates/perry-runtime/src/object/native_call_method.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ pub unsafe extern "C" fn js_native_call_method_apply_by_id(
226226
)
227227
}
228228

229+
/// The numeric property key of an `obj[key](...)` call, as the raw `f64` index
230+
/// `js_object_get_index_polymorphic` consumes, or `None` when `key` is not a
231+
/// number. Both representations a numeric key can arrive in are accepted: a
232+
/// plain IEEE double (what a boxed `Any` capture reads back as — the #6328
233+
/// async-loop shape) and a NaN-boxed INT32 (the i32 loop-counter lowering).
234+
#[inline]
235+
fn numeric_index_key(key: JSValue) -> Option<f64> {
236+
if key.is_int32() {
237+
return Some(key.as_int32() as f64);
238+
}
239+
// `is_number` accepts every non-Perry-tagged bit pattern, NaN included; a
240+
// NaN key is not an index and would only make the polymorphic read return
241+
// `undefined`, so screen it out here rather than paying for the probe.
242+
let raw = f64::from_bits(key.bits());
243+
(key.is_number() && !raw.is_nan()).then_some(raw)
244+
}
245+
229246
/// Dispatch `obj[key](args)` where `key` is a *runtime value* whose static type
230247
/// is not provably a string (`cur._op`, `arr[i]`, a `let`-rebound key, etc.).
231248
///
@@ -339,6 +356,39 @@ pub unsafe extern "C" fn js_native_call_method_value(
339356
}
340357
}
341358

359+
// #6328: NUMERIC key — `fns[i](x)`, `resolvers[i](i)`. `js_to_property_key`
360+
// canonicalizes the index to the string `"i"`, and the string branch below
361+
// hands that to `js_native_call_method`, which dispatches by *method name*:
362+
// own-field scan + prototype/class-id chain. An Array's ELEMENT storage is
363+
// none of those, so the lookup misses, the tower returns `undefined`, and
364+
// the call SILENTLY EVAPORATES — no throw, no diagnostic, exit code 0.
365+
//
366+
// Codegen only routes an `arr[i](...)` call here when it cannot prove `i`
367+
// numeric (`try_lower_index_get_call` bails to the array element-call
368+
// lowering when `is_numeric_expr` holds). Inside an async function it never
369+
// can: the async-to-generator transform turns every body local into a
370+
// boxed mutable capture typed `Any`, so `i` reads back as an untyped value
371+
// and the call lands here. That is why `await Promise.all(ps)` evaporated —
372+
// the `for (…) resolvers[i](i)` loop resolved nothing (#6328).
373+
//
374+
// Per spec `obj[k](...)` is Get(obj, k) then Call — the property READ wins.
375+
// Resolve the element/own value first and invoke it when the key names
376+
// something; only fall through to the name tower when it names nothing, so
377+
// a numerically-named vtable method (`class C { 3() {} }`) keeps working.
378+
if !is_symbol_key {
379+
if let Some(index) = numeric_index_key(key_jsval) {
380+
let field =
381+
crate::object::js_object_get_index_polymorphic(object.to_bits() as i64, index);
382+
let fv = JSValue::from_bits(field.to_bits());
383+
if !fv.is_undefined() && !fv.is_null() {
384+
let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
385+
let result = crate::closure::js_native_call_value(field, args_ptr, args_len);
386+
IMPLICIT_THIS.with(|c| c.set(prev_this));
387+
return result;
388+
}
389+
}
390+
}
391+
342392
let property_key = if is_symbol_key {
343393
key
344394
} else {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// #6328: `await Promise.all([...])` silently evaporated (exit 0, no output) when
2+
// the input promises were settled through executor-captured resolvers invoked
3+
// from a loop.
4+
//
5+
// Root cause was not in the promise machinery at all: `arr[i](x)` — a call whose
6+
// callee is a computed member with a *numeric* key — is routed to
7+
// `js_native_call_method_value` whenever codegen cannot statically prove the key
8+
// numeric. Inside an `async` function it never can (the async-to-generator
9+
// transform turns body locals into boxed `Any` captures), and that runtime helper
10+
// stringified the index and dispatched by METHOD NAME — which can never see an
11+
// Array's element storage. The lookup missed, `undefined` came back, and the call
12+
// was a silent no-op. The loop resolved nothing, so `await all` never resumed.
13+
//
14+
// The `for` loops below matter: at a low trip count the loop is unrolled to
15+
// constant indices, which codegen *can* prove numeric — the bug only shows up
16+
// once the index stays a runtime value.
17+
18+
async function combinator() {
19+
const resolvers: ((v: number) => void)[] = [];
20+
const ps = Array.from(
21+
{ length: 100 },
22+
() => new Promise<number>((res) => { resolvers.push(res); }),
23+
);
24+
const all = Promise.all(ps);
25+
for (let i = 0; i < 100; i++) resolvers[i](i);
26+
const r = await all;
27+
console.log("all", r.length, r[0], r[99]);
28+
29+
const settled = await Promise.allSettled(ps);
30+
console.log("allSettled", settled.length, settled[7].status);
31+
console.log("race", await Promise.race(ps));
32+
console.log("any", await Promise.any(ps));
33+
}
34+
35+
// The underlying defect, with no promise anywhere: calling closures out of an
36+
// array by runtime index, inside an async function.
37+
async function indexCall() {
38+
const fns: ((v: number) => string)[] = [];
39+
for (let i = 0; i < 9; i++) fns.push((v) => "fn:" + v);
40+
const out: string[] = [];
41+
for (let i = 0; i < 9; i++) out.push(fns[i](i));
42+
await 0;
43+
console.log("indexCall", out.join(","));
44+
}
45+
46+
// `this` must still be bound by the dynamic-key dispatch (#321): a numerically
47+
// keyed own method on a plain object reads `this` through the same helper.
48+
async function thisBinding() {
49+
const obj: any = { tag: "obj", 3: function () { return this.tag; } };
50+
let k = 3;
51+
console.log("thisBinding", obj[k]());
52+
await 0;
53+
}
54+
55+
// A numerically named class method still resolves through the vtable — the
56+
// element-read fast path must fall through when the key names no own value.
57+
class Numbered {
58+
3() {
59+
return "vtable-3";
60+
}
61+
}
62+
63+
async function vtable() {
64+
const c: any = new Numbered();
65+
let k = 3;
66+
console.log("vtable", c[k]());
67+
await 0;
68+
}
69+
70+
// A rejecting member must still reject, and `await` must still see it.
71+
async function rejects() {
72+
const resolvers: ((v: number) => void)[] = [];
73+
const rejecters: ((e: unknown) => void)[] = [];
74+
const ps = Array.from(
75+
{ length: 10 },
76+
() =>
77+
new Promise<number>((res, rej) => {
78+
resolvers.push(res);
79+
rejecters.push(rej);
80+
}),
81+
);
82+
const all = Promise.all(ps);
83+
for (let i = 0; i < 9; i++) resolvers[i](i);
84+
rejecters[9](new Error("boom"));
85+
try {
86+
await all;
87+
console.log("rejects UNREACHABLE");
88+
} catch (e) {
89+
console.log("rejects", (e as Error).message);
90+
}
91+
}
92+
93+
async function main() {
94+
await combinator();
95+
await indexCall();
96+
await thisBinding();
97+
await vtable();
98+
await rejects();
99+
console.log("done");
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)