-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalue.go
248 lines (226 loc) · 4.3 KB
/
value.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package gates
import (
"fmt"
)
var intCache [256]Value
type Value interface {
IsString() bool
IsInt() bool
IsFloat() bool
IsBool() bool
IsFunction() bool
ToString() string
ToInt() int64
ToFloat() float64
ToNumber() Number
ToBool() bool
ToFunction() Function
ToNative(...ToNativeOption) interface{}
Equals(Value) bool
SameAs(Value) bool
}
type Iterable interface {
Iterator() Iterator
}
type Iterator interface {
Next() (value Value, ok bool)
}
func intToValue(i int64) Value {
if i >= -128 && i <= 127 {
return intCache[i+128]
}
return Int(i)
}
func init() {
for i := 0; i < 256; i++ {
intCache[i] = Int(i - 128)
}
}
func ToValue(i interface{}) Value {
switch i := i.(type) {
case nil:
return Null
case Value:
return i
case string:
return String(i)
case bool:
return Bool(i)
case int:
return Int(int64(i))
case int8:
return Int(int64(i))
case int16:
return Int(int64(i))
case int32:
return Int(int64(i))
case int64:
return Int(i)
case uint:
return Int(int64(i))
case uint8:
return Int(int64(i))
case uint16:
return Int(int64(i))
case uint32:
return Int(int64(i))
case uint64:
return Int(int64(i))
case float32:
return Float(float64(i))
case float64:
return Float(i)
case map[string]Value:
return Map(i)
case []Value:
return NewArray(i)
default:
return Ref{i}
}
}
func GetIterable(v Value) (Iterable, bool) {
iter, ok := unref(v).(Iterable)
return iter, ok
}
func GetIterator(v Value) (Iterator, bool) {
iterable, ok := GetIterable(v)
if !ok {
return nil, false
}
return iterable.Iterator(), true
}
type typer interface {
Type() string
}
// Type returns the type tag of the given value.
func Type(v Value) string {
switch {
case v == Null:
return "null"
case v.IsBool():
return "bool"
case v.IsFloat() || v.IsInt():
return "number"
case v.IsFunction():
return "function"
case v.IsString():
return "string"
}
if t, haveTyper := unref(v).(typer); haveTyper {
return t.Type()
}
return ""
}
type ErrTypeMismatch struct {
expected Value
actual Value
}
func (e *ErrTypeMismatch) Error() string {
return fmt.Sprint(Type(e.expected), " expected, got ", Type(e.actual))
}
type ErrTypeNotSupported struct {
v interface{}
}
func (e *ErrTypeNotSupported) Error() string {
return fmt.Sprintf("type %T not supported", e.v)
}
func convertValue(r *Runtime, dst interface{}, src Value) error {
convertArray := func() (result []Value, err error) {
if src == Null {
return make([]Value, 0), nil
}
if Type(src) != Type(Array{}) {
return nil, &ErrTypeMismatch{
expected: Array{},
actual: src,
}
}
length := objectGet(r, src, String("length")).ToInt()
result = make([]Value, 0, length)
for i := int64(0); i < length; i++ {
result = append(result, objectGet(r, src, Int(i)))
}
return
}
convertMap := func() (result map[string]Value, err error) {
if src == Null {
return make(map[string]Value), nil
}
if Type(src) != Type(Map{}) {
return nil, &ErrTypeMismatch{
expected: Map{},
actual: src,
}
}
it, ok := GetIterator(src)
if !ok {
return nil, &ErrTypeMismatch{
expected: Map{},
actual: src,
}
}
result = make(map[string]Value)
for {
elem, ok := it.Next()
if !ok {
break
}
key := objectGet(r, elem, String("key")).ToString()
value := objectGet(r, elem, String("value"))
result[key] = value
}
return
}
switch dst := dst.(type) {
case *Value:
*dst = src
case *bool:
*dst = src.ToBool()
case *Bool:
*dst = Bool(src.ToBool())
case *float64:
*dst = src.ToFloat()
case *Float:
*dst = Float(src.ToFloat())
case *int64:
*dst = src.ToInt()
case *Int:
*dst = Int(src.ToInt())
case *Callback:
f := src.ToFunction()
*dst = func(args ...Value) Value {
return r.Call(f, args...)
}
case *string:
*dst = src.ToString()
case *String:
*dst = String(src.ToString())
case *[]Value:
result, err := convertArray()
if err != nil {
return err
}
*dst = result
case *Array:
result, err := convertArray()
if err != nil {
return err
}
*dst = NewArray(result)
case *map[string]Value:
result, err := convertMap()
if err != nil {
return err
}
*dst = result
case *Map:
result, err := convertMap()
if err != nil {
return err
}
*dst = Map(result)
default:
return &ErrTypeNotSupported{v: dst}
}
return nil
}