forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect_unsafe.go
139 lines (99 loc) · 3 KB
/
reflect_unsafe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//go:build unsafe
// +build unsafe
package protocol
import (
"reflect"
"unsafe"
)
type iface struct {
typ unsafe.Pointer
ptr unsafe.Pointer
}
type slice struct {
ptr unsafe.Pointer
len int
cap int
}
type index uintptr
type _type struct {
ptr unsafe.Pointer
}
func typeOf(x interface{}) _type {
return _type{ptr: ((*iface)(unsafe.Pointer(&x))).typ}
}
func elemTypeOf(x interface{}) _type {
return makeType(reflect.TypeOf(x).Elem())
}
func makeType(t reflect.Type) _type {
return _type{ptr: ((*iface)(unsafe.Pointer(&t))).ptr}
}
type value struct {
ptr unsafe.Pointer
}
func nonAddressableValueOf(x interface{}) value {
return valueOf(x)
}
func valueOf(x interface{}) value {
return value{ptr: ((*iface)(unsafe.Pointer(&x))).ptr}
}
func makeValue(t reflect.Type) value {
return value{ptr: unsafe.Pointer(reflect.New(t).Pointer())}
}
func (v value) bool() bool { return *(*bool)(v.ptr) }
func (v value) int8() int8 { return *(*int8)(v.ptr) }
func (v value) int16() int16 { return *(*int16)(v.ptr) }
func (v value) int32() int32 { return *(*int32)(v.ptr) }
func (v value) int64() int64 { return *(*int64)(v.ptr) }
func (v value) string() string { return *(*string)(v.ptr) }
func (v value) bytes() []byte { return *(*[]byte)(v.ptr) }
func (v value) iface(t reflect.Type) interface{} {
return *(*interface{})(unsafe.Pointer(&iface{
typ: ((*iface)(unsafe.Pointer(&t))).ptr,
ptr: v.ptr,
}))
}
func (v value) array(t reflect.Type) array {
return array{
size: uintptr(t.Size()),
elem: ((*slice)(v.ptr)).ptr,
len: ((*slice)(v.ptr)).len,
}
}
func (v value) setBool(b bool) { *(*bool)(v.ptr) = b }
func (v value) setInt8(i int8) { *(*int8)(v.ptr) = i }
func (v value) setInt16(i int16) { *(*int16)(v.ptr) = i }
func (v value) setInt32(i int32) { *(*int32)(v.ptr) = i }
func (v value) setInt64(i int64) { *(*int64)(v.ptr) = i }
func (v value) setString(s string) { *(*string)(v.ptr) = s }
func (v value) setBytes(b []byte) { *(*[]byte)(v.ptr) = b }
func (v value) setArray(a array) { *(*slice)(v.ptr) = slice{ptr: a.elem, len: a.len, cap: a.len} }
func (v value) fieldByIndex(i index) value {
return value{ptr: unsafe.Pointer(uintptr(v.ptr) + uintptr(i))}
}
type array struct {
elem unsafe.Pointer
size uintptr
len int
}
var (
emptyArray struct{}
)
func makeArray(t reflect.Type, n int) array {
var elem unsafe.Pointer
var size = uintptr(t.Size())
if n == 0 {
elem = unsafe.Pointer(&emptyArray)
} else {
elem = unsafe_NewArray(((*iface)(unsafe.Pointer(&t))).ptr, n)
}
return array{elem: elem, size: size, len: n}
}
func (a array) index(i int) value {
return value{ptr: unsafe.Pointer(uintptr(a.elem) + (uintptr(i) * a.size))}
}
func (a array) length() int { return a.len }
func (a array) isNil() bool { return a.elem == nil }
func indexOf(s reflect.StructField) index { return index(s.Offset) }
func bytesToString(b []byte) string { return *(*string)(unsafe.Pointer(&b)) }
//go:linkname unsafe_NewArray reflect.unsafe_NewArray
func unsafe_NewArray(rtype unsafe.Pointer, length int) unsafe.Pointer