forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
1052 lines (909 loc) · 31.4 KB
/
proxy.go
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
package goja
import (
"fmt"
"reflect"
"github.com/kmcsr/goja/unistring"
)
// Proxy is a Go wrapper around ECMAScript Proxy. Calling Runtime.ToValue() on it
// returns the underlying Proxy. Calling Export() on an ECMAScript Proxy returns a wrapper.
// Use Runtime.NewProxy() to create one.
type Proxy struct {
proxy *proxyObject
}
var (
proxyType = reflect.TypeOf(Proxy{})
)
type proxyPropIter struct {
p *proxyObject
names []Value
idx int
}
func (i *proxyPropIter) next() (propIterItem, iterNextFunc) {
for i.idx < len(i.names) {
name := i.names[i.idx]
i.idx++
return propIterItem{name: name}, i.next
}
return propIterItem{}, nil
}
func (r *Runtime) newProxyObject(target, handler, proto *Object) *proxyObject {
return r._newProxyObject(target, &jsProxyHandler{handler: handler}, proto)
}
func (r *Runtime) _newProxyObject(target *Object, handler proxyHandler, proto *Object) *proxyObject {
v := &Object{runtime: r}
p := &proxyObject{}
v.self = p
p.val = v
p.class = classObject
if proto == nil {
p.prototype = r.global.ObjectPrototype
} else {
p.prototype = proto
}
p.extensible = false
p.init()
p.target = target
p.handler = handler
if call, ok := target.self.assertCallable(); ok {
p.call = call
}
if ctor := target.self.assertConstructor(); ctor != nil {
p.ctor = ctor
}
return p
}
func (p Proxy) Revoke() {
p.proxy.revoke()
}
func (p Proxy) Handler() *Object {
if handler := p.proxy.handler; handler != nil {
return handler.toObject(p.proxy.val.runtime)
}
return nil
}
func (p Proxy) Target() *Object {
return p.proxy.target
}
func (p Proxy) toValue(r *Runtime) Value {
if p.proxy == nil {
return _null
}
proxy := p.proxy.val
if proxy.runtime != r {
panic(r.NewTypeError("Illegal runtime transition of a Proxy"))
}
return proxy
}
type proxyTrap string
const (
proxy_trap_getPrototypeOf = "getPrototypeOf"
proxy_trap_setPrototypeOf = "setPrototypeOf"
proxy_trap_isExtensible = "isExtensible"
proxy_trap_preventExtensions = "preventExtensions"
proxy_trap_getOwnPropertyDescriptor = "getOwnPropertyDescriptor"
proxy_trap_defineProperty = "defineProperty"
proxy_trap_has = "has"
proxy_trap_get = "get"
proxy_trap_set = "set"
proxy_trap_deleteProperty = "deleteProperty"
proxy_trap_ownKeys = "ownKeys"
proxy_trap_apply = "apply"
proxy_trap_construct = "construct"
)
func (p proxyTrap) String() (name string) {
return string(p)
}
type proxyHandler interface {
getPrototypeOf(target *Object) (Value, bool)
setPrototypeOf(target *Object, proto *Object) (bool, bool)
isExtensible(target *Object) (bool, bool)
preventExtensions(target *Object) (bool, bool)
getOwnPropertyDescriptorStr(target *Object, prop unistring.String) (Value, bool)
getOwnPropertyDescriptorIdx(target *Object, prop valueInt) (Value, bool)
getOwnPropertyDescriptorSym(target *Object, prop *Symbol) (Value, bool)
definePropertyStr(target *Object, prop unistring.String, desc PropertyDescriptor) (bool, bool)
definePropertyIdx(target *Object, prop valueInt, desc PropertyDescriptor) (bool, bool)
definePropertySym(target *Object, prop *Symbol, desc PropertyDescriptor) (bool, bool)
hasStr(target *Object, prop unistring.String) (bool, bool)
hasIdx(target *Object, prop valueInt) (bool, bool)
hasSym(target *Object, prop *Symbol) (bool, bool)
getStr(target *Object, prop unistring.String, receiver Value) (Value, bool)
getIdx(target *Object, prop valueInt, receiver Value) (Value, bool)
getSym(target *Object, prop *Symbol, receiver Value) (Value, bool)
setStr(target *Object, prop unistring.String, value Value, receiver Value) (bool, bool)
setIdx(target *Object, prop valueInt, value Value, receiver Value) (bool, bool)
setSym(target *Object, prop *Symbol, value Value, receiver Value) (bool, bool)
deleteStr(target *Object, prop unistring.String) (bool, bool)
deleteIdx(target *Object, prop valueInt) (bool, bool)
deleteSym(target *Object, prop *Symbol) (bool, bool)
ownKeys(target *Object) (*Object, bool)
apply(target *Object, this Value, args []Value) (Value, bool)
construct(target *Object, args []Value, newTarget *Object) (Value, bool)
toObject(*Runtime) *Object
}
type jsProxyHandler struct {
handler *Object
}
func (h *jsProxyHandler) toObject(*Runtime) *Object {
return h.handler
}
func (h *jsProxyHandler) proxyCall(trap proxyTrap, args ...Value) (Value, bool) {
r := h.handler.runtime
if m := toMethod(r.getVStr(h.handler, unistring.String(trap.String()))); m != nil {
return m(FunctionCall{
This: h.handler,
Arguments: args,
}), true
}
return nil, false
}
func (h *jsProxyHandler) boolProxyCall(trap proxyTrap, args ...Value) (bool, bool) {
if v, ok := h.proxyCall(trap, args...); ok {
return v.ToBoolean(), true
}
return false, false
}
func (h *jsProxyHandler) getPrototypeOf(target *Object) (Value, bool) {
return h.proxyCall(proxy_trap_getPrototypeOf, target)
}
func (h *jsProxyHandler) setPrototypeOf(target *Object, proto *Object) (bool, bool) {
var protoVal Value
if proto != nil {
protoVal = proto
} else {
protoVal = _null
}
return h.boolProxyCall(proxy_trap_setPrototypeOf, target, protoVal)
}
func (h *jsProxyHandler) isExtensible(target *Object) (bool, bool) {
return h.boolProxyCall(proxy_trap_isExtensible, target)
}
func (h *jsProxyHandler) preventExtensions(target *Object) (bool, bool) {
return h.boolProxyCall(proxy_trap_preventExtensions, target)
}
func (h *jsProxyHandler) getOwnPropertyDescriptorStr(target *Object, prop unistring.String) (Value, bool) {
return h.proxyCall(proxy_trap_getOwnPropertyDescriptor, target, stringValueFromRaw(prop))
}
func (h *jsProxyHandler) getOwnPropertyDescriptorIdx(target *Object, prop valueInt) (Value, bool) {
return h.proxyCall(proxy_trap_getOwnPropertyDescriptor, target, prop.toString())
}
func (h *jsProxyHandler) getOwnPropertyDescriptorSym(target *Object, prop *Symbol) (Value, bool) {
return h.proxyCall(proxy_trap_getOwnPropertyDescriptor, target, prop)
}
func (h *jsProxyHandler) definePropertyStr(target *Object, prop unistring.String, desc PropertyDescriptor) (bool, bool) {
return h.boolProxyCall(proxy_trap_defineProperty, target, stringValueFromRaw(prop), desc.toValue(h.handler.runtime))
}
func (h *jsProxyHandler) definePropertyIdx(target *Object, prop valueInt, desc PropertyDescriptor) (bool, bool) {
return h.boolProxyCall(proxy_trap_defineProperty, target, prop.toString(), desc.toValue(h.handler.runtime))
}
func (h *jsProxyHandler) definePropertySym(target *Object, prop *Symbol, desc PropertyDescriptor) (bool, bool) {
return h.boolProxyCall(proxy_trap_defineProperty, target, prop, desc.toValue(h.handler.runtime))
}
func (h *jsProxyHandler) hasStr(target *Object, prop unistring.String) (bool, bool) {
return h.boolProxyCall(proxy_trap_has, target, stringValueFromRaw(prop))
}
func (h *jsProxyHandler) hasIdx(target *Object, prop valueInt) (bool, bool) {
return h.boolProxyCall(proxy_trap_has, target, prop.toString())
}
func (h *jsProxyHandler) hasSym(target *Object, prop *Symbol) (bool, bool) {
return h.boolProxyCall(proxy_trap_has, target, prop)
}
func (h *jsProxyHandler) getStr(target *Object, prop unistring.String, receiver Value) (Value, bool) {
return h.proxyCall(proxy_trap_get, target, stringValueFromRaw(prop), receiver)
}
func (h *jsProxyHandler) getIdx(target *Object, prop valueInt, receiver Value) (Value, bool) {
return h.proxyCall(proxy_trap_get, target, prop.toString(), receiver)
}
func (h *jsProxyHandler) getSym(target *Object, prop *Symbol, receiver Value) (Value, bool) {
return h.proxyCall(proxy_trap_get, target, prop, receiver)
}
func (h *jsProxyHandler) setStr(target *Object, prop unistring.String, value Value, receiver Value) (bool, bool) {
return h.boolProxyCall(proxy_trap_set, target, stringValueFromRaw(prop), value, receiver)
}
func (h *jsProxyHandler) setIdx(target *Object, prop valueInt, value Value, receiver Value) (bool, bool) {
return h.boolProxyCall(proxy_trap_set, target, prop.toString(), value, receiver)
}
func (h *jsProxyHandler) setSym(target *Object, prop *Symbol, value Value, receiver Value) (bool, bool) {
return h.boolProxyCall(proxy_trap_set, target, prop, value, receiver)
}
func (h *jsProxyHandler) deleteStr(target *Object, prop unistring.String) (bool, bool) {
return h.boolProxyCall(proxy_trap_deleteProperty, target, stringValueFromRaw(prop))
}
func (h *jsProxyHandler) deleteIdx(target *Object, prop valueInt) (bool, bool) {
return h.boolProxyCall(proxy_trap_deleteProperty, target, prop.toString())
}
func (h *jsProxyHandler) deleteSym(target *Object, prop *Symbol) (bool, bool) {
return h.boolProxyCall(proxy_trap_deleteProperty, target, prop)
}
func (h *jsProxyHandler) ownKeys(target *Object) (*Object, bool) {
if v, ok := h.proxyCall(proxy_trap_ownKeys, target); ok {
return h.handler.runtime.toObject(v), true
}
return nil, false
}
func (h *jsProxyHandler) apply(target *Object, this Value, args []Value) (Value, bool) {
return h.proxyCall(proxy_trap_apply, target, this, h.handler.runtime.newArrayValues(args))
}
func (h *jsProxyHandler) construct(target *Object, args []Value, newTarget *Object) (Value, bool) {
return h.proxyCall(proxy_trap_construct, target, h.handler.runtime.newArrayValues(args), newTarget)
}
type proxyObject struct {
baseObject
target *Object
handler proxyHandler
call func(FunctionCall) Value
ctor func(args []Value, newTarget *Object) *Object
}
func (p *proxyObject) checkHandler() proxyHandler {
r := p.val.runtime
if handler := p.handler; handler != nil {
return handler
}
panic(r.NewTypeError("Proxy already revoked"))
}
func (p *proxyObject) proto() *Object {
target := p.target
if v, ok := p.checkHandler().getPrototypeOf(target); ok {
var handlerProto *Object
if v != _null {
handlerProto = p.val.runtime.toObject(v)
}
if !target.self.isExtensible() && !p.__sameValue(handlerProto, target.self.proto()) {
panic(p.val.runtime.NewTypeError("'getPrototypeOf' on proxy: proxy target is non-extensible but the trap did not return its actual prototype"))
}
return handlerProto
}
return target.self.proto()
}
func (p *proxyObject) setProto(proto *Object, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().setPrototypeOf(target, proto); ok {
if v {
if !target.self.isExtensible() && !p.__sameValue(proto, target.self.proto()) {
panic(p.val.runtime.NewTypeError("'setPrototypeOf' on proxy: trap returned truish for setting a new prototype on the non-extensible proxy target"))
}
return true
} else {
p.val.runtime.typeErrorResult(throw, "'setPrototypeOf' on proxy: trap returned falsish")
return false
}
}
return target.self.setProto(proto, throw)
}
func (p *proxyObject) isExtensible() bool {
target := p.target
if booleanTrapResult, ok := p.checkHandler().isExtensible(p.target); ok {
if te := target.self.isExtensible(); booleanTrapResult != te {
panic(p.val.runtime.NewTypeError("'isExtensible' on proxy: trap result does not reflect extensibility of proxy target (which is '%v')", te))
}
return booleanTrapResult
}
return target.self.isExtensible()
}
func (p *proxyObject) preventExtensions(throw bool) bool {
target := p.target
if booleanTrapResult, ok := p.checkHandler().preventExtensions(target); ok {
if !booleanTrapResult {
p.val.runtime.typeErrorResult(throw, "'preventExtensions' on proxy: trap returned falsish")
return false
}
if te := target.self.isExtensible(); booleanTrapResult && te {
panic(p.val.runtime.NewTypeError("'preventExtensions' on proxy: trap returned truish but the proxy target is extensible"))
}
}
return target.self.preventExtensions(throw)
}
func propToValueProp(v Value) *valueProperty {
if v == nil {
return nil
}
if v, ok := v.(*valueProperty); ok {
return v
}
return &valueProperty{
value: v,
writable: true,
configurable: true,
enumerable: true,
}
}
func (p *proxyObject) proxyDefineOwnPropertyPreCheck(trapResult, throw bool) bool {
if !trapResult {
p.val.runtime.typeErrorResult(throw, "'defineProperty' on proxy: trap returned falsish")
return false
}
return true
}
func (p *proxyObject) proxyDefineOwnPropertyPostCheck(prop Value, target *Object, descr PropertyDescriptor) {
targetDesc := propToValueProp(prop)
extensibleTarget := target.self.isExtensible()
settingConfigFalse := descr.Configurable == FLAG_FALSE
if targetDesc == nil {
if !extensibleTarget {
panic(p.val.runtime.NewTypeError())
}
if settingConfigFalse {
panic(p.val.runtime.NewTypeError())
}
} else {
if !p.__isCompatibleDescriptor(extensibleTarget, &descr, targetDesc) {
panic(p.val.runtime.NewTypeError())
}
if settingConfigFalse && targetDesc.configurable {
panic(p.val.runtime.NewTypeError())
}
if targetDesc.value != nil && !targetDesc.configurable && targetDesc.writable {
if descr.Writable == FLAG_FALSE {
panic(p.val.runtime.NewTypeError())
}
}
}
}
func (p *proxyObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
target := p.target
if booleanTrapResult, ok := p.checkHandler().definePropertyStr(target, name, descr); ok {
if !p.proxyDefineOwnPropertyPreCheck(booleanTrapResult, throw) {
return false
}
p.proxyDefineOwnPropertyPostCheck(target.self.getOwnPropStr(name), target, descr)
return true
}
return target.self.defineOwnPropertyStr(name, descr, throw)
}
func (p *proxyObject) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
target := p.target
if booleanTrapResult, ok := p.checkHandler().definePropertyIdx(target, idx, descr); ok {
if !p.proxyDefineOwnPropertyPreCheck(booleanTrapResult, throw) {
return false
}
p.proxyDefineOwnPropertyPostCheck(target.self.getOwnPropIdx(idx), target, descr)
return true
}
return target.self.defineOwnPropertyIdx(idx, descr, throw)
}
func (p *proxyObject) defineOwnPropertySym(s *Symbol, descr PropertyDescriptor, throw bool) bool {
target := p.target
if booleanTrapResult, ok := p.checkHandler().definePropertySym(target, s, descr); ok {
if !p.proxyDefineOwnPropertyPreCheck(booleanTrapResult, throw) {
return false
}
p.proxyDefineOwnPropertyPostCheck(target.self.getOwnPropSym(s), target, descr)
return true
}
return target.self.defineOwnPropertySym(s, descr, throw)
}
func (p *proxyObject) proxyHasChecks(targetProp Value, target *Object, name fmt.Stringer) {
targetDesc := propToValueProp(targetProp)
if targetDesc != nil {
if !targetDesc.configurable {
panic(p.val.runtime.NewTypeError("'has' on proxy: trap returned falsish for property '%s' which exists in the proxy target as non-configurable", name.String()))
}
if !target.self.isExtensible() {
panic(p.val.runtime.NewTypeError("'has' on proxy: trap returned falsish for property '%s' but the proxy target is not extensible", name.String()))
}
}
}
func (p *proxyObject) hasPropertyStr(name unistring.String) bool {
target := p.target
if b, ok := p.checkHandler().hasStr(target, name); ok {
if !b {
p.proxyHasChecks(target.self.getOwnPropStr(name), target, name)
}
return b
}
return target.self.hasPropertyStr(name)
}
func (p *proxyObject) hasPropertyIdx(idx valueInt) bool {
target := p.target
if b, ok := p.checkHandler().hasIdx(target, idx); ok {
if !b {
p.proxyHasChecks(target.self.getOwnPropIdx(idx), target, idx)
}
return b
}
return target.self.hasPropertyIdx(idx)
}
func (p *proxyObject) hasPropertySym(s *Symbol) bool {
target := p.target
if b, ok := p.checkHandler().hasSym(target, s); ok {
if !b {
p.proxyHasChecks(target.self.getOwnPropSym(s), target, s)
}
return b
}
return target.self.hasPropertySym(s)
}
func (p *proxyObject) hasOwnPropertyStr(name unistring.String) bool {
return p.getOwnPropStr(name) != nil
}
func (p *proxyObject) hasOwnPropertyIdx(idx valueInt) bool {
return p.getOwnPropIdx(idx) != nil
}
func (p *proxyObject) hasOwnPropertySym(s *Symbol) bool {
return p.getOwnPropSym(s) != nil
}
func (p *proxyObject) proxyGetOwnPropertyDescriptor(targetProp Value, target *Object, trapResult Value, name fmt.Stringer) Value {
r := p.val.runtime
targetDesc := propToValueProp(targetProp)
var trapResultObj *Object
if trapResult != nil && trapResult != _undefined {
if obj, ok := trapResult.(*Object); ok {
trapResultObj = obj
} else {
panic(r.NewTypeError("'getOwnPropertyDescriptor' on proxy: trap returned neither object nor undefined for property '%s'", name.String()))
}
}
if trapResultObj == nil {
if targetDesc == nil {
return nil
}
if !targetDesc.configurable {
panic(r.NewTypeError())
}
if !target.self.isExtensible() {
panic(r.NewTypeError())
}
return nil
}
extensibleTarget := target.self.isExtensible()
resultDesc := r.toPropertyDescriptor(trapResultObj)
resultDesc.complete()
if !p.__isCompatibleDescriptor(extensibleTarget, &resultDesc, targetDesc) {
panic(r.NewTypeError("'getOwnPropertyDescriptor' on proxy: trap returned descriptor for property '%s' that is incompatible with the existing property in the proxy target", name.String()))
}
if resultDesc.Configurable == FLAG_FALSE {
if targetDesc == nil {
panic(r.NewTypeError("'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property '%s' which is non-existent in the proxy target", name.String()))
}
if targetDesc.configurable {
panic(r.NewTypeError("'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property '%s' which is configurable in the proxy target", name.String()))
}
if resultDesc.Writable == FLAG_FALSE && targetDesc.writable {
panic(r.NewTypeError("'getOwnPropertyDescriptor' on proxy: trap reported non-configurable and writable for property '%s' which is non-configurable, non-writable in the proxy target", name.String()))
}
}
if resultDesc.Writable == FLAG_TRUE && resultDesc.Configurable == FLAG_TRUE &&
resultDesc.Enumerable == FLAG_TRUE {
return resultDesc.Value
}
return r.toValueProp(trapResultObj)
}
func (p *proxyObject) getOwnPropStr(name unistring.String) Value {
target := p.target
if v, ok := p.checkHandler().getOwnPropertyDescriptorStr(target, name); ok {
return p.proxyGetOwnPropertyDescriptor(target.self.getOwnPropStr(name), target, v, name)
}
return target.self.getOwnPropStr(name)
}
func (p *proxyObject) getOwnPropIdx(idx valueInt) Value {
target := p.target
if v, ok := p.checkHandler().getOwnPropertyDescriptorIdx(target, idx); ok {
return p.proxyGetOwnPropertyDescriptor(target.self.getOwnPropIdx(idx), target, v, idx)
}
return target.self.getOwnPropIdx(idx)
}
func (p *proxyObject) getOwnPropSym(s *Symbol) Value {
target := p.target
if v, ok := p.checkHandler().getOwnPropertyDescriptorSym(target, s); ok {
return p.proxyGetOwnPropertyDescriptor(target.self.getOwnPropSym(s), target, v, s)
}
return target.self.getOwnPropSym(s)
}
func (p *proxyObject) proxyGetChecks(targetProp, trapResult Value, name fmt.Stringer) {
if targetDesc, ok := targetProp.(*valueProperty); ok {
if !targetDesc.accessor {
if !targetDesc.writable && !targetDesc.configurable && !trapResult.SameAs(targetDesc.value) {
panic(p.val.runtime.NewTypeError("'get' on proxy: property '%s' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected '%s' but got '%s')", name.String(), nilSafe(targetDesc.value), ret))
}
} else {
if !targetDesc.configurable && targetDesc.getterFunc == nil && trapResult != _undefined {
panic(p.val.runtime.NewTypeError("'get' on proxy: property '%s' is a non-configurable accessor property on the proxy target and does not have a getter function, but the trap did not return 'undefined' (got '%s')", name.String(), ret))
}
}
}
}
func (p *proxyObject) getStr(name unistring.String, receiver Value) Value {
target := p.target
if receiver == nil {
receiver = p.val
}
if v, ok := p.checkHandler().getStr(target, name, receiver); ok {
p.proxyGetChecks(target.self.getOwnPropStr(name), v, name)
return v
}
return target.self.getStr(name, receiver)
}
func (p *proxyObject) getIdx(idx valueInt, receiver Value) Value {
target := p.target
if receiver == nil {
receiver = p.val
}
if v, ok := p.checkHandler().getIdx(target, idx, receiver); ok {
p.proxyGetChecks(target.self.getOwnPropIdx(idx), v, idx)
return v
}
return target.self.getIdx(idx, receiver)
}
func (p *proxyObject) getSym(s *Symbol, receiver Value) Value {
target := p.target
if receiver == nil {
receiver = p.val
}
if v, ok := p.checkHandler().getSym(target, s, receiver); ok {
p.proxyGetChecks(target.self.getOwnPropSym(s), v, s)
return v
}
return target.self.getSym(s, receiver)
}
func (p *proxyObject) proxySetPreCheck(trapResult, throw bool, name fmt.Stringer) bool {
if !trapResult {
p.val.runtime.typeErrorResult(throw, "'set' on proxy: trap returned falsish for property '%s'", name.String())
}
return trapResult
}
func (p *proxyObject) proxySetPostCheck(targetProp, value Value, name fmt.Stringer) {
if prop, ok := targetProp.(*valueProperty); ok {
if prop.accessor {
if !prop.configurable && prop.setterFunc == nil {
panic(p.val.runtime.NewTypeError("'set' on proxy: trap returned truish for property '%s' which exists in the proxy target as a non-configurable and non-writable accessor property without a setter", name.String()))
}
} else if !prop.configurable && !prop.writable && !p.__sameValue(prop.value, value) {
panic(p.val.runtime.NewTypeError("'set' on proxy: trap returned truish for property '%s' which exists in the proxy target as a non-configurable and non-writable data property with a different value", name.String()))
}
}
}
func (p *proxyObject) proxySetStr(name unistring.String, value, receiver Value, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().setStr(target, name, value, receiver); ok {
if p.proxySetPreCheck(v, throw, name) {
p.proxySetPostCheck(target.self.getOwnPropStr(name), value, name)
return true
}
return false
}
return target.setStr(name, value, receiver, throw)
}
func (p *proxyObject) proxySetIdx(idx valueInt, value, receiver Value, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().setIdx(target, idx, value, receiver); ok {
if p.proxySetPreCheck(v, throw, idx) {
p.proxySetPostCheck(target.self.getOwnPropIdx(idx), value, idx)
return true
}
return false
}
return target.setIdx(idx, value, receiver, throw)
}
func (p *proxyObject) proxySetSym(s *Symbol, value, receiver Value, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().setSym(target, s, value, receiver); ok {
if p.proxySetPreCheck(v, throw, s) {
p.proxySetPostCheck(target.self.getOwnPropSym(s), value, s)
return true
}
return false
}
return target.setSym(s, value, receiver, throw)
}
func (p *proxyObject) setOwnStr(name unistring.String, v Value, throw bool) bool {
return p.proxySetStr(name, v, p.val, throw)
}
func (p *proxyObject) setOwnIdx(idx valueInt, v Value, throw bool) bool {
return p.proxySetIdx(idx, v, p.val, throw)
}
func (p *proxyObject) setOwnSym(s *Symbol, v Value, throw bool) bool {
return p.proxySetSym(s, v, p.val, throw)
}
func (p *proxyObject) setForeignStr(name unistring.String, v, receiver Value, throw bool) (bool, bool) {
return p.proxySetStr(name, v, receiver, throw), true
}
func (p *proxyObject) setForeignIdx(idx valueInt, v, receiver Value, throw bool) (bool, bool) {
return p.proxySetIdx(idx, v, receiver, throw), true
}
func (p *proxyObject) setForeignSym(s *Symbol, v, receiver Value, throw bool) (bool, bool) {
return p.proxySetSym(s, v, receiver, throw), true
}
func (p *proxyObject) proxyDeleteCheck(trapResult bool, targetProp Value, name fmt.Stringer, target *Object, throw bool) {
if trapResult {
if targetProp == nil {
return
}
if targetDesc, ok := targetProp.(*valueProperty); ok {
if !targetDesc.configurable {
panic(p.val.runtime.NewTypeError("'deleteProperty' on proxy: property '%s' is a non-configurable property but the trap returned truish", name.String()))
}
}
if !target.self.isExtensible() {
panic(p.val.runtime.NewTypeError("'deleteProperty' on proxy: trap returned truish for property '%s' but the proxy target is non-extensible", name.String()))
}
} else {
p.val.runtime.typeErrorResult(throw, "'deleteProperty' on proxy: trap returned falsish for property '%s'", name.String())
}
}
func (p *proxyObject) deleteStr(name unistring.String, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().deleteStr(target, name); ok {
p.proxyDeleteCheck(v, target.self.getOwnPropStr(name), name, target, throw)
return v
}
return target.self.deleteStr(name, throw)
}
func (p *proxyObject) deleteIdx(idx valueInt, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().deleteIdx(target, idx); ok {
p.proxyDeleteCheck(v, target.self.getOwnPropIdx(idx), idx, target, throw)
return v
}
return target.self.deleteIdx(idx, throw)
}
func (p *proxyObject) deleteSym(s *Symbol, throw bool) bool {
target := p.target
if v, ok := p.checkHandler().deleteSym(target, s); ok {
p.proxyDeleteCheck(v, target.self.getOwnPropSym(s), s, target, throw)
return v
}
return target.self.deleteSym(s, throw)
}
func (p *proxyObject) keys(all bool, _ []Value) []Value {
if v, ok := p.proxyOwnKeys(); ok {
if !all {
k := 0
for i, key := range v {
prop := p.val.getOwnProp(key)
if prop == nil || prop == _undefined {
continue
}
if prop, ok := prop.(*valueProperty); ok && !prop.enumerable {
continue
}
if k != i {
v[k] = v[i]
}
k++
}
v = v[:k]
}
return v
}
return p.target.self.keys(all, nil)
}
func (p *proxyObject) proxyOwnKeys() ([]Value, bool) {
target := p.target
if v, ok := p.checkHandler().ownKeys(target); ok {
keys := p.val.runtime.toObject(v)
var keyList []Value
keySet := make(map[Value]struct{})
l := toLength(keys.self.getStr("length", nil))
for k := int64(0); k < l; k++ {
item := keys.self.getIdx(valueInt(k), nil)
if _, ok := item.(valueString); !ok {
if _, ok := item.(*Symbol); !ok {
panic(p.val.runtime.NewTypeError("%s is not a valid property name", item.String()))
}
}
if _, exists := keySet[item]; exists {
panic(p.val.runtime.NewTypeError("'ownKeys' on proxy: trap returned duplicate entries"))
}
keyList = append(keyList, item)
keySet[item] = struct{}{}
}
ext := target.self.isExtensible()
for item, next := target.self.iterateKeys()(); next != nil; item, next = next() {
if _, exists := keySet[item.name]; exists {
delete(keySet, item.name)
} else {
if !ext {
panic(p.val.runtime.NewTypeError("'ownKeys' on proxy: trap result did not include '%s'", item.name.String()))
}
var prop Value
if item.value == nil {
prop = target.getOwnProp(item.name)
} else {
prop = item.value
}
if prop, ok := prop.(*valueProperty); ok && !prop.configurable {
panic(p.val.runtime.NewTypeError("'ownKeys' on proxy: trap result did not include non-configurable '%s'", item.name.String()))
}
}
}
if !ext && len(keyList) > 0 && len(keySet) > 0 {
panic(p.val.runtime.NewTypeError("'ownKeys' on proxy: trap returned extra keys but proxy target is non-extensible"))
}
return keyList, true
}
return nil, false
}
func (p *proxyObject) iterateStringKeys() iterNextFunc {
return (&proxyPropIter{
p: p,
names: p.stringKeys(true, nil),
}).next
}
func (p *proxyObject) iterateSymbols() iterNextFunc {
return (&proxyPropIter{
p: p,
names: p.symbols(true, nil),
}).next
}
func (p *proxyObject) iterateKeys() iterNextFunc {
return (&proxyPropIter{
p: p,
names: p.keys(true, nil),
}).next
}
func (p *proxyObject) assertCallable() (call func(FunctionCall) Value, ok bool) {
if p.call != nil {
return func(call FunctionCall) Value {
return p.apply(call)
}, true
}
return nil, false
}
func (p *proxyObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
if p.ctor != nil {
return p.construct
}
return nil
}
func (p *proxyObject) apply(call FunctionCall) Value {
if p.call == nil {
panic(p.val.runtime.NewTypeError("proxy target is not a function"))
}
if v, ok := p.checkHandler().apply(p.target, nilSafe(call.This), call.Arguments); ok {
return v
}
return p.call(call)
}
func (p *proxyObject) construct(args []Value, newTarget *Object) *Object {
if p.ctor == nil {
panic(p.val.runtime.NewTypeError("proxy target is not a constructor"))
}
if newTarget == nil {
newTarget = p.val
}
if v, ok := p.checkHandler().construct(p.target, args, newTarget); ok {
return p.val.runtime.toObject(v)
}
return p.ctor(args, newTarget)
}
func (p *proxyObject) __isCompatibleDescriptor(extensible bool, desc *PropertyDescriptor, current *valueProperty) bool {
if current == nil {
return extensible
}
if !current.configurable {
if desc.Configurable == FLAG_TRUE {
return false
}
if desc.Enumerable != FLAG_NOT_SET && desc.Enumerable.Bool() != current.enumerable {
return false
}
if desc.IsGeneric() {
return true
}
if desc.IsData() != !current.accessor {
return desc.Configurable != FLAG_FALSE
}
if desc.IsData() && !current.accessor {
if !current.configurable {
if desc.Writable == FLAG_TRUE && !current.writable {
return false
}
if !current.writable {
if desc.Value != nil && !desc.Value.SameAs(current.value) {
return false
}
}
}
return true
}
if desc.IsAccessor() && current.accessor {
if !current.configurable {
if desc.Setter != nil && desc.Setter.SameAs(current.setterFunc) {
return false
}
if desc.Getter != nil && desc.Getter.SameAs(current.getterFunc) {
return false
}
}
}
}
return true
}
func (p *proxyObject) __sameValue(val1, val2 Value) bool {
if val1 == nil && val2 == nil {
return true
}
if val1 != nil {
return val1.SameAs(val2)
}
return false
}
func (p *proxyObject) filterKeys(vals []Value, all, symbols bool) []Value {
if !all {
k := 0
for i, val := range vals {
var prop Value
if symbols {
if s, ok := val.(*Symbol); ok {
prop = p.getOwnPropSym(s)
} else {
continue
}
} else {
if _, ok := val.(*Symbol); !ok {
prop = p.getOwnPropStr(val.string())
} else {
continue
}
}
if prop == nil {
continue
}
if prop, ok := prop.(*valueProperty); ok && !prop.enumerable {
continue
}
if k != i {
vals[k] = vals[i]
}
k++
}
vals = vals[:k]
} else {
k := 0
for i, val := range vals {
if _, ok := val.(*Symbol); ok != symbols {
continue
}
if k != i {
vals[k] = vals[i]
}
k++
}
vals = vals[:k]
}
return vals