github.com/lxt1045/json@v0.0.0-20231013032136-54d6b1d6e525/go_type.go (about)

     1  // MIT License
     2  //
     3  // Copyright (c) 2021 Xiantu Li
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package json
    24  
    25  import (
    26  	"reflect"
    27  	"unsafe"
    28  )
    29  
    30  type SliceHeader struct {
    31  	Data unsafe.Pointer
    32  	Len  int
    33  	Cap  int
    34  }
    35  
    36  type StringHeader struct {
    37  	Data unsafe.Pointer
    38  	Len  int
    39  }
    40  
    41  type Value struct {
    42  	typ  *GoType
    43  	ptr  unsafe.Pointer
    44  	flag uintptr
    45  }
    46  
    47  func reflectValueToPointer(v *reflect.Value) unsafe.Pointer {
    48  	return (*Value)(unsafe.Pointer(v)).ptr
    49  }
    50  func reflectValueToValue(v *reflect.Value) *Value {
    51  	return (*Value)(unsafe.Pointer(v))
    52  }
    53  
    54  func bytesString(b []byte) string {
    55  	return *(*string)(unsafe.Pointer(&b))
    56  }
    57  
    58  func stringBytes(str string) []byte {
    59  	return (*(*[]byte)(unsafe.Pointer(&str)))[:len(str):len(str)]
    60  }
    61  func bytesCopyToString(b []byte, str *string) {
    62  	*str = *(*string)(unsafe.Pointer(&b))
    63  }
    64  
    65  // emptyInterface is the header for an interface{} value.
    66  type emptyInterface struct {
    67  	typ  uintptr
    68  	word unsafe.Pointer
    69  }
    70  
    71  type nonEmptyInterface struct {
    72  	itab *struct {
    73  		ityp *GoType // static interface type
    74  		typ  *GoType // dynamic concrete type
    75  		hash uint32  // copy of typ.hash
    76  		_    [4]byte
    77  		fun  [100000]unsafe.Pointer // method table
    78  	}
    79  	word *GoType
    80  }
    81  
    82  func UnpackNonEface(p unsafe.Pointer) *GoType {
    83  	neface := (*nonEmptyInterface)(p)
    84  	return neface.word
    85  }
    86  
    87  func UnpackType(t reflect.Type) *GoType {
    88  	return (*GoType)((*GoIface)(unsafe.Pointer(&t)).Value)
    89  }
    90  
    91  func unpackEface(v interface{}) *emptyInterface {
    92  	empty := (*emptyInterface)(unsafe.Pointer(&v))
    93  	return empty
    94  }
    95  func UnpackEface(v interface{}) GoEface {
    96  	return *(*GoEface)(unsafe.Pointer(&v))
    97  }
    98  
    99  type GoEface struct {
   100  	Type  *GoType
   101  	Value unsafe.Pointer
   102  }
   103  
   104  type GoIface struct {
   105  	Itab  *GoItab
   106  	Value unsafe.Pointer
   107  }
   108  type GoItab struct {
   109  	it unsafe.Pointer
   110  	vt *GoType
   111  	hv uint32
   112  	_  [4]byte
   113  	fn [1]uintptr
   114  }
   115  
   116  type GoType struct {
   117  	Size       uintptr
   118  	PtrData    uintptr
   119  	Hash       uint32
   120  	Flags      uint8
   121  	Align      uint8
   122  	FieldAlign uint8
   123  	KindFlags  uint8
   124  	Traits     unsafe.Pointer
   125  	GCData     *byte
   126  	Str        int32
   127  	PtrToSelf  int32
   128  }
   129  
   130  type interfacetype struct {
   131  	typ GoType
   132  	//  /src/runtime/type.go: interfacetype
   133  }
   134  
   135  func PtrElem(t *GoType) *GoType {
   136  	return (*GoPtrType)(unsafe.Pointer(t)).Elem
   137  }
   138  
   139  type GoPtrType struct {
   140  	GoType
   141  	Elem *GoType
   142  }