-
Notifications
You must be signed in to change notification settings - Fork 152
Add ES2020+ -> ES2019 syntax-repair polyfill for Chakra Playground #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
bkaradzic-microsoft
wants to merge
1
commit into
BabylonJS:master
from
bkaradzic-microsoft:weekend/tpc-1575-chakra-parse
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // es2019_transpile.js | ||
| // | ||
| // Lightweight regex-based ES2020+ -> ES2019 syntax repair for engines that | ||
| // lack optional chaining (?.), nullish coalescing (??), and numeric | ||
| // separators (1_000_000). | ||
| // | ||
| // This is a SYNTAX REPAIR, not a full transpile. It rewrites parse-time | ||
| // failing tokens to the closest legal ES2019 expressions so that the host | ||
| // engine accepts the code. Where native ES2020+ semantics differ from the | ||
| // rewritten form, the rewritten form runs the "happy path" -- assumes the | ||
| // target value is present. If the target is null at runtime the rewritten | ||
| // code throws a TypeError where native ?. would have short-circuited to | ||
| // undefined. This is an intentional trade-off: it produces valid syntax | ||
| // (the prerequisite for the test running at all) while preserving the | ||
| // common case behaviour. Tests that rely on the nullish-short-circuit | ||
| // semantics may surface runtime errors that were previously hidden by | ||
| // the parse failure -- a strict improvement for debuggability. | ||
| // | ||
| // Transforms applied: | ||
| // * 1_000_000 -> 1000000 (strip numeric separators) | ||
| // * a?.b -> a.b (optional chaining -> required chaining) | ||
| // * a?.[x] -> a[x] | ||
| // * a?.() -> a() | ||
| // * a ?? b -> (a != null ? a : b) | ||
| // | ||
| // Not handled (still cause parse failures on Chakra): | ||
| // * Logical assignment ||= &&= ??= | ||
| // * Class private fields #name | ||
| // * BigInt literals 1n | ||
| // | ||
| // String/regex/comment literals containing ? . sequences are not skipped, | ||
| // but in Babylon snippet code such occurrences are rare. | ||
| // | ||
| // On engines with native ES2020+ support (V8, JSC) this code is never | ||
| // invoked because the caller only retries with this transform after | ||
| // initial eval throws a SyntaxError. | ||
| // | ||
| // Public API: top-level global function __bnTranspileES2019(code) -> string. | ||
| // Chakra in Babylon Native does not define `self` or `globalThis`, so we | ||
| // install via a top-level `var` declaration which becomes a property of | ||
| // the script-global object across all our supported engines. | ||
| var __bnTranspileES2019 = (function () { | ||
| "use strict"; | ||
|
|
||
| function stripNumericSeparators(code) { | ||
| // Decimal / float: 1_000_000, 1.234_5, 1_000e3 | ||
| var out = code.replace(/\b(\d[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?)\b/g, function (m) { | ||
| return m.indexOf("_") === -1 ? m : m.replace(/_/g, ""); | ||
| }); | ||
| // Hex / octal / binary: 0xFFFF_FFFF, 0o7_5, 0b1010_0101 | ||
| out = out.replace(/\b(0[xXoObB][0-9A-Fa-f_]+)\b/g, function (m) { | ||
| return m.indexOf("_") === -1 ? m : m.replace(/_/g, ""); | ||
| }); | ||
| return out; | ||
| } | ||
|
|
||
| function transformLogicalAssignment(code) { | ||
| // a ||= b -> (a || (a = b)) | ||
| // a &&= b -> (a && (a = b)) | ||
| // a ??= b -> (a != null ? a : (a = b)) | ||
| // Only matches simple LHS (identifier, optional .prop / [idx] chain) to keep | ||
| // the rewrite syntactically safe; complex LHS expressions are left alone. | ||
| var lhs = "(?:[A-Za-z_$][\\w$]*)(?:\\.[A-Za-z_$][\\w$]*|\\[[^\\]\\n]*\\])*"; | ||
| var rhs = "[^;\\n]+"; | ||
| var prev = null, out = code, guard = 0; | ||
| while (prev !== out && guard++ < 10) { | ||
| prev = out; | ||
| out = out | ||
| .replace(new RegExp("(" + lhs + ")\\s*\\?\\?=\\s*(" + rhs + ")", "g"), | ||
| "$1 = ($1 != null ? $1 : ($2))") | ||
| .replace(new RegExp("(" + lhs + ")\\s*\\|\\|=\\s*(" + rhs + ")", "g"), | ||
| "$1 = ($1 || ($2))") | ||
| .replace(new RegExp("(" + lhs + ")\\s*&&=\\s*(" + rhs + ")", "g"), | ||
| "$1 = ($1 && ($2))"); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function transformOptionalChaining(code) { | ||
| var prev = null, out = code, guard = 0; | ||
| while (prev !== out && guard++ < 50) { | ||
| prev = out; | ||
| out = out | ||
| .replace(/\?\.(?=\()/g, "") | ||
| .replace(/\?\.(?=\[)/g, "") | ||
| .replace(/\?\./g, "."); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function transformNullishCoalescing(code) { | ||
| var prev = null, out = code, guard = 0; | ||
| while (prev !== out && guard++ < 50) { | ||
| prev = out; | ||
| out = out.replace( | ||
| /(\b[A-Za-z_$][\w$.]*(?:\[[^\]]*\])?)\s*\?\?\s*(\b[A-Za-z_$][\w$.]*(?:\[[^\]]*\])?|"[^"]*"|'[^']*'|\d+(?:\.\d+)?|true|false|null|undefined)/g, | ||
| "($1 != null ? $1 : $2)" | ||
| ); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function transpileES2019(code) { | ||
| if (typeof code !== "string") { return code; } | ||
| var out = code; | ||
| out = stripNumericSeparators(out); | ||
| out = transformLogicalAssignment(out); | ||
| out = transformOptionalChaining(out); | ||
| out = transformNullishCoalescing(out); | ||
| return out; | ||
| } | ||
|
|
||
| return transpileES2019; | ||
| })(); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we doing this with a custom script rather than just using babel or swc or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point. Honest assessment of the tradeoff:
@babel/standalone(~1 MB shipped + parsed by Chakra at startup), swc-wasm (Chakra doesn't support WASM), or this regex stopgap.?.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.Closing this PR. Filed #1711 to investigate
@babel/standaloneproperly (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.