This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
duktape.go
358 lines (296 loc) · 7.66 KB
/
duktape.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package duktape
/*
#cgo !windows CFLAGS: -std=c99 -O3 -Wall -fomit-frame-pointer -fstrict-aliasing
#cgo windows CFLAGS: -O3 -Wall -fomit-frame-pointer -fstrict-aliasing
#cgo linux LDFLAGS: -lm
#cgo freebsd LDFLAGS: -lm
#cgo openbsd LDFLAGS: -lm
#cgo dragonfly LDFLAGS: -lm
#include "duktape.h"
#include "duk_logging.h"
#include "duk_print_alert.h"
#include "duk_module_duktape.h"
#include "duk_console.h"
extern duk_ret_t goFunctionCall(duk_context *ctx);
extern void goFinalizeCall(duk_context *ctx);
*/
import "C"
import (
"errors"
"fmt"
"regexp"
"sync"
"unsafe"
)
var reFuncName = regexp.MustCompile("^[a-z_][a-z0-9_]*([A-Z_][a-z0-9_]*)*$")
const (
goFunctionPtrProp = "\xff" + "goFunctionPtrProp"
goContextPtrProp = "\xff" + "goContextPtrProp"
)
type Context struct {
*context
}
// transmute replaces the value from Context with the value of pointer
func (c *Context) transmute(p unsafe.Pointer) {
*c = *(*Context)(p)
}
// this is a pojo containing only the values of the Context
type context struct {
sync.Mutex
duk_context *C.duk_context
fnIndex *functionIndex
timerIndex *timerIndex
}
// New returns plain initialized duktape context object
// See: http://duktape.org/api.html#duk_create_heap_default
func New() *Context {
d := &Context{
&context{
duk_context: C.duk_create_heap(nil, nil, nil, nil, nil),
fnIndex: newFunctionIndex(),
timerIndex: &timerIndex{},
},
}
ctx := d.duk_context
C.duk_logging_init(ctx, 0)
C.duk_print_alert_init(ctx, 0)
C.duk_module_duktape_init(ctx)
C.duk_console_init(ctx, 0)
return d
}
// Flags is a set of flags for controlling the behaviour of duktape.
type Flags struct {
Logging uint
PrintAlert uint
Console uint
}
// FlagConsoleProxyWrapper is a Console flag.
// Use a proxy wrapper to make undefined methods (console.foo()) no-ops.
const FlagConsoleProxyWrapper = 1 << 0
// FlagConsoleFlush is a Console flag.
// Flush output after every call.
const FlagConsoleFlush = 1 << 1
// NewWithFlags returns plain initialized duktape context object
// You can control the behaviour of duktape by setting flags.
// See: http://duktape.org/api.html#duk_create_heap_default
func NewWithFlags(flags *Flags) *Context {
d := &Context{
&context{
duk_context: C.duk_create_heap(nil, nil, nil, nil, nil),
fnIndex: newFunctionIndex(),
timerIndex: &timerIndex{},
},
}
ctx := d.duk_context
C.duk_logging_init(ctx, C.duk_uint_t(flags.Logging))
C.duk_print_alert_init(ctx, C.duk_uint_t(flags.PrintAlert))
C.duk_module_duktape_init(ctx)
C.duk_console_init(ctx, C.duk_uint_t(flags.Console))
return d
}
func contextFromPointer(ctx *C.duk_context) *Context {
return &Context{&context{duk_context: ctx}}
}
// PushGlobalGoFunction push the given function into duktape global object
// Returns non-negative index (relative to stack bottom) of the pushed function
// also returns error if the function name is invalid
func (d *Context) PushGlobalGoFunction(name string, fn func(*Context) int) (int, error) {
if !reFuncName.MatchString(name) {
return -1, errors.New("Malformed function name '" + name + "'")
}
d.PushGlobalObject()
idx := d.PushGoFunction(fn)
d.PutPropString(-2, name)
d.Pop()
return idx, nil
}
// PushGoFunction push the given function into duktape stack, returns non-negative
// index (relative to stack bottom) of the pushed function
func (d *Context) PushGoFunction(fn func(*Context) int) int {
funPtr := d.fnIndex.add(fn)
ctxPtr := contexts.add(d)
idx := d.PushCFunction((*[0]byte)(C.goFunctionCall), C.DUK_VARARGS)
d.PushCFunction((*[0]byte)(C.goFinalizeCall), 1)
d.PushPointer(funPtr)
d.PutPropString(-2, goFunctionPtrProp)
d.PushPointer(ctxPtr)
d.PutPropString(-2, goContextPtrProp)
d.SetFinalizer(-2)
d.PushPointer(funPtr)
d.PutPropString(-2, goFunctionPtrProp)
d.PushPointer(ctxPtr)
d.PutPropString(-2, goContextPtrProp)
return idx
}
//export goFunctionCall
func goFunctionCall(cCtx *C.duk_context) C.duk_ret_t {
d := contextFromPointer(cCtx)
funPtr, ctx := d.getFunctionPtrs()
d.transmute(unsafe.Pointer(ctx))
result := d.fnIndex.get(funPtr)(d)
return C.duk_ret_t(result)
}
//export goFinalizeCall
func goFinalizeCall(cCtx *C.duk_context) {
d := contextFromPointer(cCtx)
funPtr, ctx := d.getFunctionPtrs()
d.transmute(unsafe.Pointer(ctx))
d.fnIndex.delete(funPtr)
}
func (d *Context) getFunctionPtrs() (unsafe.Pointer, *Context) {
d.PushCurrentFunction()
d.GetPropString(-1, goFunctionPtrProp)
funPtr := d.GetPointer(-1)
d.Pop()
d.GetPropString(-1, goContextPtrProp)
ctx := contexts.get(d.GetPointer(-1))
d.Pop2()
return funPtr, ctx
}
// Destroy destroy all the references to the functions and freed the pointers
func (d *Context) Destroy() {
d.fnIndex.destroy()
contexts.delete(d)
}
type Error struct {
Type string
Message string
FileName string
LineNumber int
Stack string
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Type, e.Message)
}
type Type uint
func (t Type) IsNone() bool { return t == TypeNone }
func (t Type) IsUndefined() bool { return t == TypeUndefined }
func (t Type) IsNull() bool { return t == TypeNull }
func (t Type) IsBool() bool { return t == TypeBoolean }
func (t Type) IsNumber() bool { return t == TypeNumber }
func (t Type) IsString() bool { return t == TypeString }
func (t Type) IsObject() bool { return t == TypeObject }
func (t Type) IsBuffer() bool { return t == TypeBuffer }
func (t Type) IsPointer() bool { return t == TypePointer }
func (t Type) IsLightFunc() bool { return t == TypeLightFunc }
func (t Type) String() string {
switch t {
case TypeNone:
return "None"
case TypeUndefined:
return "Undefined"
case TypeNull:
return "Null"
case TypeBoolean:
return "Boolean"
case TypeNumber:
return "Number"
case TypeString:
return "String"
case TypeObject:
return "Object"
case TypeBuffer:
return "Buffer"
case TypePointer:
return "Pointer"
case TypeLightFunc:
return "LightFunc"
default:
return "Unknown"
}
}
type functionIndex struct {
functions map[unsafe.Pointer]func(*Context) int
sync.RWMutex
}
type timerIndex struct {
c float64
sync.Mutex
}
func (t *timerIndex) get() float64 {
t.Lock()
defer t.Unlock()
t.c++
return t.c
}
func newFunctionIndex() *functionIndex {
return &functionIndex{
functions: make(map[unsafe.Pointer]func(*Context) int, 0),
}
}
func (i *functionIndex) add(fn func(*Context) int) unsafe.Pointer {
ptr := C.malloc(1)
i.Lock()
i.functions[ptr] = fn
i.Unlock()
return ptr
}
func (i *functionIndex) get(ptr unsafe.Pointer) func(*Context) int {
i.RLock()
fn := i.functions[ptr]
i.RUnlock()
return fn
}
func (i *functionIndex) delete(ptr unsafe.Pointer) {
i.Lock()
delete(i.functions, ptr)
i.Unlock()
C.free(ptr)
}
func (i *functionIndex) destroy() {
i.Lock()
for ptr, _ := range i.functions {
delete(i.functions, ptr)
C.free(ptr)
}
i.Unlock()
}
type ctxIndex struct {
sync.RWMutex
ctxs map[unsafe.Pointer]*Context
}
func (ci *ctxIndex) add(ctx *Context) unsafe.Pointer {
ci.RLock()
for ptr, ctxPtr := range ci.ctxs {
if ctxPtr == ctx {
ci.RUnlock()
return ptr
}
}
ci.RUnlock()
ci.Lock()
for ptr, ctxPtr := range ci.ctxs {
if ctxPtr == ctx {
ci.Unlock()
return ptr
}
}
ptr := C.malloc(1)
ci.ctxs[ptr] = ctx
ci.Unlock()
return ptr
}
func (ci *ctxIndex) get(ptr unsafe.Pointer) *Context {
ci.RLock()
ctx := ci.ctxs[ptr]
ci.RUnlock()
return ctx
}
func (ci *ctxIndex) delete(ctx *Context) {
ci.Lock()
for ptr, ctxPtr := range ci.ctxs {
if ctxPtr == ctx {
delete(ci.ctxs, ptr)
C.free(ptr)
ci.Unlock()
return
}
}
panic(fmt.Sprintf("context (%p) doesn't exist", ctx))
}
var contexts *ctxIndex
func init() {
contexts = &ctxIndex{
ctxs: make(map[unsafe.Pointer]*Context),
}
}