forked from mattn/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8.go
109 lines (97 loc) · 2.16 KB
/
v8.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
package v8
/*
#include <stdlib.h>
#include "v8wrap.h"
extern char* _go_v8_callback(unsigned int id, char* n, char* a);
static char*
_c_v8_callback(unsigned int id, char* n, char* a) {
return _go_v8_callback(id, n, a);
}
static void
v8_callback_init() {
v8_init((void*) _c_v8_callback);
}
*/
// #cgo LDFLAGS: -L. -lv8wrap -lstdc++
import "C"
import (
"bytes"
"encoding/json"
"errors"
"runtime"
"text/template"
"unsafe"
)
var contexts = make(map[uint32]*V8Context)
var tmpl = template.Must(template.New("go-v8").Parse(`
function {{.name}}() {
return _go_call({{.id}}, "{{.name}}", JSON.stringify([].slice.call(arguments)));
}`))
//export _go_v8_callback
func _go_v8_callback(id uint32, n, a *C.char) *C.char {
c := contexts[id]
f := c.funcs[C.GoString(n)]
if f != nil {
var argv []interface{}
json.Unmarshal([]byte(C.GoString(a)), &argv)
ret := f(argv...)
if ret != nil {
b, _ := json.Marshal(ret)
return C.CString(string(b))
}
return nil
}
return C.CString("undefined")
}
func init() {
C.v8_callback_init()
}
type V8Context struct {
id uint32
v8context unsafe.Pointer
funcs map[string]func(... interface{}) interface{}
}
func NewContext() *V8Context {
v := &V8Context{
uint32(len(contexts)),
C.v8_create(),
make(map[string]func(... interface{}) interface{}),
}
contexts[v.id] = v
runtime.SetFinalizer(v, func(p *V8Context) {
C.v8_release(p.v8context)
})
return v
}
func (v *V8Context) Eval(in string) (res interface{}, err error) {
ptr := C.CString(in)
defer C.free(unsafe.Pointer(ptr))
C.v8_callback_init()
ret := C.v8_execute(v.v8context, ptr)
if ret != nil {
out := C.GoString(ret)
if out != "" {
C.free(unsafe.Pointer(ret))
var buf bytes.Buffer
buf.Write([]byte(out))
dec := json.NewDecoder(&buf)
err = dec.Decode(&res)
return
}
return nil, nil
}
ret = C.v8_error(v.v8context)
out := C.GoString(ret)
C.free(unsafe.Pointer(ret))
return nil, errors.New(out)
}
func (v *V8Context) AddFunc(name string, f func(...interface{}) interface{}) error {
v.funcs[name] = f
b := bytes.NewBufferString("")
tmpl.Execute(b, map[string]interface{} {
"id": v.id,
"name": name,
})
_, err := v.Eval(b.String())
return err
}