diff --git a/Libraries/Core/InitializeCore.js b/Libraries/Core/InitializeCore.js index f5f3d3cc4d8429..761846f862ad4a 100644 --- a/Libraries/Core/InitializeCore.js +++ b/Libraries/Core/InitializeCore.js @@ -27,6 +27,8 @@ */ 'use strict'; +const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions'); + if (global.GLOBAL === undefined) { global.GLOBAL = global; } @@ -35,8 +37,6 @@ if (global.window === undefined) { global.window = global; } -const defineLazyObjectProperty = require('defineLazyObjectProperty'); - // Set up collections const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection'); if (_shouldPolyfillCollection('Map')) { @@ -46,50 +46,6 @@ if (_shouldPolyfillCollection('Set')) { polyfillGlobal('Set', () => require('Set')); } -/** - * Sets an object's property. If a property with the same name exists, this will - * replace it but maintain its descriptor configuration. The property will be - * replaced with a lazy getter. - * - * In DEV mode the original property value will be preserved as `original[PropertyName]` - * so that, if necessary, it can be restored. For example, if you want to route - * network requests through DevTools (to trace them): - * - * global.XMLHttpRequest = global.originalXMLHttpRequest; - * - * @see https://github.com/facebook/react-native/issues/934 - */ -function defineLazyProperty( - object: Object, - name: string, - getValue: () => T, -): void { - const descriptor = Object.getOwnPropertyDescriptor(object, name); - if (__DEV__ && descriptor) { - const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`; - Object.defineProperty(object, backupName, { - ...descriptor, - value: object[name], - }); - } - - const {enumerable, writable, configurable} = descriptor || {}; - if (descriptor && !configurable) { - console.error('Failed to set polyfill. ' + name + ' is not configurable.'); - return; - } - - defineLazyObjectProperty(object, name, { - get: getValue, - enumerable: enumerable !== false, - writable: writable !== false, - }); -} - -function polyfillGlobal(name: string, getValue: () => T): void { - defineLazyProperty(global, name, getValue); -} - // Set up process global.process = global.process || {}; global.process.env = global.process.env || {}; @@ -191,8 +147,8 @@ if (navigator === undefined) { } // see https://github.com/facebook/react-native/issues/10881 -defineLazyProperty(navigator, 'product', () => 'ReactNative'); -defineLazyProperty(navigator, 'geolocation', () => require('Geolocation')); +polyfillObjectProperty(navigator, 'product', () => 'ReactNative'); +polyfillObjectProperty(navigator, 'geolocation', () => require('Geolocation')); // Just to make sure the JS gets packaged up. Wait until the JS environment has // been initialized before requiring them. diff --git a/Libraries/Utilities/PolyfillFunctions.js b/Libraries/Utilities/PolyfillFunctions.js new file mode 100644 index 00000000000000..aa35c149915aa5 --- /dev/null +++ b/Libraries/Utilities/PolyfillFunctions.js @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule PolyfillFunctions + * @flow + * @format + */ + +'use strict'; + +const defineLazyObjectProperty = require('defineLazyObjectProperty'); + +/** + * Sets an object's property. If a property with the same name exists, this will + * replace it but maintain its descriptor configuration. The property will be + * replaced with a lazy getter. + * + * In DEV mode the original property value will be preserved as `original[PropertyName]` + * so that, if necessary, it can be restored. For example, if you want to route + * network requests through DevTools (to trace them): + * + * global.XMLHttpRequest = global.originalXMLHttpRequest; + * + * @see https://github.com/facebook/react-native/issues/934 + */ +function polyfillObjectProperty( + object: Object, + name: string, + getValue: () => T, +): void { + const descriptor = Object.getOwnPropertyDescriptor(object, name); + if (__DEV__ && descriptor) { + const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`; + Object.defineProperty(object, backupName, { + ...descriptor, + value: object[name], + }); + } + + const {enumerable, writable, configurable} = descriptor || {}; + if (descriptor && !configurable) { + console.error('Failed to set polyfill. ' + name + ' is not configurable.'); + return; + } + + defineLazyObjectProperty(object, name, { + get: getValue, + enumerable: enumerable !== false, + writable: writable !== false, + }); +} + +function polyfillGlobal(name: string, getValue: () => T): void { + polyfillObjectProperty(global, name, getValue); +} + +module.exports = {polyfillObjectProperty, polyfillGlobal};