-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from hengwu0/master
support PrivateMethodPatch on go1.17, go1.18
- Loading branch information
Showing
3 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters