github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/rt/fastvalue.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors.
     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  
    17  package rt
    18  
    19  import (
    20  	"reflect"
    21  	"unsafe"
    22  )
    23  
    24  var (
    25  	reflectRtypeItab = findReflectRtypeItab()
    26  )
    27  
    28  const (
    29  	F_direct    = 1 << 5
    30  	F_kind_mask = (1 << 5) - 1
    31  )
    32  
    33  type GoType struct {
    34  	Size       uintptr
    35  	PtrData    uintptr
    36  	Hash       uint32
    37  	Flags      uint8
    38  	Align      uint8
    39  	FieldAlign uint8
    40  	KindFlags  uint8
    41  	Traits     unsafe.Pointer
    42  	GCData     *byte
    43  	Str        int32
    44  	PtrToSelf  int32
    45  }
    46  
    47  func (self *GoType) Kind() reflect.Kind {
    48  	return reflect.Kind(self.KindFlags & F_kind_mask)
    49  }
    50  
    51  func (self *GoType) Pack() (t reflect.Type) {
    52  	(*GoIface)(unsafe.Pointer(&t)).Itab = reflectRtypeItab
    53  	(*GoIface)(unsafe.Pointer(&t)).Value = unsafe.Pointer(self)
    54  	return
    55  }
    56  
    57  func (self *GoType) String() string {
    58  	return self.Pack().String()
    59  }
    60  
    61  type GoMap struct {
    62  	Count      int
    63  	Flags      uint8
    64  	B          uint8
    65  	Overflow   uint16
    66  	Hash0      uint32
    67  	Buckets    unsafe.Pointer
    68  	OldBuckets unsafe.Pointer
    69  	Evacuate   uintptr
    70  	Extra      unsafe.Pointer
    71  }
    72  
    73  type GoMapIterator struct {
    74  	K           unsafe.Pointer
    75  	V           unsafe.Pointer
    76  	T           *GoMapType
    77  	H           *GoMap
    78  	Buckets     unsafe.Pointer
    79  	Bptr        *unsafe.Pointer
    80  	Overflow    *[]unsafe.Pointer
    81  	OldOverflow *[]unsafe.Pointer
    82  	StartBucket uintptr
    83  	Offset      uint8
    84  	Wrapped     bool
    85  	B           uint8
    86  	I           uint8
    87  	Bucket      uintptr
    88  	CheckBucket uintptr
    89  }
    90  
    91  type GoItab struct {
    92  	it unsafe.Pointer
    93  	vt *GoType
    94  	hv uint32
    95  	_  [4]byte
    96  	fn [1]uintptr
    97  }
    98  
    99  type GoIface struct {
   100  	Itab  *GoItab
   101  	Value unsafe.Pointer
   102  }
   103  
   104  type GoEface struct {
   105  	Type  *GoType
   106  	Value unsafe.Pointer
   107  }
   108  
   109  func (self GoEface) Pack() (v interface{}) {
   110  	*(*GoEface)(unsafe.Pointer(&v)) = self
   111  	return
   112  }
   113  
   114  type GoPtrType struct {
   115  	GoType
   116  	Elem *GoType
   117  }
   118  
   119  type GoMapType struct {
   120  	GoType
   121  	Key        *GoType
   122  	Elem       *GoType
   123  	Bucket     *GoType
   124  	Hasher     func(unsafe.Pointer, uintptr) uintptr
   125  	KeySize    uint8
   126  	ElemSize   uint8
   127  	BucketSize uint16
   128  	Flags      uint32
   129  }
   130  
   131  func (self *GoMapType) IndirectElem() bool {
   132  	return self.Flags&2 != 0
   133  }
   134  
   135  type GoStructType struct {
   136  	GoType
   137  	Pkg    *byte
   138  	Fields []GoStructField
   139  }
   140  
   141  type GoStructField struct {
   142  	Name     *byte
   143  	Type     *GoType
   144  	OffEmbed uintptr
   145  }
   146  
   147  type GoInterfaceType struct {
   148  	GoType
   149  	PkgPath *byte
   150  	Methods []GoInterfaceMethod
   151  }
   152  
   153  type GoInterfaceMethod struct {
   154  	Name int32
   155  	Type int32
   156  }
   157  
   158  type GoSlice struct {
   159  	Ptr unsafe.Pointer
   160  	Len int
   161  	Cap int
   162  }
   163  
   164  type GoString struct {
   165  	Ptr unsafe.Pointer
   166  	Len int
   167  }
   168  
   169  func PtrElem(t *GoType) *GoType {
   170  	return (*GoPtrType)(unsafe.Pointer(t)).Elem
   171  }
   172  
   173  func MapType(t *GoType) *GoMapType {
   174  	return (*GoMapType)(unsafe.Pointer(t))
   175  }
   176  
   177  func IfaceType(t *GoType) *GoInterfaceType {
   178  	return (*GoInterfaceType)(unsafe.Pointer(t))
   179  }
   180  
   181  func UnpackType(t reflect.Type) *GoType {
   182  	return (*GoType)((*GoIface)(unsafe.Pointer(&t)).Value)
   183  }
   184  
   185  func UnpackEface(v interface{}) GoEface {
   186  	return *(*GoEface)(unsafe.Pointer(&v))
   187  }
   188  
   189  func findReflectRtypeItab() *GoItab {
   190  	v := reflect.TypeOf(struct{}{})
   191  	return (*GoIface)(unsafe.Pointer(&v)).Itab
   192  }