This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from kingland/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 3
/
v8_context.go
223 lines (183 loc) · 5.04 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
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
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
}
type EscapableScope struct {
ContextScope
}
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)})
}
//export go_escapable_scope_callback
func go_escapable_scope_callback(c unsafe.Pointer, callback unsafe.Pointer) {
(*(*func(EscapableScope))(callback))(EscapableScope{ContextScope{(*Context)(c)}})
}
func (c *Context) Scope(callback func(ContextScope)) {
C.V8_Context_Scope(c.self, unsafe.Pointer(c), unsafe.Pointer(&callback))
}
func (c *Context) GetEngine() *Engine {
return c.engine
}
func (c *Context) EscapableScope(callback func(EscapableScope)) {
C.V8_Escapable_Scope(c.self, unsafe.Pointer(c), unsafe.Pointer(&callback))
}
func (c *Context) SetSecurityToken(value *Value) {
C.V8_Context_SetSecurityToken(c.self, value.self)
}
func (c *Context) GetSecurityToken() *Value {
return newValue(c.GetEngine(), C.V8_Context_GetSecurityToken(c.self))
}
func (c *Context) UseDefaultSecurityToken() {
C.V8_Context_UseDefaultSecurityToken(c.self)
}
func (c *Context) GetEmbedderData(index int) *Value {
return newValue(c.GetEngine(), C.V8_Context_GetEmbedderData(c.self, C.int(index)))
}
func (c *Context) SetEmbedderData(index int, value *Value) {
C.V8_Context_SetEmbedderData(c.self, C.int(index), value.self)
}
func (c *Context) SetAlignedPointerInEmbedderData(index int, ptr unsafe.Pointer) {
C.V8_Context_SetAlignedPointerInEmbedderData(c.self, C.int(index), ptr)
}
func (c *Context) GetAlignedPointerFromEmbedderData(index int) unsafe.Pointer {
return C.V8_Context_GetAlignedPointerFromEmbedderData(c.self, C.int(index))
}
func (c *Context) Global() *Object {
return newValue(c.GetEngine(), C.V8_Context_Global(c.self)).ToObject()
}
//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 (es EscapableScope) Escape(escontext *Context) *Context {
self := C.V8_Context_Escape(es.context.self, escontext.self)
if self == nil {
return nil
}
e := es.GetEngine()
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
//return newValue(es.GetEngine(), C.V8_Escapable_Escape())
}
func (es EscapableScope) EscapeValue(value *Value) *Value {
self := C.V8_Value_Escape(es.context.self, value.self)
return newValue(es.GetEngine(), self)
}
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()) *Message {
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.p)
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()
}