Skip to content

Commit

Permalink
@build-break Back out "[RN] Remove Map/Set from RN Open Source"
Browse files Browse the repository at this point in the history
Summary: Backing out D14786123 as it's causing failures on fbandroid/stable.

Differential Revision:
D15693250
Ninja: sheriff

fbshipit-source-id: 526054d4f0dab2a811f2328540e7418ece9810b1
  • Loading branch information
foghina authored and facebook-github-bot committed Jun 6, 2019
1 parent bdc530b commit 63bc4b4
Show file tree
Hide file tree
Showing 8 changed files with 1,060 additions and 0 deletions.
1 change: 1 addition & 0 deletions Libraries/Core/InitializeCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
const start = Date.now();

require('./setUpGlobals');
require('./polyfillES6Collections');
require('./setUpSystrace');
require('./setUpErrorHandling');
require('./polyfillPromise');
Expand Down
102 changes: 102 additions & 0 deletions Libraries/Core/__tests__/MapAndSetPolyfills-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';

// Save these methods so that we can restore them afterward.
const {freeze, seal, preventExtensions} = Object;

function setup() {
jest.setMock('../../vendor/core/_shouldPolyfillES6Collection', () => true);
}

function cleanup() {
Object.assign(Object, {freeze, seal, preventExtensions});
}

describe('Map polyfill', () => {
setup();

const Map = require('../../vendor/core/Map');

it('is not native', () => {
const getCode = Function.prototype.toString.call(Map.prototype.get);
expect(getCode).not.toContain('[native code]');
expect(getCode).toContain('getIndex');
});

it('should tolerate non-extensible object keys', () => {
const map = new Map();
const key = Object.create(null);
Object.freeze(key);
map.set(key, key);
expect(map.size).toBe(1);
expect(map.has(key)).toBe(true);
map.delete(key);
expect(map.size).toBe(0);
expect(map.has(key)).toBe(false);
});

it('should not get confused by prototypal inheritance', () => {
const map = new Map();
const proto = Object.create(null);
const base = Object.create(proto);
map.set(proto, proto);
expect(map.size).toBe(1);
expect(map.has(proto)).toBe(true);
expect(map.has(base)).toBe(false);
map.set(base, base);
expect(map.size).toBe(2);
expect(map.get(proto)).toBe(proto);
expect(map.get(base)).toBe(base);
});

afterAll(cleanup);
});

describe('Set polyfill', () => {
setup();

const Set = require('../../vendor/core/Set');

it('is not native', () => {
const addCode = Function.prototype.toString.call(Set.prototype.add);
expect(addCode).not.toContain('[native code]');
});

it('should tolerate non-extensible object elements', () => {
const set = new Set();
const elem = Object.create(null);
Object.freeze(elem);
set.add(elem);
expect(set.size).toBe(1);
expect(set.has(elem)).toBe(true);
set.add(elem);
expect(set.size).toBe(1);
set.delete(elem);
expect(set.size).toBe(0);
expect(set.has(elem)).toBe(false);
});

it('should not get confused by prototypal inheritance', () => {
const set = new Set();
const proto = Object.create(null);
const base = Object.create(proto);
set.add(proto);
expect(set.size).toBe(1);
expect(set.has(proto)).toBe(true);
expect(set.has(base)).toBe(false);
set.add(base);
expect(set.size).toBe(2);
expect(set.has(proto)).toBe(true);
expect(set.has(base)).toBe(true);
});

afterAll(cleanup);
});
27 changes: 27 additions & 0 deletions Libraries/Core/polyfillES6Collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';

const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');

/**
* Polyfill ES6 collections (Map and Set).
* If you don't need these polyfills, don't use InitializeCore; just directly
* require the modules you need from InitializeCore for setup.
*/
const _shouldPolyfillCollection = require('../vendor/core/_shouldPolyfillES6Collection');
if (_shouldPolyfillCollection('Map')) {
// $FlowFixMe: even in strict-local mode Flow expects Map to be Flow-typed
polyfillGlobal('Map', () => require('../vendor/core/Map'));
}
if (_shouldPolyfillCollection('Set')) {
// $FlowFixMe: even in strict-local mode Flow expects Set to be Flow-typed
polyfillGlobal('Set', () => require('../vendor/core/Set'));
}
Loading

0 comments on commit 63bc4b4

Please sign in to comment.