mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
reflect: support slices and indexing of strings and slices
This commit is contained in:
Vendored
+30
-11
@@ -39,34 +39,53 @@ func main() {
|
||||
complex128(1.3 + 0.4i),
|
||||
"foo",
|
||||
unsafe.Pointer(new(int)),
|
||||
[]byte{1, 2, 3},
|
||||
make([]uint8, 2, 5),
|
||||
[]rune{3, 5},
|
||||
[]string{"xyz", "Z"},
|
||||
} {
|
||||
showValue(v)
|
||||
showValue(v, "")
|
||||
}
|
||||
}
|
||||
|
||||
func showValue(v interface{}) {
|
||||
func showValue(v interface{}, indent string) {
|
||||
rv := reflect.ValueOf(v)
|
||||
rt := rv.Type()
|
||||
if reflect.TypeOf(v) != rt {
|
||||
panic("direct TypeOf() is different from ValueOf().Type()")
|
||||
}
|
||||
println("reflect type:", rt.Kind().String())
|
||||
if rt.Kind() != rv.Kind() {
|
||||
panic("type kind is different from value kind")
|
||||
}
|
||||
if reflect.ValueOf(rv.Interface()) != rv {
|
||||
panic("reflect.ValueOf(Value.Interface()) did not return the same value")
|
||||
}
|
||||
println(indent+"reflect type:", rt.Kind().String())
|
||||
switch rt.Kind() {
|
||||
case reflect.Bool:
|
||||
println(" bool:", rv.Bool())
|
||||
println(indent+" bool:", rv.Bool())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
println(" int:", rv.Int())
|
||||
println(indent+" int:", rv.Int())
|
||||
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
println(" uint:", rv.Uint())
|
||||
println(indent+" uint:", rv.Uint())
|
||||
case reflect.Float32, reflect.Float64:
|
||||
println(" float:", rv.Float())
|
||||
println(indent+" float:", rv.Float())
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
println(" complex:", rv.Complex())
|
||||
println(indent+" complex:", rv.Complex())
|
||||
case reflect.String:
|
||||
println(" string:", rv.String())
|
||||
println(indent+" string:", rv.String(), rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
showValue(rv.Index(i).Interface(), indent+" ")
|
||||
}
|
||||
case reflect.UnsafePointer:
|
||||
println(" pointer:", rv.Pointer() != 0)
|
||||
println(indent+" pointer:", rv.Pointer() != 0)
|
||||
case reflect.Slice:
|
||||
println(indent+" slice:", rt.Elem().Kind().String(), rv.Len(), rv.Cap())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
println(indent+" indexing:", i)
|
||||
showValue(rv.Index(i).Interface(), indent+" ")
|
||||
}
|
||||
default:
|
||||
println(" unknown type kind!")
|
||||
println(indent + " unknown type kind!")
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+50
-1
@@ -49,6 +49,55 @@ reflect type: complex64
|
||||
reflect type: complex128
|
||||
complex: (+1.300000e+000+4.000000e-001i)
|
||||
reflect type: string
|
||||
string: foo
|
||||
string: foo 3
|
||||
reflect type: uint8
|
||||
uint: 102
|
||||
reflect type: uint8
|
||||
uint: 111
|
||||
reflect type: uint8
|
||||
uint: 111
|
||||
reflect type: unsafe.Pointer
|
||||
pointer: true
|
||||
reflect type: slice
|
||||
slice: uint8 3 3
|
||||
indexing: 0
|
||||
reflect type: uint8
|
||||
uint: 1
|
||||
indexing: 1
|
||||
reflect type: uint8
|
||||
uint: 2
|
||||
indexing: 2
|
||||
reflect type: uint8
|
||||
uint: 3
|
||||
reflect type: slice
|
||||
slice: uint8 2 5
|
||||
indexing: 0
|
||||
reflect type: uint8
|
||||
uint: 0
|
||||
indexing: 1
|
||||
reflect type: uint8
|
||||
uint: 0
|
||||
reflect type: slice
|
||||
slice: int32 2 2
|
||||
indexing: 0
|
||||
reflect type: int32
|
||||
int: 3
|
||||
indexing: 1
|
||||
reflect type: int32
|
||||
int: 5
|
||||
reflect type: slice
|
||||
slice: string 2 2
|
||||
indexing: 0
|
||||
reflect type: string
|
||||
string: xyz 3
|
||||
reflect type: uint8
|
||||
uint: 120
|
||||
reflect type: uint8
|
||||
uint: 121
|
||||
reflect type: uint8
|
||||
uint: 122
|
||||
indexing: 1
|
||||
reflect type: string
|
||||
string: Z 1
|
||||
reflect type: uint8
|
||||
uint: 90
|
||||
|
||||
Reference in New Issue
Block a user