2 Star 7 Fork 11

王布衣/engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reflect.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-11-23 08:07 . 调整package
package labs
import (
"fmt"
"gitee.com/quant1x/engine/factors"
"reflect"
"strings"
"sync"
"unsafe"
)
//go:linkname typelinks2 reflect.typelinks
func typelinks2() (sections []unsafe.Pointer, offset [][]int32)
//go:linkname resolveTypeOff reflect.resolveTypeOff
func resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer
var (
typeOnce sync.Once
types = map[string]reflect.Type{}
packages = map[string]map[string]reflect.Type{}
)
type emptyInterface struct {
typ unsafe.Pointer
word unsafe.Pointer
}
func loadGoTypes() {
fia := reflect.TypeOf((*factors.Feature)(nil)).Elem()
var obj interface{} = reflect.TypeOf(0)
sections, offset := typelinks2()
for i, offs := range offset {
rodata := sections[i]
for _, off := range offs {
(*emptyInterface)(unsafe.Pointer(&obj)).word = resolveTypeOff(unsafe.Pointer(rodata), off)
typ := obj.(reflect.Type)
if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
loadedType := typ.Elem()
pkgTypes := packages[loadedType.PkgPath()]
if pkgTypes == nil {
pkgTypes = map[string]reflect.Type{}
packages[loadedType.PkgPath()] = pkgTypes
}
//types[typeString] = loadedType
pkgPath := loadedType.PkgPath()
typeName := loadedType.Name()
//typeString := loadedType.String()
//fmt.Println(pkgPath, "=>", typeString, "=>", typeName)
structName := fmt.Sprintf("%s.%s", pkgPath, typeName)
types[structName] = loadedType
pkgTypes[loadedType.Name()] = loadedType
if strings.HasPrefix(pkgPath, "gitee.com/quant1x/engine") {
//fmt.Println(structName, "==>", loadedType.PkgPath())
if reflect.PtrTo(loadedType).Implements(fia) {
fmt.Println("found", pkgPath, "==>", loadedType.String(), "==>", structName)
}
}
}
}
}
}
func lazyInit() {
loadGoTypes()
}
func FindImplements(u reflect.Type) (list []reflect.Type) {
typeOnce.Do(lazyInit)
for name, t := range types {
//fmt.Println(name)
if reflect.PtrTo(t).Implements(u) {
list = append(list, t)
}
_ = name
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/engine.git
git@gitee.com:quant1x/engine.git
quant1x
engine
engine
v1.8.33

搜索帮助