-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.go
199 lines (184 loc) · 4.12 KB
/
builtin.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Derived from Go's package reflect
// --------------------------------------------------------------------------
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Copyright 2014 The ZxxLang Authors. All rights reserved.
package surface
import (
"unsafe"
)
func (v Value) Bool() bool {
v.mustBe(KBool)
if v.flag&flagIndir != 0 {
return *(*bool)(v.val)
}
return *(*bool)(unsafe.Pointer(&v.val))
}
func (v Value) Int() int {
v.mustBe(KInt)
if v.flag&flagIndir != 0 {
return *(*int)(v.val)
}
return *(*int)(unsafe.Pointer(&v.val))
}
func (v Value) Uint() uint {
v.mustBe(KUint)
if v.flag&flagIndir != 0 {
return *(*uint)(v.val)
}
return *(*uint)(unsafe.Pointer(&v.val))
}
func (v Value) Int8() int8 {
v.mustBe(KInt8)
if v.flag&flagIndir != 0 {
return *(*int8)(v.val)
}
return *(*int8)(unsafe.Pointer(&v.val))
}
func (v Value) Int16() int16 {
v.mustBe(KInt16)
if v.flag&flagIndir != 0 {
return *(*int16)(v.val)
}
return *(*int16)(unsafe.Pointer(&v.val))
}
func (v Value) Int32() int32 {
v.mustBe(KInt32)
if v.flag&flagIndir != 0 {
return *(*int32)(v.val)
}
return *(*int32)(unsafe.Pointer(&v.val))
}
func (v Value) Int64() int64 {
k := v.Kind()
var p unsafe.Pointer
if v.flag&flagIndir != 0 {
p = v.val
} else {
// The escape analysis is good enough that &v.val
// does not trigger a heap allocation.
p = unsafe.Pointer(&v.val)
}
switch k {
case KInt:
return int64(*(*int)(p))
case KInt8:
return int64(*(*int8)(p))
case KInt16:
return int64(*(*int16)(p))
case KInt32:
return int64(*(*int32)(p))
case KInt64:
return int64(*(*int64)(p))
}
panic(&ValueError{"rtype.Value.Int64", k})
}
func (v Value) Uint64() uint64 {
k := v.Kind()
var p unsafe.Pointer
if v.flag&flagIndir != 0 {
p = v.val
} else {
// The escape analysis is good enough that &v.val
// does not trigger a heap allocation.
p = unsafe.Pointer(&v.val)
}
switch k {
case KUint:
return uint64(*(*uint)(p))
case KUint8:
return uint64(*(*uint8)(p))
case KUint16:
return uint64(*(*uint16)(p))
case KUint32:
return uint64(*(*uint32)(p))
case KUint64:
return uint64(*(*uint64)(p))
case KUintptr:
return uint64(*(*uintptr)(p))
}
panic(&ValueError{"rtype.Value.Uint64", k})
}
func (v Value) Uint8() uint8 {
v.mustBe(KUint8)
if v.flag&flagIndir != 0 {
return *(*uint8)(v.val)
}
return *(*uint8)(unsafe.Pointer(&v.val))
}
func (v Value) Uint16() uint16 {
v.mustBe(KUint16)
if v.flag&flagIndir != 0 {
return *(*uint16)(v.val)
}
return *(*uint16)(unsafe.Pointer(&v.val))
}
func (v Value) Uint32() uint32 {
v.mustBe(KUint32)
if v.flag&flagIndir != 0 {
return *(*uint32)(v.val)
}
return *(*uint32)(unsafe.Pointer(&v.val))
}
func (v Value) Uintptr() uintptr {
v.mustBe(KUintptr)
return *(*uintptr)(v.val)
}
func (v Value) Float32() float32 {
v.mustBe(KFloat32)
if v.flag&flagIndir != 0 {
return *(*float32)(v.val)
}
return *(*float32)(unsafe.Pointer(&v.val))
}
func (v Value) Float64() float64 {
k := v.Kind()
switch k {
case KFloat32:
if v.flag&flagIndir != 0 {
return float64(*(*float32)(v.val))
}
return float64(*(*float32)(unsafe.Pointer(&v.val)))
case KFloat64:
if v.flag&flagIndir != 0 {
return *(*float64)(v.val)
}
return *(*float64)(unsafe.Pointer(&v.sur.val))
}
panic(&ValueError{"rtype.Value.Float64", k})
}
func (v Value) Complex64() complex64 {
v.mustBe(KComplex64)
if v.flag&flagIndir != 0 {
return *(*complex64)(v.val)
}
return *(*complex64)(unsafe.Pointer(&v.val))
}
func (v Value) Complex128() complex128 {
v.mustBe(KComplex128)
if v.flag&flagIndir != 0 {
return *(*complex128)(v.val)
}
return *(*complex128)(unsafe.Pointer(&v.val))
}
func (v Value) String() string {
v.mustBe(KString)
if v.flag&flagIndir != 0 {
return *(*string)(v.val)
}
return *(*string)(unsafe.Pointer(&v.val))
}
func (v Value) StringHeader() StringHeader {
v.mustBe(KString)
if v.flag&flagIndir != 0 {
return *(*StringHeader)(unsafe.Pointer(v.val))
}
return *(*StringHeader)(unsafe.Pointer(&v.val))
}
func (v Value) Bytes() []byte {
v.mustBe(KString)
return *(*[]byte)(v.val)
}