-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shim.js
92 lines (82 loc) · 2.29 KB
/
shim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var define = require('define-properties');
var globalThis = require('globalthis')();
var SLOT = require('internal-slot');
var getPolyfill = require('./polyfill');
var support = require('./lib/support');
var addIterableToMap = require('./lib/map-helpers').addIterableToMap;
var OrdinarySetPrototypeOf = require('es-abstract/2024/OrdinarySetPrototypeOf');
var Call = require('es-abstract/2024/Call');
var force = function () {
return true;
};
var replaceGlobal = function (MapShim) {
define(globalThis, { Map: MapShim }, { Map: force });
return MapShim;
};
// eslint-disable-next-line max-lines-per-function
module.exports = function shimMap() {
if (typeof Map !== 'function' || support.mapHasOldFirefoxInterface() || !support.mapIterationFinishes()) {
return replaceGlobal(getPolyfill());
}
var OrigMap = Map;
var OrigMap$prototype = OrigMap.prototype;
var OrigMap$get = OrigMap$prototype.get;
var OrigMap$has = OrigMap$prototype.has;
var OrigMap$set = OrigMap$prototype.set;
if (!support.mapCompliantConstructor()) {
var MapShim = function Map() {
if (!(this instanceof MapShim)) {
throw new TypeError('Constructor Map requires "new"');
}
if (this && SLOT.has(this, '[[MapCompliantConstructorShim]]')) {
throw new TypeError('Bad construction');
}
var m = new OrigMap();
SLOT.set(m, '[[MapCompliantConstructorShim]]', true);
if (arguments.length > 0) {
addIterableToMap(m, arguments[0]);
}
delete m.constructor;
OrdinarySetPrototypeOf(m, MapShim.prototype);
return m;
};
MapShim.prototype = OrigMap$prototype;
define(
MapShim.prototype,
{ constructor: MapShim },
{ constructor: force }
);
replaceGlobal(MapShim);
}
if (!support.mapUsesSameValueZero()) {
define(Map.prototype, {
get: function get(k) {
return Call(OrigMap$get, this, [k === 0 ? 0 : k]);
},
has: function has(k) {
return Call(OrigMap$has, this, [k === 0 ? 0 : k]);
},
set: function set(k, v) {
Call(OrigMap$set, this, [k === 0 ? 0 : k, v]);
return this;
}
}, {
get: force,
has: force,
set: force
});
} else if (!support.mapSupportsChaining()) {
define(
Map.prototype,
{
set: function set(k, v) {
Call(OrigMap$set, this, [k, v]);
return this;
}
},
{ set: force }
);
}
return globalThis.Map;
};