Exemple #1
0
func NewJSFunction(js *JS, function C.jsval) *JSFunction {
	rval := &JSFunction{js, function}
	C.JS_AddValueRoot(rval.js.context, &(rval.function))
	runtime.SetFinalizer(rval, func(f *JSFunction) {
		C.JS_RemoveValueRoot(f.js.context, &(f.function))
	})
	return rval
}
Exemple #2
0
func (r *Runtime) init() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	r.goid = goid.Get()

	r.jsrt = C.JS_NewRuntime(C.uint32(r.maxbytes))
	if r.jsrt == nil {
		r.initChan <- false
		return
	}

	r.workChan = make(chan jswork, 20)
	r.closeChan = make(chan int, 1)
	r.ctxDisposeChan = make(chan *Context, 50)
	r.objDisposeChan = make(chan *Object, 100)
	r.aryDisposeChan = make(chan *Array, 100)
	r.valDisposeChan = make(chan *Value, 100)
	r.sptDisposeChan = make(chan *Script, 100)

	runtime.SetFinalizer(r, func(r *Runtime) {
		r.Dispose()
	})

	r.initChan <- true

L:
	for {
		select {
		case work := <-r.workChan:
			work.callback()
			work.resultChan <- 1
		case ctx := <-r.ctxDisposeChan:
			C.JS_DestroyContext(ctx.jscx)
		case obj := <-r.objDisposeChan:
			C.JS_RemoveObjectRoot(obj.cx.jscx, &obj.obj)
		case ary := <-r.aryDisposeChan:
			C.JS_RemoveObjectRoot(ary.cx.jscx, &ary.obj)
		case val := <-r.valDisposeChan:
			C.JS_RemoveValueRoot(val.cx.jscx, &val.val)
		case spt := <-r.sptDisposeChan:
			C.JS_RemoveObjectRoot(spt.cx.jscx, &spt.obj)
		case _ = <-r.closeChan:
			break L
		}
	}

	C.JS_DestroyRuntime(r.jsrt)
}