Repository navigation
Conversation
…rflow" Valdi compiles TypeScript to ES2016, so `for (const x of await y)` in an async function becomes `for (const x of yield y)` inside a generator. QuickJS 2021-03-27 marked the loop as holding an iterator before it parsed the iterable expression, so a `yield` there emitted iterator cleanup for an iterator that did not exist yet and popped the stack too far. The bytecode step then failed: "Compiler Toolbox error: stack underflow". Backport of upstream bellard/quickjs@69090b9 ("Fix stack underflow with generator in iterable", saghul, #488, MIT): set has_iterator and drop_count after the iterable is parsed.
Valdi hot systems touchedWhat to do: your PR got this comment, now what?
Significant changes to the guard systems above can't be covered by a runtime flag; they need the system's tests to pass and a release bake instead. A change is significant if it alters lifetime or teardown order, threading or locks, a bridge contract, error behavior, a default, a base-class contract, the startup path or a build-host format, or if it removes something that looks dead, adds hot-path logging, or refactors hot code. See the policy.
Advisory only. This comment never blocks a merge. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Valdi compiles TypeScript to ES2016 (
_configs/base.tsconfig.json), so in an async functionbecomes
for (const n of yield items)inside a generator. Valdi's QuickJS (2021-03-27) marks the loop as holding an iterator before it parses the iterable expression, so ayieldin that expression emits iterator cleanup for an iterator that doesn't exist yet and pops the stack too far. The bytecode step fails and the module doesn't build:Any module with this common pattern is affected. No module in the repo uses it today.
This backports upstream bellard/quickjs@69090b9 ("Fix stack underflow with generator in iterable", saghul, #488, MIT). It sets
has_iteratoranddrop_countafter the iterable is parsed. The change is 6 added and 2 removed lines injs_parse_for_in_of, the same as upstream apart from this file's formatting.Type of Change
Testing
Evidence
1. Valdi's compiler on a 7-line module. I bootstrapped an app with
valdi bootstrap -l <this checkout>and added:bazel build //modules/<app>:<app>fails withCompiler Toolbox error: stack underflow (op=114, pc=13).INFO: Build completed successfully. The Android.valdimodule(zstd-decompressed) containsForOfAwait.js. Valdi's TypeScript output for the loop is exactlyfor (const n of yield items), the form that failed.2. Compile-only, vendored QuickJS built standalone (
JS_EVAL_FLAG_COMPILE_ONLY).function* g(){ for (const k of yield 1) {} }function* g(){ for (var k of yield 1) {} }function* g(){ for (const k of (0, yield 1)) {} }function* g(){ for (const k of [...(yield 1)]) {} }function* g(){ for (const k of id(yield 1)) {} }function* g(){ try { for (const k of yield 1) {} } finally {} }async function g(){ for await (const k of await f()) {} }function* g(){ const a = yield 1; for (const k of a) {} }function* g(){ for (const k in yield 1) {} }3. Runtime semantics, fixed QuickJS vs V8 (Node). One script exercises:
returninside the loop, which must close the iterator;break, after which the generator continues;return()andthrow()while suspended at theyieldin the loop head, before any iterator exists;for (… of await …)shape.Output from the fixed QuickJS and from Node is identical:
The unfixed QuickJS can't compile that script (
stack underflow).Checklist
third-party/quickjs/**(hot systemjsengine, gateguard). This is a compiler front-end fix, so a runtime tweak can't cover it. It changes bytecode only for sources that failed to compile before. The other engines (Hermes, JSC) don't share this parser.Related Issues
None open. This is a known upstream QuickJS bug (bellard/quickjs#189, fixed in #488).
Reverts / partially reverts: none
Additional Context
Third-party code: the change is upstream QuickJS's own fix (MIT), attributed above and in the commit message.