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 */
|
|
|
|
|
2015-08-04 12:23:31 +00:00
|
|
|
require('regenerator/runtime');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (typeof GLOBAL === 'undefined') {
|
|
|
|
GLOBAL = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
window = GLOBAL;
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
function setUpConsole() {
|
|
|
|
// ExceptionsManager transitively requires Promise so we install it after
|
|
|
|
var ExceptionsManager = require('ExceptionsManager');
|
|
|
|
ExceptionsManager.installConsoleErrorReporter();
|
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
|
|
|
|
*/
|
2015-12-01 15:24:14 +00:00
|
|
|
function polyfillGlobal(name, newValue, scope = GLOBAL) {
|
2015-10-12 18:50:43 +00:00
|
|
|
var descriptor = Object.getOwnPropertyDescriptor(scope, name) || {
|
|
|
|
// 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 :(
|
|
|
|
writable: true,
|
|
|
|
};
|
2015-10-09 21:39:24 +00:00
|
|
|
|
|
|
|
if (scope[name] !== undefined) {
|
|
|
|
var backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
|
|
|
|
Object.defineProperty(scope, backupName, {...descriptor, value: scope[name]});
|
|
|
|
}
|
2015-10-12 18:50:43 +00:00
|
|
|
|
2015-10-09 21:39:24 +00:00
|
|
|
Object.defineProperty(scope, name, {...descriptor, value: newValue});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
} catch(ee) {
|
|
|
|
console.log('Failed to print error: ', ee.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:02:42 +00:00
|
|
|
var ErrorUtils = require('ErrorUtils');
|
2015-06-03 00:51:36 +00:00
|
|
|
ErrorUtils.setGlobalHandler(handleError);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:13:24 +00:00
|
|
|
function setUpFlowChecker() {
|
2015-08-17 09:05:29 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
var checkFlowAtRuntime = require('checkFlowAtRuntime');
|
|
|
|
checkFlowAtRuntime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2015-03-04 02:02:42 +00:00
|
|
|
var JSTimers = require('JSTimers');
|
2015-02-20 04:10:52 +00:00
|
|
|
GLOBAL.setTimeout = JSTimers.setTimeout;
|
|
|
|
GLOBAL.setInterval = JSTimers.setInterval;
|
|
|
|
GLOBAL.setImmediate = JSTimers.setImmediate;
|
|
|
|
GLOBAL.clearTimeout = JSTimers.clearTimeout;
|
|
|
|
GLOBAL.clearInterval = JSTimers.clearInterval;
|
|
|
|
GLOBAL.clearImmediate = JSTimers.clearImmediate;
|
|
|
|
GLOBAL.cancelAnimationFrame = JSTimers.clearInterval;
|
|
|
|
GLOBAL.requestAnimationFrame = function(cb) {
|
|
|
|
/*requestAnimationFrame() { [native code] };*/ // Trick scroller library
|
|
|
|
return JSTimers.requestAnimationFrame(cb); // into thinking it's native
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpAlert() {
|
2015-03-18 05:22:03 +00:00
|
|
|
var RCTAlertManager = require('NativeModules').AlertManager;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (!GLOBAL.alert) {
|
|
|
|
GLOBAL.alert = function(text) {
|
|
|
|
var alertOpts = {
|
|
|
|
title: 'Alert',
|
|
|
|
message: '' + text,
|
2015-06-05 22:23:30 +00:00
|
|
|
buttons: [{'cancel': 'OK'}],
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
2015-08-10 20:09:38 +00:00
|
|
|
RCTAlertManager.alertWithArgs(alertOpts, function () {});
|
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.
|
|
|
|
GLOBAL.Promise = require('Promise');
|
|
|
|
}
|
|
|
|
|
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
|
2015-10-09 21:39:24 +00:00
|
|
|
polyfillGlobal('XMLHttpRequest', require('XMLHttpRequest'));
|
|
|
|
polyfillGlobal('FormData', require('FormData'));
|
2015-05-07 19:29:36 +00:00
|
|
|
|
|
|
|
var fetchPolyfill = require('fetch');
|
2015-10-09 21:39:24 +00:00
|
|
|
polyfillGlobal('fetch', fetchPolyfill.fetch);
|
|
|
|
polyfillGlobal('Headers', fetchPolyfill.Headers);
|
|
|
|
polyfillGlobal('Request', fetchPolyfill.Request);
|
|
|
|
polyfillGlobal('Response', fetchPolyfill.Response);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpGeolocation() {
|
2015-03-01 04:46:42 +00:00
|
|
|
GLOBAL.navigator = GLOBAL.navigator || {};
|
2015-10-09 21:39:24 +00:00
|
|
|
polyfillGlobal('geolocation', require('Geolocation'), GLOBAL.navigator);
|
2015-03-01 04:46:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 20:43:39 +00:00
|
|
|
function setUpProduct() {
|
|
|
|
Object.defineProperty(GLOBAL.navigator, 'product', {value: 'ReactNative'});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpWebSockets() {
|
2015-10-09 21:39:24 +00:00
|
|
|
polyfillGlobal('WebSocket', require('WebSocket'));
|
2015-05-14 16:28:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:13:24 +00:00
|
|
|
function setUpProfile() {
|
2015-10-01 21:08:13 +00:00
|
|
|
if (__DEV__) {
|
2015-12-11 11:49:15 +00:00
|
|
|
var Systrace = require('Systrace');
|
|
|
|
Systrace.swizzleReactPerf();
|
2015-10-01 21:08:13 +00:00
|
|
|
}
|
2015-05-14 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 02:31:05 +00:00
|
|
|
function setUpProcessEnv() {
|
2015-08-17 22:41:47 +00:00
|
|
|
GLOBAL.process = GLOBAL.process || {};
|
2015-08-27 02:57:21 +00:00
|
|
|
GLOBAL.process.env = GLOBAL.process.env || {};
|
|
|
|
if (!GLOBAL.process.env.NODE_ENV) {
|
|
|
|
GLOBAL.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
|
|
|
|
}
|
2015-08-11 02:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:13:24 +00:00
|
|
|
function setUpNumber() {
|
|
|
|
Number.EPSILON = Number.EPSILON || Math.pow(2, -52);
|
|
|
|
Number.MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
|
|
|
|
Number.MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
|
|
|
|
}
|
|
|
|
|
2015-11-05 16:16:05 +00:00
|
|
|
function setUpDevTools() {
|
|
|
|
// not when debugging in chrome
|
2015-11-18 17:11:09 +00:00
|
|
|
if (__DEV__) { // TODO(9123099) Strip `__DEV__ &&`
|
|
|
|
if (!window.document && require('Platform').OS === 'ios') {
|
|
|
|
var setupDevtools = require('setupDevtools');
|
|
|
|
setupDevtools();
|
|
|
|
}
|
2015-11-05 16:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 22:46:45 +00:00
|
|
|
setUpProcessEnv();
|
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();
|
2015-11-25 20:43:39 +00:00
|
|
|
setUpProduct();
|
2015-05-14 16:28:09 +00:00
|
|
|
setUpWebSockets();
|
2015-10-01 21:13:24 +00:00
|
|
|
setUpProfile();
|
|
|
|
setUpFlowChecker();
|
|
|
|
setUpNumber();
|
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.
|
2015-11-18 17:11:09 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
require('RCTDebugComponentOwnership');
|
|
|
|
}
|
2015-10-22 22:46:45 +00:00
|
|
|
require('RCTDeviceEventEmitter');
|
|
|
|
require('PerformanceLogger');
|