github.com/aykevl/tinygo@v0.5.0/src/runtime/interface.go (about)

     1  package runtime
     2  
     3  // This file implements Go interfaces.
     4  //
     5  // Interfaces are represented as a pair of {typecode, value}, where value can be
     6  // anything (including non-pointers).
     7  
     8  import "unsafe"
     9  
    10  type _interface struct {
    11  	typecode uintptr
    12  	value    unsafe.Pointer
    13  }
    14  
    15  // Return true iff both interfaces are equal.
    16  func interfaceEqual(x, y _interface) bool {
    17  	if x.typecode != y.typecode {
    18  		// Different dynamic type so always unequal.
    19  		return false
    20  	}
    21  	if x.typecode == 0 {
    22  		// Both interfaces are nil, so they are equal.
    23  		return true
    24  	}
    25  	// TODO: depends on reflection.
    26  	panic("unimplemented: interface equality")
    27  }
    28  
    29  // interfaceTypeAssert is called when a type assert without comma-ok still
    30  // returns false.
    31  func interfaceTypeAssert(ok bool) {
    32  	if !ok {
    33  		runtimePanic("type assert failed")
    34  	}
    35  }
    36  
    37  // The following declarations are only used during IR construction. They are
    38  // lowered to inline IR in the interface lowering pass.
    39  // See compiler/interface-lowering.go for details.
    40  
    41  type interfaceMethodInfo struct {
    42  	signature *uint8  // external *i8 with a name identifying the Go function signature
    43  	funcptr   uintptr // bitcast from the actual function pointer
    44  }
    45  
    46  type typecodeID struct{}
    47  
    48  // Pseudo type used before interface lowering. By using a struct instead of a
    49  // function call, this is simpler to reason about during init interpretation
    50  // than a function call. Also, by keeping the method set around it is easier to
    51  // implement interfaceImplements in the interp package.
    52  type typeInInterface struct {
    53  	typecode  *typecodeID
    54  	methodSet *interfaceMethodInfo // nil or a GEP of an array
    55  }
    56  
    57  // Pseudo function call used during a type assert. It is used during interface
    58  // lowering, to assign the lowest type numbers to the types with the most type
    59  // asserts. Also, it is replaced with const false if this type assert can never
    60  // happen.
    61  func typeAssert(actualType uintptr, assertedType *typecodeID) bool
    62  
    63  // Pseudo function call that returns whether a given type implements all methods
    64  // of the given interface.
    65  func interfaceImplements(typecode uintptr, interfaceMethodSet **uint8) bool
    66  
    67  // Pseudo function that returns a function pointer to the method to call.
    68  // See the interface lowering pass for how this is lowered to a real call.
    69  func interfaceMethod(typecode uintptr, interfaceMethodSet **uint8, signature *uint8) uintptr