Repository navigation
Expand file tree
/
Copy pathruntime.js
More file actions
528 lines (415 loc) · 14.8 KB
/
Copy pathruntime.js
File metadata and controls
528 lines (415 loc) · 14.8 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* eslint-disable prefer-rest-params, no-var, comma-dangle, prefer-template, no-eval, prefer-spread, prefer-arrow-callback, no-invalid-this */
window.toStringBackup = window.toStringBackup || Function.prototype.toString;
(function() { //eslint-disable-line
if (window.nativePatchCalled) return;
function buildNativeless(nativeFn, newFn) {
nativeFn.__$nativeless$__ = newFn;
}
function buildObjectAssign() {
buildNativeless(Object.assign, function() {
var target = arguments[0];
var sources = [];
var argumentLength = arguments.length;
var i;
if (argumentLength > 1) {
for (i = 1; i < argumentLength; i++) {
sources[i] = formatTargetObject(arguments[i], false);
}
}
if (isProxy(target)) {
if (argumentLength === 1) return target;
for (i = 1; i < argumentLength; i++) {
var source = sources[i];
var keys = Object.keys(source);
for (var keyIndex = 0; keyIndex < keys.length; keyIndex++) {
var key = keys[keyIndex];
target.set(key, source[key]);
}
}
return target;
}
sources.unshift(target);
return Object.assign.apply(this, sources);
});
}
function buildObjectCreate() {
var backup = Object.create;
buildNativeless(Object.create, function(proto, propertiesObject) {
var argumentLength = arguments.length;
for (var i = 0; i < argumentLength; i++) {
if (isProxy(arguments[i])) {
/*
this is not implemented yet because of complexity.
example:
var obj = { a: 3 };
var proxyWithHandler = new Proxy(obj, { get(t, p) { return t[p]; } });
var proxyWithoutHandler = new Proxy(obj, {});
Object.keys(Object.create(proxyWithHandler).__proto__).length === 0;
Object.keys(Object.create(proxyWithoutHandler).__proto__).length === 1;
*/
throw TypeError('Cannot call Object.create with a proxy');
}
}
return backup.call(Object, proto, propertiesObject);
});
}
function buildSetPrototypeOf() {
var backup = Object.setPrototypeOf;
buildNativeless(Object.setPrototypeOf, function(obj, proto) {
if (isProxy(obj) || isProxy(proto)) {
throw '[babel-plugin-es5-proxy] \'Object.setPrototypeOf\' not implemented with proxy as arguments'; //eslint-disable-line
}
return backup.call(Object, obj, proto);
});
}
function buildConcat() {
var backup = Array.prototype.concat;
buildNativeless(Array.prototype.concat, function() { //eslint-disable-line
var target = isProxy(this) ? this.formatTarget() : this;
var argLen = arguments.length;
var args = [];
for (var argIndex = 0; argIndex < argLen; argIndex++) {
var arg = arguments[argIndex];
args[argIndex] = isProxy(arg) ? arg.formatTarget() : arg;
}
return backup.apply(target, args);
});
}
function buildDefineProperties() {
var backup = Object.defineProperties;
buildNativeless(Object.defineProperties, function(obj, props) { //eslint-disable-line
var args = [objectTarget(obj), formatTargetObject(props, true)];
return backup.apply(this, args);
});
}
function buildDefineProperty() {
var backup = Object.defineProperty;
buildNativeless(Object.defineProperty, function(obj, prop, descriptor) { //eslint-disable-line
var args = [objectTarget(obj), prop, formatTargetObject(descriptor, true)];
return backup.apply(this, args);
});
}
function buildPop() {
var backup = Array.prototype.pop;
buildNativeless(Array.prototype.pop, function() { //eslint-disable-line
if (isProxy(this)) {
var length = this.get('length');
var value = this.get(length - 1);
backup.apply(this.target(), arguments);
return value;
}
return backup.apply(this, arguments);
});
}
function buildShift() {
var backup = Array.prototype.shift;
buildNativeless(Array.prototype.shift, function() { //eslint-disable-line
if (isProxy(this)) {
var value = this.get(0);
backup.apply(this.target(), arguments);
return value;
}
return backup.apply(this, arguments);
});
}
function buildReverse() {
var backup = Array.prototype.reverse;
buildNativeless(Array.prototype.reverse, function() { //eslint-disable-line
var target = objectTarget(this);
var thisIsProxy = isProxy(this);
var index, length, middleIndex;
if (thisIsProxy) {
length = this.get('length');
middleIndex = (length - 1) / 2; // eslint-disable-line no-magic-numbers
middleIndex = Math.round(middleIndex) === middleIndex ? middleIndex : -1;
for (index = 0; index < length; index++) {
target[index] = middleIndex === index ? this.target()[index] : this.get(index);
}
}
backup.apply(target, arguments);
if (thisIsProxy) {
for (index = 0; index < length; index++) {
if (middleIndex !== index) this.set(index, target[index]);
}
}
return this;
});
}
function buildSort() {
var backup = Array.prototype.sort;
buildNativeless(Array.prototype.sort, function() { //eslint-disable-line
var target = objectTarget(this);
if (isProxy(this)) {
var length = this.get('length');
var newArray = this.formatTarget();
backup.apply(newArray, arguments);
target.length = 0;
for (var i = 0; i < length; i++) {
this.set(i, newArray[i]);
}
} else {
backup.apply(target, arguments);
}
return this;
});
}
function buildSplice() {
var backup = Array.prototype.splice;
buildNativeless(Array.prototype.splice, function(start, deleteCount) { //eslint-disable-line
if (isProxy(this)) {
var len = this.get('length');
var relativeStart = parseInt(start, 10);
if (relativeStart < 0) {
start = Math.max(len + relativeStart, 0);
} else {
start = Math.min(relativeStart, len);
}
var items = [];
var argLength = arguments.length;
for (var i = 2; i < argLength; i++) {
items[i - 2] = arguments[i]; //eslint-disable-line no-magic-numbers
}
var nbr = start + deleteCount;
var deletedElements = [];
for (i = start; i < nbr; i++) {
deletedElements[i - start] = this.get(i);
}
var toKeep = [];
for (i = nbr; i < len; i++) {
toKeep[i - nbr] = this.get(i);
}
var itemToAddLength = items.length;
var toKeepLength = toKeep.length;
this.set('length', start + itemToAddLength + toKeepLength);
for (i = 0; i < itemToAddLength; i++) {
this.set(start + i, items[i]);
}
for (i = 0; i < toKeepLength; i++) {
this.set(start + i + itemToAddLength, toKeep[i]);
}
return deletedElements;
}
return backup.apply(this, arguments);
});
}
function buildFunctions(fnNames, fn) {
var len = fnNames.length;
for (var elementIndex = 0; elementIndex < len; elementIndex++) {
var element = fnNames[elementIndex];
fn(element);
}
}
function buildWithThisAsFormattedTarget(fnName) {
var backup = eval(fnName);
var fn = function() { //eslint-disable-line
var target = isProxy(this) ? this.formatTarget() : this; //eslint-disable-line no-invalid-this
return backup.apply(target, arguments);
};
buildNativeless(fnName, fn);
}
function buildWithThisAsTarget(fnName) {
var backup = eval(fnName);
var fn = function() { //eslint-disable-line
return backup.apply(objectTarget(this), arguments); //eslint-disable-line no-invalid-this
};
buildNativeless(fnName, fn);
}
function buildWithFirstParamAsTarget(fnName) {
var backup = eval(fnName);
var fn = function() { //eslint-disable-line
var argLen = arguments.length;
var args = [objectTarget(arguments[0])];
for (var argIndex = 1; argIndex < argLen; argIndex++) {
args[argIndex] = arguments[argIndex];
}
return backup.apply(this, args); //eslint-disable-line no-invalid-this
};
buildNativeless(fnName, fn);
}
buildObjectAssign();
buildConcat();
buildDefineProperty();
buildDefineProperties();
buildObjectCreate();
buildPop();
buildReverse();
buildSetPrototypeOf();
buildShift();
buildSort();
buildSplice();
buildFunctions([
Object.prototype.hasOwnProperty,
Object.prototype.propertyIsEnumerable,
Object.prototype.toLocaleString,
Object.prototype.toString,
Object.prototype.__defineGetter__,
Object.prototype.__defineSetter__,
Object.prototype.__lookupGetter__,
Object.prototype.__lookupSetter__,
Array.prototype.push,
Array.prototype.unshift
], buildWithThisAsTarget);
buildFunctions([
Object.getOwnPropertyDescriptor,
Object.freeze,
Object.getOwnPropertyNames,
Object.getPrototypeOf,
Object.isExtensible,
Object.isFrozen,
Object.isSealed,
Object.keys,
Object.preventExtensions,
Object.seal,
Object.prototype.isPrototypeOf,
Array.isArray
], buildWithFirstParamAsTarget);
buildFunctions([
Array.prototype.join,
Array.prototype.slice,
Array.prototype.toLocaleString,
Array.prototype.toString,
Array.prototype.some,
Array.prototype.filter,
Array.prototype.every,
Array.prototype.reduce,
Array.prototype.reduceRight,
Array.prototype.map,
Array.prototype.forEach,
Array.prototype.indexOf,
Array.prototype.lastIndexOf
], buildWithThisAsFormattedTarget);
})();
window.nativePatchCalled = true;
function globalDeleter(object, propertyName) {
return isProxy(object) ? object.deleteProperty(propertyName) : delete object[propertyName];
}
function globalGetter(object, propertyName, proxy) {
var value;
if (isProxy(object)) {
value = object.get(propertyName, proxy);
} else {
var getter = Object.prototype.__lookupGetter__.call(object, propertyName);
value = getter ? getter.call(proxy || object) : object[propertyName];
}
return value;
}
function globalMemberCaller(target, property, args) { // eslint-disable-line no-unused-vars
var fn = globalGetter(target, property);
return globalCaller(fn, target, args);
}
function globalCaller(fn, target, args) {
if (fn === Function.prototype.call) {
fn = target;
target = args[0];
args = args.slice(1);
} else if (fn === Function.prototype.apply) {
fn = target;
target = args[0];
args = args[1] || [];
}
if (fn.__$nativeless$__) {
fn = fn.__$nativeless$__;
}
return fn.apply(target, args);
}
function globalHas(object, propertyName) {
return isProxy(object) ? object.has(propertyName) : propertyName in object;
}
function globalInstanceof(object, cls) { // eslint-disable-line no-unused-vars
return isProxy(object) ? object.instanceOf(cls) : object instanceof cls;
}
function globalSetter(object, propertyName, value) {
if (isProxy(object)) {
return object.set(propertyName, value);
}
object[propertyName] = value;
return value;
}
function isProxy(object) {
return object instanceof window.Proxy;
}
function objectTarget(object) {
return isProxy(object) ? objectTarget(object.target()) : object;
}
if (!window.Proxy) {
window.Proxy = function(target, handlers) {
if (target === undefined || handlers === undefined) throw TypeError('Cannot create proxy with a non-object as target or handler');
if (handlers.getPrototypeOf) throw TypeError('[babel-plugin-es5-proxy] handler \'getPrototypeOf\' not implemented');
if (handlers.setPrototypeOf) throw TypeError('[babel-plugin-es5-proxy] handler \'setPrototypeOf\' not implemented');
if (handlers.isExtensible) throw TypeError('[babel-plugin-es5-proxy] handler \'isExtensible\' not implemented');
if (handlers.preventExtensions) throw TypeError('[babel-plugin-es5-proxy] handler \'preventExtensions\' not implemented');
if (handlers.getOwnPropertyDescriptor) throw TypeError('[babel-plugin-es5-proxy] handler \'getOwnPropertyDescriptor\' not implemented');
if (handlers.defineProperty) throw TypeError('[babel-plugin-es5-proxy] handler \'defineProperty\' not implemented');
if (handlers.ownKeys) throw TypeError('[babel-plugin-es5-proxy] handler \'ownKeys\' not implemented');
if (handlers.apply) throw TypeError('[babel-plugin-es5-proxy] handler \'apply\' not implemented');
if (handlers.construct) throw TypeError('[babel-plugin-es5-proxy] handler \'construct\' not implemented');
this.target = function() {
return target;
};
this.get = function(property, proxy) {
if (handlers.get) {
return handlers.get.call(this, target, property);
}
return globalGetter(target, property, proxy || this);
};
this.has = function(property) {
if (handlers.has) {
return !!handlers.has.call(this, target, property);
}
return !!globalHas(target, property);
};
this.set = function(property, value) {
if (handlers.set) {
handlers.set.call(this, target, property, value);
} else {
globalSetter(target, property, value);
}
return value;
};
this.deleteProperty = function(property) {
if (handlers.deleteProperty) {
handlers.deleteProperty.call(this, target, property);
} else {
globalDeleter(target, property);
}
return true;
};
this.instanceOf = function(cls) {
return this.target() instanceof cls;
};
};
window.Proxy.prototype.toJSON = function() {
return this.formatTarget();
};
window.Proxy.prototype.formatTarget = function() {
return Array.isArray.__$nativeless$__(this.target()) ? this.formatTargetArray() : this.formatTargetObject();
};
window.Proxy.prototype.formatTargetArray = function() {
var newArr = [];
var length = this.get('length', this);
for (var i = 0; i < length; i++) {
newArr[i] = this.get(i);
}
return newArr;
};
window.Proxy.prototype.formatTargetObject = function(deep) {
return formatTargetObject(this, deep);
};
}
function formatTargetObject(obj, deep) {
if (!isProxy(obj)) return obj;
var newObj = {};
var keys = Object.getOwnPropertyNames.__$nativeless$__.call(Object, obj.target());
var keyLength = keys.length;
for (var i = 0; i < keyLength; i++) {
var key = keys[i];
var descriptor = Object.getOwnPropertyDescriptor.__$nativeless$__.call(Object, obj, key);
if (!descriptor.get) descriptor.value = obj.get(key, obj);
if (deep && descriptor.value) {
descriptor.value = formatTargetObject(descriptor.value, deep);
}
Object.defineProperty(newObj, key, descriptor);
}
return newObj;
}
/* eslint-enable prefer-rest-params, no-var, comma-dangle */