fastvalue.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 GoItab struct {
  67. it unsafe.Pointer
  68. Vt *GoType
  69. hv uint32
  70. _ [4]byte
  71. fn [1]uintptr
  72. }
  73. type GoIface struct {
  74. Itab *GoItab
  75. Value unsafe.Pointer
  76. }
  77. type GoEface struct {
  78. Type *GoType
  79. Value unsafe.Pointer
  80. }
  81. func (self GoEface) Pack() (v interface{}) {
  82. *(*GoEface)(unsafe.Pointer(&v)) = self
  83. return
  84. }
  85. type GoPtrType struct {
  86. GoType
  87. Elem *GoType
  88. }
  89. type GoMapType struct {
  90. GoType
  91. Key *GoType
  92. Elem *GoType
  93. Bucket *GoType
  94. Hasher func(unsafe.Pointer, uintptr) uintptr
  95. KeySize uint8
  96. ElemSize uint8
  97. BucketSize uint16
  98. Flags uint32
  99. }
  100. func (self *GoMapType) IndirectElem() bool {
  101. return self.Flags & 2 != 0
  102. }
  103. type GoStructType struct {
  104. GoType
  105. Pkg *byte
  106. Fields []GoStructField
  107. }
  108. type GoStructField struct {
  109. Name *byte
  110. Type *GoType
  111. OffEmbed uintptr
  112. }
  113. type GoInterfaceType struct {
  114. GoType
  115. PkgPath *byte
  116. Methods []GoInterfaceMethod
  117. }
  118. type GoInterfaceMethod struct {
  119. Name int32
  120. Type int32
  121. }
  122. type GoSlice struct {
  123. Ptr unsafe.Pointer
  124. Len int
  125. Cap int
  126. }
  127. type GoString struct {
  128. Ptr unsafe.Pointer
  129. Len int
  130. }
  131. func PtrElem(t *GoType) *GoType {
  132. return (*GoPtrType)(unsafe.Pointer(t)).Elem
  133. }
  134. func MapType(t *GoType) *GoMapType {
  135. return (*GoMapType)(unsafe.Pointer(t))
  136. }
  137. func IfaceType(t *GoType) *GoInterfaceType {
  138. return (*GoInterfaceType)(unsafe.Pointer(t))
  139. }
  140. func UnpackType(t reflect.Type) *GoType {
  141. return (*GoType)((*GoIface)(unsafe.Pointer(&t)).Value)
  142. }
  143. func UnpackEface(v interface{}) GoEface {
  144. return *(*GoEface)(unsafe.Pointer(&v))
  145. }
  146. func UnpackIface(v interface{}) GoIface {
  147. return *(*GoIface)(unsafe.Pointer(&v))
  148. }
  149. func findReflectRtypeItab() *GoItab {
  150. v := reflect.TypeOf(struct{}{})
  151. return (*GoIface)(unsafe.Pointer(&v)).Itab
  152. }