Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions linkname.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@ func resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer
//go:linkname addReflectOff reflect.addReflectOff
func addReflectOff(ptr unsafe.Pointer) int32

// resolveReflectType adds a *rtype to the reflection lookup map in the runtime.
// It returns a new typeOff that can be used to refer to the pointer.
func resolveReflectType(t *rtype) typeOff {
return typeOff(addReflectOff(unsafe.Pointer(t)))
}

// resolveReflectText adds a function pointer to the reflection lookup map in
// the runtime. It returns a new textOff that can be used to refer to the
// pointer.
func resolveReflectText(ptr unsafe.Pointer) textOff {
return textOff(addReflectOff(ptr))
}

// resolveReflectName adds a name to the reflection lookup map in the runtime.
// It returns a new nameOff that can be used to refer to the pointer.
func resolveReflectName(n name) nameOff {
return nameOff(addReflectOff(unsafe.Pointer(n.bytes)))
return nameOff(addReflectOff(unsafe.Pointer(n.Bytes)))
}

//go:linkname resolveNameOff reflect.resolveNameOff
Expand All @@ -51,7 +64,7 @@ func resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer
func toType(t *rtype) reflect.Type

func rtype_nameOff(t *rtype, off nameOff) name {
return name{bytes: (*byte)(resolveNameOff(unsafe.Pointer(t), int32(off)))}
return name{Bytes: (*byte)(resolveNameOff(unsafe.Pointer(t), int32(off)))}
}

