From dad39eb502d44ab656d9efbe83d9361534ccbc28 Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Tue, 26 Apr 2016 11:23:47 -0700 Subject: [PATCH] Move `Number` polyfills into the `/polyfills/` directory Reviewed By: vjeux Differential Revision: D3223317 fb-gh-sync-id: a49a14f217b27d6542b65c4780c557e73da2443f fbshipit-source-id: a49a14f217b27d6542b65c4780c557e73da2443f --- .../InitializeJavaScriptAppEngine.js | 7 ---- .../src/Resolver/polyfills/Number.es6.js | 15 ++++++++ .../{Number.es6.js => Number.es6-test.js} | 34 +++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) rename packager/react-packager/src/Resolver/polyfills/__tests__/{Number.es6.js => Number.es6-test.js} (52%) diff --git a/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js b/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js index a78d93fb3..b6332da47 100644 --- a/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js +++ b/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js @@ -184,12 +184,6 @@ function setUpProcessEnv() { } } -function setUpNumber() { - polyfillIfNeeded('EPSILON', Math.pow(2, -52), Number); - polyfillIfNeeded('MAX_SAFE_INTEGER', Math.pow(2, 53) - 1, Number); - polyfillIfNeeded('MIN_SAFE_INTEGER', -(Math.pow(2, 53) - 1), Number); -} - function setUpDevTools() { // not when debugging in chrome if (__DEV__) { // TODO(9123099) Strip `__DEV__ &&` @@ -212,7 +206,6 @@ setUpMapAndSet(); setUpProduct(); setUpWebSockets(); setUpProfile(); -setUpNumber(); setUpDevTools(); // Just to make sure the JS gets packaged up. Wait until the JS environment has diff --git a/packager/react-packager/src/Resolver/polyfills/Number.es6.js b/packager/react-packager/src/Resolver/polyfills/Number.es6.js index 333bfb946..abd59bbaa 100644 --- a/packager/react-packager/src/Resolver/polyfills/Number.es6.js +++ b/packager/react-packager/src/Resolver/polyfills/Number.es6.js @@ -10,6 +10,21 @@ * @polyfill */ +if (Number.EPSILON === undefined) { + Object.defineProperty(Number, 'EPSILON', { + value: Math.pow(2, -52), + }); +} +if (Number.MAX_SAFE_INTEGER === undefined) { + Object.defineProperty(Number, 'MAX_SAFE_INTEGER', { + value: Math.pow(2, 53) - 1, + }); +} +if (Number.MIN_SAFE_INTEGER === undefined) { + Object.defineProperty(Number, 'MIN_SAFE_INTEGER', { + value: -(Math.pow(2, 53) - 1), + }); +} if (!Number.isNaN) { // https://github.com/dherman/tc39-codex-wiki/blob/master/data/es6/number/index.md#polyfill-for-numberisnan const globalIsNaN = global.isNaN; diff --git a/packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6.js b/packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6-test.js similarity index 52% rename from packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6.js rename to packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6-test.js index 44f93d0c1..d91a3aa54 100644 --- a/packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6.js +++ b/packager/react-packager/src/Resolver/polyfills/__tests__/Number.es6-test.js @@ -12,6 +12,40 @@ jest.autoMockOff(); describe('Number (ES6)', () => { + describe('EPSILON', () => { + beforeEach(() => { + delete Number.EPSILON; + jest.resetModuleRegistry(); + require('../Number.es6'); + }); + it('is 2^(-52)', () => { + expect(Number.EPSILON).toBe(Math.pow(2, -52)); + }); + it('can be used to test equality', () => { + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON#Testing_equality + expect(Number.EPSILON).toBeGreaterThan(Math.abs(0.2 - 0.3 + 0.1)); + }); + }); + describe('MAX_SAFE_INTEGER', () => { + beforeEach(() => { + delete Number.MAX_SAFE_INTEGER; + jest.resetModuleRegistry(); + require('../Number.es6'); + }); + it('is 2^53 - 1', () => { + expect(Number.MAX_SAFE_INTEGER).toBe(Math.pow(2, 53) - 1); + }); + }); + describe('MIN_SAFE_INTEGER', () => { + beforeEach(() => { + delete Number.MIN_SAFE_INTEGER; + jest.resetModuleRegistry(); + require('../Number.es6'); + }); + it('is -(2^53 - 1)', () => { + expect(Number.MIN_SAFE_INTEGER).toBe(-(Math.pow(2, 53) - 1)); + }); + }); describe('isNaN()', () => { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Examples beforeEach(() => {