github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/transform/testdata/reflect.go (about)

     1  package main
     2  
     3  // This file tests the type codes assigned by the reflect lowering pass.
     4  // This test is not complete, most importantly, sidetables are not currently
     5  // being tested.
     6  
     7  import (
     8  	"reflect"
     9  	"unsafe"
    10  )
    11  
    12  const (
    13  	// See the top of src/reflect/type.go
    14  	prefixChan      = 0b0001
    15  	prefixInterface = 0b0011
    16  	prefixPtr       = 0b0101
    17  	prefixSlice     = 0b0111
    18  	prefixArray     = 0b1001
    19  	prefixFunc      = 0b1011
    20  	prefixMap       = 0b1101
    21  	prefixStruct    = 0b1111
    22  )
    23  
    24  func main() {
    25  	// Check for some basic types.
    26  	assertType(3, uintptr(reflect.Int)<<1)
    27  	assertType(uint8(3), uintptr(reflect.Uint8)<<1)
    28  	assertType(byte(3), uintptr(reflect.Uint8)<<1)
    29  	assertType(int64(3), uintptr(reflect.Int64)<<1)
    30  	assertType("", uintptr(reflect.String)<<1)
    31  	assertType(3.5, uintptr(reflect.Float64)<<1)
    32  	assertType(unsafe.Pointer(nil), uintptr(reflect.UnsafePointer)<<1)
    33  
    34  	// Check for named types: they are given names in order.
    35  	// They are sorted in reverse, for no good reason.
    36  	const intNum = uintptr(reflect.Int) << 1
    37  	assertType(namedInt1(0), (3<<6)|intNum)
    38  	assertType(namedInt2(0), (2<<6)|intNum)
    39  	assertType(namedInt3(0), (1<<6)|intNum)
    40  
    41  	// Check for some "prefix-style" types.
    42  	assertType(make(chan int), (intNum<<5)|prefixChan)
    43  	assertType(new(int), (intNum<<5)|prefixPtr)
    44  	assertType([]int{}, (intNum<<5)|prefixSlice)
    45  }
    46  
    47  type (
    48  	namedInt1 int
    49  	namedInt2 int
    50  	namedInt3 int
    51  )
    52  
    53  // Pseudo call that is being checked by the code in reflect_test.go.
    54  // After reflect lowering, the type code as part of the interface should match
    55  // the asserted type code.
    56  func assertType(itf interface{}, assertedTypeCode uintptr)