Skip to content

Commit

Permalink
runtime: check "for-of" not "=>" for es6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 6, 2021
1 parent 488fe7c commit 236039d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions internal/runtime/runtime.go
Expand Up @@ -16,7 +16,7 @@ import (
const SourceIndex = uint32(0)

func CanUseES6(unsupportedFeatures compat.JSFeature) bool {
return !unsupportedFeatures.Has(compat.Let) && !unsupportedFeatures.Has(compat.Arrow)
return !unsupportedFeatures.Has(compat.Let) && !unsupportedFeatures.Has(compat.ForOf)
}

func code(isES6 bool) string {
Expand Down Expand Up @@ -291,20 +291,20 @@ func code(isES6 bool) string {
// This is for the "binary" loader (custom code is ~2x faster than "atob")
export var __toBinaryNode = base64 => new Uint8Array(Buffer.from(base64, 'base64'))
export var __toBinary = /* @__PURE__ */ (() => {
var table = new Uint8Array(128)
for (var i = 0; i < 64; i++) table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i
return base64 => {
var n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == '=') - (base64[n - 2] == '=')) * 3 / 4 | 0)
for (var i = 0, j = 0; i < n;) {
var c0 = table[base64.charCodeAt(i++)], c1 = table[base64.charCodeAt(i++)]
var c2 = table[base64.charCodeAt(i++)], c3 = table[base64.charCodeAt(i++)]
bytes[j++] = (c0 << 2) | (c1 >> 4)
bytes[j++] = (c1 << 4) | (c2 >> 2)
bytes[j++] = (c2 << 6) | c3
}
return bytes
var table = new Uint8Array(128)
for (var i = 0; i < 64; i++) table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i
return base64 => {
var n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == '=') - (base64[n - 2] == '=')) * 3 / 4 | 0)
for (var i = 0, j = 0; i < n;) {
var c0 = table[base64.charCodeAt(i++)], c1 = table[base64.charCodeAt(i++)]
var c2 = table[base64.charCodeAt(i++)], c3 = table[base64.charCodeAt(i++)]
bytes[j++] = (c0 << 2) | (c1 >> 4)
bytes[j++] = (c1 << 4) | (c2 >> 2)
bytes[j++] = (c2 << 6) | c3
}
})()
return bytes
}
})()
`

return text
Expand Down

3 comments on commit 236039d

@kzc
Copy link
Contributor

@kzc kzc commented on 236039d Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, neither => support nor for/of support is a reliable indicator of supporting ES6.

$ echo 'for (var x of "AB") console.log(x);' | node-v0.12.9 
A
B
$ echo 'console.log((x => x * 7)(6));' | node-v0.12.9 
[stdin]:1
console.log((x => x * 7)(6));
               ^^
SyntaxError: Unexpected token =>

class may be a better indicator.

$ for i in node-v0.10.41 node-v0.12.9 node-v4.2.1 node-v6.9.0 node-v8.0.0; do echo -n "$i: "; $i -e 'class C{}' 2>/dev/null && echo "supports class" || echo "fail"; done
node-v0.10.41: fail
node-v0.12.9: fail
node-v4.2.1: fail
node-v6.9.0: supports class
node-v8.0.0: supports class

@evanw
Copy link
Owner Author

@evanw evanw commented on 236039d Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check checks for the set of ES6 features that the code below uses that can’t be transpiled to ES5 (since esbuild’s ES6-to-ES5 transpiler is incomplete). I tried to avoid using such features so the runtime is portable, except I’m using these two features here (let and for-of) since the resulting ES6 code is shorter and therefore better when minified. That means the code now needs to versions to work around esbuild’s ES5 limitations. Arrow functions used to be an issue but I have since implemented translation to ES5 for them, so they are no longer an issue.

@kzc
Copy link
Contributor

@kzc kzc commented on 236039d Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Sorry for the noise.

Please sign in to comment.