Fixed detection of generators
MODIFY
shim/src/functions.js
shim/src/functions.js
@@ -16,7 +16,17 @@ import { defineProperty, defineProperties, getPrototypeOf, setPrototypeOf } from
function repairFunction(contextRec, functionName, functionDecl) {
const { contextEval, contextFunction } = contextRec;
- const FunctionInstance = contextEval(`(${functionDecl}(){})`);
+ let FunctionInstance;
+ try {
+ FunctionInstance = contextEval(`(${functionDecl}(){})`);
+ } catch (e) {
+ if (!(e instanceof SyntaxError)) {
+ // Re-throw
+ throw e;
+ }
+ // Prevent failure on platforms where generators are not supported.
+ return;
+ }
const FunctionPrototype = getPrototypeOf(FunctionInstance);
// Block evaluation of source when calling constructor on the prototype of functions.
@@ -43,16 +53,10 @@ function repairFunction(contextRec, functionName, functionDecl) {
* the originals should no longer be reachable.
*/
export function repairFunctions(contextRec) {
- const { contextGlobal: g } = contextRec;
-
// Here, the order of operation is important: Function needs to be
// repaired first since the other constructors need it.
repairFunction(contextRec, 'Function', 'function');
repairFunction(contextRec, 'GeneratorFunction', 'function*');
repairFunction(contextRec, 'AsyncFunction', 'async function');
-
- const hasAsyncIteration = typeof g.Symbol.asyncIterator !== 'undefined';
- if (hasAsyncIteration) {
repairFunction(contextRec, 'AsyncGeneratorFunction', 'async function*');
}
-}

MODIFY
shim/src/intrinsics.js
shim/src/intrinsics.js
@@ -23,19 +23,37 @@ export function getIntrinsics(contextRec) {
const AsyncFunction = AsyncFunctionInstance.constructor;
const AsyncFunctionPrototype = AsyncFunction.prototype;
- const GeneratorFunctionInstance = g.eval('(function*(){})');
- const GeneratorFunction = GeneratorFunctionInstance.constructor;
- const Generator = GeneratorFunction.prototype;
- const GeneratorPrototype = Generator.prototype;
-
- const hasAsyncIterator = typeof g.Symbol.asyncIterator !== 'undefined';
+ // Ensure parsing doesn't fail on platforms that don't support Generator Functions.
+ let GeneratorFunctionInstance;
+ try {
+ GeneratorFunctionInstance = g.eval('(function*(){})');
+ } catch (e) {
+ if (!(e instanceof SyntaxError)) {
+ // Re-throw
+ throw e;
+ }
+ }
+ const GeneratorFunction = GeneratorFunctionInstance && GeneratorFunctionInstance.constructor;
+ const Generator = GeneratorFunctionInstance && GeneratorFunction.prototype;
+ const GeneratorPrototype = GeneratorFunctionInstance && Generator.prototype;
- const AsyncGeneratorFunctionInstance = hasAsyncIterator && g.eval('(async function*(){})');
- const AsyncGeneratorFunction = hasAsyncIterator && AsyncGeneratorFunctionInstance.constructor;
- const AsyncGenerator = hasAsyncIterator && AsyncGeneratorFunction.prototype;
- const AsyncGeneratorPrototype = hasAsyncIterator && AsyncGenerator.prototype;
+ // Ensure parsing doesn't fail on platforms that don't support Async Generator Functions.
+ let AsyncGeneratorFunctionInstance;
+ try {
+ AsyncGeneratorFunctionInstance = g.eval('(async function*(){})');
+ } catch (e) {
+ if (!(e instanceof SyntaxError)) {
+ // Re-throw
+ throw e;
+ }
+ }
+ const AsyncGeneratorFunction =
+ AsyncGeneratorFunctionInstance && AsyncGeneratorFunctionInstance.constructor;
+ const AsyncGenerator = AsyncGeneratorFunctionInstance && AsyncGeneratorFunction.prototype;
+ const AsyncGeneratorPrototype = AsyncGeneratorFunctionInstance && AsyncGenerator.prototype;
- const AsyncIteratorPrototype = hasAsyncIterator && getPrototypeOf(AsyncGeneratorPrototype);
+ const AsyncIteratorPrototype =
+ AsyncGeneratorFunctionInstance && getPrototypeOf(AsyncGeneratorPrototype);
// const AsyncFromSyncIteratorPrototype = undefined; // Not reacheable.
const MapIteratorObject = new g.Map()[SymbolIterator]();

Fixed detection of generators