Extract feature detection as an utilitiy module
Summary: Changelog: [Internal] This diff only extracted the `isNativeFunction` used in `setUpTimers` into the `FeatureDetection` utility, but later we will add more functions in it and reuse them in other places. Reviewed By: RSNara Differential Revision: D29986750 fbshipit-source-id: 6e48e38d92ceccb35eead3c52e00e1eecb81b5b0
This commit is contained in:
Родитель
8efbb2e71e
Коммит
06388891a3
|
@ -11,6 +11,7 @@
|
|||
'use strict';
|
||||
|
||||
const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
|
||||
const {isNativeFunction} = require('../Utilities/FeatureDetection');
|
||||
|
||||
if (__DEV__) {
|
||||
if (typeof global.Promise !== 'function') {
|
||||
|
@ -23,14 +24,7 @@ const hasHermesPromiseQueuedToJSVM =
|
|||
global?.HermesInternal?.hasPromise?.() &&
|
||||
global?.HermesInternal?.useEngineQueue?.();
|
||||
|
||||
// An util function to tell whether a function is provided natively by calling
|
||||
// the `toString` and check if the result includes `[native code]` in it.
|
||||
// N.B. a polyfill can fake this behavior but they usually won't. Hence this
|
||||
// is usually good enough for our purpose.
|
||||
const isNativeFunction = f =>
|
||||
typeof f === 'function' && f.toString().indexOf('[native code]') > -1;
|
||||
const hasNativePromise = isNativeFunction(Promise);
|
||||
|
||||
const hasPromiseQueuedToJSVM = hasNativePromise || hasHermesPromiseQueuedToJSVM;
|
||||
|
||||
// In bridgeless mode, timers are host functions installed from cpp.
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @format
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return whether or not a @param {function} f is provided natively by calling
|
||||
* `toString` and check if the result includes `[native code]` in it.
|
||||
*
|
||||
* Note that a polyfill can technically fake this behavior but few does it.
|
||||
* Therefore, this is usually good enough for our purpose.
|
||||
*/
|
||||
function isNativeFunction(f: Function): boolean {
|
||||
return typeof f === 'function' && f.toString().indexOf('[native code]') > -1;
|
||||
}
|
||||
|
||||
module.exports = {isNativeFunction};
|
Загрузка…
Ссылка в новой задаче