Improve optimizer 3
Block reserved null, true, false, this
MODIFY
shim/src/optimizer.js
shim/src/optimizer.js
@@ -9,13 +9,23 @@ import {
// todo: think about how this interacts with endowments, check for conflicts
// between the names being optimized and the ones added by endowments
-// Admits many (but not all) legal variable names: starts with letter/_/$,
-// continues with letter/digit/_/$. It will reject many legal names that
-// involve unicode characters. \w is equivalent [a-zA-Z_0-9]
+/**
+ * Simplified validation of indentifier names: may only contain alphanumeric
+ * characters (or "$" or "_"), and may not start with a digit. This is safe
+ * and does not reduces the compatibility of the shim. The motivation for
+ * this limitation was to decrease the complexity of the implementation,
+ * and to maintain a resonable level of performance.
+ * Note: \w is equivalent [a-zA-Z_0-9]
+ * See 11.6.1 Identifier Names
+ */
const identifierPattern = /^[a-zA-Z_$][\w$]*$/;
+/**
+ * In JavaScript you cannot use these reserved words as variables.
+ * See 11.6.1 Identifier Names
+ */
const keywords = new Set([
- // actual keywords
+ // 11.6.2.1 Keywords
'await',
'break',
'case',
@@ -51,10 +61,14 @@ const keywords = new Set([
'with',
'yield',
- // future reserved word
+ // Also reserved when parsing strict mode code
+ 'let',
+ 'static',
+
+ // 11.6.2.2 Future Reserved Words
'enum',
- // future reserved word in strict mode
+ // Also reserved when parsing strict mode code
'implements',
'package',
'protected',
@@ -62,9 +76,14 @@ const keywords = new Set([
'private',
'public',
- // contextual worth refusing
- 'let',
- 'async',
+ // Reserved but not mentioned in specs
+ 'await',
+
+ 'null',
+ 'true',
+ 'false',
+
+ 'this',
'arguments'
]);

MODIFY
shim/test/realm/optimizer.js
shim/test/realm/optimizer.js
@@ -2,7 +2,7 @@ import test from 'tape';
import { getOptimizableGlobals } from '../../src/optimizer';
test('getOptimizableGlobals', t => {
- t.plan(18);
+ t.plan(20);
t.deepEqual(getOptimizableGlobals({}), [], 'should return empty if no global');
@@ -44,7 +44,21 @@ test('getOptimizableGlobals', t => {
t.deepEqual(
getOptimizableGlobals(Object.create(null, { const: { value: true } })),
[],
- 'should reject keyword'
+ 'should reject reserved keyword'
+ );
+ t.deepEqual(
+ getOptimizableGlobals(
+ Object.create(null, { null: { value: true }, true: { value: true }, false: { value: true } })
+ ),
+ [],
+ 'should reject literals (reserved)'
+ );
+ t.deepEqual(
+ getOptimizableGlobals(
+ Object.create(null, { this: { value: true }, arguments: { value: true } })
+ ),
+ [],
+ 'should reject this and arguments'
);
t.deepEqual(
getOptimizableGlobals(Object.create(null, { [Symbol.iterator]: { value: true } })),
@@ -66,7 +80,7 @@ test('getOptimizableGlobals', t => {
t.deepEqual(
getOptimizableGlobals(Object.create(null, { _123: { value: true } })),
['_123'],
- 'should return leading dash'
+ 'should return leading underscore'
);
t.deepEqual(
getOptimizableGlobals(Object.create(null, { $123: { value: true } })),

Improve optimizer 3
Block reserved null, true, false, this