forked from tvcutsem/harmony-reflect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflect.js
1945 lines (1770 loc) · 65.6 KB
/
reflect.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2011-2012 Software Languages Lab, Vrije Universiteit Brussel
// This code is dual-licensed under both the Apache License and the MPL
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is a shim for the ES-Harmony reflection module
*
* The Initial Developer of the Original Code is
* Tom Van Cutsem, Vrije Universiteit Brussel.
* Portions created by the Initial Developer are Copyright (C) 2011-2012
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*/
// ----------------------------------------------------------------------------
// This file is a polyfill for the upcoming ECMAScript Reflect API,
// including support for Proxies. See the draft specification at:
// http://wiki.ecmascript.org/doku.php?id=harmony:reflect_api
// http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies
// http://wiki.ecmascript.org/doku.php?id=harmony:virtual_object_api
// It supersedes the earlier polyfill at:
// code.google.com/p/es-lab/source/browse/trunk/src/proxies/DirectProxies.js
// This code was tested on tracemonkey / Firefox 12
// (and should run fine on older Firefox versions starting with FF4)
// The code also works correctly on
// v8 --harmony_proxies --harmony_weakmaps (v3.6.5.1)
// Language Dependencies:
// - ECMAScript 5/strict
// - "old" (i.e. non-direct) Harmony Proxies
// - Harmony WeakMaps
// Patches:
// - Object.{freeze,seal,preventExtensions}
// - Object.{isFrozen,isSealed,isExtensible}
// - Object.getPrototypeOf
// - Object.prototype.valueOf
// - Object.getOwnPropertyDescriptor
// - Function.prototype.toString
// - Proxy
// Adds new globals:
// - Reflect
// Direct proxies can be created via Proxy(target, handler)
// ----------------------------------------------------------------------------
(function(global, nonstrictDelete){ // function-as-module pattern
"use strict";
// === Direct Proxies: Invariant Enforcement ===
// Direct proxies build on non-direct proxies by automatically wrapping
// all user-defined proxy handlers in a Validator handler that checks and
// enforces ES5 invariants.
// A direct proxy is a proxy for an existing object called the target object.
// A Validator handler is a wrapper for a target proxy handler H.
// The Validator forwards all operations to H, but additionally
// performs a number of integrity checks on the results of some traps,
// to make sure H does not violate the ES5 invariants w.r.t. non-configurable
// properties and non-extensible, sealed or frozen objects.
// For each property that H exposes as own, non-configurable
// (e.g. by returning a descriptor from a call to getOwnPropertyDescriptor)
// the Validator handler defines those properties on the target object.
// When the proxy becomes non-extensible, also configurable own properties
// are checked against the target.
// We will call properties that are defined on the target object
// "fixed properties".
// We will name fixed non-configurable properties "sealed properties".
// We will name fixed non-configurable non-writable properties "frozen
// properties".
// The Validator handler upholds the following invariants w.r.t. non-configurability:
// - getOwnPropertyDescriptor cannot report sealed properties as non-existent
// - getOwnPropertyDescriptor cannot report incompatible changes to the
// attributes of a sealed property (e.g. reporting a non-configurable
// property as configurable, or reporting a non-configurable, non-writable
// property as writable)
// - getPropertyDescriptor cannot report sealed properties as non-existent
// - getPropertyDescriptor cannot report incompatible changes to the
// attributes of a sealed property. It _can_ report incompatible changes
// to the attributes of non-own, inherited properties.
// - defineProperty cannot make incompatible changes to the attributes of
// sealed properties
// - deleteProperty cannot report a successful deletion of a sealed property
// - hasOwn cannot report a sealed property as non-existent
// - has cannot report a sealed property as non-existent
// - get cannot report inconsistent values for frozen data
// properties, and must report undefined for sealed accessors with an
// undefined getter
// - set cannot report a successful assignment for frozen data
// properties or sealed accessors with an undefined setter.
// - get{Own}PropertyNames lists all sealed properties of the target.
// - keys lists all enumerable sealed properties of the target.
// - enumerate lists all enumerable sealed properties of the target.
// - if a property of a non-extensible proxy is reported as non-existent,
// then it must forever be reported as non-existent. This applies to
// own and inherited properties and is enforced in the
// deleteProperty, get{Own}PropertyDescriptor, has{Own},
// get{Own}PropertyNames, keys and enumerate traps
// Violation of any of these invariants by H will result in TypeError being
// thrown.
// Additionally, once Object.preventExtensions, Object.seal or Object.freeze
// is invoked on the proxy, the set of own property names for the proxy is
// fixed. Any property name that is not fixed is called a 'new' property.
// The Validator upholds the following invariants regarding extensibility:
// - getOwnPropertyDescriptor cannot report new properties as existent
// (it must report them as non-existent by returning undefined)
// - defineProperty cannot successfully add a new property (it must reject)
// - getOwnPropertyNames cannot list new properties
// - hasOwn cannot report true for new properties (it must report false)
// - keys cannot list new properties
// Invariants currently not enforced:
// - getOwnPropertyNames lists only own property names
// - keys lists only enumerable own property names
// Both traps may list more property names than are actually defined on the
// target.
// Invariants with regard to inheritance are currently not enforced.
// - a non-configurable potentially inherited property on a proxy with
// non-mutable ancestry cannot be reported as non-existent
// (An object with non-mutable ancestry is a non-extensible object whose
// [[Prototype]] is either null or an object with non-mutable ancestry.)
// Changes in Handler API compared to previous harmony:proxies, see:
// http://wiki.ecmascript.org/doku.php?id=strawman:direct_proxies
// http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies
// ----------------------------------------------------------------------------
// ---- WeakMap polyfill ----
// TODO: find a proper WeakMap polyfill
// define an empty WeakMap so that at least the Reflect module code
// will work in the absence of WeakMaps. Proxy emulation depends on
// actual WeakMaps, so will not work with this little shim.
if (typeof WeakMap === "undefined") {
global.WeakMap = function(){};
global.WeakMap.prototype = {
get: function(k) { return undefined; },
set: function(k,v) { throw new Error("WeakMap not supported"); }
};
}
// ---- Normalization functions for property descriptors ----
function isStandardAttribute(name) {
return /^(get|set|value|writable|enumerable|configurable)$/.test(name);
}
// Adapted from ES5 section 8.10.5
function toPropertyDescriptor(obj) {
if (Object(obj) !== obj) {
throw new TypeError("property descriptor should be an Object, given: "+
obj);
}
var desc = {};
if ('enumerable' in obj) { desc.enumerable = !!obj.enumerable; }
if ('configurable' in obj) { desc.configurable = !!obj.configurable; }
if ('value' in obj) { desc.value = obj.value; }
if ('writable' in obj) { desc.writable = !!obj.writable; }
if ('get' in obj) {
var getter = obj.get;
if (getter !== undefined && typeof getter !== "function") {
throw new TypeError("property descriptor 'get' attribute must be "+
"callable or undefined, given: "+getter);
}
desc.get = getter;
}
if ('set' in obj) {
var setter = obj.set;
if (setter !== undefined && typeof setter !== "function") {
throw new TypeError("property descriptor 'set' attribute must be "+
"callable or undefined, given: "+setter);
}
desc.set = setter;
}
if ('get' in desc || 'set' in desc) {
if ('value' in desc || 'writable' in desc) {
throw new TypeError("property descriptor cannot be both a data and an "+
"accessor descriptor: "+obj);
}
}
return desc;
}
function isAccessorDescriptor(desc) {
if (desc === undefined) return false;
return ('get' in desc || 'set' in desc);
}
function isDataDescriptor(desc) {
if (desc === undefined) return false;
return ('value' in desc || 'writable' in desc);
}
function isGenericDescriptor(desc) {
if (desc === undefined) return false;
return !isAccessorDescriptor(desc) && !isDataDescriptor(desc);
}
function toCompletePropertyDescriptor(desc) {
var internalDesc = toPropertyDescriptor(desc);
if (isGenericDescriptor(internalDesc) || isDataDescriptor(internalDesc)) {
if (!('value' in internalDesc)) { internalDesc.value = undefined; }
if (!('writable' in internalDesc)) { internalDesc.writable = false; }
} else {
if (!('get' in internalDesc)) { internalDesc.get = undefined; }
if (!('set' in internalDesc)) { internalDesc.set = undefined; }
}
if (!('enumerable' in internalDesc)) { internalDesc.enumerable = false; }
if (!('configurable' in internalDesc)) { internalDesc.configurable = false; }
return internalDesc;
}
function isEmptyDescriptor(desc) {
return !('get' in desc) &&
!('set' in desc) &&
!('value' in desc) &&
!('writable' in desc) &&
!('enumerable' in desc) &&
!('configurable' in desc);
}
function isEquivalentDescriptor(desc1, desc2) {
return sameValue(desc1.get, desc2.get) &&
sameValue(desc1.set, desc2.set) &&
sameValue(desc1.value, desc2.value) &&
sameValue(desc1.writable, desc2.writable) &&
sameValue(desc1.enumerable, desc2.enumerable) &&
sameValue(desc1.configurable, desc2.configurable);
}
// copied from http://wiki.ecmascript.org/doku.php?id=harmony:egal
function sameValue(x, y) {
if (x === y) {
// 0 === -0, but they are not identical
return x !== 0 || 1 / x === 1 / y;
}
// NaN !== NaN, but they are identical.
// NaNs are the only non-reflexive value, i.e., if x !== x,
// then x is a NaN.
// isNaN is broken: it converts its argument to number, so
// isNaN("foo") => true
return x !== x && y !== y;
}
/**
* Returns a fresh property descriptor that is guaranteed
* to be complete (i.e. contain all the standard attributes).
* Additionally, any non-standard enumerable properties of
* attributes are copied over to the fresh descriptor.
*
* If attributes is undefined, returns undefined.
*
* See also: http://wiki.ecmascript.org/doku.php?id=harmony:proxies_semantics
*/
function normalizeAndCompletePropertyDescriptor(attributes) {
if (attributes === undefined) { return undefined; }
var desc = toCompletePropertyDescriptor(attributes);
// Note: no need to call FromPropertyDescriptor(desc), as we represent
// "internal" property descriptors as proper Objects from the start
for (var name in attributes) {
if (!isStandardAttribute(name)) {
Object.defineProperty(desc, name,
{ value: attributes[name],
writable: true,
enumerable: true,
configurable: true });
}
}
return desc;
}
/**
* Returns a fresh property descriptor whose standard
* attributes are guaranteed to be data properties of the right type.
* Additionally, any non-standard enumerable properties of
* attributes are copied over to the fresh descriptor.
*
* If attributes is undefined, will throw a TypeError.
*
* See also: http://wiki.ecmascript.org/doku.php?id=harmony:proxies_semantics
*/
function normalizePropertyDescriptor(attributes) {
var desc = toPropertyDescriptor(attributes);
// Note: no need to call FromGenericPropertyDescriptor(desc), as we represent
// "internal" property descriptors as proper Objects from the start
for (var name in attributes) {
if (!isStandardAttribute(name)) {
Object.defineProperty(desc, name,
{ value: attributes[name],
writable: true,
enumerable: true,
configurable: true });
}
}
return desc;
}
// store a reference to the real ES5 primitives before patching them later
var prim_preventExtensions = Object.preventExtensions,
prim_seal = Object.seal,
prim_freeze = Object.freeze,
prim_isExtensible = Object.isExtensible,
prim_isSealed = Object.isSealed,
prim_isFrozen = Object.isFrozen,
prim_getPrototypeOf = Object.getPrototypeOf,
prim_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
// these will point to the patched versions of the respective methods on
// Object. They are used within this module as the "intrinsic" bindings
// of these methods (i.e. the "original" bindings as defined in the spec)
var Object_isFrozen, Object_isSealed, Object_isExtensible, Object_getPrototypeOf;
/**
* A property 'name' is fixed if it is an own property of the target.
*/
function isFixed(name, target) {
return ({}).hasOwnProperty.call(target, name);
}
function isSealed(name, target) {
var desc = Object.getOwnPropertyDescriptor(target, name);
if (desc === undefined) { return false; }
return !desc.configurable;
}
/**
* Performs all validation that Object.defineProperty performs,
* without actually defining the property. Returns a boolean
* indicating whether validation succeeded.
*
* Implementation transliterated from ES5.1 section 8.12.9
*/
function validateProperty(target, name, desc) {
var current = Object.getOwnPropertyDescriptor(target, name);
var extensible = Object.isExtensible(target);
if (current === undefined && extensible === false) {
return false;
}
if (current === undefined && extensible === true) {
return true;
}
if (isEmptyDescriptor(desc)) {
return true;
}
if (isEquivalentDescriptor(current, desc)) {
return true;
}
if (current.configurable === false) {
if (desc.configurable === true) {
return false;
}
if ('enumerable' in desc && desc.enumerable !== current.enumerable) {
return false;
}
}
if (isGenericDescriptor(desc)) {
return true;
}
if (isDataDescriptor(current) !== isDataDescriptor(desc)) {
if (current.configurable === false) {
return false;
}
return true;
}
if (isDataDescriptor(current) && isDataDescriptor(desc)) {
if (current.configurable === false) {
if (current.writable === false && desc.writable === true) {
return false;
}
if (current.writable === false) {
if ('value' in desc && !sameValue(desc.value, current.value)) {
return false;
}
}
}
return true;
}
if (isAccessorDescriptor(current) && isAccessorDescriptor(desc)) {
if (current.configurable === false) {
if ('set' in desc && !sameValue(desc.set, current.set)) {
return false;
}
if ('get' in desc && !sameValue(desc.get, current.get)) {
return false;
}
}
}
return true;
}
// ---- The Validator handler wrapper around user handlers ----
/**
* @param target the object wrapped by this proxy.
* As long as the proxy is extensible, only non-configurable properties
* are checked against the target. Once the proxy becomes non-extensible,
* invariants w.r.t. non-extensibility are also enforced.
*
* @param handler the handler of the direct proxy. The object emulated by
* this handler is validated against the target object of the direct proxy.
* Any violations that the handler makes against the invariants
* of the target will cause a TypeError to be thrown.
*
* Both target and handler must be proper Objects at initialization time.
*/
function Validator(target, handler) {
// this is a const reference, this.target should never change
this.target = target;
// this is a const reference, this.handler should never change
this.handler = handler;
}
Validator.prototype = {
/**
* If getTrap returns undefined, the caller should perform the
* default forwarding behavior.
* If getTrap returns normally otherwise, the return value
* will be a callable trap function whose |this| binding is
* pre-bound to this.handler (for convenience in the rest of the code)
*/
getTrap: function(trapName) {
var trap = this.handler[trapName];
if (trap === undefined) {
// the trap was not defined,
// perform the default forwarding behavior
return undefined;
}
if (typeof trap !== "function") {
throw new TypeError(trapName + " trap is not callable: "+trap);
}
// bind the trap's |this| to this.handler, for convenience
return Function.prototype.bind.call(trap, this.handler);
},
// === fundamental traps ===
/**
* If name denotes a fixed property, check:
* - whether targetHandler reports it as existent
* - whether the returned descriptor is compatible with the fixed property
* If the proxy is non-extensible, check:
* - whether name is not a new property
* Additionally, the returned descriptor is normalized and completed.
*/
getOwnPropertyDescriptor: function(name) {
"use strict";
var trap = this.getTrap("getOwnPropertyDescriptor");
if (trap === undefined) {
return Reflect.getOwnPropertyDescriptor(this.target, name);
}
name = String(name);
var desc = trap(this.target, name);
desc = normalizeAndCompletePropertyDescriptor(desc);
if (desc === undefined) {
if (isSealed(name, this.target)) {
throw new TypeError("cannot report non-configurable property '"+name+
"' as non-existent");
}
if (!Object.isExtensible(this.target) &&
isFixed(name, this.target)) {
// if handler is allowed to return undefined, we cannot guarantee
// that it will not return a descriptor for this property later.
// Once a property has been reported as non-existent on a non-extensible
// object, it should forever be reported as non-existent
throw new TypeError("cannot report existing own property '"+name+
"' as non-existent on a non-extensible object");
}
return undefined;
}
// at this point, we know (desc !== undefined), i.e.
// targetHandler reports 'name' as an existing property
// Note: we could collapse the following two if-tests into a single
// test. Separating out the cases to improve error reporting.
if (!Object.isExtensible(this.target)) {
if (!isFixed(name, this.target)) {
throw new TypeError("cannot report a new own property '"+
name + "' on a non-extensible object");
}
}
if (isFixed(name, this.target)) {
if (!validateProperty(this.target, name, desc)) {
throw new TypeError("cannot report incompatible property descriptor "+
"for property '"+name+"'");
}
}
if (!desc.configurable && !isFixed(name, this.target)) {
// if the property is non-existent on the target, but is reported
// as a non-configurable property, it may later be reported as
// non-existent, which violates the invariant that if the property
// might disappear, the configurable attribute must be true.
throw new TypeError("cannot report a non-configurable descriptor "+
"for non-existent property '"+name+"'");
}
return desc;
},
/**
* In the direct proxies design with refactored prototype climbing,
* this trap is deprecated. For proxies-as-prototypes, instead
* of calling this trap, the get, set, has or enumerate traps are
* called instead.
*
* In this implementation, we "abuse" getPropertyDescriptor to
* support trapping the get or set traps for proxies-as-prototypes.
* We do this by returning a getter/setter pair that invokes
* the corresponding traps.
*
* In Firefox, this trap is only called after a prior invocation
* of the 'has' trap has returned true. Hence, expect the following
* behavior:
* <code>
* var child = Object.create(Proxy(target, handler));
* child[name] // triggers handler.has(target, name)
* // if that returns true, triggers handler.get(target, name, child)
* </code>
*/
getPropertyDescriptor: function(name) {
var handler = this;
return {
get: function() {
return handler.get(this, name);
},
set: function(val) {
if (handler.set(this, name, val)) {
return val;
} else {
throw new TypeError("failed assignment to "+name);
}
},
enumerable: true,
configurable: true
};
},
/**
* If name denotes a fixed property, check for incompatible changes.
* If the proxy is non-extensible, check that new properties are rejected.
*/
defineProperty: function(name, desc) {
// TODO(tvcutsem): the current tracemonkey implementation of proxies
// auto-completes 'desc', which is not correct. 'desc' should be
// normalized, but not completed. Consider:
// Object.defineProperty(proxy, 'foo', {enumerable:false})
// This trap will receive desc =
// {value:undefined,writable:false,enumerable:false,configurable:false}
// This will also set all other attributes to their default value,
// which is unexpected and different from [[DefineOwnProperty]].
// Bug filed: https://bugzilla.mozilla.org/show_bug.cgi?id=601329
var trap = this.getTrap("defineProperty");
if (trap === undefined) {
// default forwarding behavior
return Reflect.defineProperty(this.target, name, desc);
}
name = String(name);
desc = normalizePropertyDescriptor(desc);
var success = trap(this.target, name, desc);
success = !!success; // coerce to Boolean
if (success === true) {
// Note: we could collapse the following two if-tests into a single
// test. Separating out the cases to improve error reporting.
if (!Object.isExtensible(this.target)) {
if (!isFixed(name, this.target)) {
throw new TypeError("cannot successfully add a new property '"+
name + "' to a non-extensible object");
}
}
if (isFixed(name, this.target)) {
if (!validateProperty(this.target, name, desc)) {
throw new TypeError("cannot define incompatible property "+
"descriptor for property '"+name+"'");
}
}
if (!desc.configurable && !isFixed(name, this.target)) {
throw new TypeError("cannot successfully define a non-configurable "+
"descriptor for non-existent property '"+
name+"'");
}
}
return success;
},
/**
* On success, check whether the target object is indeed frozen.
*/
freeze: function() {
var trap = this.getTrap("freeze");
if (trap === undefined) {
// default forwarding behavior
return Reflect.freeze(this.target);
}
var success = trap(this.target);
success = !!success; // coerce to Boolean
if (success) {
if (!Object_isFrozen(this.target)) {
throw new TypeError("can't report non-frozen object as frozen: "+
this.target);
}
}
return success;
},
/**
* On success, check whether the target object is indeed sealed.
*/
seal: function() {
var trap = this.getTrap("seal");
if (trap === undefined) {
// default forwarding behavior
return Reflect.seal(this.target);
}
var success = trap(this.target);
success = !!success; // coerce to Boolean
if (success) {
if (!Object_isSealed(this.target)) {
throw new TypeError("can't report non-sealed object as sealed: "+
this.target);
}
}
return success;
},
/**
* On success, check whether the target object is indeed non-extensible.
*/
preventExtensions: function() {
var trap = this.getTrap("preventExtensions");
if (trap === undefined) {
// default forwarding behavior
return Reflect.preventExtensions(this.target);
}
var success = trap(this.target);
success = !!success; // coerce to Boolean
if (success) {
if (Object_isExtensible(this.target)) {
throw new TypeError("can't report extensible object as non-extensible: "+
this.target);
}
}
return success;
},
/**
* If name denotes a sealed property, check whether handler rejects.
*/
delete: function(name) {
"use strict";
var trap = this.getTrap("deleteProperty");
if (trap === undefined) {
// default forwarding behavior
return Reflect.deleteProperty(this.target, name);
}
name = String(name);
var res = trap(this.target, name);
res = !!res; // coerce to Boolean
if (res === true) {
if (isSealed(name, this.target)) {
throw new TypeError("property '"+name+"' is non-configurable "+
"and can't be deleted");
}
}
return res;
},
/**
* Checks whether the trap result does not contain any new properties
* if the proxy is non-extensible.
*
* Any own non-configurable properties of the target that are not included
* in the trap result give rise to a TypeError. As such, we check whether the
* returned result contains at least all sealed properties of the target
* object.
*
* Additionally, the trap result is normalized.
* Instead of returning the trap result directly:
* - create and return a fresh Array,
* - of which each element is coerced to String,
* - which does not contain duplicates.
*/
getOwnPropertyNames: function() {
var trap = this.getTrap("getOwnPropertyNames");
if (trap === undefined) {
// default forwarding behavior
return Reflect.getOwnPropertyNames(this.target);
}
var trapResult = trap(this.target);
// propNames is used as a set of strings
var propNames = Object.create(null);
var numProps = +trapResult.length;
var result = new Array(numProps);
for (var i = 0; i < numProps; i++) {
var s = String(trapResult[i]);
if (propNames[s]) {
throw new TypeError("getOwnPropertyNames cannot list a "+
"duplicate property '"+s+"'");
}
if (!Object.isExtensible(this.target) && !isFixed(s, this.target)) {
// non-extensible proxies don't tolerate new own property names
throw new TypeError("getOwnPropertyNames cannot list a new "+
"property '"+s+"' on a non-extensible object");
}
propNames[s] = true;
result[i] = s;
}
var ownProps = Object.getOwnPropertyNames(this.target);
var target = this.target;
ownProps.forEach(function (ownProp) {
if (!propNames[ownProp]) {
if (isSealed(ownProp, target)) {
throw new TypeError("getOwnPropertyNames trap failed to include "+
"non-configurable property '"+ownProp+"'");
}
if (!Object.isExtensible(target) &&
isFixed(ownProp, target)) {
// if handler is allowed to report ownProp as non-existent,
// we cannot guarantee that it will never later report it as
// existent. Once a property has been reported as non-existent
// on a non-extensible object, it should forever be reported as
// non-existent
throw new TypeError("cannot report existing own property '"+ownProp+
"' as non-existent on a non-extensible object");
}
}
});
return result;
},
/**
* Checks whether the trap result is consistent with the state of the
* wrapped target.
*/
isExtensible: function() {
var trap = this.getTrap("isExtensible");
if (trap === undefined) {
// default forwarding behavior
return Reflect.isExtensible(this.target);
}
var result = trap(this.target);
result = !!result; // coerce to Boolean
var state = Object_isExtensible(this.target);
if (result !== state) {
if (result) {
throw new TypeError("cannot report non-extensible object as extensible: "+
this.target);
} else {
throw new TypeError("cannot report extensible object as non-extensible: "+
this.target);
}
}
return state;
},
/**
* Check whether the trap result corresponds to the target's [[Prototype]]
*/
getPrototypeOf: function() {
var trap = this.getTrap("getPrototypeOf");
if (trap === undefined) {
// default forwarding behavior
return Reflect.getPrototypeOf(this.target);
}
var allegedProto = trap(this.target);
var actualProto = Object_getPrototypeOf(this.target);
if (allegedProto !== actualProto) {
throw new TypeError("prototype value does not match: " + this.target);
}
return actualProto;
},
/**
* In the direct proxies design with refactored prototype climbing,
* this trap is deprecated. For proxies-as-prototypes, for-in will
* call the enumerate() trap. If that trap is not defined, the
* operation is forwarded to the target, no more fallback on this
* fundamental trap.
*/
getPropertyNames: function() {
throw new TypeError("getPropertyNames trap is deprecated");
},
// === derived traps ===
/**
* If name denotes a fixed property, check whether the trap returns true.
* If name denotes a new property on a non-extensible proxy, check whether
* the trap returns false.
*/
hasOwn: function(name) {
"use strict";
var trap = this.getTrap("hasOwn");
if (trap === undefined) {
// default forwarding behavior
return Reflect.hasOwn(this.target, name);
}
name = String(name);
var res = trap(this.target, name);
res = !!res; // coerce to Boolean
if (res === false) {
if (isSealed(name, this.target)) {
throw new TypeError("cannot report existing non-configurable own "+
"property '"+name + "' as a non-existent own "+
"property");
}
if (!Object.isExtensible(this.target) &&
isFixed(name, this.target)) {
// if handler is allowed to return false, we cannot guarantee
// that it will return true for this property later.
// Once a property has been reported as non-existent on a non-extensible
// object, it should forever be reported as non-existent
throw new TypeError("cannot report existing own property '"+name+
"' as non-existent on a non-extensible object");
}
} else {
// res === true, if the proxy is non-extensible,
// check that name is no new property
if (!Object.isExtensible(this.target)) {
if (!isFixed(name, this.target)) {
throw new TypeError("cannot report a new own property '"+
name + "' on a non-extensible object");
}
}
}
return res;
},
/**
* If name denotes a fixed property, check whether the trap returns true.
*/
has: function(name) {
var trap = this.getTrap("has");
if (trap === undefined) {
// default forwarding behavior
return Reflect.has(this.target, name);
}
name = String(name);
var res = trap(this.target, name);
res = !!res; // coerce to Boolean
if (res === false) {
if (isSealed(name, this.target)) {
throw new TypeError("cannot report existing non-configurable own "+
"property '"+ name + "' as a non-existent "+
"property");
}
if (!Object.isExtensible(this.target) &&
isFixed(name, this.target)) {
// if handler is allowed to return false, we cannot guarantee
// that it will not return true for this property later.
// Once a property has been reported as non-existent on a non-extensible
// object, it should forever be reported as non-existent
throw new TypeError("cannot report existing own property '"+name+
"' as non-existent on a non-extensible object");
}
}
// if res === true, we don't need to check for extensibility
// even for a non-extensible proxy that has no own name property,
// the property may have been inherited
return res;
},
/**
* If name denotes a fixed non-configurable, non-writable data property,
* check its return value against the previously asserted value of the
* fixed property.
*/
get: function(receiver, name) {
var trap = this.getTrap("get");
if (trap === undefined) {
// default forwarding behavior
return Reflect.get(this.target, name, receiver);
}
name = String(name);
var res = trap(this.target, name, receiver);
var fixedDesc = Object.getOwnPropertyDescriptor(this.target, name);
// check consistency of the returned value
if (fixedDesc !== undefined) { // getting an existing property
if (isDataDescriptor(fixedDesc) &&
fixedDesc.configurable === false &&
fixedDesc.writable === false) { // own frozen data property
if (!sameValue(res, fixedDesc.value)) {
throw new TypeError("cannot report inconsistent value for "+
"non-writable, non-configurable property '"+
name+"'");
}
} else { // it's an accessor property
if (isAccessorDescriptor(fixedDesc) &&
fixedDesc.configurable === false &&
fixedDesc.get === undefined) {
if (res !== undefined) {
throw new TypeError("must report undefined for non-configurable "+
"accessor property '"+name+"' without getter");
}
}
}
}
return res;
},
/**
* If name denotes a fixed non-configurable, non-writable data property,
* check that the trap rejects the assignment.
*/
set: function(receiver, name, val) {
var trap = this.getTrap("set");
if (trap === undefined) {
// default forwarding behavior
return Reflect.set(this.target, name, val, receiver);
}
name = String(name);
var res = trap(this.target, name, val, receiver);
res = !!res; // coerce to Boolean
// if success is reported, check whether property is truly assignable
if (res === true) {
var fixedDesc = Object.getOwnPropertyDescriptor(this.target, name);
if (fixedDesc !== undefined) { // setting an existing property
if (isDataDescriptor(fixedDesc) &&
fixedDesc.configurable === false &&
fixedDesc.writable === false) {