forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
v8_value.go
361 lines (304 loc) · 8.2 KB
/
v8_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package v8
/*
#include "v8_wrap.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "runtime"
import "reflect"
import "time"
// The superclass of all JavaScript values and objects.
//
type Value struct {
engine *Engine
self unsafe.Pointer
isType int
notType int
fieldOwnerId int64
}
func newValue(engine *Engine, self unsafe.Pointer) *Value {
if self == nil {
return nil
}
result := &Value{
engine: engine,
self: self,
}
runtime.SetFinalizer(result, func(v *Value) {
if traceDispose {
println("v8.Value.Dispose()", v.self)
}
C.V8_DisposeValue(v.self)
})
return result
}
func (e *Engine) Undefined() *Value {
if e._undefined == nil {
e._undefined = newValue(e, C.V8_Undefined(e.self))
}
return e._undefined
}
func (e *Engine) Null() *Value {
if e._null == nil {
e._null = newValue(e, C.V8_Null(e.self))
}
return e._null
}
func (e *Engine) True() *Value {
if e._true == nil {
e._true = newValue(e, C.V8_True(e.self))
}
return e._true
}
func (e *Engine) False() *Value {
if e._false == nil {
e._false = newValue(e, C.V8_False(e.self))
}
return e._false
}
func (e *Engine) NewBoolean(value bool) *Value {
if value {
return e.True()
}
return e.False()
}
func (e *Engine) NewNumber(value float64) *Value {
return newValue(e, C.V8_NewNumber(
e.self, C.double(value),
))
}
func (e *Engine) NewDate(value time.Time) *Value {
return newValue(e, C.V8_NewDate(
e.self, C.double(value.Unix()*1000),
))
}
func (e *Engine) NewInteger(value int64) *Value {
return newValue(e, C.V8_NewNumber(
e.self, C.double(value),
))
}
func (e *Engine) NewString(value string) *Value {
valPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&value)).Data)
return newValue(e, C.V8_NewString(
e.self, (*C.char)(valPtr), C.int(len(value)),
))
}
func (e *Engine) NewRangeError(message string) *Value {
msgPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&message)).Data)
return newValue(e, C.V8_Exception_RangeError(e.self, (*C.char)(msgPtr), C.int(len(message))))
}
func (e *Engine) NewReferenceError(message string) *Value {
msgPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&message)).Data)
return newValue(e, C.V8_Exception_ReferenceError(e.self, (*C.char)(msgPtr), C.int(len(message))))
}
func (e *Engine) NewSyntaxError(message string) *Value {
msgPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&message)).Data)
return newValue(e, C.V8_Exception_SyntaxError(e.self, (*C.char)(msgPtr), C.int(len(message))))
}
func (e *Engine) NewTypeError(message string) *Value {
msgPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&message)).Data)
return newValue(e, C.V8_Exception_TypeError(e.self, (*C.char)(msgPtr), C.int(len(message))))
}
func (e *Engine) NewError(message string) *Value {
msgPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&message)).Data)
return newValue(e, C.V8_Exception_Error(e.self, (*C.char)(msgPtr), C.int(len(message))))
}
func (v *Value) ToBoolean() bool {
return C.V8_Value_ToBoolean(v.self) == 1
}
func (v *Value) ToNumber() float64 {
return float64(C.V8_Value_ToNumber(v.self))
}
func (v *Value) ToInteger() int64 {
return int64(C.V8_Value_ToInteger(v.self))
}
func (v *Value) ToUint32() uint32 {
return uint32(C.V8_Value_ToUint32(v.self))
}
func (v *Value) ToInt32() int32 {
return int32(C.V8_Value_ToInt32(v.self))
}
func (v *Value) ToString() string {
cstring := C.V8_Value_ToString(v.self)
gostring := C.GoString(cstring)
C.free(unsafe.Pointer(cstring))
return gostring
}
func (v *Value) ToTime() time.Time {
return time.Unix(0, int64(C.V8_Value_ToInteger(v.self))*1e6)
}
func (v *Value) ToObject() *Object {
if v == nil {
return nil
}
return &Object{v, nil, nil}
}
func (v *Value) ToArray() *Array {
if v == nil {
return nil
}
return &Array{&Object{v, nil, nil}}
}
func (v *Value) ToRegExp() *RegExp {
if v == nil {
return nil
}
return &RegExp{&Object{v, nil, nil}, "", false, RF_None, false}
}
func (v *Value) ToFunction() *Function {
if v == nil {
return nil
}
return &Function{&Object{v, nil, nil}, nil, nil}
}
func (v *Value) ToExternal() *External {
if v == nil {
return nil
}
return &External{v, nil}
}
func (v *Value) String() string {
return v.ToString()
}
// Keep the Object alive when it refence by JS
func (v *Value) setOwner(self interface{}) {
// the object reference by engine
if v.fieldOwnerId == 0 {
v.engine.fieldOwnerId += 1
v.fieldOwnerId = v.engine.fieldOwnerId
v.engine.fieldOwners[v.fieldOwnerId] = self
C.V8_Value_SetFieldOwnerInfo(v.self, unsafe.Pointer(v.engine), C.int64_t(v.fieldOwnerId))
}
}
const (
isUndefined = 1 << iota
isNull = 1 << iota
isTrue = 1 << iota
isFalse = 1 << iota
isString = 1 << iota
isFunction = 1 << iota
isArray = 1 << iota
isObject = 1 << iota
isBoolean = 1 << iota
isNumber = 1 << iota
isExternal = 1 << iota
isInt32 = 1 << iota
isUint32 = 1 << iota
isDate = 1 << iota
isBooleanObject = 1 << iota
isNumberObject = 1 << iota
isStringObject = 1 << iota
isNativeError = 1 << iota
isRegExp = 1 << iota
)
func (v *Value) checkJsType(typeCode int, check func(unsafe.Pointer) bool) bool {
if v.isType&typeCode == typeCode {
return true
}
if v.notType&typeCode == typeCode {
return false
}
if check(v.self) {
v.isType |= typeCode
return true
} else {
v.notType |= typeCode
return false
}
}
func (v *Value) IsUndefined() bool {
return v.checkJsType(isUndefined, func(self unsafe.Pointer) bool {
return C.V8_Value_IsUndefined(self) == 1
})
}
func (v *Value) IsNull() bool {
return v.checkJsType(isNull, func(self unsafe.Pointer) bool {
return C.V8_Value_IsNull(self) == 1
})
}
func (v *Value) IsTrue() bool {
return v.checkJsType(isTrue, func(self unsafe.Pointer) bool {
return C.V8_Value_IsTrue(self) == 1
})
}
func (v *Value) IsFalse() bool {
return v.checkJsType(isFalse, func(self unsafe.Pointer) bool {
return C.V8_Value_IsFalse(self) == 1
})
}
func (v *Value) IsString() bool {
return v.checkJsType(isString, func(self unsafe.Pointer) bool {
return C.V8_Value_IsString(self) == 1
})
}
func (v *Value) IsFunction() bool {
return v.checkJsType(isFunction, func(self unsafe.Pointer) bool {
return C.V8_Value_IsFunction(self) == 1
})
}
func (v *Value) IsArray() bool {
return v.checkJsType(isArray, func(self unsafe.Pointer) bool {
return C.V8_Value_IsArray(self) == 1
})
}
func (v *Value) IsObject() bool {
return v.checkJsType(isObject, func(self unsafe.Pointer) bool {
return C.V8_Value_IsObject(self) == 1
})
}
func (v *Value) IsBoolean() bool {
return v.checkJsType(isBoolean, func(self unsafe.Pointer) bool {
return C.V8_Value_IsBoolean(self) == 1
})
}
func (v *Value) IsNumber() bool {
return v.checkJsType(isNumber, func(self unsafe.Pointer) bool {
return C.V8_Value_IsNumber(self) == 1
})
}
func (v *Value) IsExternal() bool {
return v.checkJsType(isExternal, func(self unsafe.Pointer) bool {
return C.V8_Value_IsExternal(self) == 1
})
}
func (v *Value) IsInt32() bool {
return v.checkJsType(isInt32, func(self unsafe.Pointer) bool {
return C.V8_Value_IsInt32(self) == 1
})
}
func (v *Value) IsUint32() bool {
return v.checkJsType(isUint32, func(self unsafe.Pointer) bool {
return C.V8_Value_IsUint32(self) == 1
})
}
func (v *Value) IsDate() bool {
return v.checkJsType(isDate, func(self unsafe.Pointer) bool {
return C.V8_Value_IsDate(self) == 1
})
}
func (v *Value) IsBooleanObject() bool {
return v.checkJsType(isBooleanObject, func(self unsafe.Pointer) bool {
return C.V8_Value_IsBooleanObject(self) == 1
})
}
func (v *Value) IsNumberObject() bool {
return v.checkJsType(isNumberObject, func(self unsafe.Pointer) bool {
return C.V8_Value_IsNumberObject(self) == 1
})
}
func (v *Value) IsStringObject() bool {
return v.checkJsType(isStringObject, func(self unsafe.Pointer) bool {
return C.V8_Value_IsStringObject(self) == 1
})
}
func (v *Value) IsNativeError() bool {
return v.checkJsType(isNativeError, func(self unsafe.Pointer) bool {
return C.V8_Value_IsNativeError(self) == 1
})
}
func (v *Value) IsRegExp() bool {
return v.checkJsType(isRegExp, func(self unsafe.Pointer) bool {
return C.V8_Value_IsRegExp(self) == 1
})
}