func rtype_typeOff(t *rtype, off typeOff) *rtype {
Expand Down
35 changes: 13 additions & 22 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@

package reflectx

import "unsafe"
import (
"github.com/goplus/reflectx/internal/abi"
)

// mapType represents a map type.
type mapType struct {
rtype
key *rtype // map key type
elem *rtype // map element (value) type
bucket *rtype // internal bucket structure
// function for hashing keys (ptr to key, seed) -> hash
hasher func(unsafe.Pointer, uintptr) uintptr
keysize uint8 // size of key slot
valuesize uint8 // size of value slot
bucketsize uint16 // size of bucket
flags uint32
}
// mapType is abi.MapType for the noswiss map implementation
type mapType = abi.MapType

func cloneMap(st, ost *mapType) {
st.key = ost.key
st.elem = ost.elem
st.bucket = ost.bucket
st.hasher = ost.hasher
st.keysize = ost.keysize
st.valuesize = ost.valuesize
st.bucketsize = ost.bucketsize
st.flags = ost.flags
st.Key = ost.Key
st.Elem = ost.Elem
st.Bucket = ost.Bucket
st.Hasher = ost.Hasher
st.KeySize = ost.KeySize
st.ValueSize = ost.ValueSize
st.BucketSize = ost.BucketSize
st.Flags = ost.Flags
}
34 changes: 13 additions & 21 deletions map_go124.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@

package reflectx

import "unsafe"
import (
"github.com/goplus/reflectx/internal/abi"
)

type mapType struct {
rtype
key *rtype
elem *rtype
group *rtype // internal type representing a slot group
// function for hashing keys (ptr to key, seed) -> hash
hasher func(unsafe.Pointer, uintptr) uintptr
groupSize uintptr // == Group.Size_
slotSize uintptr // size of key/elem slot
elemOff uintptr // offset of elem in key/elem slot
flags uint32
}
// mapType is abi.MapType for the swiss map implementation
type mapType = abi.MapType

func cloneMap(st, ost *mapType) {
st.key = ost.key
st.elem = ost.elem
st.group = ost.group
st.hasher = ost.hasher
st.groupSize = ost.groupSize
st.slotSize = ost.slotSize
st.elemOff = ost.elemOff
st.flags = ost.flags
st.Key = ost.Key
st.Elem = ost.Elem
st.Group = ost.Group
st.Hasher = ost.Hasher
st.GroupSize = ost.GroupSize
st.SlotSize = ost.SlotSize
st.ElemOff = ost.ElemOff
st.Flags = ost.Flags
}
32 changes: 16 additions & 16 deletions method.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ func UpdateField(typ reflect.Type, rmap map[reflect.Type]reflect.Type) bool {
}
rt := totype(typ)
st := toStructType(rt)
for i := 0; i < len(st.fields); i++ {
t := replaceType(toType(st.fields[i].typ), rmap)
st.fields[i].typ = totype(t)
for i := 0; i < len(st.Fields); i++ {
t := replaceType(toType(st.Fields[i].Typ), rmap)
st.Fields[i].Typ = totype(t)
}
return true
}
Expand Down Expand Up @@ -329,7 +329,7 @@ func SetInterfaceType(typ reflect.Type, embedded []reflect.Type, methods []refle
})
rt := totype(typ)
st := (*interfaceType)(toKindType(rt))
st.methods = nil
st.Methods = nil
var info []string
var lastname string
var unnamed bool
Expand All @@ -347,14 +347,14 @@ func SetInterfaceType(typ reflect.Type, embedded []reflect.Type, methods []refle
nm := newNameEx(m.Name, "", isexport, !isexport)
mname = resolveReflectName(nm)
if !isexport {
nm.setPkgPath(m.PkgPath)
setPkgPath(nm, m.PkgPath)
}
} else {
mname = resolveReflectName(newName(m.Name, "", isexport))
}
st.methods = append(st.methods, imethod{
name: mname,
typ: resolveReflectType(totype(m.Type)),
st.Methods = append(st.Methods, imethod{
Name: mname,
Typ: resolveReflectType(totype(m.Type)),
})
info = append(info, methodStr(m.Name, m.Type))
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func (ctx *Context) InterfaceOf(embedded []reflect.Type, methods []reflect.Metho
})
rt, _ := newType("", "", tyEmptyInterface, 0, 0)
st := (*interfaceType)(toKindType(rt))
st.methods = nil
st.Methods = nil
var info []string
var lastname string
for _, m := range methods {
Expand All @@ -400,16 +400,16 @@ func (ctx *Context) InterfaceOf(embedded []reflect.Type, methods []reflect.Metho
nm := newNameEx(m.Name, "", isexport, !isexport)
mname = resolveReflectName(nm)
if !isexport {
nm.setPkgPath(m.PkgPath)
setPkgPath(nm, m.PkgPath)
}
st.methods = append(st.methods, imethod{
name: mname,
typ: resolveReflectType(totype(m.Type)),
st.Methods = append(st.Methods, imethod{
Name: mname,
Typ: resolveReflectType(totype(m.Type)),
})
info = append(info, methodStr(m.Name, m.Type))
}
if len(st.methods) > 0 {
rt.equal = interequal
if len(st.Methods) > 0 {
rt.Equal = interequal
}
var str string
if len(info) > 0 {
Expand All @@ -420,7 +420,7 @@ func (ctx *Context) InterfaceOf(embedded []reflect.Type, methods []reflect.Metho
if t, ok := ctx.interfceLookupCache[str]; ok {
return t
}
rt.str = resolveReflectName(newName(str, "", false))
rt.Str = resolveReflectName(newName(str, "", false))
typ := toType(rt)
ctx.interfceLookupCache[str] = typ
return typ
Expand Down
46 changes: 23 additions & 23 deletions methodof.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (ctx *Context) registerMethod(info *abi.MethodInfo) (ifn unsafe.Pointer, al
}

func isMethod(typ reflect.Type) (ok bool) {
return totype(typ).tflag&tflagUserMethod != 0
return totype(typ).TFlag&tflagUserMethod != 0
}

type MethodInfo struct {
Expand All @@ -113,11 +113,11 @@ type MethodInfo struct {
}

func MethodByIndex(typ reflect.Type, index int) reflect.Method {
return totype(typ).MethodX(index)
return rtypeMethodX(totype(typ), index)
}

func MethodByName(typ reflect.Type, name string) (m reflect.Method, ok bool) {
m, ok = totype(typ).MethodByNameX(name)
m, ok = rtypeMethodByNameX(totype(typ), name)
return
}

Expand All @@ -127,10 +127,10 @@ func resizeMethod(typ reflect.Type, mcount int, xcount int) error {
if ut == nil {
return fmt.Errorf("not found uncommonType of %v", typ)
}
if uint16(mcount) > ut.mcount {
if uint16(mcount) > ut.Mcount {
return fmt.Errorf("too many methods of %v", typ)
}
ut.xcount = uint16(xcount)
ut.Xcount = uint16(xcount)
return nil
}

Expand Down Expand Up @@ -227,8 +227,8 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
rt := totype(typ)
prt := totype(ptyp)

ms := rt.methods()
pms := prt.methods()
ms := rtypeMethods(rt)
pms := rtypeMethods(prt)

var onePtr bool
switch typ.Kind() {
Expand All @@ -252,7 +252,7 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
isexport := methodIsExported(m.Name)
nm := newNameEx(m.Name, "", isexport, !isexport)
if !isexport {
nm.setPkgPath(m.PkgPath)
setPkgPath(nm, m.PkgPath)
}
mname := resolveReflectName(nm)
mfn, inTyp, outTyp, mtyp, tfn, ptfn := createMethod(typ, ptyp, m, index)
Expand All @@ -272,15 +272,15 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
OnePtr: onePtr,
FuncId: m.FuncId,
}
pms[i].name = mname
pms[i].mtyp = mtyp
pms[i].tfn = ptfn
pms[i].Name = mname
pms[i].Mtyp = mtyp
pms[i].Tfn = ptfn
var pifn unsafe.Pointer = zeroIfn
hasIfn := ctx.hasImethod(typ, m)
if hasIfn {
pifn, _ = ctx.registerMethod(pinfo)
}
pms[i].ifn = resolveReflectText(pifn)
pms[i].Ifn = resolveReflectText(pifn)
if m.FuncId > 0 {
if hasIfn {
globalIfnCached++
Expand Down Expand Up @@ -308,10 +308,10 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
globalIfnCached++
}
}
ms[index].name = mname
ms[index].mtyp = mtyp
ms[index].tfn = tfn
ms[index].ifn = resolveReflectText(ifn)
ms[index].Name = mname
ms[index].Mtyp = mtyp
ms[index].Tfn = tfn
ms[index].Ifn = resolveReflectText(ifn)
if m.FuncId > 0 {
if hasIfn {
globalIfnCached++
Expand All @@ -321,8 +321,8 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
index++
}
}
rt.tflag |= tflagUserMethod
prt.tflag |= tflagUserMethod
rt.TFlag |= tflagUserMethod
prt.TFlag |= tflagUserMethod

if ctx.nAllocateError != 0 {
ncap := abi.Default.Cap()
Expand All @@ -342,10 +342,10 @@ func (ctx *Context) setMethodSet(typ reflect.Type, methods []Method, sortMethods
func newMethodSet(styp reflect.Type, maxmfunc, maxpfunc int) reflect.Type {
rt, _ := newType("", "", styp, maxmfunc, 0)
prt, _ := newType("", "", PtrTo(styp), maxpfunc, 0)
rt.ptrToThis = resolveReflectType(prt)
(*ptrType)(unsafe.Pointer(prt)).elem = rt
rt.PtrToThis = resolveReflectType(prt)
(*ptrType)(unsafe.Pointer(prt)).Elem = rt
setTypeName(rt, styp.PkgPath(), styp.Name())
prt.uncommon().pkgPath = resolveReflectName(newName(styp.PkgPath(), "", false))
prt.Uncommon().PkgPath = resolveReflectName(newName(styp.PkgPath(), "", false))
return toType(rt)
}

Expand Down Expand Up @@ -386,9 +386,9 @@ func argsTypeSize(typ reflect.Type, offset bool) (off uintptr) {
for i := 0; i < numIn; i++ {
t := typ.Field(i).Type
targ := totype(t)
a := uintptr(targ.align)
a := uintptr(targ.Align_)
off = (off + a - 1) &^ (a - 1)
n := targ.size
n := targ.Size_
if n == 0 {
continue
}
Expand Down
Loading
Loading