Example #1
0
// Define a function into runtime
// @name     The function name
// @callback The function implement
func (c *Context) DefineFunction(name string, callback JsFunc) bool {
	var result bool

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

		if C.JS_DefineFunction(c.jscx, c.jsglobal, cname, C.the_go_func_callback, 0, 0) == nil {
			return
		}

		if c.funcs == nil {
			c.funcs = make(map[string]JsFunc)
		}

		c.funcs[name] = callback

		result = true
	})

	return result
}
Example #2
0
// Define a function into object
// @name     The function name
// @callback The function implement
func (o *Object) DefineFunction(name string, callback JsObjectFunc) bool {
	var result bool

	o.cx.rt.Use(func() {
		cname := C.CString(name)
		defer C.free(unsafe.Pointer(cname))

		if C.JS_DefineFunction(o.cx.jscx, o.obj, cname, C.the_go_obj_func_callback, 0, 0) == nil {
			result = false
		}

		if o.funcs == nil {
			o.funcs = make(map[string]JsObjectFunc)
		}

		o.funcs[name] = callback

		result = true
	})

	return result
}