-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvalue.go
219 lines (185 loc) · 4.76 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
package gojs
// #include <stdlib.h>
// #include <JavaScriptCore/JSStringRef.h>
// #include <JavaScriptCore/JSValueRef.h>
import "C"
import (
"encoding/json"
"fmt"
"unsafe"
)
type Value struct {
ref C.JSValueRef
ctx *Context
}
const (
TypeUndefined = 0
TypeNull = iota
TypeBoolean = iota
TypeNumber = iota
TypeString = iota
TypeObject = iota
)
func (ctx *Context) newValue(ref C.JSValueRef) *Value {
if ref == nil {
return nil
}
val := new(Value)
val.ctx = ctx
val.ref = ref
return val
}
type RawValue C.JSValueRef
func (ctx *Context) NewValueFrom(raw RawValue) *Value {
return ctx.newValue(C.JSValueRef(raw))
}
func (ctx *Context) NewUndefinedValue() *Value {
return ctx.newValue(C.JSValueMakeUndefined(ctx.ref))
}
func (ctx *Context) NewNullValue() *Value {
return ctx.newValue(C.JSValueMakeNull(ctx.ref))
}
func (ctx *Context) NewBooleanValue(value bool) *Value {
return ctx.newValue(C.JSValueMakeBoolean(ctx.ref, C.bool(value)))
}
func (ctx *Context) NewNumberValue(value float64) *Value {
return ctx.newValue(C.JSValueMakeNumber(ctx.ref, C.double(value)))
}
func (ctx *Context) NewStringValue(value string) *Value {
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
jsstr := C.JSStringCreateWithUTF8CString(cvalue)
defer C.JSStringRelease(jsstr)
ref := C.JSValueMakeString(ctx.ref, jsstr)
return ctx.newValue(ref)
}
func (v *Value) String() string {
str, err := v.ToString()
if err != nil {
return "Error:" + err.Error()
}
return str
}
// GoVal converts a JavaScript value to a Go value. TODO(sqs): might it be
// easier to just have JavaScriptCore serialize this to JSON and then
// deserialize it in Go?
func (v *Value) GoValue() (goval interface{}, err error) {
switch v.Type() {
case TypeUndefined, TypeNull:
return nil, nil
case TypeBoolean:
return v.ToBoolean(), nil
case TypeNumber:
return v.ToNumber()
case TypeString:
return v.ToString()
case TypeObject:
jsonData, err := v.ToJSON()
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonData, &goval)
return goval, err
}
return nil, fmt.Errorf("JS value type %d is not convertible to a Go value", v.Type())
}
func (v *Value) Type() uint8 {
return uint8(C.JSValueGetType(v.ctx.ref, v.ref))
}
func (v *Value) IsUndefined() bool {
return bool(C.JSValueIsUndefined(v.ctx.ref, v.ref))
}
func (v *Value) IsNull() bool {
return bool(C.JSValueIsNull(v.ctx.ref, v.ref))
}
func (v *Value) IsBoolean() bool {
return bool(C.JSValueIsBoolean(v.ctx.ref, v.ref))
}
func (v *Value) IsNumber() bool {
return bool(C.JSValueIsNumber(v.ctx.ref, v.ref))
}
func (v *Value) IsString() bool {
return bool(C.JSValueIsString(v.ctx.ref, v.ref))
}
func (v *Value) IsObject() bool {
return bool(C.JSValueIsObject(v.ctx.ref, v.ref))
}
func (v *Value) Equals(b *Value) bool {
return bool(C.JSValueIsStrictEqual(v.ctx.ref, v.ref, b.ref))
}
// JavaScript ==
func (v *Value) LooseEquals(b *Value) (bool, error) {
errVal := v.ctx.newErrorValue()
ret := C.JSValueIsEqual(v.ctx.ref, v.ref, b.ref, &errVal.ref)
if errVal.ref != nil {
return false, errVal
}
return bool(ret), nil
}
func (v *Value) ToBoolean() bool {
return bool(C.JSValueToBoolean(v.ctx.ref, v.ref))
}
func (v *Value) ToNumber() (num float64, err error) {
errVal := v.ctx.newErrorValue()
ret := C.JSValueToNumber(v.ctx.ref, v.ref, &errVal.ref)
if errVal.ref != nil {
return float64(ret), errVal
}
// Successful conversion
return float64(ret), nil
}
// TODO(crazy2be): Should this return NaN instead of panicing?
func (v *Value) ToNumberOrDie() float64 {
ret, err := v.ToNumber()
if err != nil {
panic(err)
}
return ret
}
func (v *Value) ToString() (str string, err error) {
errVal := v.ctx.newErrorValue()
ret := C.JSValueToStringCopy(v.ctx.ref, v.ref, &errVal.ref)
if errVal.ref != nil {
return "", errVal
}
defer C.JSStringRelease(ret)
return newStringFromRef(ret).String(), nil
}
func (v *Value) ToStringOrDie() string {
str, err := v.ToString()
if err != nil {
panic(err)
}
return str
}
func (v *Value) ToObject() (*Object, error) {
errVal := v.ctx.newErrorValue()
ret := C.JSValueToObject(v.ctx.ref, v.ref, &errVal.ref)
if errVal.ref != nil {
return nil, errVal
}
return v.ctx.newObject(ret), nil
}
func (v *Value) ToObjectOrDie() *Object {
ret, err := v.ToObject()
if err != nil {
panic(err)
}
return ret
}
// JSON returns the JSON representation of the JavaScript value.
func (v *Value) ToJSON() ([]byte, error) {
errVal := v.ctx.newErrorValue()
jsstr := C.JSValueCreateJSONString(v.ctx.ref, v.ref, 0, &errVal.ref)
if errVal.ref != nil {
return nil, errVal
}
defer C.JSStringRelease(jsstr)
return (*String)(unsafe.Pointer(jsstr)).Bytes(), nil
}
func (v *Value) Protect() {
C.JSValueProtect(v.ctx.ref, v.ref)
}
func (v *Value) UnProtect() {
C.JSValueProtect(v.ctx.ref, v.ref)
}