-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Critical improvements for Map and Set polyfills.
Summary: Pull Request resolved: #21492 Differential Revision: D10288094 Pulled By: cpojer fbshipit-source-id: b1ca7344dda3043553be6945614b439a0f42a46a
- Loading branch information
1 parent
dd8f5de
commit 90850ca
Showing
4 changed files
with
186 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* 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); | ||
jest.unmock('_wrapObjectFreezeAndFriends'); | ||
require('_wrapObjectFreezeAndFriends'); | ||
} | ||
|
||
function cleanup() { | ||
Object.assign(Object, {freeze, seal, preventExtensions}); | ||
} | ||
|
||
describe('Map polyfill', () => { | ||
setup(); | ||
|
||
const Map = require('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('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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 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. | ||
* | ||
* @author Ben Newman (@benjamn) <[email protected]> | ||
* @flow | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let testMap; // Initialized lazily. | ||
function getTestMap() { | ||
return testMap || (testMap = new (require('./Map'))()); | ||
} | ||
|
||
// Wrap Object.{freeze,seal,preventExtensions} so each function adds its | ||
// argument to a Map first, which gives our ./Map.js polyfill a chance to | ||
// tag the object before it becomes non-extensible. | ||
["freeze", "seal", "preventExtensions"].forEach(name => { | ||
const method = Object[name]; | ||
if (typeof method === "function") { | ||
(Object: any)[name] = function (obj) { | ||
try { | ||
// If .set succeeds, also call .delete to avoid leaking memory. | ||
getTestMap().set(obj, obj).delete(obj); | ||
} finally { | ||
// If .set fails, the exception will be silently swallowed | ||
// by this return-from-finally statement, and the method will | ||
// behave exactly as it did before it was wrapped. | ||
return method.call(Object, obj); | ||
} | ||
}; | ||
} | ||
}); |