Conditionalize the Regenerator Setup
Summary: Changelog: [Internal] If generators are provided natively, that should suggest that the JS source code did not go through the regenerator-transform (e.g. in Metro Hermes profile), then there is no need to set up the regenerator runtime. This should save some work during the Core initialization. Reviewed By: motiz88 Differential Revision: D29986751 fbshipit-source-id: 129f5122e8e4c05535ee2aa5da6970a66843e8cd
This commit is contained in:
Родитель
06388891a3
Коммит
d9a9ae38d1
|
@ -11,17 +11,34 @@
|
|||
'use strict';
|
||||
|
||||
const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
|
||||
const {hasNativeConstructor} = require('../Utilities/FeatureDetection');
|
||||
|
||||
/**
|
||||
* Set up regenerator.
|
||||
* You can use this module directly, or just require InitializeCore.
|
||||
*/
|
||||
polyfillGlobal('regeneratorRuntime', () => {
|
||||
// The require just sets up the global, so make sure when we first
|
||||
// invoke it the global does not exist
|
||||
delete global.regeneratorRuntime;
|
||||
|
||||
// regenerator-runtime/runtime exports the regeneratorRuntime object, so we
|
||||
// can return it safely.
|
||||
return require('regenerator-runtime/runtime'); // flowlint-line untyped-import:off
|
||||
});
|
||||
let hasNativeGenerator;
|
||||
try {
|
||||
// If this function was lowered by regenerator-transform, it will try to
|
||||
// access `global.regeneratorRuntime` which doesn't exist yet and will throw.
|
||||
hasNativeGenerator = hasNativeConstructor(function*() {},
|
||||
'GeneratorFunction');
|
||||
} catch {
|
||||
// In this case, we know generators are not provided natively.
|
||||
hasNativeGenerator = false;
|
||||
}
|
||||
|
||||
// If generators are provided natively, which suggests that there was no
|
||||
// regenerator-transform, then there is no need to set up the runtime.
|
||||
if (!hasNativeGenerator) {
|
||||
polyfillGlobal('regeneratorRuntime', () => {
|
||||
// The require just sets up the global, so make sure when we first
|
||||
// invoke it the global does not exist
|
||||
delete global.regeneratorRuntime;
|
||||
|
||||
// regenerator-runtime/runtime exports the regeneratorRuntime object, so we
|
||||
// can return it safely.
|
||||
return require('regenerator-runtime/runtime'); // flowlint-line untyped-import:off
|
||||
});
|
||||
}
|
||||
|
|
|
@ -19,4 +19,13 @@ function isNativeFunction(f: Function): boolean {
|
|||
return typeof f === 'function' && f.toString().indexOf('[native code]') > -1;
|
||||
}
|
||||
|
||||
module.exports = {isNativeFunction};
|
||||
/**
|
||||
* @return whether or not the constructor of @param {object} o is an native
|
||||
* function named with @param {string} expectedName.
|
||||
*/
|
||||
function hasNativeConstructor(o: Object, expectedName: string): boolean {
|
||||
const con = Object.getPrototypeOf(o).constructor;
|
||||
return con.name === expectedName && isNativeFunction(con);
|
||||
}
|
||||
|
||||
module.exports = {isNativeFunction, hasNativeConstructor};
|
||||
|
|
Загрузка…
Ссылка в новой задаче