-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontext.go
51 lines (39 loc) · 1.02 KB
/
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
package gojs
// #include <stdlib.h>
// #include <JavaScriptCore/JSContextRef.h>
import "C"
import "unsafe"
// Context wraps a JavaScriptCore JSContextRef.
type Context struct {
ref C.JSContextRef
}
// GlobalContext wraps a JavaScriptCore JSGlobalContextRef.
type GlobalContext Context
func NewContext() *Context {
ctx := new(Context)
c_nil := C.JSClassRef(unsafe.Pointer(uintptr(0)))
ctx.ref = C.JSContextRef(C.JSGlobalContextCreate(c_nil))
return ctx
}
type RawContext C.JSContextRef
type RawGlobalContext C.JSGlobalContextRef
func NewContextFrom(raw RawContext) *Context {
ctx := new(Context)
ctx.ref = C.JSContextRef(raw)
return ctx
}
func NewGlobalContextFrom(raw RawGlobalContext) *GlobalContext {
ctx := new(GlobalContext)
ctx.ref = C.JSContextRef(raw)
return ctx
}
func (ctx *Context) Retain() {
C.JSGlobalContextRetain(ctx.ref)
}
func (ctx *Context) Release() {
C.JSGlobalContextRelease(ctx.ref)
}
func (ctx *Context) GlobalObject() *Object {
ret := C.JSContextGetGlobalObject(ctx.ref)
return ctx.newObject(ret)
}