/
js_value.go
165 lines (137 loc) · 3.22 KB
/
js_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
package jsgo
/*
#include "mujs.h"
typedef int (*checkfunc)(js_State*, int);
int check(void* f, js_State* state) {
return ((checkfunc) f)(state, 0);
}
*/
import "C"
import (
"runtime"
"unsafe"
)
// JsValue ...
type JsValue interface {
Float() float64
Integer() int
String() string
Bool() bool
IsDefined() bool
IsUndefined() bool
IsNumber() bool
IsString() bool
IsBool() bool
IsNull() bool
IsPrimitive() bool
IsCallable() bool
IsObject() bool
IsArray() bool
IsRegExp() bool
reference() *C.char
unreference()
}
type basicValue struct {
state *JsState
ref *C.char
}
func newJsValue(state *JsState) JsValue {
basic := &basicValue{state, C.js_ref(state.vm)}
var value JsValue = basic
if basic.IsObject() {
object := &JsObject{basic}
switch {
case object.IsCallable():
value = &JsCallable{object}
case object.IsArray():
value = &JsArray{object}
case object.IsRegExp():
value = &JsRegExp{object}
default:
value = object
}
}
runtime.SetFinalizer(value, JsValue.unreference)
return value
}
func (value *basicValue) reference() *C.char {
return value.ref
}
func (value *basicValue) unreference() {
C.js_unref(value.state.vm, value.ref)
}
// Float ...
func (value *basicValue) Float() float64 {
C.js_getregistry(value.state.vm, value.ref)
defer C.js_pop(value.state.vm, 1)
return float64(C.js_tonumber(value.state.vm, 0))
}
// Integer ...
func (value *basicValue) Integer() int {
C.js_getregistry(value.state.vm, value.ref)
defer C.js_pop(value.state.vm, 1)
return int(C.js_toint32(value.state.vm, 0))
}
// String ...
func (value *basicValue) String() string {
C.js_getregistry(value.state.vm, value.ref)
defer C.js_pop(value.state.vm, 1)
return C.GoString(C.js_tostring(value.state.vm, 0))
}
// Bool ...
func (value *basicValue) Bool() bool {
C.js_getregistry(value.state.vm, value.ref)
defer C.js_pop(value.state.vm, 1)
return C.js_toboolean(value.state.vm, 0) == 1
}
func (value *basicValue) check(f unsafe.Pointer) bool {
// put the reference on stack
C.js_getregistry(value.state.vm, value.ref)
defer C.js_pop(value.state.vm, 1)
// use the 'check' wrapper to call the validation function
return C.check(f, value.state.vm) == 1
}
// IsDefined ...
func (value *basicValue) IsDefined() bool {
return value.check(C.js_isdefined)
}
// IsUndefined ...
func (value *basicValue) IsUndefined() bool {
return value.check(C.js_isundefined)
}
// IsNumber ...
func (value *basicValue) IsNumber() bool {
return value.check(C.js_isnumber)
}
// IsString ...
func (value *basicValue) IsString() bool {
return value.check(C.js_isstring)
}
// IsBool ...
func (value *basicValue) IsBool() bool {
return value.check(C.js_isboolean)
}
// IsNull ...
func (value *basicValue) IsNull() bool {
return value.check(C.js_isnull)
}
// IsPrimitive ...
func (value *basicValue) IsPrimitive() bool {
return value.check(C.js_isprimitive)
}
// IsCallable ...
func (value *basicValue) IsCallable() bool {
return value.check(C.js_iscallable)
}
// IsObject ...
func (value *basicValue) IsObject() bool {
return value.check(C.js_isobject)
}
// IsArray ...
func (value *basicValue) IsArray() bool {
return value.check(C.js_isarray)
}
// IsRegexp ...
func (value *basicValue) IsRegExp() bool {
return value.check(C.js_isregexp)
}