forked from tzzzoz/monkey
/
js_context.go
372 lines (299 loc) · 7.24 KB
/
js_context.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
362
363
364
365
366
367
368
369
370
371
372
package monkey
/*
#include "monkey.h"
*/
import "C"
import (
"runtime"
"sync/atomic"
"unsafe"
)
// JavaScript Context
type Context struct {
rt *Runtime
jscx *C.JSContext
jsglobal *C.JSObject
funcs map[string]JsFunc
errorReporter ErrorReporter
disposed int64
}
func (r *Runtime) NewContext() *Context {
var result *Context
r.Use(func() {
c := new(Context)
c.rt = r
c.jscx = C.JS_NewContext(r.jsrt, 8192)
if c.jscx == nil {
return
}
C.JS_SetOptions(c.jscx, C.JSOPTION_VAROBJFIX|C.JSOPTION_JIT|C.JSOPTION_METHODJIT)
C.JS_SetVersion(c.jscx, C.JSVERSION_LATEST)
c.jsglobal = C.JS_NewCompartmentAndGlobalObject(c.jscx, &C.global_class, nil)
if C.JS_InitStandardClasses(c.jscx, c.jsglobal) != C.JS_TRUE {
return
}
// User defined function use this to find callback.
C.JS_SetContextPrivate(c.jscx, unsafe.Pointer(c))
runtime.SetFinalizer(c, func(c *Context) {
c.Dispose()
})
result = c
})
return result
}
// Free by manual
func (c *Context) Dispose() {
if atomic.CompareAndSwapInt64(&c.disposed, 0, 1) {
c.rt.ctxDisposeChan <- c
}
}
type ErrorReporter func(report *ErrorReport)
type ErrorReportFlags uint
const (
JSREPORT_WARNING = ErrorReportFlags(C.JSREPORT_WARNING)
JSREPORT_EXCEPTION = ErrorReportFlags(C.JSREPORT_EXCEPTION)
JSREPORT_STRICT = ErrorReportFlags(C.JSREPORT_STRICT)
)
type ErrorReport struct {
Context *Context
Message string
FileName string
LineBuf string
LineNum int
ErrorNum int
TokenIndex int
Flags ErrorReportFlags
}
//export call_error_func
func call_error_func(c unsafe.Pointer, message *C.char, report *C.JSErrorReport) {
cx := (*Context)(c)
if cx.errorReporter != nil {
cx.errorReporter(&ErrorReport{
Context: cx,
Message: C.GoString(message),
FileName: C.GoString(report.filename),
LineNum: int(report.lineno),
ErrorNum: int(report.errorNumber),
LineBuf: C.GoString(report.linebuf),
TokenIndex: int(uintptr(unsafe.Pointer(report.tokenptr)) - uintptr(unsafe.Pointer(report.linebuf))),
})
}
}
// Set a error reporter
func (c *Context) SetErrorReporter(reporter ErrorReporter) {
if c.errorReporter == nil {
C.JS_SetErrorReporter(c.jscx, C.the_error_callback)
}
c.errorReporter = reporter
}
// Evaluate JavaScript
// When you need high efficiency or run same script many times, please look at Compile() method.
func (c *Context) Eval(script string) *Value {
var result *Value
c.rt.Use(func() {
cscript := C.CString(script)
defer C.free(unsafe.Pointer(cscript))
var rval C.jsval
if C.JS_EvaluateScript(c.jscx, c.jsglobal, cscript, C.uintN(len(script)), C.eval_filename, 0, &rval) == C.JS_TRUE {
result = newValue(c, rval)
}
})
return result
}
// Compiled Script
type Script struct {
cx *Context
obj *C.JSObject
disposed int64
}
// Free by manual
func (s *Script) Dispose() {
if atomic.CompareAndSwapInt64(&s.disposed, 0, 1) {
s.cx.rt.sptDisposeChan <- s
}
}
func (s *Script) Context() *Context {
return s.cx
}
func (s *Script) Runtime() *Runtime {
return s.cx.rt
}
// Execute the script
func (s *Script) Execute() *Value {
var result *Value
s.cx.rt.Use(func() {
var rval C.jsval
if C.JS_ExecuteScript(s.cx.jscx, s.cx.jsglobal, s.obj, &rval) == C.JS_TRUE {
result = newValue(s.cx, rval)
}
})
return result
}
// Execute the script
func (s *Script) ExecuteIn(cx *Context) *Value {
var result *Value
cx.rt.Use(func() {
var rval C.jsval
if C.JS_ExecuteScript(cx.jscx, cx.jsglobal, s.obj, &rval) == C.JS_TRUE {
result = newValue(cx, rval)
}
})
return result
}
// Compile JavaScript
// When you need run a script many times, you can use this to avoid dynamic compile.
func (c *Context) Compile(code, filename string, lineno int) *Script {
var result *Script
c.rt.Use(func() {
ccode := C.CString(code)
defer C.free(unsafe.Pointer(ccode))
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
var obj = C.JS_CompileScript(c.jscx, c.jsglobal, ccode, C.size_t(len(code)), cfilename, C.uintN(lineno))
if obj != nil {
script := &Script{c, obj, 0}
C.JS_AddObjectRoot(c.jscx, &script.obj)
runtime.SetFinalizer(script, func(s *Script) {
s.Dispose()
})
result = script
}
})
return result
}
// Go defined JS function callback info
type Func struct {
context *Context
name string
args []*Value
result *Value
}
func (f *Func) Context() *Context {
return f.context
}
func (f *Func) Name() string {
return f.name
}
func (f *Func) Argc() int {
return len(f.args)
}
func (f *Func) Argv(n int) *Value {
return f.args[n]
}
func (f *Func) Return(v *Value) {
f.result = v
}
// Go defined JS function callback
type JsFunc func(f *Func)
//export call_go_func
func call_go_func(c unsafe.Pointer, name *C.char, argc C.uintN, vp *C.jsval) C.JSBool {
var context = (*Context)(c)
var args = make([]*Value, int(argc))
for i := 0; i < len(args); i++ {
args[i] = newValue(context, C.GET_ARGV(context.jscx, vp, C.int(i)))
}
var gname = C.GoString(name)
var f = Func{
context: context,
name: gname,
args: args,
}
context.funcs[gname](&f)
if f.result != nil {
C.SET_RVAL(context.jscx, vp, f.result.val)
return C.JS_TRUE
}
return C.JS_FALSE
}
// Define a function into runtime
// @name The function name
// @callback The function implement
func (c *Context) DefineFunction(name string, callback JsFunc) bool {
var result bool
c.rt.Use(func() {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if C.JS_DefineFunction(c.jscx, c.jsglobal, cname, C.the_go_func_callback, 0, 0) == nil {
return
}
if c.funcs == nil {
c.funcs = make(map[string]JsFunc)
}
c.funcs[name] = callback
result = true
})
return result
}
func (c *Context) Runtime() *Runtime {
return c.rt
}
// Warp null
func (c *Context) Null() *Value {
var result *Value
c.rt.Use(func() {
result = newValue(c, C.GET_NULL())
})
return result
}
// Warp void
func (c *Context) Void() *Value {
var result *Value
c.rt.Use(func() {
result = newValue(c, C.GET_VOID())
})
return result
}
// Warp integer
func (c *Context) Int(v int32) *Value {
var result *Value
c.rt.Use(func() {
result = newValue(c, C.INT_TO_JSVAL(C.int32(v)))
})
return result
}
// Warp float
func (c *Context) Number(v float64) *Value {
var result *Value
c.rt.Use(func() {
result = newValue(c, C.DOUBLE_TO_JSVAL(C.jsdouble(v)))
})
return result
}
// Warp string
func (c *Context) String(v string) *Value {
var result *Value
c.rt.Use(func() {
cv := C.CString(v)
defer C.free(unsafe.Pointer(cv))
result = newValue(c, C.STRING_TO_JSVAL(C.JS_NewStringCopyN(c.jscx, cv, C.size_t(len(v)))))
})
return result
}
// Warp boolean
func (c *Context) Boolean(v bool) *Value {
var result *Value
c.rt.Use(func() {
if v {
result = newValue(c, C.JS_TRUE)
} else {
result = newValue(c, C.JS_FALSE)
}
})
return result
}
// Create an empty array, like: []
func (c *Context) NewArray() *Array {
var result *Array
c.rt.Use(func() {
result = newArray(c, C.JS_NewArrayObject(c.jscx, 0, nil))
})
return result
}
// Create an empty object, like: {}
func (c *Context) NewObject(gval interface{}) *Object {
var result *Object
c.rt.Use(func() {
result = newObject(c, C.JS_NewObject(c.jscx, nil, nil, nil), gval)
})
return result
}