reflect2.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package reflect2
  2. import (
  3. "reflect"
  4. "runtime"
  5. "sync"
  6. "unsafe"
  7. )
  8. type Type interface {
  9. Kind() reflect.Kind
  10. // New return pointer to data of this type
  11. New() interface{}
  12. // UnsafeNew return the allocated space pointed by unsafe.Pointer
  13. UnsafeNew() unsafe.Pointer
  14. // PackEFace cast a unsafe pointer to object represented pointer
  15. PackEFace(ptr unsafe.Pointer) interface{}
  16. // Indirect dereference object represented pointer to this type
  17. Indirect(obj interface{}) interface{}
  18. // UnsafeIndirect dereference pointer to this type
  19. UnsafeIndirect(ptr unsafe.Pointer) interface{}
  20. // Type1 returns reflect.Type
  21. Type1() reflect.Type
  22. Implements(thatType Type) bool
  23. String() string
  24. RType() uintptr
  25. // interface{} of this type has pointer like behavior
  26. LikePtr() bool
  27. IsNullable() bool
  28. IsNil(obj interface{}) bool
  29. UnsafeIsNil(ptr unsafe.Pointer) bool
  30. Set(obj interface{}, val interface{})
  31. UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer)
  32. AssignableTo(anotherType Type) bool
  33. }
  34. type ListType interface {
  35. Type
  36. Elem() Type
  37. SetIndex(obj interface{}, index int, elem interface{})
  38. UnsafeSetIndex(obj unsafe.Pointer, index int, elem unsafe.Pointer)
  39. GetIndex(obj interface{}, index int) interface{}
  40. UnsafeGetIndex(obj unsafe.Pointer, index int) unsafe.Pointer
  41. }
  42. type ArrayType interface {
  43. ListType
  44. Len() int
  45. }
  46. type SliceType interface {
  47. ListType
  48. MakeSlice(length int, cap int) interface{}
  49. UnsafeMakeSlice(length int, cap int) unsafe.Pointer
  50. Grow(obj interface{}, newLength int)
  51. UnsafeGrow(ptr unsafe.Pointer, newLength int)
  52. Append(obj interface{}, elem interface{})
  53. UnsafeAppend(obj unsafe.Pointer, elem unsafe.Pointer)
  54. LengthOf(obj interface{}) int
  55. UnsafeLengthOf(ptr unsafe.Pointer) int
  56. SetNil(obj interface{})
  57. UnsafeSetNil(ptr unsafe.Pointer)
  58. Cap(obj interface{}) int
  59. UnsafeCap(ptr unsafe.Pointer) int
  60. }
  61. type StructType interface {
  62. Type
  63. NumField() int
  64. Field(i int) StructField
  65. FieldByName(name string) StructField
  66. FieldByIndex(index []int) StructField
  67. FieldByNameFunc(match func(string) bool) StructField
  68. }
  69. type StructField interface {
  70. Offset() uintptr
  71. Name() string
  72. PkgPath() string
  73. Type() Type
  74. Tag() reflect.StructTag
  75. Index() []int
  76. Anonymous() bool
  77. Set(obj interface{}, value interface{})
  78. UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer)
  79. Get(obj interface{}) interface{}
  80. UnsafeGet(obj unsafe.Pointer) unsafe.Pointer
  81. }
  82. type MapType interface {
  83. Type
  84. Key() Type
  85. Elem() Type
  86. MakeMap(cap int) interface{}
  87. UnsafeMakeMap(cap int) unsafe.Pointer
  88. SetIndex(obj interface{}, key interface{}, elem interface{})
  89. UnsafeSetIndex(obj unsafe.Pointer, key unsafe.Pointer, elem unsafe.Pointer)
  90. TryGetIndex(obj interface{}, key interface{}) (interface{}, bool)
  91. GetIndex(obj interface{}, key interface{}) interface{}
  92. UnsafeGetIndex(obj unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer
  93. Iterate(obj interface{}) MapIterator
  94. UnsafeIterate(obj unsafe.Pointer) MapIterator
  95. }
  96. type MapIterator interface {
  97. HasNext() bool
  98. Next() (key interface{}, elem interface{})
  99. UnsafeNext() (key unsafe.Pointer, elem unsafe.Pointer)
  100. }
  101. type PtrType interface {
  102. Type
  103. Elem() Type
  104. }
  105. type InterfaceType interface {
  106. NumMethod() int
  107. }
  108. type Config struct {
  109. UseSafeImplementation bool
  110. }
  111. type API interface {
  112. TypeOf(obj interface{}) Type
  113. Type2(type1 reflect.Type) Type
  114. }
  115. var ConfigUnsafe = Config{UseSafeImplementation: false}.Froze()
  116. var ConfigSafe = Config{UseSafeImplementation: true}.Froze()
  117. type frozenConfig struct {
  118. useSafeImplementation bool
  119. cache *sync.Map
  120. }
  121. func (cfg Config) Froze() *frozenConfig {
  122. return &frozenConfig{
  123. useSafeImplementation: cfg.UseSafeImplementation,
  124. cache: new(sync.Map),
  125. }
  126. }
  127. func (cfg *frozenConfig) TypeOf(obj interface{}) Type {
  128. cacheKey := uintptr(unpackEFace(obj).rtype)
  129. typeObj, found := cfg.cache.Load(cacheKey)
  130. if found {
  131. return typeObj.(Type)
  132. }
  133. return cfg.Type2(reflect.TypeOf(obj))
  134. }
  135. func (cfg *frozenConfig) Type2(type1 reflect.Type) Type {
  136. if type1 == nil {
  137. return nil
  138. }
  139. cacheKey := uintptr(unpackEFace(type1).data)
  140. typeObj, found := cfg.cache.Load(cacheKey)
  141. if found {
  142. return typeObj.(Type)
  143. }
  144. type2 := cfg.wrapType(type1)
  145. cfg.cache.Store(cacheKey, type2)
  146. return type2
  147. }
  148. func (cfg *frozenConfig) wrapType(type1 reflect.Type) Type {
  149. safeType := safeType{Type: type1, cfg: cfg}
  150. switch type1.Kind() {
  151. case reflect.Struct:
  152. if cfg.useSafeImplementation {
  153. return &safeStructType{safeType}
  154. }
  155. return newUnsafeStructType(cfg, type1)
  156. case reflect.Array:
  157. if cfg.useSafeImplementation {
  158. return &safeSliceType{safeType}
  159. }
  160. return newUnsafeArrayType(cfg, type1)
  161. case reflect.Slice:
  162. if cfg.useSafeImplementation {
  163. return &safeSliceType{safeType}
  164. }
  165. return newUnsafeSliceType(cfg, type1)
  166. case reflect.Map:
  167. if cfg.useSafeImplementation {
  168. return &safeMapType{safeType}
  169. }
  170. return newUnsafeMapType(cfg, type1)
  171. case reflect.Ptr, reflect.Chan, reflect.Func:
  172. if cfg.useSafeImplementation {
  173. return &safeMapType{safeType}
  174. }
  175. return newUnsafePtrType(cfg, type1)
  176. case reflect.Interface:
  177. if cfg.useSafeImplementation {
  178. return &safeMapType{safeType}
  179. }
  180. if type1.NumMethod() == 0 {
  181. return newUnsafeEFaceType(cfg, type1)
  182. }
  183. return newUnsafeIFaceType(cfg, type1)
  184. default:
  185. if cfg.useSafeImplementation {
  186. return &safeType
  187. }
  188. return newUnsafeType(cfg, type1)
  189. }
  190. }
  191. func TypeOf(obj interface{}) Type {
  192. return ConfigUnsafe.TypeOf(obj)
  193. }
  194. func TypeOfPtr(obj interface{}) PtrType {
  195. return TypeOf(obj).(PtrType)
  196. }
  197. func Type2(type1 reflect.Type) Type {
  198. if type1 == nil {
  199. return nil
  200. }
  201. return ConfigUnsafe.Type2(type1)
  202. }
  203. func PtrTo(typ Type) Type {
  204. return Type2(reflect.PtrTo(typ.Type1()))
  205. }
  206. func PtrOf(obj interface{}) unsafe.Pointer {
  207. return unpackEFace(obj).data
  208. }
  209. func RTypeOf(obj interface{}) uintptr {
  210. return uintptr(unpackEFace(obj).rtype)
  211. }
  212. func IsNil(obj interface{}) bool {
  213. if obj == nil {
  214. return true
  215. }
  216. return unpackEFace(obj).data == nil
  217. }
  218. func IsNullable(kind reflect.Kind) bool {
  219. switch kind {
  220. case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Func, reflect.Slice, reflect.Interface:
  221. return true
  222. }
  223. return false
  224. }
  225. func likePtrKind(kind reflect.Kind) bool {
  226. switch kind {
  227. case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Func:
  228. return true
  229. }
  230. return false
  231. }
  232. func likePtrType(typ reflect.Type) bool {
  233. if likePtrKind(typ.Kind()) {
  234. return true
  235. }
  236. if typ.Kind() == reflect.Struct {
  237. if typ.NumField() != 1 {
  238. return false
  239. }
  240. return likePtrType(typ.Field(0).Type)
  241. }
  242. if typ.Kind() == reflect.Array {
  243. if typ.Len() != 1 {
  244. return false
  245. }
  246. return likePtrType(typ.Elem())
  247. }
  248. return false
  249. }
  250. // NoEscape hides a pointer from escape analysis. noescape is
  251. // the identity function but escape analysis doesn't think the
  252. // output depends on the input. noescape is inlined and currently
  253. // compiles down to zero instructions.
  254. // USE CAREFULLY!
  255. //go:nosplit
  256. func NoEscape(p unsafe.Pointer) unsafe.Pointer {
  257. x := uintptr(p)
  258. return unsafe.Pointer(x ^ 0)
  259. }
  260. func UnsafeCastString(str string) []byte {
  261. bytes := make([]byte, 0)
  262. stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&str))
  263. sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
  264. sliceHeader.Data = stringHeader.Data
  265. sliceHeader.Cap = stringHeader.Len
  266. sliceHeader.Len = stringHeader.Len
  267. runtime.KeepAlive(str)
  268. return bytes
  269. }