Skip to content

Commit

Permalink
Merge pull request #85 from hengwu0/master
Browse files Browse the repository at this point in the history
support PrivateMethodPatch on go1.17, go1.18
  • Loading branch information
agiledragon authored Apr 3, 2022
2 parents e1bb229 + 02e886b commit 92b9f21
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
39 changes: 39 additions & 0 deletions creflect/ae1.17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build go1.17
// +build go1.17

package creflect

import (
"unsafe"
)

// name is an encoded type name with optional extra data.
type name struct {
bytes *byte
}

func (n name) data(off int, whySafe string) *byte {
return (*byte)(add(unsafe.Pointer(n.bytes), uintptr(off), whySafe))
}

func (n name) readVarint(off int) (int, int) {
v := 0
for i := 0; ; i++ {
x := *n.data(off+i, "read varint")
v += int(x&0x7f) << (7 * i)
if x&0x80 == 0 {
return i + 1, v
}
}
}

func (n name) name() (s string) {
if n.bytes == nil {
return
}
i, l := n.readVarint(1)
hdr := (*String)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(n.data(1+i, "non-empty string"))
hdr.Len = l
return
}
25 changes: 25 additions & 0 deletions creflect/be1.16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !go1.17
// +build !go1.17

package creflect

import (
"unsafe"
)

// name is an encoded type name with optional extra data.
type name struct {
bytes *byte
}

func (n name) name() (s string) {
if n.bytes == nil {
return
}
b := (*[4]byte)(unsafe.Pointer(n.bytes))

hdr := (*String)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(&b[3])
hdr.Len = int(b[1])<<8 | int(b[2])
return s
}
21 changes: 3 additions & 18 deletions creflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type funcValue struct {
_ uintptr
p unsafe.Pointer
}

func funcPointer(v reflect.Method, ok bool) (unsafe.Pointer, bool) {
return (*funcValue)(unsafe.Pointer(&v.Func)).p, ok
}
Expand Down Expand Up @@ -84,6 +85,7 @@ func (t *rtype) nameOff(off nameOff) name {
const (
tflagUncommon tflag = 1 << 0
)

// uncommonType is present only for defined types or types with methods
type uncommonType struct {
pkgPath nameOff // import path; empty for built-in types like int, string
Expand Down Expand Up @@ -122,28 +124,11 @@ type imethod struct {
typ typeOff // .(*FuncType) underneath
}

// name is an encoded type name with optional extra data.
type name struct {
bytes *byte
}

type String struct {
Data unsafe.Pointer
Len int
}

func (n name) name() (s string) {
if n.bytes == nil {
return
}
b := (*[4]byte)(unsafe.Pointer(n.bytes))

hdr := (*String)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(&b[3])
hdr.Len = int(b[1])<<8 | int(b[2])
return s
}

func (t *rtype) uncommon(r reflect.Type) *uncommonType {
if t.tflag&tflagUncommon == 0 {
return nil
Expand Down Expand Up @@ -191,4 +176,4 @@ func (t *uncommonType) methods() []method {
return nil
}
return (*[1 << 16]method)(add(unsafe.Pointer(t), uintptr(t.moff), "t.mcount > 0"))[:t.mcount:t.mcount]
}
}

0 comments on commit 92b9f21

Please sign in to comment.