Debian Codegraph @

compiler/natives/src/reflect/reflect.go

raw
1 // +build js
2
3 package reflect
4
5 import (
6 "errors"
7 "strconv"
8 "unsafe"
9
10 "github.com/gopherjs/gopherjs/js"
11 )
12
13 var initialized = false
14
15 func init() {
16 // avoid dead code elimination
17 used := func(i interface{}) {}
18 used(rtype{})
19 used(uncommonType{})
20 used(method{})
21 used(arrayType{})
22 used(chanType{})
23 used(funcType{})
24 used(interfaceType{})
25 used(mapType{})
26 used(ptrType{})
27 used(sliceType{})
28 used(structType{})
29 used(imethod{})
30 used(structField{})
31
32 initialized = true
33 uint8Type = TypeOf(uint8(0)).(*rtype) // set for real
34 }
35
36 func jsType(typ Type) *js.Object {
37 return js.InternalObject(typ).Get("jsType")
38 }
39
40 func reflectType(typ *js.Object) *rtype {
41 if typ.Get("reflectType") == js.Undefined {
42 rt := &rtype{
43 size: uintptr(typ.Get("size").Int()),
44 kind: uint8(typ.Get("kind").Int()),
45 str: newNameOff(newName(internalStr(typ.Get("string")), "", "", typ.Get("exported").Bool())),
46 }
47 js.InternalObject(rt).Set("jsType", typ)
48 typ.Set("reflectType", js.InternalObject(rt))
49
50 methodSet := js.Global.Call("$methodSet", typ)
51 if methodSet.Length() != 0 || typ.Get("named").Bool() {
52 rt.tflag |= tflagUncommon
53 if typ.Get("named").Bool() {
54 rt.tflag |= tflagNamed
55 }
56 reflectMethods := make([]method, methodSet.Length())
57 for i := range reflectMethods {
58 m := methodSet.Index(i)
59 reflectMethods[i] = method{
60 name: newNameOff(newName(internalStr(m.Get("name")), "", "", internalStr(m.Get("pkg")) == "")),
61 mtyp: newTypeOff(reflectType(m.Get("typ"))),
62 }
63 }
64 ut := &uncommonType{
65 pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", "", false)),
66 mcount: uint16(methodSet.Length()),
67 _methods: reflectMethods,
68 }
69 uncommonTypeMap[rt] = ut
70 js.InternalObject(ut).Set("jsType", typ)
71 }
72
73 switch rt.Kind() {
74 case Array:
75 setKindType(rt, &arrayType{
76 elem: reflectType(typ.Get("elem")),
77 len: uintptr(typ.Get("len").Int()),
78 })
79 case Chan:
80 dir := BothDir
81 if typ.Get("sendOnly").Bool() {
82 dir = SendDir
83 }
84 if typ.Get("recvOnly").Bool() {
85 dir = RecvDir
86 }
87 setKindType(rt, &chanType{
88 elem: reflectType(typ.Get("elem")),
89 dir: uintptr(dir),
90 })
91 case Func:
92 params := typ.Get("params")
93 in := make([]*rtype, params.Length())
94 for i := range in {
95 in[i] = reflectType(params.Index(i))
96 }
97 results := typ.Get("results")
98 out := make([]*rtype, results.Length())
99 for i := range out {
100 out[i] = reflectType(results.Index(i))
101 }
102 outCount := uint16(results.Length())
103 if typ.Get("variadic").Bool() {
104 outCount |= 1 << 15
105 }
106 setKindType(rt, &funcType{
107 rtype: *rt,
108 inCount: uint16(params.Length()),
109 outCount: outCount,
110 _in: in,
111 _out: out,
112 })
113 case Interface:
114 methods := typ.Get("methods")
115 imethods := make([]imethod, methods.Length())
116 for i := range imethods {
117 m := methods.Index(i)
118 imethods[i] = imethod{
119 name: newNameOff(newName(internalStr(m.Get("name")), "", "", internalStr(m.Get("pkg")) == "")),
120 typ: newTypeOff(reflectType(m.Get("typ"))),
121 }
122 }
123 setKindType(rt, &interfaceType{
124 rtype: *rt,
125 pkgPath: newName(internalStr(typ.Get("pkg")), "", "", false),
126 methods: imethods,
127 })
128 case Map:
129 setKindType(rt, &mapType{
130 key: reflectType(typ.Get("key")),
131 elem: reflectType(typ.Get("elem")),
132 })
133 case Ptr:
134 setKindType(rt, &ptrType{
135 elem: reflectType(typ.Get("elem")),
136 })
137 case Slice:
138 setKindType(rt, &sliceType{
139 elem: reflectType(typ.Get("elem")),
140 })
141 case Struct:
142 fields := typ.Get("fields")
143 reflectFields := make([]structField, fields.Length())
144 for i := range reflectFields {
145 f := fields.Index(i)
146 offsetAnon := uintptr(i) << 1
147 if f.Get("anonymous").Bool() {
148 offsetAnon |= 1
149 }
150 reflectFields[i] = structField{
151 name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), "", f.Get("exported").Bool()),
152 typ: reflectType(f.Get("typ")),
153 offsetAnon: offsetAnon,
154 }
155 }
156 setKindType(rt, &structType{
157 rtype: *rt,
158 pkgPath: newName(internalStr(typ.Get("pkgPath")), "", "", false),
159 fields: reflectFields,
160 })
161 }
162 }
163
164 return (*rtype)(unsafe.Pointer(typ.Get("reflectType").Unsafe()))
165 }
166
167 func setKindType(rt *rtype, kindType interface{}) {
168 js.InternalObject(rt).Set("kindType", js.InternalObject(kindType))
169 js.InternalObject(kindType).Set("rtype", js.InternalObject(rt))
170 }
171
172 type uncommonType struct {
173 pkgPath nameOff
174 mcount uint16
175 _ uint16
176 moff uint32
177 _ uint32
178
179 _methods []method
180 }
181
182 func (t *uncommonType) methods() []method {
183 return t._methods
184 }
185
186 var uncommonTypeMap = make(map[*rtype]*uncommonType)
187
188 func (t *rtype) uncommon() *uncommonType {
189 return uncommonTypeMap[t]
190 }
191
192 type funcType struct {
193 rtype `reflect:"func"`
194 inCount uint16
195 outCount uint16
196
197 _in []*rtype
198 _out []*rtype
199 }
200
201 func (t *funcType) in() []*rtype {
202 return t._in
203 }
204
205 func (t *funcType) out() []*rtype {
206 return t._out
207 }
208
209 type name struct {
210 bytes *byte
211 }
212
213 type nameData struct {
214 name string
215 tag string
216 pkgPath string
217 exported bool
218 }
219
220 var nameMap = make(map[*byte]*nameData)
221
222 func (n name) name() (s string) {
223 return nameMap[n.bytes].name
224 }
225
226 func (n name) tag() (s string) {
227 return nameMap[n.bytes].tag
228 }
229
230 func (n name) pkgPath() string {
231 return nameMap[n.bytes].pkgPath
232 }
233
234 func (n name) isExported() bool {
235 return nameMap[n.bytes].exported
236 }
237
238 func newName(n, tag, pkgPath string, exported bool) name {
239 b := new(byte)
240 nameMap[b] = &nameData{
241 name: n,
242 tag: tag,
243 pkgPath: pkgPath,
244 exported: exported,
245 }
246 return name{
247 bytes: b,
248 }
249 }
250
251 var nameOffList []name
252
253 func (t *rtype) nameOff(off nameOff) name {
254 return nameOffList[int(off)]
255 }
256
257 func newNameOff(n name) nameOff {
258 i := len(nameOffList)
259 nameOffList = append(nameOffList, n)
260 return nameOff(i)
261 }
262
263 var typeOffList []*rtype
264
265 func (t *rtype) typeOff(off typeOff) *rtype {
266 return typeOffList[int(off)]
267 }
268
269 func newTypeOff(t *rtype) typeOff {
270 i := len(typeOffList)
271 typeOffList = append(typeOffList, t)
272 return typeOff(i)
273 }
274
275 func internalStr(strObj *js.Object) string {
276 var c struct{ str string }
277 js.InternalObject(c).Set("str", strObj) // get string without internalizing
278 return c.str
279 }
280
281 func isWrapped(typ Type) bool {
282 return jsType(typ).Get("wrapped").Bool()
283 }
284
285 func copyStruct(dst, src *js.Object, typ Type) {
286 fields := jsType(typ).Get("fields")
287 for i := 0; i < fields.Length(); i++ {
288 prop := fields.Index(i).Get("prop").String()
289 dst.Set(prop, src.Get(prop))
290 }
291 }
292
293 func makeValue(t Type, v *js.Object, fl flag) Value {
294 rt := t.common()
295 if t.Kind() == Array || t.Kind() == Struct || t.Kind() == Ptr {
296 return Value{rt, unsafe.Pointer(v.Unsafe()), fl | flag(t.Kind())}
297 }
298 return Value{rt, unsafe.Pointer(js.Global.Call("$newDataPointer", v, jsType(rt.ptrTo())).Unsafe()), fl | flag(t.Kind()) | flagIndir}
299 }
300
301 func MakeSlice(typ Type, len, cap int) Value {
302 if typ.Kind() != Slice {
303 panic("reflect.MakeSlice of non-slice type")
304 }
305 if len < 0 {
306 panic("reflect.MakeSlice: negative len")
307 }
308 if cap < 0 {
309 panic("reflect.MakeSlice: negative cap")
310 }
311 if len > cap {
312 panic("reflect.MakeSlice: len > cap")
313 }
314
315 return makeValue(typ, js.Global.Call("$makeSlice", jsType(typ), len, cap, js.InternalObject(func() *js.Object { return jsType(typ.Elem()).Call("zero") })), 0)
316 }
317
318 func TypeOf(i interface{}) Type {
319 if !initialized { // avoid error of uint8Type
320 return &rtype{}
321 }
322 if i == nil {
323 return nil
324 }
325 return reflectType(js.InternalObject(i).Get("constructor"))
326 }
327
328 func ValueOf(i interface{}) Value {
329 if i == nil {
330 return Value{}
331 }
332 return makeValue(reflectType(js.InternalObject(i).Get("constructor")), js.InternalObject(i).Get("$val"), 0)
333 }
334
335 func ArrayOf(count int, elem Type) Type {
336 return reflectType(js.Global.Call("$arrayType", jsType(elem), count))
337 }
338
339 func ChanOf(dir ChanDir, t Type) Type {
340 return reflectType(js.Global.Call("$chanType", jsType(t), dir == SendDir, dir == RecvDir))
341 }
342
343 func FuncOf(in, out []Type, variadic bool) Type {
344 if variadic && (len(in) == 0 || in[len(in)-1].Kind() != Slice) {
345 panic("reflect.FuncOf: last arg of variadic func must be slice")
346 }
347
348 jsIn := make([]*js.Object, len(in))
349 for i, v := range in {
350 jsIn[i] = jsType(v)
351 }
352 jsOut := make([]*js.Object, len(out))
353 for i, v := range out {
354 jsOut[i] = jsType(v)
355 }
356 return reflectType(js.Global.Call("$funcType", jsIn, jsOut, variadic))
357 }
358
359 func MapOf(key, elem Type) Type {
360 switch key.Kind() {
361 case Func, Map, Slice:
362 panic("reflect.MapOf: invalid key type " + key.String())
363 }
364
365 return reflectType(js.Global.Call("$mapType", jsType(key), jsType(elem)))
366 }
367
368 func (t *rtype) ptrTo() *rtype {
369 return reflectType(js.Global.Call("$ptrType", jsType(t)))
370 }
371
372 func SliceOf(t Type) Type {
373 return reflectType(js.Global.Call("$sliceType", jsType(t)))
374 }
375
376 // func StructOf(fields []StructField) Type {
377 // jsFields := make([]*js.Object, len(fields))
378 // fset := map[string]struct{}{}
379 // for i, f := range fields {
380 // if f.Type == nil {
381 // panic("reflect.StructOf: field " + strconv.Itoa(i) + " has no type")
382 // }
383
384 // name := f.Name
385 // if name == "" {
386 // // Embedded field
387 // if f.Type.Kind() == Ptr {
388 // // Embedded ** and *interface{} are illegal
389 // elem := f.Type.Elem()
390 // if k := elem.Kind(); k == Ptr || k == Interface {
391 // panic("reflect.StructOf: illegal anonymous field type " + f.Type.String())
392 // }
393 // name = elem.String()
394 // } else {
395 // name = f.Type.String()
396 // }
397 // }
398
399 // if _, dup := fset[name]; dup {
400 // panic("reflect.StructOf: duplicate field " + name)
401 // }
402 // fset[name] = struct{}{}
403
404 // jsf := js.Global.Get("Object").New()
405 // jsf.Set("prop", name)
406 // jsf.Set("name", name)
407 // jsf.Set("exported", true)
408 // jsf.Set("typ", jsType(f.Type))
409 // jsf.Set("tag", f.Tag)
410 // jsFields[i] = jsf
411 // }
412 // return reflectType(js.Global.Call("$structType", "", jsFields))
413 // }
414
415 func Zero(typ Type) Value {
416 return makeValue(typ, jsType(typ).Call("zero"), 0)
417 }
418
419 func unsafe_New(typ *rtype) unsafe.Pointer {
420 switch typ.Kind() {
421 case Struct:
422 return unsafe.Pointer(jsType(typ).Get("ptr").New().Unsafe())
423 case Array:
424 return unsafe.Pointer(jsType(typ).Call("zero").Unsafe())
425 default:
426 return unsafe.Pointer(js.Global.Call("$newDataPointer", jsType(typ).Call("zero"), jsType(typ.ptrTo())).Unsafe())
427 }
428 }
429
430 func makeInt(f flag, bits uint64, t Type) Value {
431 typ := t.common()
432 ptr := unsafe_New(typ)
433 switch typ.Kind() {
434 case Int8:
435 *(*int8)(ptr) = int8(bits)
436 case Int16:
437 *(*int16)(ptr) = int16(bits)
438 case Int, Int32:
439 *(*int32)(ptr) = int32(bits)
440 case Int64:
441 *(*int64)(ptr) = int64(bits)
442 case Uint8:
443 *(*uint8)(ptr) = uint8(bits)
444 case Uint16:
445 *(*uint16)(ptr) = uint16(bits)
446 case Uint, Uint32, Uintptr:
447 *(*uint32)(ptr) = uint32(bits)
448 case Uint64:
449 *(*uint64)(ptr) = uint64(bits)
450 }
451 return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
452 }
453
454 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
455 if typ.Kind() != Func {
456 panic("reflect: call of MakeFunc with non-Func type")
457 }
458
459 t := typ.common()
460 ftyp := (*funcType)(unsafe.Pointer(t))
461
462 fv := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
463 args := make([]Value, ftyp.NumIn())
464 for i := range args {
465 argType := ftyp.In(i).common()
466 args[i] = makeValue(argType, arguments[i], 0)
467 }
468 resultsSlice := fn(args)
469 switch ftyp.NumOut() {
470 case 0:
471 return nil
472 case 1:
473 return resultsSlice[0].object()
474 default:
475 results := js.Global.Get("Array").New(ftyp.NumOut())
476 for i, r := range resultsSlice {
477 results.SetIndex(i, r.object())
478 }
479 return results
480 }
481 })
482
483 return Value{t, unsafe.Pointer(fv.Unsafe()), flag(Func)}
484 }
485
486 func typedmemmove(t *rtype, dst, src unsafe.Pointer) {
487 js.InternalObject(dst).Call("$set", js.InternalObject(src).Call("$get"))
488 }
489
490 func loadScalar(p unsafe.Pointer, n uintptr) uintptr {
491 return js.InternalObject(p).Call("$get").Unsafe()
492 }
493
494 func makechan(typ *rtype, size uint64) (ch unsafe.Pointer) {
495 ctyp := (*chanType)(unsafe.Pointer(typ))
496 return unsafe.Pointer(js.Global.Get("$Chan").New(jsType(ctyp.elem), size).Unsafe())
497 }
498
499 func makemap(t *rtype, cap int) (m unsafe.Pointer) {
500 return unsafe.Pointer(js.Global.Get("Object").New().Unsafe())
501 }
502
503 func keyFor(t *rtype, key unsafe.Pointer) (*js.Object, string) {
504 kv := js.InternalObject(key)
505 if kv.Get("$get") != js.Undefined {
506 kv = kv.Call("$get")
507 }
508 k := jsType(t.Key()).Call("keyFor", kv).String()
509 return kv, k
510 }
511
512 func mapaccess(t *rtype, m, key unsafe.Pointer) unsafe.Pointer {
513 _, k := keyFor(t, key)
514 entry := js.InternalObject(m).Get(k)
515 if entry == js.Undefined {
516 return nil
517 }
518 return unsafe.Pointer(js.Global.Call("$newDataPointer", entry.Get("v"), jsType(PtrTo(t.Elem()))).Unsafe())
519 }
520
521 func mapassign(t *rtype, m, key, val unsafe.Pointer) {
522 kv, k := keyFor(t, key)
523 jsVal := js.InternalObject(val).Call("$get")
524 et := t.Elem()
525 if et.Kind() == Struct {
526 newVal := jsType(et).Call("zero")
527 copyStruct(newVal, jsVal, et)
528 jsVal = newVal
529 }
530 entry := js.Global.Get("Object").New()
531 entry.Set("k", kv)
532 entry.Set("v", jsVal)
533 js.InternalObject(m).Set(k, entry)
534 }
535
536 func mapdelete(t *rtype, m unsafe.Pointer, key unsafe.Pointer) {
537 _, k := keyFor(t, key)
538 js.InternalObject(m).Delete(k)
539 }
540
541 type mapIter struct {
542 t Type
543 m *js.Object
544 keys *js.Object
545 i int
546 }
547
548 func mapiterinit(t *rtype, m unsafe.Pointer) *byte {
549 return (*byte)(unsafe.Pointer(&mapIter{t, js.InternalObject(m), js.Global.Call("$keys", js.InternalObject(m)), 0}))
550 }
551
552 func mapiterkey(it *byte) unsafe.Pointer {
553 iter := (*mapIter)(unsafe.Pointer(it))
554 k := iter.keys.Index(iter.i)
555 return unsafe.Pointer(js.Global.Call("$newDataPointer", iter.m.Get(k.String()).Get("k"), jsType(PtrTo(iter.t.Key()))).Unsafe())
556 }
557
558 func mapiternext(it *byte) {
559 iter := (*mapIter)(unsafe.Pointer(it))
560 iter.i++
561 }
562
563 func maplen(m unsafe.Pointer) int {
564 return js.Global.Call("$keys", js.InternalObject(m)).Length()
565 }
566
567 func cvtDirect(v Value, typ Type) Value {
568 var srcVal = v.object()
569 if srcVal == jsType(v.typ).Get("nil") {
570 return makeValue(typ, jsType(typ).Get("nil"), v.flag)
571 }
572
573 var val *js.Object
574 switch k := typ.Kind(); k {
575 case Slice:
576 slice := jsType(typ).New(srcVal.Get("$array"))
577 slice.Set("$offset", srcVal.Get("$offset"))
578 slice.Set("$length", srcVal.Get("$length"))
579 slice.Set("$capacity", srcVal.Get("$capacity"))
580 val = js.Global.Call("$newDataPointer", slice, jsType(PtrTo(typ)))
581 case Ptr:
582 if typ.Elem().Kind() == Struct {
583 if typ.Elem() == v.typ.Elem() {
584 val = srcVal
585 break
586 }
587 val = jsType(typ).New()
588 copyStruct(val, srcVal, typ.Elem())
589 break
590 }
591 val = jsType(typ).New(srcVal.Get("$get"), srcVal.Get("$set"))
592 case Struct:
593 val = jsType(typ).Get("ptr").New()
594 copyStruct(val, srcVal, typ)
595 case Array, Bool, Chan, Func, Interface, Map, String:
596 val = js.InternalObject(v.ptr)
597 default:
598 panic(&ValueError{"reflect.Convert", k})
599 }
600 return Value{typ.common(), unsafe.Pointer(val.Unsafe()), v.flag&(flagRO|flagIndir) | flag(typ.Kind())}
601 }
602
603 func Copy(dst, src Value) int {
604 dk := dst.kind()
605 if dk != Array && dk != Slice {
606 panic(&ValueError{"reflect.Copy", dk})
607 }
608 if dk == Array {
609 dst.mustBeAssignable()
610 }
611 dst.mustBeExported()
612
613 sk := src.kind()
614 if sk != Array && sk != Slice {
615 panic(&ValueError{"reflect.Copy", sk})
616 }
617 src.mustBeExported()
618
619 typesMustMatch("reflect.Copy", dst.typ.Elem(), src.typ.Elem())
620
621 dstVal := dst.object()
622 if dk == Array {
623 dstVal = jsType(SliceOf(dst.typ.Elem())).New(dstVal)
624 }
625
626 srcVal := src.object()
627 if sk == Array {
628 srcVal = jsType(SliceOf(src.typ.Elem())).New(srcVal)
629 }
630
631 return js.Global.Call("$copySlice", dstVal, srcVal).Int()
632 }
633
634 func methodReceiver(op string, v Value, i int) (_, t *rtype, fn unsafe.Pointer) {
635 var prop string
636 if v.typ.Kind() == Interface {
637 tt := (*interfaceType)(unsafe.Pointer(v.typ))
638 if i < 0 || i >= len(tt.methods) {
639 panic("reflect: internal error: invalid method index")
640 }
641 m := &tt.methods[i]
642 if !tt.nameOff(m.name).isExported() {
643 panic("reflect: " + op + " of unexported method")
644 }
645 t = tt.typeOff(m.typ)
646 prop = tt.nameOff(m.name).name()
647 } else {
648 ut := v.typ.uncommon()
649 if ut == nil || uint(i) >= uint(ut.mcount) {
650 panic("reflect: internal error: invalid method index")
651 }
652 m := ut.methods()[i]
653 if !v.typ.nameOff(m.name).isExported() {
654 panic("reflect: " + op + " of unexported method")
655 }
656 t = v.typ.typeOff(m.mtyp)
657 prop = js.Global.Call("$methodSet", jsType(v.typ)).Index(i).Get("prop").String()
658 }
659 rcvr := v.object()
660 if isWrapped(v.typ) {
661 rcvr = jsType(v.typ).New(rcvr)
662 }
663 fn = unsafe.Pointer(rcvr.Get(prop).Unsafe())
664 return
665 }
666
667 func valueInterface(v Value, safe bool) interface{} {
668 if v.flag == 0 {
669 panic(&ValueError{"reflect.Value.Interface", 0})
670 }
671 if safe && v.flag&flagRO != 0 {
672 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
673 }
674 if v.flag&flagMethod != 0 {
675 v = makeMethodValue("Interface", v)
676 }
677
678 if isWrapped(v.typ) {
679 return interface{}(unsafe.Pointer(jsType(v.typ).New(v.object()).Unsafe()))
680 }
681 return interface{}(unsafe.Pointer(v.object().Unsafe()))
682 }
683
684 func ifaceE2I(t *rtype, src interface{}, dst unsafe.Pointer) {
685 js.InternalObject(dst).Call("$set", js.InternalObject(src))
686 }
687
688 func methodName() string {
689 return "?FIXME?"
690 }
691
692 func makeMethodValue(op string, v Value) Value {
693 if v.flag&flagMethod == 0 {
694 panic("reflect: internal error: invalid use of makePartialFunc")
695 }
696
697 _, _, fn := methodReceiver(op, v, int(v.flag)>>flagMethodShift)
698 rcvr := v.object()
699 if isWrapped(v.typ) {
700 rcvr = jsType(v.typ).New(rcvr)
701 }
702 fv := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
703 return js.InternalObject(fn).Call("apply", rcvr, arguments)
704 })
705 return Value{v.Type().common(), unsafe.Pointer(fv.Unsafe()), v.flag&flagRO | flag(Func)}
706 }
707
708 func (t *rtype) pointers() bool {
709 switch t.Kind() {
710 case Ptr, Map, Chan, Func, Struct, Array:
711 return true
712 default:
713 return false
714 }
715 }
716
717 func (t *rtype) Comparable() bool {
718 switch t.Kind() {
719 case Func, Slice, Map:
720 return false
721 case Array:
722 return t.Elem().Comparable()
723 case Struct:
724 for i := 0; i < t.NumField(); i++ {
725 if !t.Field(i).Type.Comparable() {
726 return false
727 }
728 }
729 }
730 return true
731 }
732
733 func (t *rtype) Method(i int) (m Method) {
734 if t.Kind() == Interface {
735 tt := (*interfaceType)(unsafe.Pointer(t))
736 return tt.Method(i)
737 }
738 methods := t.exportedMethods()
739 if i < 0 || i >= len(methods) {
740 panic("reflect: Method index out of range")
741 }
742 p := methods[i]
743 pname := t.nameOff(p.name)
744 m.Name = pname.name()
745 fl := flag(Func)
746 mtyp := t.typeOff(p.mtyp)
747 ft := (*funcType)(unsafe.Pointer(mtyp))
748 in := make([]Type, 0, 1+len(ft.in()))
749 in = append(in, t)
750 for _, arg := range ft.in() {
751 in = append(in, arg)
752 }
753 out := make([]Type, 0, len(ft.out()))
754 for _, ret := range ft.out() {
755 out = append(out, ret)
756 }
757 mt := FuncOf(in, out, ft.IsVariadic())
758 m.Type = mt
759 prop := js.Global.Call("$methodSet", js.InternalObject(t).Get("jsType")).Index(i).Get("prop").String()
760 fn := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
761 rcvr := arguments[0]
762 return rcvr.Get(prop).Call("apply", rcvr, arguments[1:])
763 })
764 m.Func = Value{mt.(*rtype), unsafe.Pointer(fn.Unsafe()), fl}
765
766 m.Index = i
767 return m
768 }
769
770 func (v Value) object() *js.Object {
771 if v.typ.Kind() == Array || v.typ.Kind() == Struct {
772 return js.InternalObject(v.ptr)
773 }
774 if v.flag&flagIndir != 0 {
775 val := js.InternalObject(v.ptr).Call("$get")
776 if val != js.Global.Get("$ifaceNil") && val.Get("constructor") != jsType(v.typ) {
777 switch v.typ.Kind() {
778 case Uint64, Int64:
779 val = jsType(v.typ).New(val.Get("$high"), val.Get("$low"))
780 case Complex64, Complex128:
781 val = jsType(v.typ).New(val.Get("$real"), val.Get("$imag"))
782 case Slice:
783 if val == val.Get("constructor").Get("nil") {
784 val = jsType(v.typ).Get("nil")
785 break
786 }
787 newVal := jsType(v.typ).New(val.Get("$array"))
788 newVal.Set("$offset", val.Get("$offset"))
789 newVal.Set("$length", val.Get("$length"))
790 newVal.Set("$capacity", val.Get("$capacity"))
791 val = newVal
792 }
793 }
794 return js.InternalObject(val.Unsafe())
795 }
796 return js.InternalObject(v.ptr)
797 }
798
799 var callHelper = js.Global.Get("$call").Interface().(func(...interface{}) *js.Object)
800
801 func (v Value) call(op string, in []Value) []Value {
802 var (
803 t *rtype
804 fn unsafe.Pointer
805 rcvr *js.Object
806 )
807 if v.flag&flagMethod != 0 {
808 _, t, fn = methodReceiver(op, v, int(v.flag)>>flagMethodShift)
809 rcvr = v.object()
810 if isWrapped(v.typ) {
811 rcvr = jsType(v.typ).New(rcvr)
812 }
813 } else {
814 t = v.typ
815 fn = unsafe.Pointer(v.object().Unsafe())
816 rcvr = js.Undefined
817 }
818
819 if fn == nil {
820 panic("reflect.Value.Call: call of nil function")
821 }
822
823 isSlice := op == "CallSlice"
824 n := t.NumIn()
825 if isSlice {
826 if !t.IsVariadic() {
827 panic("reflect: CallSlice of non-variadic function")
828 }
829 if len(in) < n {
830 panic("reflect: CallSlice with too few input arguments")
831 }
832 if len(in) > n {
833 panic("reflect: CallSlice with too many input arguments")
834 }
835 } else {
836 if t.IsVariadic() {
837 n--
838 }
839 if len(in) < n {
840 panic("reflect: Call with too few input arguments")
841 }
842 if !t.IsVariadic() && len(in) > n {
843 panic("reflect: Call with too many input arguments")
844 }
845 }
846 for _, x := range in {
847 if x.Kind() == Invalid {
848 panic("reflect: " + op + " using zero Value argument")
849 }
850 }
851 for i := 0; i < n; i++ {
852 if xt, targ := in[i].Type(), t.In(i); !xt.AssignableTo(targ) {
853 panic("reflect: " + op + " using " + xt.String() + " as type " + targ.String())
854 }
855 }
856 if !isSlice && t.IsVariadic() {
857 // prepare slice for remaining values
858 m := len(in) - n
859 slice := MakeSlice(t.In(n), m, m)
860 elem := t.In(n).Elem()
861 for i := 0; i < m; i++ {
862 x := in[n+i]
863 if xt := x.Type(); !xt.AssignableTo(elem) {
864 panic("reflect: cannot use " + xt.String() + " as type " + elem.String() + " in " + op)
865 }
866 slice.Index(i).Set(x)
867 }
868 origIn := in
869 in = make([]Value, n+1)
870 copy(in[:n], origIn)
871 in[n] = slice
872 }
873
874 nin := len(in)
875 if nin != t.NumIn() {
876 panic("reflect.Value.Call: wrong argument count")
877 }
878 nout := t.NumOut()
879
880 argsArray := js.Global.Get("Array").New(t.NumIn())
881 for i, arg := range in {
882 argsArray.SetIndex(i, unwrapJsObject(t.In(i), arg.assignTo("reflect.Value.Call", t.In(i).common(), nil).object()))
883 }
884 results := callHelper(js.InternalObject(fn), rcvr, argsArray)
885
886 switch nout {
887 case 0:
888 return nil
889 case 1:
890 return []Value{makeValue(t.Out(0), wrapJsObject(t.Out(0), results), 0)}
891 default:
892 ret := make([]Value, nout)
893 for i := range ret {
894 ret[i] = makeValue(t.Out(i), wrapJsObject(t.Out(i), results.Index(i)), 0)
895 }
896 return ret
897 }
898 }
899
900 func (v Value) Cap() int {
901 k := v.kind()
902 switch k {
903 case Array:
904 return v.typ.Len()
905 case Chan, Slice:
906 return v.object().Get("$capacity").Int()
907 }
908 panic(&ValueError{"reflect.Value.Cap", k})
909 }
910
911 var jsObjectPtr = reflectType(js.Global.Get("$jsObjectPtr"))
912
913 func wrapJsObject(typ Type, val *js.Object) *js.Object {
914 if typ == jsObjectPtr {
915 return jsType(jsObjectPtr).New(val)
916 }
917 return val
918 }
919
920 func unwrapJsObject(typ Type, val *js.Object) *js.Object {
921 if typ == jsObjectPtr {
922 return val.Get("object")
923 }
924 return val
925 }
926
927 func (v Value) Elem() Value {
928 switch k := v.kind(); k {
929 case Interface:
930 val := v.object()
931 if val == js.Global.Get("$ifaceNil") {
932 return Value{}
933 }
934 typ := reflectType(val.Get("constructor"))
935 return makeValue(typ, val.Get("$val"), v.flag&flagRO)
936
937 case Ptr:
938 if v.IsNil() {
939 return Value{}
940 }
941 val := v.object()
942 tt := (*ptrType)(unsafe.Pointer(v.typ))
943 fl := v.flag&flagRO | flagIndir | flagAddr
944 fl |= flag(tt.elem.Kind())
945 return Value{tt.elem, unsafe.Pointer(wrapJsObject(tt.elem, val).Unsafe()), fl}
946
947 default:
948 panic(&ValueError{"reflect.Value.Elem", k})
949 }
950 }
951
952 func (v Value) Field(i int) Value {
953 if v.kind() != Struct {
954 panic(&ValueError{"reflect.Value.Field", v.kind()})
955 }
956 tt := (*structType)(unsafe.Pointer(v.typ))
957 if uint(i) >= uint(len(tt.fields)) {
958 panic("reflect: Field index out of range")
959 }
960
961 prop := jsType(v.typ).Get("fields").Index(i).Get("prop").String()
962 field := &tt.fields[i]
963 typ := field.typ
964
965 fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
966 if !field.name.isExported() {
967 if field.anon() {
968 fl |= flagEmbedRO
969 } else {
970 fl |= flagStickyRO
971 }
972 }
973
974 if tag := tt.fields[i].name.tag(); tag != "" && i != 0 {
975 if jsTag := getJsTag(tag); jsTag != "" {
976 for {
977 v = v.Field(0)
978 if v.typ == jsObjectPtr {
979 o := v.object().Get("object")
980 return Value{typ, unsafe.Pointer(jsType(PtrTo(typ)).New(
981 js.InternalObject(func() *js.Object { return js.Global.Call("$internalize", o.Get(jsTag), jsType(typ)) }),
982 js.InternalObject(func(x *js.Object) { o.Set(jsTag, js.Global.Call("$externalize", x, jsType(typ))) }),
983 ).Unsafe()), fl}
984 }
985 if v.typ.Kind() == Ptr {
986 v = v.Elem()
987 }
988 }
989 }
990 }
991
992 s := js.InternalObject(v.ptr)
993 if fl&flagIndir != 0 && typ.Kind() != Array && typ.Kind() != Struct {
994 return Value{typ, unsafe.Pointer(jsType(PtrTo(typ)).New(
995 js.InternalObject(func() *js.Object { return wrapJsObject(typ, s.Get(prop)) }),
996 js.InternalObject(func(x *js.Object) { s.Set(prop, unwrapJsObject(typ, x)) }),
997 ).Unsafe()), fl}
998 }
999 return makeValue(typ, wrapJsObject(typ, s.Get(prop)), fl)
1000 }
1001
1002 func getJsTag(tag string) string {
1003 for tag != "" {
1004 // skip leading space
1005 i := 0
1006 for i < len(tag) && tag[i] == ' ' {
1007 i++
1008 }
1009 tag = tag[i:]
1010 if tag == "" {
1011 break
1012 }
1013
1014 // scan to colon.
1015 // a space or a quote is a syntax error
1016 i = 0
1017 for i < len(tag) && tag[i] != ' ' && tag[i] != ':' && tag[i] != '"' {
1018 i++
1019 }
1020 if i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
1021 break
1022 }
1023 name := string(tag[:i])
1024 tag = tag[i+1:]
1025
1026 // scan quoted string to find value
1027 i = 1
1028 for i < len(tag) && tag[i] != '"' {
1029 if tag[i] == '\\' {
1030 i++
1031 }
1032 i++
1033 }
1034 if i >= len(tag) {
1035 break
1036 }
1037 qvalue := string(tag[:i+1])
1038 tag = tag[i+1:]
1039
1040 if name == "js" {
1041 value, _ := strconv.Unquote(qvalue)
1042 return value
1043 }
1044 }
1045 return ""
1046 }
1047
1048 func (v Value) Index(i int) Value {
1049 switch k := v.kind(); k {
1050 case Array:
1051 tt := (*arrayType)(unsafe.Pointer(v.typ))
1052 if i < 0 || i > int(tt.len) {
1053 panic("reflect: array index out of range")
1054 }
1055 typ := tt.elem
1056 fl := v.flag & (flagRO | flagIndir | flagAddr)
1057 fl |= flag(typ.Kind())
1058
1059 a := js.InternalObject(v.ptr)
1060 if fl&flagIndir != 0 && typ.Kind() != Array && typ.Kind() != Struct {
1061 return Value{typ, unsafe.Pointer(jsType(PtrTo(typ)).New(
1062 js.InternalObject(func() *js.Object { return wrapJsObject(typ, a.Index(i)) }),
1063 js.InternalObject(func(x *js.Object) { a.SetIndex(i, unwrapJsObject(typ, x)) }),
1064 ).Unsafe()), fl}
1065 }
1066 return makeValue(typ, wrapJsObject(typ, a.Index(i)), fl)
1067
1068 case Slice:
1069 s := v.object()
1070 if i < 0 || i >= s.Get("$length").Int() {
1071 panic("reflect: slice index out of range")
1072 }
1073 tt := (*sliceType)(unsafe.Pointer(v.typ))
1074 typ := tt.elem
1075 fl := flagAddr | flagIndir | v.flag&flagRO
1076 fl |= flag(typ.Kind())
1077
1078 i += s.Get("$offset").Int()
1079 a := s.Get("$array")
1080 if fl&flagIndir != 0 && typ.Kind() != Array && typ.Kind() != Struct {
1081 return Value{typ, unsafe.Pointer(jsType(PtrTo(typ)).New(
1082 js.InternalObject(func() *js.Object { return wrapJsObject(typ, a.Index(i)) }),
1083 js.InternalObject(func(x *js.Object) { a.SetIndex(i, unwrapJsObject(typ, x)) }),
1084 ).Unsafe()), fl}
1085 }
1086 return makeValue(typ, wrapJsObject(typ, a.Index(i)), fl)
1087
1088 case String:
1089 str := *(*string)(v.ptr)
1090 if i < 0 || i >= len(str) {
1091 panic("reflect: string index out of range")
1092 }
1093 fl := v.flag&flagRO | flag(Uint8)
1094 c := str[i]
1095 return Value{uint8Type, unsafe.Pointer(&c), fl | flagIndir}
1096
1097 default:
1098 panic(&ValueError{"reflect.Value.Index", k})
1099 }
1100 }
1101
1102 func (v Value) InterfaceData() [2]uintptr {
1103 panic(errors.New("InterfaceData is not supported by GopherJS"))
1104 }
1105
1106 func (v Value) IsNil() bool {
1107 switch k := v.kind(); k {
1108 case Ptr, Slice:
1109 return v.object() == jsType(v.typ).Get("nil")
1110 case Chan:
1111 return v.object() == js.Global.Get("$chanNil")
1112 case Func:
1113 return v.object() == js.Global.Get("$throwNilPointerError")
1114 case Map:
1115 return v.object() == js.InternalObject(false)
1116 case Interface:
1117 return v.object() == js.Global.Get("$ifaceNil")
1118 default:
1119 panic(&ValueError{"reflect.Value.IsNil", k})
1120 }
1121 }
1122
1123 func (v Value) Len() int {
1124 switch k := v.kind(); k {
1125 case Array, String:
1126 return v.object().Length()
1127 case Slice:
1128 return v.object().Get("$length").Int()
1129 case Chan:
1130 return v.object().Get("$buffer").Get("length").Int()
1131 case Map:
1132 return js.Global.Call("$keys", v.object()).Length()
1133 default:
1134 panic(&ValueError{"reflect.Value.Len", k})
1135 }
1136 }
1137
1138 func (v Value) Pointer() uintptr {
1139 switch k := v.kind(); k {
1140 case Chan, Map, Ptr, UnsafePointer:
1141 if v.IsNil() {
1142 return 0
1143 }
1144 return v.object().Unsafe()
1145 case Func:
1146 if v.IsNil() {
1147 return 0
1148 }
1149 return 1
1150 case Slice:
1151 if v.IsNil() {
1152 return 0
1153 }
1154 return v.object().Get("$array").Unsafe()
1155 default:
1156 panic(&ValueError{"reflect.Value.Pointer", k})
1157 }
1158 }
1159
1160 func (v Value) Set(x Value) {
1161 v.mustBeAssignable()
1162 x.mustBeExported()
1163 x = x.assignTo("reflect.Set", v.typ, nil)
1164 if v.flag&flagIndir != 0 {
1165 switch v.typ.Kind() {
1166 case Array:
1167 jsType(v.typ).Call("copy", js.InternalObject(v.ptr), js.InternalObject(x.ptr))
1168 case Interface:
1169 js.InternalObject(v.ptr).Call("$set", js.InternalObject(valueInterface(x, false)))
1170 case Struct:
1171 copyStruct(js.InternalObject(v.ptr), js.InternalObject(x.ptr), v.typ)
1172 default:
1173 js.InternalObject(v.ptr).Call("$set", x.object())
1174 }
1175 return
1176 }
1177 v.ptr = x.ptr
1178 }
1179
1180 func (v Value) SetBytes(x []byte) {
1181 v.mustBeAssignable()
1182 v.mustBe(Slice)
1183 if v.typ.Elem().Kind() != Uint8 {
1184 panic("reflect.Value.SetBytes of non-byte slice")
1185 }
1186 slice := js.InternalObject(x)
1187 if v.typ.Name() != "" || v.typ.Elem().Name() != "" {
1188 typedSlice := jsType(v.typ).New(slice.Get("$array"))
1189 typedSlice.Set("$offset", slice.Get("$offset"))
1190 typedSlice.Set("$length", slice.Get("$length"))
1191 typedSlice.Set("$capacity", slice.Get("$capacity"))
1192 slice = typedSlice
1193 }
1194 js.InternalObject(v.ptr).Call("$set", slice)
1195 }
1196
1197 func (v Value) SetCap(n int) {
1198 v.mustBeAssignable()
1199 v.mustBe(Slice)
1200 s := js.InternalObject(v.ptr).Call("$get")
1201 if n < s.Get("$length").Int() || n > s.Get("$capacity").Int() {
1202 panic("reflect: slice capacity out of range in SetCap")
1203 }
1204 newSlice := jsType(v.typ).New(s.Get("$array"))
1205 newSlice.Set("$offset", s.Get("$offset"))
1206 newSlice.Set("$length", s.Get("$length"))
1207 newSlice.Set("$capacity", n)
1208 js.InternalObject(v.ptr).Call("$set", newSlice)
1209 }
1210
1211 func (v Value) SetLen(n int) {
1212 v.mustBeAssignable()
1213 v.mustBe(Slice)
1214 s := js.InternalObject(v.ptr).Call("$get")
1215 if n < 0 || n > s.Get("$capacity").Int() {
1216 panic("reflect: slice length out of range in SetLen")
1217 }
1218 newSlice := jsType(v.typ).New(s.Get("$array"))
1219 newSlice.Set("$offset", s.Get("$offset"))
1220 newSlice.Set("$length", n)
1221 newSlice.Set("$capacity", s.Get("$capacity"))
1222 js.InternalObject(v.ptr).Call("$set", newSlice)
1223 }
1224
1225 func (v Value) Slice(i, j int) Value {
1226 var (
1227 cap int
1228 typ Type
1229 s *js.Object
1230 )
1231 switch kind := v.kind(); kind {
1232 case Array:
1233 if v.flag&flagAddr == 0 {
1234 panic("reflect.Value.Slice: slice of unaddressable array")
1235 }
1236 tt := (*arrayType)(unsafe.Pointer(v.typ))
1237 cap = int(tt.len)
1238 typ = SliceOf(tt.elem)
1239 s = jsType(typ).New(v.object())
1240
1241 case Slice:
1242 typ = v.typ
1243 s = v.object()
1244 cap = s.Get("$capacity").Int()
1245
1246 case String:
1247 str := *(*string)(v.ptr)
1248 if i < 0 || j < i || j > len(str) {
1249 panic("reflect.Value.Slice: string slice index out of bounds")
1250 }
1251 return ValueOf(str[i:j])
1252
1253 default:
1254 panic(&ValueError{"reflect.Value.Slice", kind})
1255 }
1256
1257 if i < 0 || j < i || j > cap {
1258 panic("reflect.Value.Slice: slice index out of bounds")
1259 }
1260
1261 return makeValue(typ, js.Global.Call("$subslice", s, i, j), v.flag&flagRO)
1262 }
1263
1264 func (v Value) Slice3(i, j, k int) Value {
1265 var (
1266 cap int
1267 typ Type
1268 s *js.Object
1269 )
1270 switch kind := v.kind(); kind {
1271 case Array:
1272 if v.flag&flagAddr == 0 {
1273 panic("reflect.Value.Slice: slice of unaddressable array")
1274 }
1275 tt := (*arrayType)(unsafe.Pointer(v.typ))
1276 cap = int(tt.len)
1277 typ = SliceOf(tt.elem)
1278 s = jsType(typ).New(v.object())
1279
1280 case Slice:
1281 typ = v.typ
1282 s = v.object()
1283 cap = s.Get("$capacity").Int()
1284
1285 default:
1286 panic(&ValueError{"reflect.Value.Slice3", kind})
1287 }
1288
1289 if i < 0 || j < i || k < j || k > cap {
1290 panic("reflect.Value.Slice3: slice index out of bounds")
1291 }
1292
1293 return makeValue(typ, js.Global.Call("$subslice", s, i, j, k), v.flag&flagRO)
1294 }
1295
1296 func (v Value) Close() {
1297 v.mustBe(Chan)
1298 v.mustBeExported()
1299 js.Global.Call("$close", v.object())
1300 }
1301
1302 var selectHelper = js.Global.Get("$select").Interface().(func(...interface{}) *js.Object)
1303
1304 func chanrecv(ch unsafe.Pointer, nb bool, val unsafe.Pointer) (selected, received bool) {
1305 comms := [][]*js.Object{{js.InternalObject(ch)}}
1306 if nb {
1307 comms = append(comms, []*js.Object{})
1308 }
1309 selectRes := selectHelper(comms)
1310 if nb && selectRes.Index(0).Int() == 1 {
1311 return false, false
1312 }
1313 recvRes := selectRes.Index(1)
1314 js.InternalObject(val).Call("$set", recvRes.Index(0))
1315 return true, recvRes.Index(1).Bool()
1316 }
1317
1318 func chansend(ch unsafe.Pointer, val unsafe.Pointer, nb bool) bool {
1319 comms := [][]*js.Object{{js.InternalObject(ch), js.InternalObject(val).Call("$get")}}
1320 if nb {
1321 comms = append(comms, []*js.Object{})
1322 }
1323 selectRes := selectHelper(comms)
1324 if nb && selectRes.Index(0).Int() == 1 {
1325 return false
1326 }
1327 return true
1328 }
1329
1330 func rselect(rselects []runtimeSelect) (chosen int, recvOK bool) {
1331 comms := make([][]*js.Object, len(rselects))
1332 for i, s := range rselects {
1333 switch SelectDir(s.dir) {
1334 case SelectDefault:
1335 comms[i] = []*js.Object{}
1336 case SelectRecv:
1337 ch := js.Global.Get("$chanNil")
1338 if js.InternalObject(s.ch) != js.InternalObject(0) {
1339 ch = js.InternalObject(s.ch)
1340 }
1341 comms[i] = []*js.Object{ch}
1342 case SelectSend:
1343 ch := js.Global.Get("$chanNil")
1344 var val *js.Object
1345 if js.InternalObject(s.ch) != js.InternalObject(0) {
1346 ch = js.InternalObject(s.ch)
1347 val = js.InternalObject(s.val).Call("$get")
1348 }
1349 comms[i] = []*js.Object{ch, val}
1350 }
1351 }
1352 selectRes := selectHelper(comms)
1353 c := selectRes.Index(0).Int()
1354 if SelectDir(rselects[c].dir) == SelectRecv {
1355 recvRes := selectRes.Index(1)
1356 js.InternalObject(rselects[c].val).Call("$set", recvRes.Index(0))
1357 return c, recvRes.Index(1).Bool()
1358 }
1359 return c, false
1360 }
1361
1362 func DeepEqual(a1, a2 interface{}) bool {
1363 i1 := js.InternalObject(a1)
1364 i2 := js.InternalObject(a2)
1365 if i1 == i2 {
1366 return true
1367 }
1368 if i1 == nil || i2 == nil || i1.Get("constructor") != i2.Get("constructor") {
1369 return false
1370 }
1371 return deepValueEqualJs(ValueOf(a1), ValueOf(a2), nil)
1372 }
1373
1374 func deepValueEqualJs(v1, v2 Value, visited [][2]unsafe.Pointer) bool {
1375 if !v1.IsValid() || !v2.IsValid() {
1376 return !v1.IsValid() && !v2.IsValid()
1377 }
1378 if v1.Type() != v2.Type() {
1379 return false
1380 }
1381 if v1.Type() == jsObjectPtr {
1382 return unwrapJsObject(jsObjectPtr, v1.object()) == unwrapJsObject(jsObjectPtr, v2.object())
1383 }
1384
1385 switch v1.Kind() {
1386 case Array, Map, Slice, Struct:
1387 for _, entry := range visited {
1388 if v1.ptr == entry[0] && v2.ptr == entry[1] {
1389 return true
1390 }
1391 }
1392 visited = append(visited, [2]unsafe.Pointer{v1.ptr, v2.ptr})
1393 }
1394
1395 switch v1.Kind() {
1396 case Array, Slice:
1397 if v1.Kind() == Slice {
1398 if v1.IsNil() != v2.IsNil() {
1399 return false
1400 }
1401 if v1.object() == v2.object() {
1402 return true
1403 }
1404 }
1405 var n = v1.Len()
1406 if n != v2.Len() {
1407 return false
1408 }
1409 for i := 0; i < n; i++ {
1410 if !deepValueEqualJs(v1.Index(i), v2.Index(i), visited) {
1411 return false
1412 }
1413 }
1414 return true
1415 case Interface:
1416 if v1.IsNil() || v2.IsNil() {
1417 return v1.IsNil() && v2.IsNil()
1418 }
1419 return deepValueEqualJs(v1.Elem(), v2.Elem(), visited)
1420 case Ptr:
1421 return deepValueEqualJs(v1.Elem(), v2.Elem(), visited)
1422 case Struct:
1423 var n = v1.NumField()
1424 for i := 0; i < n; i++ {
1425 if !deepValueEqualJs(v1.Field(i), v2.Field(i), visited) {
1426 return false
1427 }
1428 }
1429 return true
1430 case Map:
1431 if v1.IsNil() != v2.IsNil() {
1432 return false
1433 }
1434 if v1.object() == v2.object() {
1435 return true
1436 }
1437 var keys = v1.MapKeys()
1438 if len(keys) != v2.Len() {
1439 return false
1440 }
1441 for _, k := range keys {
1442 val1 := v1.MapIndex(k)
1443 val2 := v2.MapIndex(k)
1444 if !val1.IsValid() || !val2.IsValid() || !deepValueEqualJs(val1, val2, visited) {
1445 return false
1446 }
1447 }
1448 return true
1449 case Func:
1450 return v1.IsNil() && v2.IsNil()
1451 case UnsafePointer:
1452 return v1.object() == v2.object()
1453 }
1454
1455 return js.Global.Call("$interfaceIsEqual", js.InternalObject(valueInterface(v1, false)), js.InternalObject(valueInterface(v2, false))).Bool()
1456 }