fastvalue.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package rt
  17. import (
  18. `reflect`
  19. `unsafe`
  20. )
  21. var (
  22. reflectRtypeItab = findReflectRtypeItab()
  23. )
  24. // GoType.KindFlags const
  25. const (
  26. F_direct = 1 << 5
  27. F_kind_mask = (1 << 5) - 1
  28. )
  29. // GoType.Flags const
  30. const (
  31. tflagUncommon uint8 = 1 << 0
  32. tflagExtraStar uint8 = 1 << 1
  33. tflagNamed uint8 = 1 << 2
  34. tflagRegularMemory uint8 = 1 << 3
  35. )
  36. type GoType struct {
  37. Size uintptr
  38. PtrData uintptr
  39. Hash uint32
  40. Flags uint8
  41. Align uint8
  42. FieldAlign uint8
  43. KindFlags uint8
  44. Traits unsafe.Pointer
  45. GCData *byte
  46. Str int32
  47. PtrToSelf int32
  48. }
  49. func (self *GoType) IsNamed() bool {
  50. return (self.Flags & tflagNamed) != 0
  51. }
  52. func (self *GoType) Kind() reflect.Kind {
  53. return reflect.Kind(self.KindFlags & F_kind_mask)
  54. }
  55. func (self *GoType) Pack() (t reflect.Type) {
  56. (*GoIface)(unsafe.Pointer(&t)).Itab = reflectRtypeItab
  57. (*GoIface)(unsafe.Pointer(&t)).Value = unsafe.Pointer(self)
  58. return
  59. }
  60. func (self *GoType) String() string {
  61. return self.Pack().String()
  62. }
  63. func (self *GoType) Indirect() bool {
  64. return self.KindFlags & F_direct == 0
  65. }
  66. type GoMap struct {
  67. Count int
  68. Flags uint8
  69. B uint8
  70. Overflow uint16
  71. Hash0 uint32
  72. Buckets unsafe.Pointer
  73. OldBuckets unsafe.Pointer
  74. Evacuate uintptr
  75. Extra unsafe.Pointer
  76. }
  77. type GoMapIterator struct {
  78. K unsafe.Pointer
  79. V unsafe.Pointer
  80. T *GoMapType
  81. H *GoMap
  82. Buckets unsafe.Pointer
  83. Bptr *unsafe.Pointer
  84. Overflow *[]unsafe.Pointer
  85. OldOverflow *[]unsafe.Pointer
  86. StartBucket uintptr
  87. Offset uint8
  88. Wrapped bool
  89. B uint8
  90. I uint8
  91. Bucket uintptr
  92. CheckBucket uintptr
  93. }
  94. type GoItab struct {
  95. it unsafe.Pointer
  96. Vt *GoType
  97. hv uint32
  98. _ [4]byte
  99. fn [1]uintptr
  100. }
  101. type GoIface struct {
  102. Itab *GoItab
  103. Value unsafe.Pointer
  104. }
  105. type GoEface struct {
  106. Type *GoType
  107. Value unsafe.Pointer
  108. }
  109. func (self GoEface) Pack() (v interface{}) {
  110. *(*GoEface)(unsafe.Pointer(&v)) = self
  111. return
  112. }
  113. type GoPtrType struct {
  114. GoType
  115. Elem *GoType
  116. }
  117. type GoMapType struct {
  118. GoType
  119. Key *GoType
  120. Elem *GoType
  121. Bucket *GoType
  122. Hasher func(unsafe.Pointer, uintptr) uintptr
  123. KeySize uint8
  124. ElemSize uint8
  125. BucketSize uint16
  126. Flags uint32
  127. }
  128. func (self *GoMapType) IndirectElem() bool {
  129. return self.Flags & 2 != 0
  130. }
  131. type GoStructType struct {
  132. GoType
  133. Pkg *byte
  134. Fields []GoStructField
  135. }
  136. type GoStructField struct {
  137. Name *byte
  138. Type *GoType
  139. OffEmbed uintptr
  140. }
  141. type GoInterfaceType struct {
  142. GoType
  143. PkgPath *byte
  144. Methods []GoInterfaceMethod
  145. }
  146. type GoInterfaceMethod struct {
  147. Name int32
  148. Type int32
  149. }
  150. type GoSlice struct {
  151. Ptr unsafe.Pointer
  152. Len int
  153. Cap int
  154. }
  155. type GoString struct {
  156. Ptr unsafe.Pointer
  157. Len int
  158. }
  159. func PtrElem(t *GoType) *GoType {
  160. return (*GoPtrType)(unsafe.Pointer(t)).Elem
  161. }
  162. func MapType(t *GoType) *GoMapType {
  163. return (*GoMapType)(unsafe.Pointer(t))
  164. }
  165. func IfaceType(t *GoType) *GoInterfaceType {
  166. return (*GoInterfaceType)(unsafe.Pointer(t))
  167. }
  168. func UnpackType(t reflect.Type) *GoType {
  169. return (*GoType)((*GoIface)(unsafe.Pointer(&t)).Value)
  170. }
  171. func UnpackEface(v interface{}) GoEface {
  172. return *(*GoEface)(unsafe.Pointer(&v))
  173. }
  174. func UnpackIface(v interface{}) GoIface {
  175. return *(*GoIface)(unsafe.Pointer(&v))
  176. }
  177. func findReflectRtypeItab() *GoItab {
  178. v := reflect.TypeOf(struct{}{})
  179. return (*GoIface)(unsafe.Pointer(&v)).Itab
  180. }