/
v8_engine.go
284 lines (253 loc) · 7.58 KB
/
v8_engine.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
// Copyright(c) 2015
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE
package gnode
/*
#cgo pkg-config: gnode.pc
#include "gnode.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "runtime"
import "reflect"
type Engine struct {
embedable
self unsafe.Pointer
disposed bool
//TODO: static value
undefinedValue *Value
nullValue *Value
trueValue *Value
falseValue *Value
//TODO: Context ID
//contextId int
//scriptId int
//unboundScriptId int
//valueId int
//TODO:
aliveContext map[int]*Context
aliveScript map[int]*Script
aliveUnboundScript map[int] *UnboundScript
aliveValue map[int32]*Value
}
var (
traceDispose bool = true
contextId int = 0
scriptId int = 0
unboundScriptId int = 0
valueId int32 = 0
)
//create engine
func NewEngine() *Engine {
self_ := C.V8_NewEngine(-1,-1)
engine_ := &Engine{
self : self_,
disposed : false,
aliveContext : make(map[int]*Context),
aliveScript : make(map[int]*Script),
aliveUnboundScript : make(map[int] *UnboundScript),
aliveValue : make(map[int32]*Value),
//contextId : 0,
//scriptId : 0,
//unboundScriptId : 0,
//valueId : 0,
}
engine_.SetOwner()
runtime.SetFinalizer(engine_, func(e *Engine) {
if traceDispose {
println("V8_Engine_Dispose()", e.self)
}
if !e.disposed {
C.V8_Engine_Dispose(e.self)
}
})
return engine_
}
func (engine_ *Engine) SetOwner() {
C.V8_Go_Engine(engine_.self, unsafe.Pointer(engine_))
}
func (engine_ *Engine) IsDisposed() bool{
return engine_.disposed
}
func (engine_ *Engine) Disposed(forceAll bool) {
engine_.checkDisposedException()
if forceAll {
//TODO: Disposed context
/*foreach (var aliveContext in _aliveContexts) {
JsContext.jscontext_dispose(aliveContext.Value.Handle);
}
_aliveContexts.Clear();
//TODO: Disposed script
foreach (var aliveScript in _aliveScripts) {
JsScript.jsscript_dispose(aliveScript.Value.Handle);
}*/
/*for _ , ctx_ := range engine_.aliveContext {
ctx_.Disposed()
}
for _, scp_ := range engine_.aliveScript {
scp_.Disposed()
}
for _, uscp_ := range engine_.aliveUnboundScript {
uscp_.Disposed()
}
for _, val_ := range engine_.aliveValue {
val_.Disposed();
}*/
}
if !engine_.disposed {
C.V8_Engine_Dispose(engine_.self)
}
engine_.disposed = true
}
func (engine_ *Engine) checkDisposedException(){
if engine_.disposed {
//throw new ObjectDisposedException("JsEngine:" + _engine.Handle);
panic("JsEngine Disposed Exception")
}
}
func (engine_ *Engine) TerminateExecution() {
C.V8_Engine_TerminateExecution(engine_.self)
}
/*
TODO:
func (engine_ *Engine) DumpHeapStats() {
C.V8_Engine_DumpHeapStats(engine_.self)
}
//TODO: DumpAllocatedItems
func (engine_ *Engine) DumpAllocatedItems() {
}
*/
func (engine_ *Engine) DisposeValue(val *Value){
if !val.IsDisposed() {
val.Disposed();
}
}
func StringToPointer(value string) unsafe.Pointer {
valPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&value)).Data)
return valPtr
}
func ByteToPointer(value []byte) unsafe.Pointer{
valPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&value)).Data)
return valPtr
}
//export go_panic
func go_panic(message *C.char) {
panic(C.GoString(message))
}
//export go_keepalive_value_remove
func go_keepalive_value_remove(engine unsafe.Pointer, context, id int) {
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ != nil {
engine_ := context_.GetEngine()
val_ := engine_.GetAliveValue(id)
val_.Disposed()
}*/
}
//export go_keepalive_object_remove
func go_keepalive_object_remove(engine unsafe.Pointer, context, id int){
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ != nil {
engine_ := context_.GetEngine()
val_ := engine_.GetAliveValue(id)
val_.Disposed()
}*/
}
//export go_keepalive_function_remove
func go_keepalive_function_remove(engine unsafe.Pointer, context, id int){
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ != nil {
engine_ := context_.GetEngine()
val_ := engine_.GetAliveValue(id)
val_.Disposed()
}*/
}
//export go_keepalive_remove
func go_keepalive_remove(engine unsafe.Pointer, context, id int){
//TODO:
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
}
//export go_keepalive_get_property_value
func go_keepalive_get_property_value(engine unsafe.Pointer, context int, id int, name *C.char){
//TODO: return unsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveGetPropertyValue(id, C.GoString(name))
}
//export go_keepalive_set_property_value
func go_keepalive_set_property_value(engine unsafe.Pointer, context int, id int, name *C.char, value *C.jsvalue) {
//TODO: return unsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveSetPropertyValue(id, C.GoString(name), value)
}
//export go_keepalive_valueof
func go_keepalive_valueof(engine unsafe.Pointer, context, id int) {
//TODO: return unsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveValueOf(id)
}
//export go_keepalive_invoke
func go_keepalive_invoke(engine unsafe.Pointer, context int, id int, args *C.jsvalue) {
//TODO: return unsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveInvoke(id, args)
}
//export go_keepalive_delete_property
func go_keepalive_delete_property(engine unsafe.Pointer, context int, id int, name *C.char) {
//TODO: return unsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveDeleteProperty(id, C.GoString(name))
}
//export go_keepalive_enumerate_properties
func go_keepalive_enumerate_properties(engine unsafe.Pointer, context int, id int) {
//TODO: retru nunsafe.Pointer
/*engine_ := (*Engine)(engine)
context_ := engine_.aliveContext[context]
if context_ == nil {
panic("fail")
}*/
//context_.keepAliveEnumerateProperties(id)
}