all: complete the implementation of interface asserts

Because a few things were left unimplemented it only happened to kind-of
work before in my test cases.
This commit should complete interface-to-interface type asserts.
This commit is contained in:
Ayke van Laethem
2018-09-11 19:39:25 +02:00
parent 61e6f7cf5b
commit 4ad6df3227
2 changed files with 29 additions and 6 deletions
+19 -6
View File
@@ -37,7 +37,7 @@ type methodSetRange struct {
// in.
var (
firstTypeWithMethods uint16 // the lowest typecode that has at least one method
methodSetRanges [0]methodSetRange // indexes into methodSetSignatures and methodSetFunctions
methodSetRanges [0]methodSetRange // indices into methodSetSignatures and methodSetFunctions
methodSetSignatures [0]uint16 // uniqued method ID
methodSetFunctions [0]*uint8 // function pointer of method
interfaceIndex [0]uint16 // mapping from interface ID to an index in interfaceMethods
@@ -81,15 +81,28 @@ func interfaceEqual(x, y _interface) bool {
// This is a compiler intrinsic.
//go:nobounds
func interfaceImplements(typecode, interfaceNum uint16) bool {
// method set indexes of the concrete type
// method set indices of the interface
itfIndex := interfaceIndex[interfaceNum]
itfIndexEnd := itfIndex + uint16(interfaceLengths[interfaceNum])
if itfIndex == itfIndexEnd {
// This interface has no methods, so it satisfies all types.
// TODO: this should be figured out at compile time (as it is known at
// compile time), so that this check is unnecessary at runtime.
return true
}
if typecode < firstTypeWithMethods {
// Type has no methods while the interface has (checked above), so this
// type does not satisfy this interface.
return false
}
// method set indices of the concrete type
methodSet := methodSetRanges[typecode-firstTypeWithMethods]
methodIndex := methodSet.index
methodIndexEnd := methodSet.index + methodSet.length
// method set indexes of the interface
itfIndex := interfaceIndex[interfaceNum]
itfIndexEnd := itfIndex + uint16(interfaceLengths[interfaceNum])
// Iterate over all methods of the interface:
for itfIndex < itfIndexEnd {
methodId := interfaceMethods[itfIndex]