Array.fromAsync constructs the result before opening the input iterator
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: zloirock, Unassigned)
Details
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36
Steps to reproduce
Array.fromAsync calls the result constructor before invoking the input iterator method and reading next. This affects both synchronous and asynchronous iterables.
Reproduced with native Array.fromAsync in Firefox 115.0.3 and 159.0 on Windows 10 (Sauce Labs), Firefox 157.0 on macOS (Playwright), and Nightly Firefox 161.0a1. No polyfills are loaded in the reproducer.
const log = [];
function C() {
log.push('construct');
}
const items = {
[Symbol.asyncIterator]() {
log.push('iterator');
return {
get next() {
log.push('get next');
return () => ({ done: true });
}
};
}
};
Array.fromAsync.call(C, items).then(() => console.log(log));
Expected: ['iterator', 'get next', 'construct'].
Actual: ['construct', 'iterator', 'get next'].
Replacing Symbol.asyncIterator with Symbol.iterator gives the same discrepancy. A constructor that replaces iterator.next also changes which method is used, although the method should already have been captured. If the iterator method or next getter throws, the constructor should not run.
Array.fromAsync obtains the iterator record before constructing the output. GetIteratorFromMethod includes reading next.
In js/src/builtin/Array.js, ArrayFromAsync currently constructs A before entering for await (... of allowContentIterWith(...)); iterator acquisition therefore occurs after construction.
Related: bug 1834560 tracks editorial updates but does not describe this observable ordering defect. WebKit bug 326074 covers a narrower counterpart in its async path. I did not find an existing Firefox report for this ordering defect.
Actual results
^
Expected results
^
Comment 1•18 days ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::JavaScript Engine' component, but is not confident enough to move the bug to that component.
Comment 2•18 days ago
|
||
| frontend-triage | ||
This is a SpiderMonkey builtin, so it belongs in Core :: JavaScript: Standard Library, not Firefox :: General.
The report is right. In ArrayFromAsync (Array.js), A is constructed with constructContentFunction(C, C) before the for await (... of allowContentIterWith(asyncItems, usingAsyncIterator, usingSyncIterator)) loop (Array.js). The loop is what calls the iterator method and reads next. The current proposal text runs GetIteratorFromMethod (or CreateAsyncFromSyncIterator(GetIteratorFromMethod(...))) first and only then does ? Construct(C) / ArrayCreate(0). Construction was moved to its current spot in bug 1833946 (the "remove double construction" change), but it still runs before the iterator is opened.
Suggested fix:
- In
ArrayFromAsync, open the iterator by hand before constructingA:- Call the method with
callContentFunction. - If the result isn't an object, throw
JSMSG_GET_ASYNC_ITER_RETURNED_PRIMITIVE(async case) orJSMSG_GET_ITER_RETURNED_PRIMITIVE(sync case). These keep the messages identical tofor-of/for await, which from_async.js checks. - Read
next. - For the sync case, wrap with the existing
CreateAsyncFromSyncIterator(iter, next)intrinsic (SelfHosting.cpp) and use theiterator/nextMethodfields of the record it returns.
- Call the method with
- Then construct
Aand loop withfor await (var nextValue of allowContentIterWithNext(iterator, nextMethod)). IfConstruct(C)throws here, the spec does not close the iterator, so no try/finally is needed. emitAsyncIteratorin BytecodeEmitter.cpp currently asserts thatAllowContentWithNextis never used. That assert needs to become the same early return thatemitIteratoralready has (BytecodeEmitter.cpp), since the stack already holdsNEXT ITER.
Existing tests don't cover this order: this-constructor-operations.js only logs construct and define/set. A new test should log the iterator method call, the next getter and the constructor, for both the async and sync iterable paths, and check that the constructor doesn't run when the method or the getter throws. It could go in non262 and/or upstream test262.
Suggested severity: S4. This is a spec-conformance ordering difference that only shows up when Array.fromAsync is called with a custom constructor as this and an observable iterator; ordinary Array.fromAsync(iterable) calls are unaffected.
If you'd like to provide feedback on this comment, please use the 👍 or 👎 reaction.
If you want to categorize your feedback you can add one of the following tags: ai-triage-{wrong-file, wrong-cause, hallucination, out-of-scope, not-a-bug, wrong-fix, shallow-fix, wrong-severity}.
Needinfo hackbot@mozilla.tld to have a patch generated for this bug, include any questions or directions as needed in your comment.
Updated•17 days ago
|
Updated•14 days ago
|
Description
•