Skip to content

Commit f7f5dc6

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Extract polyfillGlobal from InitializeCore
Reviewed By: jeanlauliac Differential Revision: D6987657 fbshipit-source-id: 8762732de671418520376a98bdd724bbb24e4e36
1 parent 991b7ab commit f7f5dc6

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

Libraries/Core/InitializeCore.js

+4-48
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
'use strict';
2929

30+
const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions');
31+
3032
if (global.GLOBAL === undefined) {
3133
global.GLOBAL = global;
3234
}
@@ -35,8 +37,6 @@ if (global.window === undefined) {
3537
global.window = global;
3638
}
3739

38-
const defineLazyObjectProperty = require('defineLazyObjectProperty');
39-
4040
// Set up collections
4141
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
4242
if (_shouldPolyfillCollection('Map')) {
@@ -46,50 +46,6 @@ if (_shouldPolyfillCollection('Set')) {
4646
polyfillGlobal('Set', () => require('Set'));
4747
}
4848

49-
/**
50-
* Sets an object's property. If a property with the same name exists, this will
51-
* replace it but maintain its descriptor configuration. The property will be
52-
* replaced with a lazy getter.
53-
*
54-
* In DEV mode the original property value will be preserved as `original[PropertyName]`
55-
* so that, if necessary, it can be restored. For example, if you want to route
56-
* network requests through DevTools (to trace them):
57-
*
58-
* global.XMLHttpRequest = global.originalXMLHttpRequest;
59-
*
60-
* @see https://github.com/facebook/react-native/issues/934
61-
*/
62-
function defineLazyProperty<T>(
63-
object: Object,
64-
name: string,
65-
getValue: () => T,
66-
): void {
67-
const descriptor = Object.getOwnPropertyDescriptor(object, name);
68-
if (__DEV__ && descriptor) {
69-
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
70-
Object.defineProperty(object, backupName, {
71-
...descriptor,
72-
value: object[name],
73-
});
74-
}
75-
76-
const {enumerable, writable, configurable} = descriptor || {};
77-
if (descriptor && !configurable) {
78-
console.error('Failed to set polyfill. ' + name + ' is not configurable.');
79-
return;
80-
}
81-
82-
defineLazyObjectProperty(object, name, {
83-
get: getValue,
84-
enumerable: enumerable !== false,
85-
writable: writable !== false,
86-
});
87-
}
88-
89-
function polyfillGlobal<T>(name: string, getValue: () => T): void {
90-
defineLazyProperty(global, name, getValue);
91-
}
92-
9349
// Set up process
9450
global.process = global.process || {};
9551
global.process.env = global.process.env || {};
@@ -191,8 +147,8 @@ if (navigator === undefined) {
191147
}
192148

193149
// see https://github.com/facebook/react-native/issues/10881
194-
defineLazyProperty(navigator, 'product', () => 'ReactNative');
195-
defineLazyProperty(navigator, 'geolocation', () => require('Geolocation'));
150+
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
151+
polyfillObjectProperty(navigator, 'geolocation', () => require('Geolocation'));
196152

197153
// Just to make sure the JS gets packaged up. Wait until the JS environment has
198154
// been initialized before requiring them.
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule PolyfillFunctions
10+
* @flow
11+
* @format
12+
*/
13+
14+
'use strict';
15+
16+
const defineLazyObjectProperty = require('defineLazyObjectProperty');
17+
18+
/**
19+
* Sets an object's property. If a property with the same name exists, this will
20+
* replace it but maintain its descriptor configuration. The property will be
21+
* replaced with a lazy getter.
22+
*
23+
* In DEV mode the original property value will be preserved as `original[PropertyName]`
24+
* so that, if necessary, it can be restored. For example, if you want to route
25+
* network requests through DevTools (to trace them):
26+
*
27+
* global.XMLHttpRequest = global.originalXMLHttpRequest;
28+
*
29+
* @see https://github.com/facebook/react-native/issues/934
30+
*/
31+
function polyfillObjectProperty<T>(
32+
object: Object,
33+
name: string,
34+
getValue: () => T,
35+
): void {
36+
const descriptor = Object.getOwnPropertyDescriptor(object, name);
37+
if (__DEV__ && descriptor) {
38+
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
39+
Object.defineProperty(object, backupName, {
40+
...descriptor,
41+
value: object[name],
42+
});
43+
}
44+
45+
const {enumerable, writable, configurable} = descriptor || {};
46+
if (descriptor && !configurable) {
47+
console.error('Failed to set polyfill. ' + name + ' is not configurable.');
48+
return;
49+
}
50+
51+
defineLazyObjectProperty(object, name, {
52+
get: getValue,
53+
enumerable: enumerable !== false,
54+
writable: writable !== false,
55+
});
56+
}
57+
58+
function polyfillGlobal<T>(name: string, getValue: () => T): void {
59+
polyfillObjectProperty(global, name, getValue);
60+
}
61+
62+
module.exports = {polyfillObjectProperty, polyfillGlobal};

0 commit comments

Comments
 (0)