forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonative.go
1539 lines (1508 loc) · 38.2 KB
/
gonative.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 gno
import (
"fmt"
"math"
"reflect"
)
// NOTE
//
// GoNative, *NativeType, and *NativeValue are experimental and subject to
// change.
//
// Go 1.15 reflect has a bug in creating new types with methods -- namely, it
// cannot, and so you cannot create types through reflection that obey any
// interface but the empty interface.
//----------------------------------------
// Go to Gno conversion
// See go2GnoValue(); this is lazy.
func go2GnoType(rt reflect.Type) Type {
if rt.PkgPath() != "" {
return &NativeType{Type: rt}
}
return go2GnoBaseType(rt)
}
// like go2GnoType() but ignores name declaration.
// for native type unary/binary expression conversion.
// also used for untyped gno -> native conversion intermediary.
// XXX support unary conversions as we did for binary.
func go2GnoBaseType(rt reflect.Type) Type {
switch rk := rt.Kind(); rk {
case reflect.Bool:
return BoolType
case reflect.String:
return StringType
case reflect.Int:
return IntType
case reflect.Int8:
return Int8Type
case reflect.Int16:
return Int16Type
case reflect.Int32:
return Int32Type
case reflect.Int64:
return Int64Type
case reflect.Uint:
return UintType
case reflect.Uint8:
return Uint8Type
case reflect.Uint16:
return Uint16Type
case reflect.Uint32:
return Uint32Type
case reflect.Uint64:
return Uint64Type
case reflect.Float32:
return Float32Type
case reflect.Float64:
return Float64Type
case reflect.Array:
return &NativeType{Type: rt}
case reflect.Slice:
return &NativeType{Type: rt}
case reflect.Chan:
return &NativeType{Type: rt}
case reflect.Func:
return &NativeType{Type: rt}
case reflect.Interface:
return &NativeType{Type: rt}
case reflect.Map:
return &NativeType{Type: rt}
case reflect.Ptr:
return &NativeType{Type: rt}
case reflect.Struct:
return &NativeType{Type: rt}
case reflect.UnsafePointer:
panic("not yet implemented")
default:
panic(fmt.Sprintf(
"unexpected type %v", rt))
}
}
// Implements Store.
func (ds *defaultStore) SetStrictGo2GnoMapping(strict bool) {
ds.go2gnoStrict = strict
}
// Implements Store.
func (ds *defaultStore) AddGo2GnoMapping(rt reflect.Type, pkgPath string, name string) {
rtPkgPath := rt.PkgPath()
if rtPkgPath == "" {
panic(fmt.Sprintf("type has no associated package path: %v", rt))
}
rtName := rt.Name()
if rtName == "" {
panic(fmt.Sprintf("type has no name: %v", rt))
}
ds.go2gnoMap[rtPkgPath+"."+rtName] = pkgPath + "." + name
}
// Implements Store.
// See go2GnoValue2(). Like go2GnoType() but also converts any
// top-level complex types (or pointers to them). The result gets
// memoized in *NativeType.GnoType() for type inference in the
// preprocessor, as well as in the cacheNativeTypes lookup map to
// support recursive translations.
// The namedness of the native type gets converted to an
// appropriate gno *DeclaredType with native methods
// converted via go2GnoFuncType().
// If store is not nil, native named types do not construct new
// gno types, but rather the store is used to fetch gno types
// from ds.go2gnoMap.
func (ds *defaultStore) Go2GnoType(rt reflect.Type) (t Type) {
if gnot, ok := ds.cacheNativeTypes[rt]; ok {
return gnot
}
defer func() {
if r := recover(); r != nil {
panic(r) // do not run the below logic upon panic.
}
// regardless of rt kind, if rt.PkgPath() is set,
// wrap t with declared type.
pkgPath := rt.PkgPath()
if pkgPath != "" {
// try to look up gno type from mapping.
gokey := pkgPath + "." + rt.Name()
gnokey, ok := ds.go2gnoMap[gokey]
if ok {
// mapping successful.
typ := ds.GetType(TypeID(gnokey))
if typ == nil {
panic(fmt.Sprintf("missing type %s", gnokey))
}
t = typ
} else if ds.go2gnoStrict {
// mapping failed and strict: error.
panic(fmt.Sprintf("native type mapping missing for %s", gokey))
} else {
// generate a new gno type for testing.
mtvs := []TypedValue(nil)
if t.Kind() == InterfaceKind {
// methods already set on t.Methods.
// *DT.Methods not used in Go for interfaces.
} else {
prt := rt
if rt.Kind() != reflect.Ptr {
// NOTE: go reflect requires ptr kind
// for methods with ptr receivers,
// whereas gno methods are all
// declared on the *DeclaredType.
prt = reflect.PtrTo(rt)
}
nm := prt.NumMethod()
mtvs = make([]TypedValue, nm)
for i := 0; i < nm; i++ {
mthd := prt.Method(i)
ft := ds.go2GnoFuncType(mthd.Type)
fv := &FuncValue{
Type: ft,
IsMethod: true,
Source: nil,
Name: Name(mthd.Name),
Closure: nil,
PkgPath: pkgPath,
body: nil, // XXX
nativeBody: nil,
}
mtvs[i] = TypedValue{T: ft, V: fv}
}
}
dt := &DeclaredType{
PkgPath: pkgPath,
Name: Name(rt.Name()),
Base: t,
Methods: mtvs,
}
dt.Seal()
t = dt
}
}
// memoize t to cache.
if debug {
if gnot, ok := ds.cacheNativeTypes[rt]; ok {
if gnot.TypeID() != baseOf(t).TypeID() {
panic("should not happen")
}
}
}
ds.cacheNativeTypes[rt] = t // may overwrite
}()
switch rk := rt.Kind(); rk {
case reflect.Bool:
return BoolType
case reflect.String:
return StringType
case reflect.Int:
return IntType
case reflect.Int8:
return Int8Type
case reflect.Int16:
return Int16Type
case reflect.Int32:
return Int32Type
case reflect.Int64:
return Int64Type
case reflect.Uint:
return UintType
case reflect.Uint8:
return Uint8Type
case reflect.Uint16:
return Uint16Type
case reflect.Uint32:
return Uint32Type
case reflect.Uint64:
return Uint64Type
case reflect.Float32:
return Float32Type
case reflect.Float64:
return Float64Type
case reflect.Array:
// predefine gno type
at := &ArrayType{}
ds.cacheNativeTypes[rt] = at
// define gno type
at.Len = rt.Len()
at.Elt = go2GnoType(rt.Elem())
at.Vrd = false
return at
case reflect.Slice:
return &SliceType{
Elt: go2GnoType(rt.Elem()),
Vrd: false,
}
case reflect.Chan:
// predefine gno type
ct := &ChanType{}
ds.cacheNativeTypes[rt] = ct
// define gno type
chdir := toChanDir(rt.ChanDir())
ct.Dir = chdir
ct.Elt = go2GnoType(rt.Elem())
return ct
case reflect.Func:
return ds.go2GnoFuncType(rt)
case reflect.Interface:
// predefine gno type
it := &InterfaceType{}
ds.cacheNativeTypes[rt] = it
// define gno type
nm := rt.NumMethod()
fs := make([]FieldType, nm)
for i := 0; i < nm; i++ {
mthd := rt.Method(i)
fs[i] = FieldType{
Name: Name(mthd.Name),
Type: ds.Go2GnoType(mthd.Type), // recursive
}
}
it.PkgPath = rt.PkgPath()
it.Methods = fs
return it
case reflect.Map:
// predefine gno type
mt := &MapType{}
ds.cacheNativeTypes[rt] = mt
// define gno type
mt.Key = go2GnoType(rt.Key())
mt.Value = go2GnoType(rt.Elem())
return mt
case reflect.Ptr:
return &PointerType{
Elt: ds.Go2GnoType(rt.Elem()), // recursive
}
case reflect.Struct:
// predefine gno type
st := &StructType{}
ds.cacheNativeTypes[rt] = st
// define gno type
nf := rt.NumField()
fs := make([]FieldType, nf)
for i := 0; i < nf; i++ {
sf := rt.Field(i)
fs[i] = FieldType{
Name: Name(sf.Name),
Type: go2GnoType(sf.Type),
}
}
st.PkgPath = rt.PkgPath()
st.Fields = fs
return st
case reflect.UnsafePointer:
panic("not yet implemented")
default:
panic("not yet implemented")
}
}
// Converts native go function type to gno *FuncType,
// for the preprocessor to infer types of arguments.
// The argument and return types are shallowly converted
// to gno types, (to preserve the original native types).
func (ds *defaultStore) go2GnoFuncType(rt reflect.Type) *FuncType {
// predefine func type
ft := &FuncType{}
ds.cacheNativeTypes[rt] = ft
// define func type
hasVargs := rt.IsVariadic()
ins := make([]FieldType, rt.NumIn())
for i := 0; i < len(ins); i++ {
it := go2GnoType(rt.In(i))
if hasVargs && i == len(ins)-1 {
it = &SliceType{
Elt: it.Elem(),
Vrd: true,
}
}
ins[i] = FieldType{
Name: "", // XXX dontcare?
Type: it,
}
}
outs := make([]FieldType, rt.NumOut())
for i := 0; i < len(outs); i++ {
ot := go2GnoType(rt.Out(i))
outs[i] = FieldType{
Name: "", // XXX dontcare?
Type: ot,
}
}
ft.Params = ins
ft.Results = outs
return ft
}
// NOTE: used by vm module. Recursively converts.
func Go2GnoValue(alloc *Allocator, store Store, rv reflect.Value) (tv TypedValue) {
return go2GnoValue2(alloc, store, rv, true)
}
// NOTE: used by vm module. Shallow, preserves native namedness.
func Go2GnoNativeValue(alloc *Allocator, rv reflect.Value) (tv TypedValue) {
return go2GnoValue(alloc, rv)
}
// NOTE: used by imports_test.go TestSetOrigCaller.
func Gno2GoValue(tv *TypedValue, rv reflect.Value) (ret reflect.Value) {
return gno2GoValue(tv, rv)
}
// Default run-time representation of go-native values. It is
// "lazy" in the sense that unnamed complex types like arrays and
// slices aren't translated to Gno canonical types except as
// *NativeType/*NativeValues, primarily for speed. To force
// translation to Gno canonical types for unnamed complex types,
// call go2GnoValue2(), which is used by the implementation of
// ConvertTo().
// Unlike go2GnoValue2(), rv may be invalid.
func go2GnoValue(alloc *Allocator, rv reflect.Value) (tv TypedValue) {
if !rv.IsValid() {
return
}
if rv.Kind() == reflect.Interface {
if rv.IsNil() {
return TypedValue{}
} else {
rv = rv.Elem()
}
}
if rv.Type().PkgPath() != "" {
rt := rv.Type()
tv.T = alloc.NewType(&NativeType{Type: rt})
tv.V = alloc.NewNative(rv)
return
}
tv.T = alloc.NewType(go2GnoType(rv.Type()))
switch rk := rv.Kind(); rk {
case reflect.Bool:
tv.SetBool(rv.Bool())
case reflect.String:
tv.V = alloc.NewString(rv.String())
case reflect.Int:
tv.SetInt(int(rv.Int()))
case reflect.Int8:
tv.SetInt8(int8(rv.Int()))
case reflect.Int16:
tv.SetInt16(int16(rv.Int()))
case reflect.Int32:
tv.SetInt32(int32(rv.Int()))
case reflect.Int64:
tv.SetInt64(int64(rv.Int()))
case reflect.Uint:
tv.SetUint(uint(rv.Uint()))
case reflect.Uint8:
tv.SetUint8(uint8(rv.Uint()))
case reflect.Uint16:
tv.SetUint16(uint16(rv.Uint()))
case reflect.Uint32:
tv.SetUint32(uint32(rv.Uint()))
case reflect.Uint64:
tv.SetUint64(uint64(rv.Uint()))
case reflect.Float32:
fl := float32(rv.Float())
u32 := math.Float32bits(fl)
if sv, ok := tv.V.(*StructValue); ok { // update
if debug {
if len(sv.Fields) != 1 {
panic("should not happen")
}
if sv.Fields[0].T != Uint32Type {
panic("invalid Float32Type, expected Uint32 field")
}
}
sv.Fields[0].SetUint32(u32)
} else { // create
ftv := TypedValue{T: Uint32Type}
ftv.SetUint32(u32)
tv.V = alloc.NewStructWithFields(ftv)
}
case reflect.Float64:
fl := rv.Float()
u64 := math.Float64bits(fl)
if sv, ok := tv.V.(*StructValue); ok { // update
if debug {
if len(sv.Fields) != 1 {
panic("should not happen")
}
if sv.Fields[0].T != Uint64Type {
panic("invalid Float64Type, expected Uint64 field")
}
}
sv.Fields[0].SetUint64(u64)
} else { // create
ftv := TypedValue{T: Uint64Type}
ftv.SetUint64(u64)
tv.V = alloc.NewStructWithFields(ftv)
}
case reflect.Array:
tv.V = alloc.NewNative(rv)
case reflect.Slice:
tv.V = alloc.NewNative(rv)
case reflect.Chan:
tv.V = alloc.NewNative(rv)
case reflect.Func:
tv.V = alloc.NewNative(rv)
case reflect.Interface:
panic("should not happen")
case reflect.Map:
tv.V = alloc.NewNative(rv)
case reflect.Ptr:
tv.V = alloc.NewNative(rv)
case reflect.Struct:
tv.V = alloc.NewNative(rv)
case reflect.UnsafePointer:
panic("not yet implemented")
default:
panic("not yet implemented")
}
return
}
// Given rv which may have been updated by a go-native
// function, and the corresponding (original) input value tv,
// scan for changes and update tv recursively as needed.
// An additional side effect is that uninitialized input values
// become initialized. NOTE: Due to limitations of Go 1.15
// reflection, any child Gno declared types cannot change
// types, and pointer values cannot change.
func go2GnoValueUpdate(alloc *Allocator, rlm *Realm, lvl int, tv *TypedValue, rv reflect.Value) {
// Special case if nil:
if tv.IsUndefined() {
return // do nothing
}
// Special case if native type:
if _, ok := tv.T.(*NativeType); ok {
return // do nothing
}
// De-interface if interface.
if rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
// General case:
switch tvk := tv.T.Kind(); tvk {
case BoolKind:
if lvl != 0 {
tv.SetBool(rv.Bool())
}
case StringKind:
if lvl != 0 {
tv.V = alloc.NewString(rv.String())
}
case IntKind:
if lvl != 0 {
tv.SetInt(int(rv.Int()))
}
case Int8Kind:
if lvl != 0 {
tv.SetInt8(int8(rv.Int()))
}
case Int16Kind:
if lvl != 0 {
tv.SetInt16(int16(rv.Int()))
}
case Int32Kind:
if lvl != 0 {
tv.SetInt32(int32(rv.Int()))
}
case Int64Kind:
if lvl != 0 {
tv.SetInt64(int64(rv.Int()))
}
case UintKind:
if lvl != 0 {
tv.SetUint(uint(rv.Uint()))
}
case Uint8Kind:
if lvl != 0 {
tv.SetUint8(uint8(rv.Uint()))
}
case Uint16Kind:
if lvl != 0 {
tv.SetUint16(uint16(rv.Uint()))
}
case Uint32Kind:
if lvl != 0 {
tv.SetUint32(uint32(rv.Uint()))
}
case Uint64Kind:
if lvl != 0 {
tv.SetUint64(uint64(rv.Uint()))
}
case BigintKind:
panic("not yet implemented")
case ArrayKind:
av := tv.V.(*ArrayValue)
rvl := rv.Len()
if debug {
if rvl != baseOf(tv.T).(*ArrayType).Len {
panic("go-native update error: array length mismmatch")
}
}
if av.Data == nil {
at := baseOf(tv.T).(*ArrayType)
et := at.Elt
for i := 0; i < rvl; i++ {
erv := rv.Index(i)
etv := &av.List[i]
// XXX use Assign and Realm?
if etv.T == nil && et.Kind() != InterfaceKind {
etv.T = et
}
if etv.V == nil {
etv.V = defaultValue(alloc, et)
}
go2GnoValueUpdate(alloc, rlm, lvl+1, etv, erv)
}
} else {
for i := 0; i < rvl; i++ {
erv := rv.Index(i)
av.Data[i] = uint8(erv.Uint())
}
}
case SliceKind:
rvl := rv.Len()
if rvl == 0 {
if debug {
if tv.V != nil && tv.V.(*SliceValue).GetLength() != 0 {
panic("should not happen")
}
}
return // do nothing
}
sv := tv.V.(*SliceValue)
svo := sv.Offset
svl := sv.Length
if debug {
if rvl != svl {
panic("go-native update error: slice length mismmatch")
}
}
if sv.GetBase(nil).Data == nil {
st := baseOf(tv.T).(*SliceType)
et := st.Elt
for i := 0; i < rvl; i++ {
erv := rv.Index(i)
etv := &sv.GetBase(nil).List[svo+i]
// XXX use Assign and Realm?
if etv.T == nil && et.Kind() != InterfaceKind {
etv.T = et
}
if etv.V == nil {
etv.V = defaultValue(alloc, et)
}
go2GnoValueUpdate(alloc, rlm, lvl+1, etv, erv)
}
} else {
for i := 0; i < rvl; i++ {
erv := rv.Index(i)
sv.GetBase(nil).Data[svo+i] = uint8(erv.Uint())
}
}
case PointerKind:
if tv.V == nil {
return // do nothing
}
pv := tv.V.(PointerValue)
etv := pv.TV
erv := rv.Elem()
go2GnoValueUpdate(alloc, rlm, lvl+1, etv, erv)
case StructKind:
st := baseOf(tv.T).(*StructType)
sv := tv.V.(*StructValue)
switch st.PkgPath {
case float32PkgPath: // Special case if Float32.
fl := float32(rv.Float())
u32 := math.Float32bits(fl)
if debug {
if len(sv.Fields) != 1 {
panic("should not happen")
}
if sv.Fields[0].T != Uint32Type {
panic("invalid Float32Type, expected Uint32 field")
}
}
sv.Fields[0].SetUint32(u32)
case float64PkgPath: // Special case if Float64.
fl := rv.Float()
u64 := math.Float64bits(fl)
if debug {
if len(sv.Fields) != 1 {
panic("should not happen")
}
if sv.Fields[0].T != Uint64Type {
panic("invalid Float64Type, expected Uint64 field")
}
}
sv.Fields[0].SetUint64(u64)
default: // General case.
for i := range st.Fields {
ft := st.Fields[i].Type
ftv := &sv.Fields[i]
// XXX use Assign and Realm?
if ftv.T == nil && ft.Kind() != InterfaceKind {
ftv.T = ft
}
if ftv.V == nil {
ftv.V = defaultValue(alloc, ft)
}
frv := rv.Field(i)
go2GnoValueUpdate(alloc, rlm, lvl+1, ftv, frv)
}
}
case PackageKind:
panic("not yet implemented")
case InterfaceKind:
if debug {
if !tv.IsNilInterface() {
panic("should not happen")
}
}
case ChanKind:
panic("not yet implemented")
case FuncKind:
panic("not yet implemented")
case MapKind:
rvl := rv.Len()
// If uninitialized map, return zero value.
if tv.V == nil {
if rvl != 0 {
panic("not yet implemented")
}
return
}
// General case.
mv := tv.V.(*MapValue)
mvl := mv.List.Size
// Copy map to new map for destructive iteration of items.
rt := rv.Type()
rv2 := reflect.MakeMapWithSize(rt, mvl)
rvi := rv.MapRange()
for rvi.Next() {
k, v := rvi.Key(), rvi.Value()
rv2.SetMapIndex(k, v)
}
// Iterate over mv (gno map) and update,
// and also remove encountered items from rv2.
head := mv.List.Head
for head != nil {
ktv, vtv := &head.Key, &head.Value
// Update in place.
krv := gno2GoValue(ktv, reflect.Value{})
vrv := rv.MapIndex(krv)
if vrv.IsZero() {
// XXX remove key from mv
panic("not yet implemented")
} else {
go2GnoValueUpdate(alloc, rlm, lvl+1, vtv, vrv)
}
// Delete from rv2
rv2.SetMapIndex(krv, reflect.Value{})
// Continue
head = head.Next
}
// Add remaining items from rv2 to map.
rv2i := rv2.MapRange()
for rv2i.Next() {
k, v := rv2i.Key(), rv2i.Value()
ktv := go2GnoValue(alloc, k)
vtv := go2GnoValue(alloc, v)
ptr := mv.GetPointerForKey(alloc, nil, &ktv)
if debug {
if !ptr.TV.IsUndefined() {
panic("should not happen")
}
}
ptr.Assign2(alloc, nil, rlm, vtv, false) // document false
}
case TypeKind:
panic("not yet implemented")
default:
panic("should not happen: unexpected gno kind")
}
return
}
// If recursive is false, this function is like go2GnoValue() but less lazy
// (but still not recursive/eager). When recursive is false, it is for
// converting Go types to Gno types upon an explicit conversion (via
// ConvertTo). Panics on unexported/private fields. Some types that cannot be
// converted remain native. Unlike go2GnoValue(), rv must be valid.
func go2GnoValue2(alloc *Allocator, store Store, rv reflect.Value, recursive bool) (tv TypedValue) {
if debug {
if !rv.IsValid() {
panic("go2GnoValue2() requires valid rv")
}
}
tv.T = store.Go2GnoType(rv.Type())
switch rk := rv.Kind(); rk {
case reflect.Bool:
tv.SetBool(rv.Bool())
case reflect.String:
tv.V = alloc.NewString(rv.String())
case reflect.Int:
tv.SetInt(int(rv.Int()))
case reflect.Int8:
tv.SetInt8(int8(rv.Int()))
case reflect.Int16:
tv.SetInt16(int16(rv.Int()))
case reflect.Int32:
tv.SetInt32(int32(rv.Int()))
case reflect.Int64:
tv.SetInt64(int64(rv.Int()))
case reflect.Uint:
tv.SetUint(uint(rv.Uint()))
case reflect.Uint8:
tv.SetUint8(uint8(rv.Uint()))
case reflect.Uint16:
tv.SetUint16(uint16(rv.Uint()))
case reflect.Uint32:
tv.SetUint32(uint32(rv.Uint()))
case reflect.Uint64:
tv.SetUint64(uint64(rv.Uint()))
case reflect.Float32:
fl := float32(rv.Float())
u32 := math.Float32bits(fl)
tv.SetUint32(u32)
case reflect.Float64:
fl := rv.Float()
u64 := math.Float64bits(fl)
tv.SetUint64(u64)
case reflect.Array:
rvl := rv.Len()
if rv.Type().Elem().Kind() == reflect.Uint8 {
av := alloc.NewDataArray(rvl)
data := av.Data
reflect.Copy(reflect.ValueOf(data), rv)
tv.V = av
} else {
av := alloc.NewListArray(rvl)
list := av.List
for i := 0; i < rvl; i++ {
if recursive {
list[i] = go2GnoValue2(alloc, store, rv.Index(i), true)
} else {
list[i] = go2GnoValue(alloc, rv.Index(i))
}
}
tv.V = av
}
case reflect.Slice:
rvl := rv.Len()
rvc := rv.Cap()
list := make([]TypedValue, rvl, rvc)
for i := 0; i < rvl; i++ {
if recursive {
list[i] = go2GnoValue2(alloc, store, rv.Index(i), true)
} else {
list[i] = go2GnoValue(alloc, rv.Index(i))
}
}
tv.V = alloc.NewSliceFromList(list)
case reflect.Chan:
panic("not yet implemented")
case reflect.Func:
// NOTE: the type may be a full gno type, either a
// *FuncType or *DeclaredType. The value may still be a
// *NativeValue though, and the function can be called
// regardless.
tv.V = alloc.NewNative(rv)
case reflect.Interface:
panic("not yet implemented")
case reflect.Map:
panic("not yet implemented")
case reflect.Ptr:
val := go2GnoValue2(alloc, store, rv.Elem(), recursive)
tv.V = PointerValue{TV: &val} // heap alloc
case reflect.Struct:
nf := rv.NumField()
fs := alloc.NewStructFields(nf)
for i := 0; i < nf; i++ {
frv := rv.Field(i)
if recursive {
fs[i] = go2GnoValue2(alloc, store, frv, true)
} else {
fs[i] = go2GnoValue(alloc, frv)
}
}
tv.V = alloc.NewStruct(fs)
case reflect.UnsafePointer:
panic("not yet implemented")
default:
panic("not yet implemented")
}
return
}
//----------------------------------------
// Gno to Go conversion
// NOTE: Recursive types are not supported, as named types are not
// supported. See https://github.com/golang/go/issues/20013 and
// https://github.com/golang/go/issues/39717.
func gno2GoType(t Type) reflect.Type {
// special case if t == Float32Type or Float64Type
if t == Float32Type {
return reflect.TypeOf(float32(0.0))
} else if t == Float64Type {
return reflect.TypeOf(float64(0.0))
}
switch ct := baseOf(t).(type) {
case PrimitiveType:
switch ct {
case BoolType, UntypedBoolType:
return reflect.TypeOf(false)
case StringType, UntypedStringType:
return reflect.TypeOf("")
case IntType:
return reflect.TypeOf(int(0))
case Int8Type:
return reflect.TypeOf(int8(0))
case Int16Type:
return reflect.TypeOf(int16(0))
case Int32Type, UntypedRuneType:
return reflect.TypeOf(int32(0))
case Int64Type:
return reflect.TypeOf(int64(0))
case UintType:
return reflect.TypeOf(uint(0))
case Uint8Type:
return reflect.TypeOf(uint8(0))
case Uint16Type:
return reflect.TypeOf(uint16(0))
case Uint32Type:
return reflect.TypeOf(uint32(0))
case Uint64Type:
return reflect.TypeOf(uint64(0))
case BigintType, UntypedBigintType:
panic("not yet implemented")
default:
panic("should not happen")
}
case *PointerType:
et := gno2GoType(ct.Elem())
return reflect.PtrTo(et)
case *ArrayType:
ne := ct.Len
et := gno2GoType(ct.Elem())
return reflect.ArrayOf(ne, et)
case *SliceType:
et := gno2GoType(ct.Elem())
return reflect.SliceOf(et)
case *StructType:
gfs := make([]reflect.StructField, len(ct.Fields))
for i, field := range ct.Fields {
gft := gno2GoType(field.Type)
fn := string(field.Name)
pkgPath := ""
if !isUpper(fn) {
pkgPath = ct.PkgPath
}
gfs[i] = reflect.StructField{
Name: fn,
PkgPath: pkgPath,
Type: gft,
Tag: reflect.StructTag(field.Tag),
Anonymous: field.Name == "",
// Offset: dontcare
// Index: dontcare
}
}
return reflect.StructOf(gfs)
case *MapType:
kt := gno2GoType(ct.Key)
vt := gno2GoType(ct.Value)
return reflect.MapOf(kt, vt)
case *FuncType:
panic("not yet supported")
case *InterfaceType:
if ct.IsEmptyInterface() {
// XXX move out
rt := reflect.TypeOf((*interface{})(nil)).Elem()
return rt
} else {
// NOTE: can this be implemented in go1.15? i think not.
panic("not yet supported")
}
case *TypeType:
panic("should not happen")
case *DeclaredType:
// NOTE: Go1.15 has issues with generating types and values using
// reflect to declare types with methods. When Go has fixed these
// issues, we can revisit. For now, all Gno objects passed to Go
// lose their names or "namedness", e.g. cannot satisfy anything
// but empty interfaces, and have no methods.
// We switch on baseOf(t).
panic("should not happen")
case *PackageType:
panic("should not happen")
case *NativeType:
return ct.Type
default:
panic(fmt.Sprintf("unexpected type %v with base %v", t, baseOf(t)))
}
}
// If gno2GoTypeMatches(t, rt) is true, a t value can
// be converted to an rt native value using gno2GoValue(v, rv).
// This is called when autoNative is true in checkType().
// This is used for all native function calls, and also
// for testing whether a native value implements a gno interface.
func gno2GoTypeMatches(t Type, rt reflect.Type) (result bool) {
if rt == nil {
panic("should not happen")
}
// special case if t == Float32Type or Float64Type
if t == Float32Type {
return rt.Kind() == reflect.Float32
} else if t == Float64Type {
return rt.Kind() == reflect.Float64
}
switch ct := baseOf(t).(type) {
case PrimitiveType:
switch ct {
case BoolType, UntypedBoolType:
return rt.Kind() == reflect.Bool
case StringType, UntypedStringType:
return rt.Kind() == reflect.String
case IntType:
return rt.Kind() == reflect.Int
case Int8Type:
return rt.Kind() == reflect.Int8
case Int16Type:
return rt.Kind() == reflect.Int16
case Int32Type, UntypedRuneType:
return rt.Kind() == reflect.Int32
case Int64Type:
return rt.Kind() == reflect.Int64
case UintType:
return rt.Kind() == reflect.Uint
case Uint8Type:
return rt.Kind() == reflect.Uint8
case Uint16Type:
return rt.Kind() == reflect.Uint16
case Uint32Type:
return rt.Kind() == reflect.Uint32
case Uint64Type:
return rt.Kind() == reflect.Uint64
case BigintType, UntypedBigintType:
panic("not yet implemented")
default:
panic("should not happen")
}
case *PointerType:
if rt.Kind() != reflect.Ptr {
return false
}