forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 2
/
v8_context.go
147 lines (123 loc) · 3 KB
/
v8_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
package v8
/*
#include "v8_wrap.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "runtime"
//import "reflect"
// A sandboxed execution context with its own set of built-in objects
// and functions.
type Context struct {
embedable
self unsafe.Pointer
engine *Engine
}
type ContextScope struct {
context *Context
}
func (cs ContextScope) GetEngine() *Engine {
return cs.context.engine
}
func (cs ContextScope) GetPrivateData() interface{} {
return cs.context.GetPrivateData()
}
func (cs ContextScope) SetPrivateData(data interface{}) {
cs.context.SetPrivateData(data)
}
func (e *Engine) NewContext(globalTemplate *ObjectTemplate) *Context {
var globalTemplatePtr unsafe.Pointer
if globalTemplate != nil {
globalTemplatePtr = globalTemplate.self
}
self := C.V8_NewContext(e.self, globalTemplatePtr)
if self == nil {
return nil
}
result := &Context{
self: self,
engine: e,
}
runtime.SetFinalizer(result, func(c *Context) {
if traceDispose {
println("v8.Context.Dispose()", c.self)
}
C.V8_DisposeContext(c.self)
})
return result
}
//export go_context_scope_callback
func go_context_scope_callback(c unsafe.Pointer, callback unsafe.Pointer) {
(*(*func(ContextScope))(callback))(ContextScope{(*Context)(c)})
}
func (c *Context) Scope(callback func(ContextScope)) {
C.V8_Context_Scope(c.self, unsafe.Pointer(c), unsafe.Pointer(&callback))
}
//export go_try_catch_callback
func go_try_catch_callback(callback unsafe.Pointer) {
(*(*func())(callback))()
}
func escape(s string) string {
output := ""
for _, r := range s {
switch r {
case '"':
output += "\\\""
case '\\':
output += "\\\\"
case '/':
output += "\\/"
case '\n':
output += "\\n"
case '\r':
output += "\\r"
case '\t':
output += "\\t"
case '\b':
output += "\\b"
case '\f':
output += "\\f"
default:
output += string(r)
}
}
return output
}
func (cs ContextScope) ThrowException(err string) {
cs.Eval(`throw "` + escape(err) + `"`)
//
// TODO: use Isolate::ThrowException() will make FunctionTemplate::GetFunction() returns NULL, why?
//
//errPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&err)).Data)
//C.V8_Context_ThrowException(cs.context.self, (*C.char)(errPtr), C.int(len(err)))
}
func (cs ContextScope) ThrowException2(value *Value) {
C.V8_Context_ThrowException2(value.self)
}
func (cs ContextScope) TryCatch(callback func()) error {
msg := C.V8_Context_TryCatch(cs.context.self, unsafe.Pointer(&callback))
if msg == nil {
return nil
}
return (*Message)(msg)
}
type Exception struct {
*Value
*Message
}
func (cs ContextScope) TryCatchException(callback func()) *Exception {
e := C.V8_Context_TryCatchException(cs.context.self, unsafe.Pointer(&callback))
if e == nil {
return nil
}
excep := (*exception)(e)
val := newValue(cs.GetEngine(), excep.Pointer)
if val == nil {
return nil
}
return &Exception{val, excep.Message}
}
func (cs ContextScope) Global() *Object {
return newValue(cs.GetEngine(), C.V8_Context_Global(cs.context.self)).ToObject()
}