-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgov7.go
165 lines (125 loc) · 3.58 KB
/
gov7.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 gov7
/*
// include v7.c because go doesn't compile .c file in a subdirectory
#include <stdlib.h>
#include "v7/v7.c"
#cgo CFLAGS: -O3 -DV7_NO_FS -DV7_LARGE_AST
#cgo LDFLAGS: -lm
*/
import "C"
import "unsafe"
import (
"errors"
"math"
)
type V7 C.struct_v7
type Val C.v7_val_t
func New() *V7 {
return (*V7)(C.v7_create())
}
func (v7 *V7) Destroy() {
C.v7_destroy((*C.struct_v7)(v7))
}
func (v7 *V7) ToJSON(v Val, size int) string {
buflen := C.size_t(size)
buf := (*C.char)(C.malloc(buflen))
defer C.free(unsafe.Pointer(buf))
C.v7_to_json((*C.struct_v7)(v7), C.v7_val_t(v), buf, buflen)
return C.GoString(buf)
}
func (v7 *V7) Exec(code string) (Val, error) {
var result C.v7_val_t
cs := C.CString(code)
defer C.free(unsafe.Pointer(cs))
e := C.v7_exec((*C.struct_v7)(v7), &result, cs)
v := Val(result)
// DEBUG
//fmt.Println(v7.ToJSON(v, 2048))
return v, v7.convertError(v, e)
}
func (v7 *V7) IsUndefined(v Val) bool {
return C.v7_is_undefined(C.v7_val_t(v)) != 0
}
func (v7 *V7) IsNumber(v Val) bool {
return C.v7_is_number((C.v7_val_t)(v)) != 0
}
func (v7 *V7) IsString(v Val) bool {
return C.v7_is_string((C.v7_val_t)(v)) != 0
}
func (v7 *V7) ToNumber(v Val) (float64, error) {
if !v7.IsNumber(v) {
return math.NaN(), errors.New("value is not a number")
}
return float64(C.v7_to_number(C.v7_val_t(v))), nil
}
func (v7 *V7) ToString(v Val) (string, error) {
if !v7.IsString(v) {
return "", errors.New("value is not a string")
}
var l *C.size_t = new(C.size_t)
cv := C.v7_val_t(v)
return C.GoString(C.v7_to_string((*C.struct_v7)(v7), &cv, l)), nil
}
func (v7 *V7) GetGlobalObject() Val {
return Val(C.v7_get_global_object((*C.struct_v7)(v7)))
}
func (v7 *V7) Get(obj Val, name string) Val {
cs := C.CString(name)
defer C.free(unsafe.Pointer(cs))
return Val(C.v7_get((*C.struct_v7)(v7), C.v7_val_t(obj), cs, C.size_t(len(name))))
}
const (
PROPERTY_READ_ONLY uint = 1
PROPERTY_DONT_ENUM uint = 2
PROPERTY_DONT_DELETE uint = 4
PROPERTY_HIDDEN uint = 8
PROPERTY_GETTER uint = 16
PROPERTY_SETTER uint = 32
)
func (v7 *V7) Set(obj Val, name string, attrs uint, v Val) error {
cs := C.CString(name)
defer C.free(unsafe.Pointer(cs))
if 0 != C.v7_set((*C.struct_v7)(v7), C.v7_val_t(obj), cs, C.size_t(len(name)), C.uint(attrs), C.v7_val_t(v)) {
return errors.New("failed to set " + name)
}
return nil
}
func (v7 *V7) CreateUndefined() Val {
return Val(C.v7_create_undefined())
}
/*
* CreateString copies the Go string twice
*/
func (v7 *V7) CreateString(str string) Val {
cs := C.CString(str)
defer C.free(unsafe.Pointer(cs))
return Val(C.v7_create_string((*C.struct_v7)(v7), cs, C.size_t(len(str)), 1 /*copy*/))
}
func (v7 *V7) CreateArray() Val {
return Val(C.v7_create_array((*C.struct_v7)(v7)))
}
func (v7 *V7) ArrayPush(ary Val, v Val) {
C.v7_array_push((*C.struct_v7)(v7), C.v7_val_t(ary), C.v7_val_t(v))
}
func (v7 *V7) Apply(fn Val, thisObj Val, args Val) (Val, error) {
var result C.v7_val_t
e := C.v7_apply((*C.struct_v7)(v7), &result, C.v7_val_t(fn), C.v7_val_t(thisObj), C.v7_val_t(args))
v := Val(result)
// DEBUG
//fmt.Println(v7.ToJSON(v, 2048))
return v, v7.convertError(v, e)
}
func (v7 *V7) convertError(v Val, e C.enum_v7_err) error {
switch e {
case C.V7_OK:
return nil
case C.V7_SYNTAX_ERROR:
// the document says the result is undefiend, but actually it's an error object
return errors.New("parse error: " + v7.ToJSON(v, 2048))
case C.V7_EXEC_EXCEPTION:
return errors.New("runtime error: " + v7.ToJSON(v, 2048))
case C.V7_STACK_OVERFLOW:
return errors.New("stack overflow")
}
return errors.New("unknown error")
}