Skip to content

Commit aa18f01

Browse files
committedDec 13, 2023
Use SameValueZero in Map polyfill
1 parent 63083c4 commit aa18f01

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed
 

‎Reflect.js

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Reflect.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Reflect.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,10 @@ namespace Reflect {
15041504
}
15051505
}
15061506

1507+
function SameValueZero(x: any, y: any) {
1508+
return x === y || x !== x && y !== y;
1509+
}
1510+
15071511
// 7.3 Operations on Objects
15081512
// https://tc39.github.io/ecma262/#sec-operations-on-objects
15091513

@@ -1659,7 +1663,7 @@ namespace Reflect {
16591663
}
16601664
this._keys.length--;
16611665
this._values.length--;
1662-
if (key === this._cacheKey) {
1666+
if (SameValueZero(key, this._cacheKey)) {
16631667
this._cacheKey = cacheSentinel;
16641668
this._cacheIndex = -2;
16651669
}
@@ -1679,8 +1683,14 @@ namespace Reflect {
16791683
"@@iterator"() { return this.entries(); }
16801684
[iteratorSymbol]() { return this.entries(); }
16811685
private _find(key: K, insert?: boolean): number {
1682-
if (this._cacheKey !== key) {
1683-
this._cacheIndex = this._keys.indexOf(this._cacheKey = key);
1686+
if (!SameValueZero(this._cacheKey, key)) {
1687+
this._cacheIndex = -1;
1688+
for (let i = 0; i < this._keys.length; i++) {
1689+
if (SameValueZero(this._keys[i], key)) {
1690+
this._cacheIndex = i;
1691+
break;
1692+
}
1693+
}
16841694
}
16851695
if (this._cacheIndex < 0 && insert) {
16861696
this._cacheIndex = this._keys.length;
@@ -1714,8 +1724,8 @@ namespace Reflect {
17141724
delete(value: T): boolean { return this._map.delete(value); }
17151725
clear(): void { this._map.clear(); }
17161726
keys() { return this._map.keys(); }
1717-
values() { return this._map.values(); }
1718-
entries() { return this._map.entries(); }
1727+
values() { return this._map.keys(); }
1728+
entries() { return this._map.keys(); }
17191729
"@@iterator"() { return this.keys(); }
17201730
[iteratorSymbol]() { return this.keys(); }
17211731
};

‎test/reflect-getmetadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ describe("Reflect.getMetadata", () => {
4343
assert.equal(result, "value");
4444
});
4545

46+
it("WhenKeyIsNaN", () => {
47+
let obj = {};
48+
Reflect.defineMetadata(NaN, "value", obj, "name");
49+
let result = Reflect.getMetadata(NaN, obj, "name");
50+
assert.equal(result, "value");
51+
});
52+
4653
it("WithTargetKeyWhenDefinedOnPrototype", () => {
4754
let prototype = {};
4855
let obj = Object.create(prototype);

0 commit comments

Comments
 (0)
Please sign in to comment.