2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-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.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* Sets up global variables typical in most JavaScript environments.
|
|
|
|
*
|
|
|
|
* 1. Global timers (via `setTimeout` etc).
|
|
|
|
* 2. Global console object.
|
|
|
|
* 3. Hooks for printing stack traces with source maps.
|
|
|
|
*
|
|
|
|
* Leaves enough room in the environment for implementing your own:
|
|
|
|
* 1. Require system.
|
|
|
|
* 2. Bridged modules.
|
|
|
|
*
|
|
|
|
* @providesModule InitializeJavaScriptAppEngine
|
|
|
|
*/
|
|
|
|
|
2015-10-01 21:13:24 +00:00
|
|
|
/* eslint strict: 0 */
|
2015-02-20 04:10:52 +00:00
|
|
|
/* globals GLOBAL: true, window: true */
|
|
|
|
|
2016-05-04 12:18:42 +00:00
|
|
|
require('regenerator-runtime/runtime');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (typeof GLOBAL === 'undefined') {
|
2016-04-29 23:14:55 +00:00
|
|
|
global.GLOBAL = global;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
2016-04-29 23:14:55 +00:00
|
|
|
global.window = global;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 17:49:29 +00:00
|
|
|
function setUpProcess() {
|
|
|
|
GLOBAL.process = GLOBAL.process || {};
|
|
|
|
GLOBAL.process.env = GLOBAL.process.env || {};
|
|
|
|
if (!GLOBAL.process.env.NODE_ENV) {
|
|
|
|
GLOBAL.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:02:31 +00:00
|
|
|
function setUpProfile() {
|
|
|
|
const Systrace = require('Systrace');
|
|
|
|
Systrace.setEnabled(global.__RCTProfileIsProfiling || false);
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
function setUpConsole() {
|
|
|
|
// ExceptionsManager transitively requires Promise so we install it after
|
2016-05-04 17:49:29 +00:00
|
|
|
const ExceptionsManager = require('ExceptionsManager');
|
2015-11-05 20:20:15 +00:00
|
|
|
ExceptionsManager.installConsoleErrorReporter();
|
2016-05-04 17:49:29 +00:00
|
|
|
|
|
|
|
require('RCTLog');
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 21:39:24 +00:00
|
|
|
/**
|
|
|
|
* Assigns a new global property, replacing the existing one if there is one.
|
2015-10-12 18:50:43 +00:00
|
|
|
*
|
2015-10-09 21:39:24 +00:00
|
|
|
* Existing properties are preserved as `originalPropertyName`. Both properties
|
|
|
|
* will maintain the same enumerability & configurability.
|
2015-10-12 18:50:43 +00:00
|
|
|
*
|
2015-10-09 21:39:24 +00:00
|
|
|
* This allows you to undo the more aggressive polyfills, should you need to.
|
|
|
|
* For example, if you want to route network requests through DevTools (to trace
|
|
|
|
* them):
|
|
|
|
*
|
2015-10-12 18:50:43 +00:00
|
|
|
* global.XMLHttpRequest = global.originalXMLHttpRequest;
|
|
|
|
*
|
2015-10-09 21:39:24 +00:00
|
|
|
* For more info on that particular case, see:
|
|
|
|
* https://github.com/facebook/react-native/issues/934
|
|
|
|
*/
|
2016-04-29 23:14:55 +00:00
|
|
|
function polyfillGlobal(name, newValue, scope = global) {
|
2016-05-24 13:48:42 +00:00
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(scope, name);
|
|
|
|
if (descriptor) {
|
2016-05-04 17:49:29 +00:00
|
|
|
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
|
2015-10-09 21:39:24 +00:00
|
|
|
Object.defineProperty(scope, backupName, {...descriptor, value: scope[name]});
|
|
|
|
}
|
2015-10-12 18:50:43 +00:00
|
|
|
|
2016-05-24 13:48:42 +00:00
|
|
|
const {enumerable, writable} = descriptor || {};
|
|
|
|
|
|
|
|
// jest for some bad reasons runs the polyfill code multiple times. In jest
|
|
|
|
// environment, XmlHttpRequest doesn't exist so getOwnPropertyDescriptor
|
|
|
|
// returns undefined and defineProperty default for writable is false.
|
|
|
|
// Therefore, the second time it runs, defineProperty will fatal :(
|
|
|
|
|
|
|
|
Object.defineProperty(scope, name, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: enumerable !== false,
|
|
|
|
writable: writable !== false,
|
|
|
|
value: newValue,
|
|
|
|
});
|
2015-10-09 21:39:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 23:14:55 +00:00
|
|
|
function polyfillLazyGlobal(name, valueFn, scope = global) {
|
2016-05-24 13:48:42 +00:00
|
|
|
const descriptor = getPropertyDescriptor(scope, name);
|
|
|
|
if (descriptor) {
|
2016-04-27 16:56:48 +00:00
|
|
|
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
|
2016-05-24 13:48:42 +00:00
|
|
|
Object.defineProperty(scope, backupName, descriptor);
|
2016-04-27 16:56:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 13:48:42 +00:00
|
|
|
const {enumerable, writable} = descriptor || {};
|
2016-04-27 16:56:48 +00:00
|
|
|
Object.defineProperty(scope, name, {
|
|
|
|
configurable: true,
|
2016-05-24 13:48:42 +00:00
|
|
|
enumerable: enumerable !== false,
|
2016-04-27 16:56:48 +00:00
|
|
|
get() {
|
2016-04-28 18:22:37 +00:00
|
|
|
return (this[name] = valueFn());
|
2016-04-27 16:56:48 +00:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
configurable: true,
|
2016-05-24 13:48:42 +00:00
|
|
|
enumerable: enumerable !== false,
|
|
|
|
writable: writable !== false,
|
|
|
|
value,
|
2016-04-27 16:56:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-21 15:22:20 +00:00
|
|
|
/**
|
|
|
|
* Polyfill a module if it is not already defined in `scope`.
|
|
|
|
*/
|
2016-04-29 23:14:55 +00:00
|
|
|
function polyfillIfNeeded(name, polyfill, scope = global, descriptor = {}) {
|
2016-01-21 15:22:20 +00:00
|
|
|
if (scope[name] === undefined) {
|
|
|
|
Object.defineProperty(scope, name, {...descriptor, value: polyfill});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
function setUpErrorHandler() {
|
2015-11-09 17:12:19 +00:00
|
|
|
if (global.__fbDisableExceptionsManager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
function handleError(e, isFatal) {
|
|
|
|
try {
|
|
|
|
require('ExceptionsManager').handleException(e, isFatal);
|
2016-01-21 15:22:20 +00:00
|
|
|
} catch (ee) {
|
2015-11-05 20:20:15 +00:00
|
|
|
console.log('Failed to print error: ', ee.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:49:29 +00:00
|
|
|
const ErrorUtils = require('ErrorUtils');
|
2015-06-03 00:51:36 +00:00
|
|
|
ErrorUtils.setGlobalHandler(handleError);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a set of window environment wrappers that ensure that the
|
|
|
|
* BatchedBridge is flushed after each tick. In both the case of the
|
2015-03-17 10:08:46 +00:00
|
|
|
* `UIWebView` based `RCTJavaScriptCaller` and `RCTContextCaller`, we
|
2015-02-20 04:10:52 +00:00
|
|
|
* implement our own custom timing bridge that should be immune to
|
|
|
|
* unexplainably dropped timing signals.
|
|
|
|
*/
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpTimers() {
|
2016-04-27 16:56:48 +00:00
|
|
|
const defineLazyTimer = (name) => {
|
|
|
|
polyfillLazyGlobal(name, () => require('JSTimers')[name]);
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
2016-04-27 16:56:48 +00:00
|
|
|
defineLazyTimer('setTimeout');
|
|
|
|
defineLazyTimer('setInterval');
|
|
|
|
defineLazyTimer('setImmediate');
|
|
|
|
defineLazyTimer('clearTimeout');
|
|
|
|
defineLazyTimer('clearInterval');
|
|
|
|
defineLazyTimer('clearImmediate');
|
|
|
|
defineLazyTimer('requestAnimationFrame');
|
|
|
|
defineLazyTimer('cancelAnimationFrame');
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpAlert() {
|
2016-04-29 23:14:55 +00:00
|
|
|
if (!global.alert) {
|
|
|
|
global.alert = function(text) {
|
2016-01-08 18:11:46 +00:00
|
|
|
// Require Alert on demand. Requiring it too early can lead to issues
|
|
|
|
// with things like Platform not being fully initialized.
|
|
|
|
require('Alert').alert('Alert', '' + text);
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpPromise() {
|
2015-02-20 04:10:52 +00:00
|
|
|
// The native Promise implementation throws the following error:
|
|
|
|
// ERROR: Event loop not supported.
|
2016-04-27 16:56:48 +00:00
|
|
|
polyfillLazyGlobal('Promise', () => require('Promise'));
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpXHR() {
|
2015-02-20 04:10:52 +00:00
|
|
|
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
|
|
|
|
// let you fetch anything from the internet
|
2016-04-27 16:56:48 +00:00
|
|
|
polyfillLazyGlobal('XMLHttpRequest', () => require('XMLHttpRequest'));
|
|
|
|
polyfillLazyGlobal('FormData', () => require('FormData'));
|
2015-05-07 19:29:36 +00:00
|
|
|
|
2016-04-27 16:56:48 +00:00
|
|
|
polyfillLazyGlobal('fetch', () => require('fetch').fetch);
|
|
|
|
polyfillLazyGlobal('Headers', () => require('fetch').Headers);
|
|
|
|
polyfillLazyGlobal('Request', () => require('fetch').Request);
|
|
|
|
polyfillLazyGlobal('Response', () => require('fetch').Response);
|
2016-05-04 17:49:29 +00:00
|
|
|
|
|
|
|
polyfillLazyGlobal('WebSocket', () => require('WebSocket'));
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpGeolocation() {
|
2016-04-29 23:14:55 +00:00
|
|
|
polyfillIfNeeded('navigator', {}, global, {
|
2016-01-21 15:22:20 +00:00
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
2016-05-04 17:49:29 +00:00
|
|
|
Object.defineProperty(global.navigator, 'product', {value: 'ReactNative'});
|
|
|
|
|
2016-04-29 23:14:55 +00:00
|
|
|
polyfillLazyGlobal('geolocation', () => require('Geolocation'), global.navigator);
|
2015-03-01 04:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 16:26:46 +00:00
|
|
|
function setUpMapAndSet() {
|
2016-04-27 16:56:48 +00:00
|
|
|
// We can't make these lazy as Map checks the global.Map to see if it's
|
|
|
|
// available but in our case it'll be a lazy getter.
|
2016-01-20 16:26:46 +00:00
|
|
|
polyfillGlobal('Map', require('Map'));
|
|
|
|
polyfillGlobal('Set', require('Set'));
|
|
|
|
}
|
|
|
|
|
2015-11-05 16:16:05 +00:00
|
|
|
function setUpDevTools() {
|
2016-05-04 17:49:29 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
// not when debugging in chrome
|
2015-11-18 17:11:09 +00:00
|
|
|
if (!window.document && require('Platform').OS === 'ios') {
|
2016-05-04 17:49:29 +00:00
|
|
|
const setupDevtools = require('setupDevtools');
|
2015-11-18 17:11:09 +00:00
|
|
|
setupDevtools();
|
|
|
|
}
|
2016-05-04 17:49:29 +00:00
|
|
|
|
|
|
|
require('RCTDebugComponentOwnership');
|
|
|
|
require('react-transform-hmr');
|
2015-11-05 16:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 13:48:42 +00:00
|
|
|
function getPropertyDescriptor(object, name) {
|
|
|
|
while (object) {
|
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(object, name);
|
|
|
|
if (descriptor) {
|
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
object = Object.getPrototypeOf(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 18:22:37 +00:00
|
|
|
setUpProcess();
|
2016-05-24 19:02:31 +00:00
|
|
|
setUpProfile();
|
2015-11-05 20:20:15 +00:00
|
|
|
setUpConsole();
|
2015-05-14 16:28:09 +00:00
|
|
|
setUpTimers();
|
|
|
|
setUpAlert();
|
|
|
|
setUpPromise();
|
2015-11-05 20:20:15 +00:00
|
|
|
setUpErrorHandler();
|
2015-05-14 16:28:09 +00:00
|
|
|
setUpXHR();
|
|
|
|
setUpGeolocation();
|
2016-01-20 16:26:46 +00:00
|
|
|
setUpMapAndSet();
|
2015-11-05 16:16:05 +00:00
|
|
|
setUpDevTools();
|
2015-10-22 22:46:45 +00:00
|
|
|
|
|
|
|
// Just to make sure the JS gets packaged up. Wait until the JS environment has
|
|
|
|
// been initialized before requiring them.
|
|
|
|
require('RCTDeviceEventEmitter');
|
2016-03-15 10:44:18 +00:00
|
|
|
require('RCTNativeAppEventEmitter');
|
2015-10-22 22:46:45 +00:00
|
|
|
require('PerformanceLogger');
|