Add ES2020+ -> ES2019 syntax-repair polyfill for Chakra Playground#1709
Closed
bkaradzic-microsoft wants to merge 1 commit into
Closed
Add ES2020+ -> ES2019 syntax-repair polyfill for Chakra Playground#1709bkaradzic-microsoft wants to merge 1 commit into
bkaradzic-microsoft wants to merge 1 commit into
Conversation
Babylon Native's Chakra-based builds parse only up to ES2019; modern Babylon.js playground snippets routinely use optional chaining (?.), nullish coalescing (??), numeric separators (1_000_000), and logical assignment (||= &&= ??=), causing 26 visual tests to fail with SyntaxError at the eval() site in validation_native.js. Add a lightweight regex-based syntax-repair polyfill loaded before babylon.max.js. The polyfill exposes __bnTranspileES2019(code) as a top-level global var. validation_native.js's new evalWithFallback helper catches SyntaxError on the first eval and retries once with the repaired source. Engines that already accept the source (V8, JSC) never hit the retry path. The polyfill is syntax-only (parse-time fix), not semantic - ?. is rewritten to . so a null target throws TypeError at runtime instead of short-circuiting to undefined. This is a deliberate trade-off: parse failures are 100% blocking; runtime TypeErrors are diagnosable and at worst surface a different bug that was previously hidden. Strip excludeFromAutomaticTesting from idx 403 (custom-handling-of-materials-for-render-target-pass) which now passes end-to-end. Updated reasons for the other 25 entries to describe their post-polyfill state: 9 hit runtime errors after parse repair, 12 hit asset/module gaps, 4 hit ES2022 syntax not covered by the polyfill (e.g. private class fields).
This was referenced May 18, 2026
ryantrem
reviewed
May 18, 2026
Member
There was a problem hiding this comment.
Why are we doing this with a custom script rather than just using babel or swc or something?
Contributor
Author
There was a problem hiding this comment.
Fair point. Honest assessment of the tradeoff:
- The custom regex polyfill runs inside the Chakra host at parse-failure time, so a Node-side build tool (babel CLI / swc CLI) wasn't directly usable. The runtime-in-host constraint left us with
@babel/standalone(~1 MB shipped + parsed by Chakra at startup), swc-wasm (Chakra doesn't support WASM), or this regex stopgap. - The regex is semantically incomplete —
?.rewrites to.sonull?.foothrowsTypeErrorinstead of returningundefined. That's 9 of the 26 affected tests still excluded because of this divergence, plus 4 more for ES2022 private fields. So this polyfill unlocks only 1 test out of 26. - The cost/benefit of "+114 lines for +1 test, with a semantic footgun baked in" is too thin to justify cementing the regex approach.
Closing this PR. Filed #1711 to investigate @babel/standalone properly (measure the actual size/startup cost, prototype, compare against alternatives, decide). If that lands, it would unlock the 9 ?. tests + 4 ES2022 tests correctly, which is a much better story than what this PR achieves.
Thanks for pushing back.
Contributor
Author
|
Closing per discussion above. Pursuing #1711 instead. |
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.
Adds a lightweight regex-based ES2020+ -> ES2019 syntax-repair polyfill so Chakra-based BN runtimes can parse playgrounds that use modern syntax. V8/JSC runtimes already support these features natively; the polyfill self-detects and no-ops when not needed.
Changes:
Apps/Playground/Scripts/es2019_transpile.js- regex repair for:?.(optional chaining),??(nullish coalescing), numeric separators (incl. hex),||=&&=??=(logical assignment). Top-levelvar __bnTranspileES2019exposes the helper.Apps/Playground/Scripts/validation_native.js-evalWithFallbackcatchesSyntaxErrorand retries once with repaired source.Apps/Playground/CMakeLists.txt- addses2019_transpile.jsto SCRIPTS list.Apps/Playground/Shared/AppContext.cpp- loadses2019_transpile.jsbeforefetch_polyfill.js.Apps/Playground/Scripts/config.json- re-enables 1 test (idx 403 custom-handling-of-materials-for-render-target-pass); rewritesreasonon 25 remaining-excluded tests with their post-polyfill state.Caveat: Polyfill is parse-only -
?.rewrites to.so null targets raiseTypeErrorat runtime instead of short-circuiting. 25 tests remain excluded for: 9 runtime TypeErrors from?.->.semantic divergence, 4 missing ADDONS module, 8 asset/url errors, 4 ES2022 private fields not handled by the polyfill.Landing context
This PR is one of 8 splits from the proven CI-green combined preview in draft PR #1702 (see #1702 for the full intended end-state and verified CI run 26044922430).
Recommended landing order
Tier 1 - parallel-reviewable, no source conflicts:
reasonrewrites (3 entries)reasonrewrites (17 entries)Tier 2 - sequential, each touches
Apps/Playground/CMakeLists.txtSCRIPTS list +Apps/Playground/Shared/AppContext.cppLoadScript order; rebase the next branch after the previous merges:Reference policy reminder
Reference PNGs across all 8 PRs come from Babylon.js; never re-baked by BN. Combined diff: 0 PNGs.