-
Notifications
You must be signed in to change notification settings - Fork 378
/
object.go
1824 lines (1611 loc) · 44.2 KB
/
object.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"
"math"
"reflect"
"sort"
"github.com/dop251/goja/unistring"
)
const (
classObject = "Object"
classArray = "Array"
classWeakSet = "WeakSet"
classWeakMap = "WeakMap"
classMap = "Map"
classMath = "Math"
classSet = "Set"
classFunction = "Function"
classAsyncFunction = "AsyncFunction"
classNumber = "Number"
classString = "String"
classBoolean = "Boolean"
classError = "Error"
classRegExp = "RegExp"
classDate = "Date"
classJSON = "JSON"
classGlobal = "global"
classPromise = "Promise"
classArrayIterator = "Array Iterator"
classMapIterator = "Map Iterator"
classSetIterator = "Set Iterator"
classStringIterator = "String Iterator"
classRegExpStringIterator = "RegExp String Iterator"
classGenerator = "Generator"
classGeneratorFunction = "GeneratorFunction"
)
var (
hintDefault Value = asciiString("default")
hintNumber Value = asciiString("number")
hintString Value = asciiString("string")
)
type Object struct {
id uint64
runtime *Runtime
self objectImpl
weakRefs map[weakMap]Value
}
type iterNextFunc func() (propIterItem, iterNextFunc)
type PropertyDescriptor struct {
jsDescriptor *Object
Value Value
Writable, Configurable, Enumerable Flag
Getter, Setter Value
}
func (p *PropertyDescriptor) Empty() bool {
var empty PropertyDescriptor
return *p == empty
}
func (p *PropertyDescriptor) IsAccessor() bool {
return p.Setter != nil || p.Getter != nil
}
func (p *PropertyDescriptor) IsData() bool {
return p.Value != nil || p.Writable != FLAG_NOT_SET
}
func (p *PropertyDescriptor) IsGeneric() bool {
return !p.IsAccessor() && !p.IsData()
}
func (p *PropertyDescriptor) toValue(r *Runtime) Value {
if p.jsDescriptor != nil {
return p.jsDescriptor
}
if p.Empty() {
return _undefined
}
o := r.NewObject()
s := o.self
if p.Value != nil {
s._putProp("value", p.Value, true, true, true)
}
if p.Writable != FLAG_NOT_SET {
s._putProp("writable", valueBool(p.Writable.Bool()), true, true, true)
}
if p.Enumerable != FLAG_NOT_SET {
s._putProp("enumerable", valueBool(p.Enumerable.Bool()), true, true, true)
}
if p.Configurable != FLAG_NOT_SET {
s._putProp("configurable", valueBool(p.Configurable.Bool()), true, true, true)
}
if p.Getter != nil {
s._putProp("get", p.Getter, true, true, true)
}
if p.Setter != nil {
s._putProp("set", p.Setter, true, true, true)
}
return o
}
func (p *PropertyDescriptor) complete() {
if p.Getter == nil && p.Setter == nil {
if p.Value == nil {
p.Value = _undefined
}
if p.Writable == FLAG_NOT_SET {
p.Writable = FLAG_FALSE
}
} else {
if p.Getter == nil {
p.Getter = _undefined
}
if p.Setter == nil {
p.Setter = _undefined
}
}
if p.Enumerable == FLAG_NOT_SET {
p.Enumerable = FLAG_FALSE
}
if p.Configurable == FLAG_NOT_SET {
p.Configurable = FLAG_FALSE
}
}
type objectExportCacheItem map[reflect.Type]interface{}
type objectExportCtx struct {
cache map[*Object]interface{}
}
type objectImpl interface {
sortable
className() string
typeOf() String
getStr(p unistring.String, receiver Value) Value
getIdx(p valueInt, receiver Value) Value
getSym(p *Symbol, receiver Value) Value
getOwnPropStr(unistring.String) Value
getOwnPropIdx(valueInt) Value
getOwnPropSym(*Symbol) Value
setOwnStr(p unistring.String, v Value, throw bool) bool
setOwnIdx(p valueInt, v Value, throw bool) bool
setOwnSym(p *Symbol, v Value, throw bool) bool
setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool)
setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool)
setForeignSym(p *Symbol, v, receiver Value, throw bool) (res bool, handled bool)
hasPropertyStr(unistring.String) bool
hasPropertyIdx(idx valueInt) bool
hasPropertySym(s *Symbol) bool
hasOwnPropertyStr(unistring.String) bool
hasOwnPropertyIdx(valueInt) bool
hasOwnPropertySym(s *Symbol) bool
defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool
defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool
defineOwnPropertySym(name *Symbol, desc PropertyDescriptor, throw bool) bool
deleteStr(name unistring.String, throw bool) bool
deleteIdx(idx valueInt, throw bool) bool
deleteSym(s *Symbol, throw bool) bool
assertCallable() (call func(FunctionCall) Value, ok bool)
vmCall(vm *vm, n int)
assertConstructor() func(args []Value, newTarget *Object) *Object
proto() *Object
setProto(proto *Object, throw bool) bool
hasInstance(v Value) bool
isExtensible() bool
preventExtensions(throw bool) bool
export(ctx *objectExportCtx) interface{}
exportType() reflect.Type
exportToMap(m reflect.Value, typ reflect.Type, ctx *objectExportCtx) error
exportToArrayOrSlice(s reflect.Value, typ reflect.Type, ctx *objectExportCtx) error
equal(objectImpl) bool
iterateStringKeys() iterNextFunc
iterateSymbols() iterNextFunc
iterateKeys() iterNextFunc
stringKeys(all bool, accum []Value) []Value
symbols(all bool, accum []Value) []Value
keys(all bool, accum []Value) []Value
_putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value
_putSym(s *Symbol, prop Value)
getPrivateEnv(typ *privateEnvType, create bool) *privateElements
}
type baseObject struct {
class string
val *Object
prototype *Object
extensible bool
values map[unistring.String]Value
propNames []unistring.String
lastSortedPropLen, idxPropCount int
symValues *orderedMap
privateElements map[*privateEnvType]*privateElements
}
type guardedObject struct {
baseObject
guardedProps map[unistring.String]struct{}
}
type primitiveValueObject struct {
baseObject
pValue Value
}
func (o *primitiveValueObject) export(*objectExportCtx) interface{} {
return o.pValue.Export()
}
func (o *primitiveValueObject) exportType() reflect.Type {
return o.pValue.ExportType()
}
type FunctionCall struct {
This Value
Arguments []Value
}
type ConstructorCall struct {
This *Object
Arguments []Value
NewTarget *Object
}
func (f FunctionCall) Argument(idx int) Value {
if idx < len(f.Arguments) {
return f.Arguments[idx]
}
return _undefined
}
func (f ConstructorCall) Argument(idx int) Value {
if idx < len(f.Arguments) {
return f.Arguments[idx]
}
return _undefined
}
func (o *baseObject) init() {
o.values = make(map[unistring.String]Value)
}
func (o *baseObject) className() string {
return o.class
}
func (o *baseObject) typeOf() String {
return stringObjectC
}
func (o *baseObject) hasPropertyStr(name unistring.String) bool {
if o.val.self.hasOwnPropertyStr(name) {
return true
}
if o.prototype != nil {
return o.prototype.self.hasPropertyStr(name)
}
return false
}
func (o *baseObject) hasPropertyIdx(idx valueInt) bool {
return o.val.self.hasPropertyStr(idx.string())
}
func (o *baseObject) hasPropertySym(s *Symbol) bool {
if o.hasOwnPropertySym(s) {
return true
}
if o.prototype != nil {
return o.prototype.self.hasPropertySym(s)
}
return false
}
func (o *baseObject) getWithOwnProp(prop, p, receiver Value) Value {
if prop == nil && o.prototype != nil {
if receiver == nil {
return o.prototype.get(p, o.val)
}
return o.prototype.get(p, receiver)
}
if prop, ok := prop.(*valueProperty); ok {
if receiver == nil {
return prop.get(o.val)
}
return prop.get(receiver)
}
return prop
}
func (o *baseObject) getStrWithOwnProp(prop Value, name unistring.String, receiver Value) Value {
if prop == nil && o.prototype != nil {
if receiver == nil {
return o.prototype.self.getStr(name, o.val)
}
return o.prototype.self.getStr(name, receiver)
}
if prop, ok := prop.(*valueProperty); ok {
if receiver == nil {
return prop.get(o.val)
}
return prop.get(receiver)
}
return prop
}
func (o *baseObject) getIdx(idx valueInt, receiver Value) Value {
return o.val.self.getStr(idx.string(), receiver)
}
func (o *baseObject) getSym(s *Symbol, receiver Value) Value {
return o.getWithOwnProp(o.getOwnPropSym(s), s, receiver)
}
func (o *baseObject) getStr(name unistring.String, receiver Value) Value {
prop := o.values[name]
if prop == nil {
if o.prototype != nil {
if receiver == nil {
return o.prototype.self.getStr(name, o.val)
}
return o.prototype.self.getStr(name, receiver)
}
}
if prop, ok := prop.(*valueProperty); ok {
if receiver == nil {
return prop.get(o.val)
}
return prop.get(receiver)
}
return prop
}
func (o *baseObject) getOwnPropIdx(idx valueInt) Value {
return o.val.self.getOwnPropStr(idx.string())
}
func (o *baseObject) getOwnPropSym(s *Symbol) Value {
if o.symValues != nil {
return o.symValues.get(s)
}
return nil
}
func (o *baseObject) getOwnPropStr(name unistring.String) Value {
return o.values[name]
}
func (o *baseObject) checkDeleteProp(name unistring.String, prop *valueProperty, throw bool) bool {
if !prop.configurable {
if throw {
r := o.val.runtime
panic(r.NewTypeError("Cannot delete property '%s' of %s", name, r.objectproto_toString(FunctionCall{This: o.val})))
}
return false
}
return true
}
func (o *baseObject) checkDelete(name unistring.String, val Value, throw bool) bool {
if val, ok := val.(*valueProperty); ok {
return o.checkDeleteProp(name, val, throw)
}
return true
}
func (o *baseObject) _delete(name unistring.String) {
delete(o.values, name)
for i, n := range o.propNames {
if n == name {
names := o.propNames
if namesMarkedForCopy(names) {
newNames := make([]unistring.String, len(names)-1, shrinkCap(len(names), cap(names)))
copy(newNames, names[:i])
copy(newNames[i:], names[i+1:])
o.propNames = newNames
} else {
copy(names[i:], names[i+1:])
names[len(names)-1] = ""
o.propNames = names[:len(names)-1]
}
if i < o.lastSortedPropLen {
o.lastSortedPropLen--
if i < o.idxPropCount {
o.idxPropCount--
}
}
break
}
}
}
func (o *baseObject) deleteIdx(idx valueInt, throw bool) bool {
return o.val.self.deleteStr(idx.string(), throw)
}
func (o *baseObject) deleteSym(s *Symbol, throw bool) bool {
if o.symValues != nil {
if val := o.symValues.get(s); val != nil {
if !o.checkDelete(s.descriptiveString().string(), val, throw) {
return false
}
o.symValues.remove(s)
}
}
return true
}
func (o *baseObject) deleteStr(name unistring.String, throw bool) bool {
if val, exists := o.values[name]; exists {
if !o.checkDelete(name, val, throw) {
return false
}
o._delete(name)
}
return true
}
func (o *baseObject) setProto(proto *Object, throw bool) bool {
current := o.prototype
if current.SameAs(proto) {
return true
}
if !o.extensible {
o.val.runtime.typeErrorResult(throw, "%s is not extensible", o.val)
return false
}
for p := proto; p != nil; p = p.self.proto() {
if p.SameAs(o.val) {
o.val.runtime.typeErrorResult(throw, "Cyclic __proto__ value")
return false
}
if _, ok := p.self.(*proxyObject); ok {
break
}
}
o.prototype = proto
return true
}
func (o *baseObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
ownDesc := o.values[name]
if ownDesc == nil {
if proto := o.prototype; proto != nil {
// we know it's foreign because prototype loops are not allowed
if res, handled := proto.self.setForeignStr(name, val, o.val, throw); handled {
return res
}
}
// new property
if !o.extensible {
o.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
return false
} else {
o.values[name] = val
names := copyNamesIfNeeded(o.propNames, 1)
o.propNames = append(names, name)
}
return true
}
if prop, ok := ownDesc.(*valueProperty); ok {
if !prop.isWritable() {
o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
return false
} else {
prop.set(o.val, val)
}
} else {
o.values[name] = val
}
return true
}
func (o *baseObject) setOwnIdx(idx valueInt, val Value, throw bool) bool {
return o.val.self.setOwnStr(idx.string(), val, throw)
}
func (o *baseObject) setOwnSym(name *Symbol, val Value, throw bool) bool {
var ownDesc Value
if o.symValues != nil {
ownDesc = o.symValues.get(name)
}
if ownDesc == nil {
if proto := o.prototype; proto != nil {
// we know it's foreign because prototype loops are not allowed
if res, handled := proto.self.setForeignSym(name, val, o.val, throw); handled {
return res
}
}
// new property
if !o.extensible {
o.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
return false
} else {
if o.symValues == nil {
o.symValues = newOrderedMap(nil)
}
o.symValues.set(name, val)
}
return true
}
if prop, ok := ownDesc.(*valueProperty); ok {
if !prop.isWritable() {
o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
return false
} else {
prop.set(o.val, val)
}
} else {
o.symValues.set(name, val)
}
return true
}
func (o *baseObject) _setForeignStr(name unistring.String, prop, val, receiver Value, throw bool) (bool, bool) {
if prop != nil {
if prop, ok := prop.(*valueProperty); ok {
if !prop.isWritable() {
o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
return false, true
}
if prop.setterFunc != nil {
prop.set(receiver, val)
return true, true
}
}
} else {
if proto := o.prototype; proto != nil {
if receiver != proto {
return proto.self.setForeignStr(name, val, receiver, throw)
}
return proto.self.setOwnStr(name, val, throw), true
}
}
return false, false
}
func (o *baseObject) _setForeignIdx(idx valueInt, prop, val, receiver Value, throw bool) (bool, bool) {
if prop != nil {
if prop, ok := prop.(*valueProperty); ok {
if !prop.isWritable() {
o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%d'", idx)
return false, true
}
if prop.setterFunc != nil {
prop.set(receiver, val)
return true, true
}
}
} else {
if proto := o.prototype; proto != nil {
if receiver != proto {
return proto.self.setForeignIdx(idx, val, receiver, throw)
}
return proto.self.setOwnIdx(idx, val, throw), true
}
}
return false, false
}
func (o *baseObject) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignStr(name, o.values[name], val, receiver, throw)
}
func (o *baseObject) setForeignIdx(name valueInt, val, receiver Value, throw bool) (bool, bool) {
if idx := toIdx(name); idx != math.MaxUint32 {
o.ensurePropOrder()
if o.idxPropCount == 0 {
return o._setForeignIdx(name, name, nil, receiver, throw)
}
}
return o.setForeignStr(name.string(), val, receiver, throw)
}
func (o *baseObject) setForeignSym(name *Symbol, val, receiver Value, throw bool) (bool, bool) {
var prop Value
if o.symValues != nil {
prop = o.symValues.get(name)
}
if prop != nil {
if prop, ok := prop.(*valueProperty); ok {
if !prop.isWritable() {
o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
return false, true
}
if prop.setterFunc != nil {
prop.set(receiver, val)
return true, true
}
}
} else {
if proto := o.prototype; proto != nil {
if receiver != o.val {
return proto.self.setForeignSym(name, val, receiver, throw)
}
return proto.self.setOwnSym(name, val, throw), true
}
}
return false, false
}
func (o *baseObject) hasOwnPropertySym(s *Symbol) bool {
if o.symValues != nil {
return o.symValues.has(s)
}
return false
}
func (o *baseObject) hasOwnPropertyStr(name unistring.String) bool {
_, exists := o.values[name]
return exists
}
func (o *baseObject) hasOwnPropertyIdx(idx valueInt) bool {
return o.val.self.hasOwnPropertyStr(idx.string())
}
func (o *baseObject) _defineOwnProperty(name unistring.String, existingValue Value, descr PropertyDescriptor, throw bool) (val Value, ok bool) {
getterObj, _ := descr.Getter.(*Object)
setterObj, _ := descr.Setter.(*Object)
var existing *valueProperty
if existingValue == nil {
if !o.extensible {
o.val.runtime.typeErrorResult(throw, "Cannot define property %s, object is not extensible", name)
return nil, false
}
existing = &valueProperty{}
} else {
if existing, ok = existingValue.(*valueProperty); !ok {
existing = &valueProperty{
writable: true,
enumerable: true,
configurable: true,
value: existingValue,
}
}
if !existing.configurable {
if descr.Configurable == FLAG_TRUE {
goto Reject
}
if descr.Enumerable != FLAG_NOT_SET && descr.Enumerable.Bool() != existing.enumerable {
goto Reject
}
}
if existing.accessor && descr.Value != nil || !existing.accessor && (getterObj != nil || setterObj != nil) {
if !existing.configurable {
goto Reject
}
} else if !existing.accessor {
if !existing.configurable {
if !existing.writable {
if descr.Writable == FLAG_TRUE {
goto Reject
}
if descr.Value != nil && !descr.Value.SameAs(existing.value) {
goto Reject
}
}
}
} else {
if !existing.configurable {
if descr.Getter != nil && existing.getterFunc != getterObj || descr.Setter != nil && existing.setterFunc != setterObj {
goto Reject
}
}
}
}
if descr.Writable == FLAG_TRUE && descr.Enumerable == FLAG_TRUE && descr.Configurable == FLAG_TRUE && descr.Value != nil {
return descr.Value, true
}
if descr.Writable != FLAG_NOT_SET {
existing.writable = descr.Writable.Bool()
}
if descr.Enumerable != FLAG_NOT_SET {
existing.enumerable = descr.Enumerable.Bool()
}
if descr.Configurable != FLAG_NOT_SET {
existing.configurable = descr.Configurable.Bool()
}
if descr.Value != nil {
existing.value = descr.Value
existing.getterFunc = nil
existing.setterFunc = nil
}
if descr.Value != nil || descr.Writable != FLAG_NOT_SET {
existing.accessor = false
}
if descr.Getter != nil {
existing.getterFunc = propGetter(o.val, descr.Getter, o.val.runtime)
existing.value = nil
existing.accessor = true
}
if descr.Setter != nil {
existing.setterFunc = propSetter(o.val, descr.Setter, o.val.runtime)
existing.value = nil
existing.accessor = true
}
if !existing.accessor && existing.value == nil {
existing.value = _undefined
}
return existing, true
Reject:
o.val.runtime.typeErrorResult(throw, "Cannot redefine property: %s", name)
return nil, false
}
func (o *baseObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
existingVal := o.values[name]
if v, ok := o._defineOwnProperty(name, existingVal, descr, throw); ok {
o.values[name] = v
if existingVal == nil {
names := copyNamesIfNeeded(o.propNames, 1)
o.propNames = append(names, name)
}
return true
}
return false
}
func (o *baseObject) defineOwnPropertyIdx(idx valueInt, desc PropertyDescriptor, throw bool) bool {
return o.val.self.defineOwnPropertyStr(idx.string(), desc, throw)
}
func (o *baseObject) defineOwnPropertySym(s *Symbol, descr PropertyDescriptor, throw bool) bool {
var existingVal Value
if o.symValues != nil {
existingVal = o.symValues.get(s)
}
if v, ok := o._defineOwnProperty(s.descriptiveString().string(), existingVal, descr, throw); ok {
if o.symValues == nil {
o.symValues = newOrderedMap(nil)
}
o.symValues.set(s, v)
return true
}
return false
}
func (o *baseObject) _put(name unistring.String, v Value) {
if _, exists := o.values[name]; !exists {
names := copyNamesIfNeeded(o.propNames, 1)
o.propNames = append(names, name)
}
o.values[name] = v
}
func valueProp(value Value, writable, enumerable, configurable bool) Value {
if writable && enumerable && configurable {
return value
}
return &valueProperty{
value: value,
writable: writable,
enumerable: enumerable,
configurable: configurable,
}
}
func (o *baseObject) _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value {
prop := valueProp(value, writable, enumerable, configurable)
o._put(name, prop)
return prop
}
func (o *baseObject) _putSym(s *Symbol, prop Value) {
if o.symValues == nil {
o.symValues = newOrderedMap(nil)
}
o.symValues.set(s, prop)
}
func (o *baseObject) getPrivateEnv(typ *privateEnvType, create bool) *privateElements {
env := o.privateElements[typ]
if env != nil && create {
panic(o.val.runtime.NewTypeError("Private fields for the class have already been set"))
}
if env == nil && create {
env = &privateElements{
fields: make([]Value, typ.numFields),
}
if o.privateElements == nil {
o.privateElements = make(map[*privateEnvType]*privateElements)
}
o.privateElements[typ] = env
}
return env
}
func (o *Object) tryPrimitive(methodName unistring.String) Value {
if method, ok := o.self.getStr(methodName, nil).(*Object); ok {
if call, ok := method.self.assertCallable(); ok {
v := call(FunctionCall{
This: o,
})
if _, fail := v.(*Object); !fail {
return v
}
}
}
return nil
}
func (o *Object) ordinaryToPrimitiveNumber() Value {
if v := o.tryPrimitive("valueOf"); v != nil {
return v
}
if v := o.tryPrimitive("toString"); v != nil {
return v
}
panic(o.runtime.NewTypeError("Could not convert %v to primitive", o.self))
}
func (o *Object) ordinaryToPrimitiveString() Value {
if v := o.tryPrimitive("toString"); v != nil {
return v
}
if v := o.tryPrimitive("valueOf"); v != nil {
return v
}
panic(o.runtime.NewTypeError("Could not convert %v (%T) to primitive", o.self, o.self))
}
func (o *Object) tryExoticToPrimitive(hint Value) Value {
exoticToPrimitive := toMethod(o.self.getSym(SymToPrimitive, nil))
if exoticToPrimitive != nil {
ret := exoticToPrimitive(FunctionCall{
This: o,
Arguments: []Value{hint},
})
if _, fail := ret.(*Object); !fail {
return ret
}
panic(o.runtime.NewTypeError("Cannot convert object to primitive value"))
}
return nil
}
func (o *Object) toPrimitiveNumber() Value {
if v := o.tryExoticToPrimitive(hintNumber); v != nil {
return v
}
return o.ordinaryToPrimitiveNumber()
}
func (o *Object) toPrimitiveString() Value {
if v := o.tryExoticToPrimitive(hintString); v != nil {
return v
}
return o.ordinaryToPrimitiveString()
}
func (o *Object) toPrimitive() Value {
if v := o.tryExoticToPrimitive(hintDefault); v != nil {
return v
}
return o.ordinaryToPrimitiveNumber()
}
func (o *baseObject) assertCallable() (func(FunctionCall) Value, bool) {
return nil, false
}
func (o *baseObject) vmCall(vm *vm, _ int) {
panic(vm.r.NewTypeError("Not a function: %s", o.val.toString()))
}
func (o *baseObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
return nil
}
func (o *baseObject) proto() *Object {
return o.prototype
}
func (o *baseObject) isExtensible() bool {
return o.extensible
}
func (o *baseObject) preventExtensions(bool) bool {
o.extensible = false
return true
}
func (o *baseObject) sortLen() int {
return toIntStrict(toLength(o.val.self.getStr("length", nil)))
}
func (o *baseObject) sortGet(i int) Value {
return o.val.self.getIdx(valueInt(i), nil)
}
func (o *baseObject) swap(i int, j int) {
ii := valueInt(i)
jj := valueInt(j)
x := o.val.self.getIdx(ii, nil)
y := o.val.self.getIdx(jj, nil)
o.val.self.setOwnIdx(ii, y, false)
o.val.self.setOwnIdx(jj, x, false)
}
func (o *baseObject) export(ctx *objectExportCtx) interface{} {
if v, exists := ctx.get(o.val); exists {
return v
}
keys := o.stringKeys(false, nil)
m := make(map[string]interface{}, len(keys))
ctx.put(o.val, m)
for _, itemName := range keys {
itemNameStr := itemName.String()
v := o.val.self.getStr(itemName.string(), nil)
if v != nil {
m[itemNameStr] = exportValue(v, ctx)
} else {
m[itemNameStr] = nil
}
}
return m
}
func (o *baseObject) exportType() reflect.Type {
return reflectTypeMap
}
func genericExportToMap(o *Object, dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
dst.Set(reflect.MakeMap(typ))
ctx.putTyped(o, typ, dst.Interface())
keyTyp := typ.Key()
elemTyp := typ.Elem()
needConvertKeys := !reflectTypeString.AssignableTo(keyTyp)
iter := &enumerableIter{
o: o,
wrapped: o.self.iterateStringKeys(),
}
r := o.runtime
for item, next := iter.next(); next != nil; item, next = next() {
var kv reflect.Value
var err error
if needConvertKeys {
kv = reflect.New(keyTyp).Elem()
err = r.toReflectValue(item.name, kv, ctx)
if err != nil {