-
Notifications
You must be signed in to change notification settings - Fork 21
/
value.go
172 lines (136 loc) · 3.78 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
package quickjs
import "unsafe"
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lquickjs
#include "quickjs-bridge.h"
*/
import "C"
var valueMap = make(map[*C.JSValue]JSValue)
type JSValue struct {
ref C.JSValue
ctx *JSContext
}
func NewJSValue(ctx *JSContext, cval C.JSValue) *JSValue {
val := new(JSValue)
val.ref = cval
val.ctx = ctx
return val
}
func (val *JSValue) Free() {
C.JS_FreeValue(val.ctx.ref, val.ref)
}
func (val *JSValue) AttachContext(ctx *JSContext) {
val.ctx = ctx
}
func (val *JSValue) String() string {
cstr := C.JS_ToCString(val.ctx.ref, val.ref)
return C.GoString(cstr)
}
func (val *JSValue) Float64() float64 {
i := C.double(0)
C.JS_ToFloat64(val.ctx.ref, &i, val.ref)
return float64(i)
}
func (val *JSValue) Int64() int64 {
i := C.int64_t(0)
C.JS_ToInt64(val.ctx.ref, &i, val.ref)
return int64(i)
}
func (val *JSValue) Int() int {
i := C.int64_t(0)
C.JS_ToInt64(val.ctx.ref, &i, val.ref)
return int(i)
}
func (val *JSValue) Int32() int32 {
i := C.int32_t(0)
C.JS_ToInt32(val.ctx.ref, &i, val.ref)
return int32(i)
}
func (val *JSValue) Bool() bool {
i := C.JS_ToBool(val.ctx.ref, val.ref)
return val.valueToBool(i)
}
func (val *JSValue) Error() *JSError {
return val.ctx.WrapError(val.ref)
}
func (val *JSValue) valueToBool(cval C.int) bool {
if cval == 1 {
return true
}
return false
}
func (val *JSValue) IsInteger() bool {
return val.valueToBool(C.JS_IsInteger(val.ref))
}
func (val *JSValue) IsBigFloat() bool {
return val.valueToBool(C.JS_IsBigFloat(val.ref))
}
func (val *JSValue) IsBool() bool {
return val.valueToBool(C.JS_IsBool(val.ref))
}
func (val *JSValue) IsUndefined() bool {
return val.valueToBool(C.JS_IsUndefined(val.ref))
}
func (val *JSValue) IsNull() bool {
return val.valueToBool(C.JS_IsNull(val.ref))
}
func (val *JSValue) IsException() bool {
return val.valueToBool(C.JS_IsException(val.ref))
}
func (val *JSValue) IsUninitialized() bool {
return val.valueToBool(C.JS_IsUninitialized(val.ref))
}
func (val *JSValue) IsString() bool {
return val.valueToBool(C.JS_IsString(val.ref))
}
func (val *JSValue) IsSymbol() bool {
return val.valueToBool(C.JS_IsSymbol(val.ref))
}
func (val *JSValue) IsObject() bool {
return val.valueToBool(C.JS_IsObject(val.ref))
}
func (val *JSValue) IsError() bool {
return val.valueToBool(C.JS_IsError(val.ctx.ref, val.ref))
}
func (val *JSValue) IsFunction() bool {
return val.valueToBool(C.JS_IsFunction(val.ctx.ref, val.ref))
}
func (val *JSValue) IsConstructor() bool {
return val.valueToBool(C.JS_IsConstructor(val.ctx.ref, val.ref))
}
func (val *JSValue) IsExtensible() bool {
return val.valueToBool(C.JS_IsExtensible(val.ctx.ref, val.ref))
}
func (val *JSValue) IsInstanceOf(ctor *JSValue) bool {
return val.valueToBool(C.JS_IsInstanceOf(val.ctx.ref, val.ref, ctor.ref))
}
func (val *JSValue) Call(args []*JSValue, this *JSValue) *JSValue {
if this == nil {
this = val.ctx.Global()
}
var cargs []C.JSValue
for _, arg := range args {
cargs = append(cargs, arg.ref)
}
return val.ctx.WrapValue(C.JS_Call(val.ctx.ref, val.ref, this.ref, C.int(len(args)), &cargs[0]))
}
func (val *JSValue) PropertyByInt(key int) *JSValue {
return val.ctx.WrapValue(C.JS_GetPropertyUint32(val.ctx.ref, val.ref, C.uint32_t(key)))
}
func (val *JSValue) Property(key string) *JSValue {
cstr := C.CString(key)
defer C.free(unsafe.Pointer(cstr))
return val.ctx.WrapValue(C.JS_GetPropertyStr(val.ctx.ref, val.ref, cstr))
}
func (val *JSValue) SetPropertyByInt(key int, value *JSValue) {
C.JS_SetPropertyInt64(val.ctx.ref, val.ref, C.int64_t(key), value.ref)
}
func (val *JSValue) SetProperty(key string, value *JSValue) {
cstr := C.CString(key)
defer C.free(unsafe.Pointer(cstr))
C.JS_SetPropertyStr(val.ctx.ref, val.ref, cstr, value.ref)
}
func (val *JSValue) Expose(name string) {
val.ctx.global.SetProperty(name, val)
}