Skip to content

Commit

Permalink
Extract polyfillGlobal from InitializeCore
Browse files Browse the repository at this point in the history
Reviewed By: jeanlauliac

Differential Revision: D6987657

fbshipit-source-id: 8762732de671418520376a98bdd724bbb24e4e36
  • Loading branch information
fromcelticpark authored and facebook-github-bot committed Feb 15, 2018
1 parent 991b7ab commit f7f5dc6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
52 changes: 4 additions & 48 deletions Libraries/Core/InitializeCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
'use strict';

const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions');

if (global.GLOBAL === undefined) {
global.GLOBAL = global;
}
Expand All @@ -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')) {
Expand All @@ -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<T>(
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<T>(name: string, getValue: () => T): void {
defineLazyProperty(global, name, getValue);
}

// Set up process
global.process = global.process || {};
global.process.env = global.process.env || {};
Expand Down Expand Up @@ -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.
Expand Down
62 changes: 62 additions & 0 deletions Libraries/Utilities/PolyfillFunctions.js
Original file line number Diff line number Diff line change
@@ -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<T>(
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<T>(name: string, getValue: () => T): void {
polyfillObjectProperty(global, name, getValue);
}

module.exports = {polyfillObjectProperty, polyfillGlobal};

0 comments on commit f7f5dc6

Please sign in to comment.