forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 2
/
v8_context.go
127 lines (105 loc) · 2.89 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
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 context_scope_callback
func 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 try_catch_callback
func try_catch_callback(callback unsafe.Pointer) {
(*(*func())(callback))()
}
func (cs ContextScope) ThrowException(err string) {
cs.context.engine.Compile([]byte(`throw "`+err+`"`), nil, nil).Run()
//
// TODO: use Isolate::ThrowException() will make FunctionTemplate::GetFunction() returns NULL, why?
//
//errPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&err)).Data)
//C.V8_Context_ThrowException(c.self, (*C.char)(errPtr), C.int(len(err)))
}
func (cs ContextScope) TryCatch(simple bool, callback func()) string {
isSimple := 0
if simple {
isSimple = 1
}
creport := C.V8_Context_TryCatch(cs.context.self, unsafe.Pointer(&callback), C.int(isSimple))
if creport == nil {
return ""
}
report := C.GoString(creport)
C.free(unsafe.Pointer(creport))
return report
}
type MessageCallback func(message string, data interface{})
func (cs ContextScope) AddMessageListener(simple bool, callback MessageCallback, data interface{}) {
var goSimple int
if simple {
goSimple = 1
}
var callbackPointer unsafe.Pointer
if callback != nil {
callbackPointer = unsafe.Pointer(&callback)
}
C.V8_AddMessageListener(
callbackPointer,
unsafe.Pointer(&data),
C.int(goSimple))
}
//export go_message_callback
func go_message_callback(message, callback, data unsafe.Pointer) {
report := C.GoString((*C.char)(message))
C.free(message)
(*(*MessageCallback)(callback))(report, *(*interface{})(data))
}
func (cs ContextScope) Global() *Object {
return newValue(C.V8_Context_Global(cs.context.self)).ToObject()
}