Beispiel #1
0
// Compile JavaScript
// When you need run a script many times, you can use this to avoid dynamic compile.
func (c *Context) Compile(code, filename string, lineno int) *Script {
	var result *Script

	c.rt.Use(func() {
		ccode := C.CString(code)
		defer C.free(unsafe.Pointer(ccode))

		cfilename := C.CString(filename)
		defer C.free(unsafe.Pointer(cfilename))

		var obj = C.JS_CompileScript(c.jscx, c.jsglobal, ccode, C.size_t(len(code)), cfilename, C.uintN(lineno))

		if obj != nil {
			script := &Script{c, obj, 0}

			C.JS_AddObjectRoot(c.jscx, &script.obj)

			runtime.SetFinalizer(script, func(s *Script) {
				s.Dispose()
			})

			result = script
		}
	})

	return result
}
Beispiel #2
0
func NewJSObject(js *JS, object *C.JSObject) *JSObject {
	rval := &JSObject{js, object}
	C.JS_AddObjectRoot(rval.js.context, &(rval.object))
	runtime.SetFinalizer(rval, func(o *JSObject) {
		C.JS_RemoveObjectRoot(o.js.context, &(o.object))
	})
	return rval
}
Beispiel #3
0
// See newObject()
func newArray(cx *Context, obj *C.JSObject) *Array {
	result := &Array{cx, obj}

	C.JS_AddObjectRoot(cx.jscx, &result.obj)

	runtime.SetFinalizer(result, func(a *Array) {
		cx.rt.aryDisposeChan <- a
	})

	return result
}
Beispiel #4
0
// Add the JSObject to the garbage collector's root set.
// See: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_AddRoot
func newObject(cx *Context, obj *C.JSObject, gval interface{}) *Object {
	gobj := (*Object)(C.JS_GetPrivate(cx.jscx, obj))
	if gobj != nil {
		return gobj
	}

	result := &Object{cx, obj, gval, nil, nil, nil}

	C.JS_AddObjectRoot(cx.jscx, &result.obj)

	runtime.SetFinalizer(result, func(o *Object) {
		cx.rt.objDisposeChan <- o
	})

	// User defined property and function object use this to find callback.
	C.JS_SetPrivate(cx.jscx, result.obj, unsafe.Pointer(result))

	return result
}