github.com/aykevl/tinygo@v0.5.0/testdata/reflect.go (about)

     1  package main
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  type (
     9  	myint    int
    10  	myslice  []byte
    11  	myslice2 []myint
    12  )
    13  
    14  func main() {
    15  	println("matching types")
    16  	println(reflect.TypeOf(int(3)) == reflect.TypeOf(int(5)))
    17  	println(reflect.TypeOf(int(3)) == reflect.TypeOf(uint(5)))
    18  	println(reflect.TypeOf(myint(3)) == reflect.TypeOf(int(5)))
    19  	println(reflect.TypeOf(myslice{}) == reflect.TypeOf([]byte{}))
    20  	println(reflect.TypeOf(myslice2{}) == reflect.TypeOf([]myint{}))
    21  	println(reflect.TypeOf(myslice2{}) == reflect.TypeOf([]int{}))
    22  
    23  	println("\nvalues of interfaces")
    24  	var zeroSlice []byte
    25  	var zeroFunc func()
    26  	var zeroMap map[string]int
    27  	var zeroChan chan int
    28  	n := 42
    29  	for _, v := range []interface{}{
    30  		// basic types
    31  		true,
    32  		false,
    33  		int(2000),
    34  		int(-2000),
    35  		uint(2000),
    36  		int8(-3),
    37  		int8(3),
    38  		uint8(200),
    39  		int16(-300),
    40  		int16(300),
    41  		uint16(50000),
    42  		int32(7 << 20),
    43  		int32(-7 << 20),
    44  		uint32(7 << 20),
    45  		int64(9 << 40),
    46  		int64(-9 << 40),
    47  		uint64(9 << 40),
    48  		uintptr(12345),
    49  		float32(3.14),
    50  		float64(3.14),
    51  		complex64(1.2 + 0.3i),
    52  		complex128(1.3 + 0.4i),
    53  		myint(32),
    54  		"foo",
    55  		unsafe.Pointer(new(int)),
    56  		// channels
    57  		zeroChan,
    58  		// pointers
    59  		new(int),
    60  		new(error),
    61  		&n,
    62  		// slices
    63  		[]byte{1, 2, 3},
    64  		make([]uint8, 2, 5),
    65  		[]rune{3, 5},
    66  		[]string{"xyz", "Z"},
    67  		zeroSlice,
    68  		[]byte{},
    69  		[]float32{1, 1.32},
    70  		[]float64{1, 1.64},
    71  		[]complex64{1, 1.64 + 0.3i},
    72  		[]complex128{1, 1.128 + 0.4i},
    73  		// array
    74  		[4]int{1, 2, 3, 4},
    75  		// functions
    76  		zeroFunc,
    77  		emptyFunc,
    78  		// maps
    79  		zeroMap,
    80  		map[string]int{},
    81  		// structs
    82  		struct{}{},
    83  		struct{ error }{},
    84  	} {
    85  		showValue(reflect.ValueOf(v), "")
    86  	}
    87  
    88  	// test sizes
    89  	println("\nsizes:")
    90  	println("int8", int(reflect.TypeOf(int8(0)).Size()))
    91  	println("int16", int(reflect.TypeOf(int16(0)).Size()))
    92  	println("int32", int(reflect.TypeOf(int32(0)).Size()))
    93  	println("int64", int(reflect.TypeOf(int64(0)).Size()))
    94  	println("uint8", int(reflect.TypeOf(uint8(0)).Size()))
    95  	println("uint16", int(reflect.TypeOf(uint16(0)).Size()))
    96  	println("uint32", int(reflect.TypeOf(uint32(0)).Size()))
    97  	println("uint64", int(reflect.TypeOf(uint64(0)).Size()))
    98  	println("float32", int(reflect.TypeOf(float32(0)).Size()))
    99  	println("float64", int(reflect.TypeOf(float64(0)).Size()))
   100  	println("complex64", int(reflect.TypeOf(complex64(0)).Size()))
   101  	println("complex128", int(reflect.TypeOf(complex128(0)).Size()))
   102  	assertSize(reflect.TypeOf(uintptr(0)).Size() == unsafe.Sizeof(uintptr(0)), "uintptr")
   103  	assertSize(reflect.TypeOf("").Size() == unsafe.Sizeof(""), "string")
   104  	assertSize(reflect.TypeOf(new(int)).Size() == unsafe.Sizeof(new(int)), "*int")
   105  
   106  	// SetBool
   107  	rv := reflect.ValueOf(new(bool)).Elem()
   108  	rv.SetBool(true)
   109  	if rv.Bool() != true {
   110  		panic("could not set bool with SetBool()")
   111  	}
   112  
   113  	// SetInt
   114  	for _, v := range []interface{}{
   115  		new(int),
   116  		new(int8),
   117  		new(int16),
   118  		new(int32),
   119  		new(int64),
   120  	} {
   121  		rv := reflect.ValueOf(v).Elem()
   122  		rv.SetInt(99)
   123  		if rv.Int() != 99 {
   124  			panic("could not set integer with SetInt()")
   125  		}
   126  	}
   127  
   128  	// SetUint
   129  	for _, v := range []interface{}{
   130  		new(uint),
   131  		new(uint8),
   132  		new(uint16),
   133  		new(uint32),
   134  		new(uint64),
   135  		new(uintptr),
   136  	} {
   137  		rv := reflect.ValueOf(v).Elem()
   138  		rv.SetUint(99)
   139  		if rv.Uint() != 99 {
   140  			panic("could not set integer with SetUint()")
   141  		}
   142  	}
   143  
   144  	// SetFloat
   145  	for _, v := range []interface{}{
   146  		new(float32),
   147  		new(float64),
   148  	} {
   149  		rv := reflect.ValueOf(v).Elem()
   150  		rv.SetFloat(2.25)
   151  		if rv.Float() != 2.25 {
   152  			panic("could not set float with SetFloat()")
   153  		}
   154  	}
   155  
   156  	// SetComplex
   157  	for _, v := range []interface{}{
   158  		new(complex64),
   159  		new(complex128),
   160  	} {
   161  		rv := reflect.ValueOf(v).Elem()
   162  		rv.SetComplex(3 + 2i)
   163  		if rv.Complex() != 3+2i {
   164  			panic("could not set complex with SetComplex()")
   165  		}
   166  	}
   167  
   168  	// SetString
   169  	rv = reflect.ValueOf(new(string)).Elem()
   170  	rv.SetString("foo")
   171  	if rv.String() != "foo" {
   172  		panic("could not set string with SetString()")
   173  	}
   174  
   175  	// Set int
   176  	rv = reflect.ValueOf(new(int)).Elem()
   177  	rv.SetInt(33)
   178  	rv.Set(reflect.ValueOf(22))
   179  	if rv.Int() != 22 {
   180  		panic("could not set int with Set()")
   181  	}
   182  
   183  	// Set uint8
   184  	rv = reflect.ValueOf(new(uint8)).Elem()
   185  	rv.SetUint(33)
   186  	rv.Set(reflect.ValueOf(uint8(22)))
   187  	if rv.Uint() != 22 {
   188  		panic("could not set uint8 with Set()")
   189  	}
   190  
   191  	// Set string
   192  	rv = reflect.ValueOf(new(string)).Elem()
   193  	rv.SetString("foo")
   194  	rv.Set(reflect.ValueOf("bar"))
   195  	if rv.String() != "bar" {
   196  		panic("could not set string with Set()")
   197  	}
   198  
   199  	// Set complex128
   200  	rv = reflect.ValueOf(new(complex128)).Elem()
   201  	rv.SetComplex(3 + 2i)
   202  	rv.Set(reflect.ValueOf(4 + 8i))
   203  	if rv.Complex() != 4+8i {
   204  		panic("could not set complex128 with Set()")
   205  	}
   206  
   207  	// Set to slice
   208  	rv = reflect.ValueOf([]int{3, 5})
   209  	rv.Index(1).SetInt(7)
   210  	if rv.Index(1).Int() != 7 {
   211  		panic("could not set int in slice")
   212  	}
   213  	rv.Index(1).Set(reflect.ValueOf(8))
   214  	if rv.Index(1).Int() != 8 {
   215  		panic("could not set int in slice")
   216  	}
   217  	if rv.Len() != 2 || rv.Index(0).Int() != 3 {
   218  		panic("slice was changed while setting part of it")
   219  	}
   220  }
   221  
   222  func emptyFunc() {
   223  }
   224  
   225  func showValue(rv reflect.Value, indent string) {
   226  	rt := rv.Type()
   227  	if rt.Kind() != rv.Kind() {
   228  		panic("type kind is different from value kind")
   229  	}
   230  	print(indent+"reflect type: ", rt.Kind().String())
   231  	if rv.CanSet() {
   232  		print(" settable=", rv.CanSet())
   233  	}
   234  	println()
   235  	switch rt.Kind() {
   236  	case reflect.Bool:
   237  		println(indent+"  bool:", rv.Bool())
   238  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   239  		println(indent+"  int:", rv.Int())
   240  	case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   241  		println(indent+"  uint:", rv.Uint())
   242  	case reflect.Float32, reflect.Float64:
   243  		println(indent+"  float:", rv.Float())
   244  	case reflect.Complex64, reflect.Complex128:
   245  		println(indent+"  complex:", rv.Complex())
   246  	case reflect.String:
   247  		println(indent+"  string:", rv.String(), rv.Len())
   248  		for i := 0; i < rv.Len(); i++ {
   249  			showValue(rv.Index(i), indent+"  ")
   250  		}
   251  	case reflect.UnsafePointer:
   252  		println(indent+"  pointer:", rv.Pointer() != 0)
   253  	case reflect.Array:
   254  		println(indent + "  array")
   255  	case reflect.Chan:
   256  		println(indent+"  chan:", rt.Elem().Kind().String())
   257  		println(indent+"  nil:", rv.IsNil())
   258  	case reflect.Func:
   259  		println(indent + "  func")
   260  		println(indent+"  nil:", rv.IsNil())
   261  	case reflect.Interface:
   262  		println(indent + "  interface")
   263  		println(indent+"  nil:", rv.IsNil())
   264  	case reflect.Map:
   265  		println(indent + "  map")
   266  		println(indent+"  nil:", rv.IsNil())
   267  	case reflect.Ptr:
   268  		println(indent+"  pointer:", rv.Pointer() != 0, rt.Elem().Kind().String())
   269  		println(indent+"  nil:", rv.IsNil())
   270  		if !rv.IsNil() {
   271  			showValue(rv.Elem(), indent+"  ")
   272  		}
   273  	case reflect.Slice:
   274  		println(indent+"  slice:", rt.Elem().Kind().String(), rv.Len(), rv.Cap())
   275  		println(indent+"  pointer:", rv.Pointer() != 0)
   276  		println(indent+"  nil:", rv.IsNil())
   277  		for i := 0; i < rv.Len(); i++ {
   278  			println(indent+"  indexing:", i)
   279  			showValue(rv.Index(i), indent+"  ")
   280  		}
   281  	case reflect.Struct:
   282  		println(indent + "  struct")
   283  	default:
   284  		println(indent + "  unknown type kind!")
   285  	}
   286  }
   287  
   288  func assertSize(ok bool, typ string) {
   289  	if !ok {
   290  		panic("size mismatch for type " + typ)
   291  	}
   292  }