compiler,reflect: fix NumMethods for Interface type

This commit is contained in:
frenkel26
2024-05-28 15:09:59 +03:00
committed by GitHub
parent 81ce7fb738
commit f7c0466f78
4 changed files with 16 additions and 7 deletions
+9 -6
View File
@@ -124,16 +124,19 @@ func (c *compilerContext) pkgPathPtr(pkgpath string) llvm.Value {
func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value { func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
ms := c.program.MethodSets.MethodSet(typ) ms := c.program.MethodSets.MethodSet(typ)
hasMethodSet := ms.Len() != 0 hasMethodSet := ms.Len() != 0
if _, ok := typ.Underlying().(*types.Interface); ok { _, isInterface := typ.Underlying().(*types.Interface)
if isInterface {
hasMethodSet = false hasMethodSet = false
} }
// As defined in https://pkg.go.dev/reflect#Type:
// NumMethod returns the number of methods accessible using Method.
// For a non-interface type, it returns the number of exported methods.
// For an interface type, it returns the number of exported and unexported methods.
var numMethods int var numMethods int
if hasMethodSet { for i := 0; i < ms.Len(); i++ {
for i := 0; i < ms.Len(); i++ { if isInterface || ms.At(i).Obj().Exported() {
if ms.At(i).Obj().Exported() { numMethods++
numMethods++
}
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ target triple = "wasm32-unknown-wasi"
@"reflect/types.type:basic:int" = linkonce_odr constant { i8, ptr } { i8 -62, ptr @"reflect/types.type:pointer:basic:int" }, align 4 @"reflect/types.type:basic:int" = linkonce_odr constant { i8, ptr } { i8 -62, ptr @"reflect/types.type:pointer:basic:int" }, align 4
@"reflect/types.type:pointer:basic:int" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:basic:int" }, align 4 @"reflect/types.type:pointer:basic:int" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:basic:int" }, align 4
@"reflect/types.type:pointer:named:error" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:named:error" }, align 4 @"reflect/types.type:pointer:named:error" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:named:error" }, align 4
@"reflect/types.type:named:error" = linkonce_odr constant { i8, i16, ptr, ptr, ptr, [7 x i8] } { i8 116, i16 0, ptr @"reflect/types.type:pointer:named:error", ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}", ptr @"reflect/types.type.pkgpath.empty", [7 x i8] c".error\00" }, align 4 @"reflect/types.type:named:error" = linkonce_odr constant { i8, i16, ptr, ptr, ptr, [7 x i8] } { i8 116, i16 1, ptr @"reflect/types.type:pointer:named:error", ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}", ptr @"reflect/types.type.pkgpath.empty", [7 x i8] c".error\00" }, align 4
@"reflect/types.type.pkgpath.empty" = linkonce_odr unnamed_addr constant [1 x i8] zeroinitializer, align 1 @"reflect/types.type.pkgpath.empty" = linkonce_odr unnamed_addr constant [1 x i8] zeroinitializer, align 1
@"reflect/types.type:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, ptr } { i8 84, ptr @"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" }, align 4 @"reflect/types.type:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, ptr } { i8 84, ptr @"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" }, align 4
@"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}" }, align 4 @"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}" }, align 4
+1
View File
@@ -456,6 +456,7 @@ func showValue(rv reflect.Value, indent string) {
case reflect.Interface: case reflect.Interface:
println(indent + " interface") println(indent + " interface")
println(indent+" nil:", rv.IsNil()) println(indent+" nil:", rv.IsNil())
println(indent+" NumMethod:", rv.NumMethod())
if !rv.IsNil() { if !rv.IsNil() {
showValue(rv.Elem(), indent+" ") showValue(rv.Elem(), indent+" ")
} }
+5
View File
@@ -80,6 +80,7 @@ reflect type: ptr
reflect type: interface settable=true addrable=true reflect type: interface settable=true addrable=true
interface interface
nil: true nil: true
NumMethod: 1
reflect type: ptr reflect type: ptr
pointer: true int pointer: true int
nil: false nil: false
@@ -240,6 +241,7 @@ reflect type: struct
reflect type: interface caninterface=false reflect type: interface caninterface=false
interface interface
nil: true nil: true
NumMethod: 1
reflect type: struct reflect type: struct
struct: 3 struct: 3
field: 0 a field: 0 a
@@ -371,12 +373,14 @@ reflect type: slice comparable=false
reflect type: interface settable=true addrable=true reflect type: interface settable=true addrable=true
interface interface
nil: false nil: false
NumMethod: 0
reflect type: int reflect type: int
int: 3 int: 3
indexing: 1 indexing: 1
reflect type: interface settable=true addrable=true reflect type: interface settable=true addrable=true
interface interface
nil: false nil: false
NumMethod: 0
reflect type: string reflect type: string
string: str 3 string: str 3
reflect type: uint8 reflect type: uint8
@@ -389,6 +393,7 @@ reflect type: slice comparable=false
reflect type: interface settable=true addrable=true reflect type: interface settable=true addrable=true
interface interface
nil: false nil: false
NumMethod: 0
reflect type: complex128 reflect type: complex128
complex: (-4.000000e+000+2.500000e+000i) complex: (-4.000000e+000+2.500000e+000i)
reflect type: ptr reflect type: ptr