-
Notifications
You must be signed in to change notification settings - Fork 7
/
polyfill.js
executable file
·174 lines (159 loc) · 3.98 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
// Note: this polyfill is currently limited from working completely across
// platforms
const kSymbol = Symbol('Symbol.builtin');
const kBuiltin = Symbol('[[Builtin]]');
const { isAsyncFunction } = process.binding('util');
Object.defineProperty(Symbol, 'builtin', {
enumerable: false,
configurable: false,
value: kSymbol
});
function GetBuiltinValue(value) {
if (value === null)
return;
const fn = value[kSymbol];
if (fn === undefined) return;
const val = fn.call(value);
if (val === undefined) return;
return `${val}`;
}
function GetOwnBuiltinValue(value) {
if (value === null)
return;
if (!Object.getOwnPropertyDescriptor(value, kSymbol))
return;
return GetBuiltinValue(value);
}
function builtin() {
if (typeof this !== 'object' && typeof this !== 'function')
throw new TypeError('Method invoked on a value that is not an object');
return this[kBuiltin];
}
const AsyncFunction = (async () => {}).constructor;
const GeneratorFunction = (function*() {}).constructor;
const labels = [
Array, 'Array',
ArrayBuffer, 'ArrayBuffer',
AsyncFunction, 'AsyncFunction',
Boolean, 'Boolean',
DataView, 'DataView',
Date, 'Date',
Error, 'Error',
EvalError, 'EvalError',
Float32Array, 'Float32Array',
Float64Array, 'Float64Array',
Function, 'function',
GeneratorFunction, 'GeneratorFunction',
Int8Array, 'Int8Array',
Int16Array, 'Int16Array',
Int32Array, 'Int32Array',
JSON, 'JSON',
Map, 'Map',
Math, 'Math',
Number, 'Number',
Object, 'object',
Promise, 'Promise',
Proxy, 'Proxy',
RangeError, 'RangeError',
ReferenceError, 'ReferenceError',
Reflect, 'Reflect',
RegExp, 'RegExp',
Set, 'Set',
String, 'String',
Symbol, 'Symbol',
SyntaxError, 'SyntaxError',
TypeError, 'TypeError',
Uint8Array, 'Uint8Array',
Uint8ClampedArray, 'Uint8ClampedArray',
Uint16Array, 'Uint16Array',
Uint32Array, 'Uint32Array',
URIError, 'URIError',
WeakMap, 'WeakMap',
WeakSet, 'WeakSet'
];
for (let n = 0; n < labels.length; n = n + 2) {
Object.defineProperties(labels[n], {
[kBuiltin]: {
enumerable: false,
configurable: false,
value: labels[n + 1]
},
[kSymbol]: {
enumerable: false,
configurable: true,
writable: true,
value: builtin
}});
}
if (typeof Atomics !== 'undefined') {
Object.defineProperties(Atomics, {
[kBuiltin]: {
enumerable: false,
configurable: false,
value: 'Atomics'
},
[kSymbol]: {
enumerable: false,
configurable: true,
writable: true,
value: builtin
}});
}
if (typeof SharedArrayBuffer !== 'undefined') {
Object.defineProperties(SharedArrayBuffer, {
[kBuiltin]: {
enumerable: false,
configurable: false,
value: 'SharedArrayBuffer'
},
[kSymbol]: {
enumerable: false,
configurable: true,
writable: true,
value: builtin
}});
}
function _is(value1, value2) {
if (typeof value1 !== 'object' && typeof value1 !== 'function')
return false;
const v1 = GetOwnBuiltinValue(value1);
if (v1 === undefined)
return false;
if (value2 === undefined)
return false;
if (typeof value2 !== 'object' && typeof value1 !== 'function')
return false;
const v2 = GetOwnBuiltinValue(value2);
return v1 === v2;
}
function _typeof(value) {
if (typeof value === 'object' || typeof value === 'function') {
const ctor = value.constructor;
if (ctor !== undefined) {
const val = GetBuiltinValue(ctor);
if (val !== undefined)
return val;
}
}
return typeof value;
}
Object.defineProperty(builtin, 'name', { value: '@@builtin' });
Object.defineProperty(_is, 'name', { value: 'is' });
Object.defineProperty(_typeof, 'name', { value: 'typeof' });
const Builtin = {};
Object.defineProperties(Builtin, {
'is': {
enumerable: false,
configurable: true,
writable: true,
value: _is
},
'typeOf': {
enumerable: false,
configurable: true,
writable: true,
value: _typeof
}
});
module.exports = Builtin;