-
Notifications
You must be signed in to change notification settings - Fork 16
/
weakmap-polyfill.js
149 lines (119 loc) · 3.48 KB
/
weakmap-polyfill.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*!
* weakmap-polyfill v2.0.4 - ECMAScript6 WeakMap polyfill
* https://github.com/polygonplanet/weakmap-polyfill
* Copyright (c) 2015-2021 polygonplanet <[email protected]>
* @license MIT
*/
(function(self) {
'use strict';
if (self.WeakMap) {
return;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
var hasDefine = Object.defineProperty && (function() {
try {
// Avoid IE8's broken Object.defineProperty
return Object.defineProperty({}, 'x', { value: 1 }).x === 1;
} catch (e) {}
})();
var defineProperty = function(object, name, value) {
if (hasDefine) {
Object.defineProperty(object, name, {
configurable: true,
writable: true,
value: value
});
} else {
object[name] = value;
}
};
self.WeakMap = (function() {
// ECMA-262 23.3 WeakMap Objects
function WeakMap() {
if (this === void 0) {
throw new TypeError("Constructor WeakMap requires 'new'");
}
defineProperty(this, '_id', genId('_WeakMap'));
// ECMA-262 23.3.1.1 WeakMap([iterable])
if (arguments.length > 0) {
// Currently, WeakMap `iterable` argument is not supported
throw new TypeError('WeakMap iterable is not supported');
}
}
// ECMA-262 23.3.3.2 WeakMap.prototype.delete(key)
defineProperty(WeakMap.prototype, 'delete', function(key) {
checkInstance(this, 'delete');
if (!isObject(key)) {
return false;
}
var entry = key[this._id];
if (entry && entry[0] === key) {
delete key[this._id];
return true;
}
return false;
});
// ECMA-262 23.3.3.3 WeakMap.prototype.get(key)
defineProperty(WeakMap.prototype, 'get', function(key) {
checkInstance(this, 'get');
if (!isObject(key)) {
return void 0;
}
var entry = key[this._id];
if (entry && entry[0] === key) {
return entry[1];
}
return void 0;
});
// ECMA-262 23.3.3.4 WeakMap.prototype.has(key)
defineProperty(WeakMap.prototype, 'has', function(key) {
checkInstance(this, 'has');
if (!isObject(key)) {
return false;
}
var entry = key[this._id];
if (entry && entry[0] === key) {
return true;
}
return false;
});
// ECMA-262 23.3.3.5 WeakMap.prototype.set(key, value)
defineProperty(WeakMap.prototype, 'set', function(key, value) {
checkInstance(this, 'set');
if (!isObject(key)) {
throw new TypeError('Invalid value used as weak map key');
}
var entry = key[this._id];
if (entry && entry[0] === key) {
entry[1] = value;
return this;
}
defineProperty(key, this._id, [key, value]);
return this;
});
function checkInstance(x, methodName) {
if (!isObject(x) || !hasOwnProperty.call(x, '_id')) {
throw new TypeError(
methodName + ' method called on incompatible receiver ' +
typeof x
);
}
}
function genId(prefix) {
return prefix + '_' + rand() + '.' + rand();
}
function rand() {
return Math.random().toString().substring(2);
}
defineProperty(WeakMap, '_polyfill', true);
return WeakMap;
})();
function isObject(x) {
return Object(x) === x;
}
})(
typeof globalThis !== 'undefined' ? globalThis :
typeof self !== 'undefined' ? self :
typeof window !== 'undefined' ? window :
typeof global !== 'undefined' ? global : this
);