コード例 #1
0
ファイル: js_context.go プロジェクト: jueebushe/monkey
// Evaluate JavaScript
// When you need high efficiency or run same script many times, please look at Compile() method.
func (c *Context) Eval(script string) *Value {
	var result *Value

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

		var rval C.jsval
		if C.JS_EvaluateScript(c.jscx, c.jsglobal, cscript, C.uintN(len(script)), C.eval_filename, 0, &rval) == C.JS_TRUE {
			result = newValue(c, rval)
		}
	})

	return result
}
コード例 #2
0
ファイル: gomonkey.go プロジェクト: zond/gomonkey
func (self *JS) Eval(script string) interface{} {
	var rval C.jsval
	c_script := C.CString(script)
	defer C.free(unsafe.Pointer(c_script))
	c_scriptname := C.CString("script")
	defer C.free(unsafe.Pointer(c_scriptname))
	C.JS_EvaluateScript(self.context,
		self.global,
		c_script,
		C.uintN(len(script)),
		c_scriptname,
		1,
		&rval)
	return self.jsval2goval(rval)
}
コード例 #3
0
ファイル: js_value.go プロジェクト: jueebushe/monkey
// Call a function value
func (v *Value) Call(argv []*Value) *Value {
	var result *Value

	v.cx.rt.Use(func() {
		argv2 := make([]C.jsval, len(argv))
		for i := 0; i < len(argv); i++ {
			argv2[i] = argv[i].val
		}
		argv3 := unsafe.Pointer(&argv2)
		argv4 := (*reflect.SliceHeader)(argv3).Data
		argv5 := (*C.jsval)(unsafe.Pointer(argv4))

		var rval C.jsval
		if C.JS_CallFunctionValue(v.cx.jscx, nil, v.val, C.uintN(len(argv)), argv5, &rval) == C.JS_TRUE {
			result = newValue(v.cx, rval)
		}
	})

	return result
}
コード例 #4
0
ファイル: gomonkey.go プロジェクト: zond/gomonkey
func (self *JSFunction) Call(receiver *JSObject, params ...interface{}) interface{} {
	var c_receiver *C.JSObject
	if receiver == nil {
		c_receiver = nil
	} else {
		c_receiver = receiver.object
	}
	var rval C.jsval
	c_params := C.AllocateJsvalArray(C.int(len(params)))
	for index, param := range params {
		C.SetJsvalArray(c_params, C.int(index), self.js.goval2jsval(param))
	}
	C.JS_CallFunctionValue(self.js.context,
		c_receiver,
		self.function,
		C.uintN(len(params)),
		c_params,
		&rval)
	return self.js.jsval2goval(rval)
}
コード例 #5
0
ファイル: js_context.go プロジェクト: jueebushe/monkey
// 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
}
コード例 #6
0
ファイル: js_object.go プロジェクト: jueebushe/monkey
func (o *Object) DefineProperty(name string, value *Value, getter JsPropertyGetter, setter JsPropertySetter, attrs JsPropertyAttrs) bool {
	var result bool

	o.cx.rt.Use(func() {
		if C.JS_IsArrayObject(o.cx.jscx, o.obj) == C.JS_TRUE {
			panic("Could't define property on array.")
		}

		cname := C.CString(name)
		defer C.free(unsafe.Pointer(cname))

		var r C.JSBool

		if getter != nil && setter != nil {
			r = C.JS_DefineProperty(o.cx.jscx, o.obj, cname, value.val, C.the_go_getter_callback, C.the_go_setter_callback, C.uintN(uint(attrs))|C.JSPROP_SHARED)
		} else if getter != nil && setter == nil {
			r = C.JS_DefineProperty(o.cx.jscx, o.obj, cname, value.val, C.the_go_getter_callback, nil, C.uintN(uint(attrs)))
		} else if getter == nil && setter != nil {
			r = C.JS_DefineProperty(o.cx.jscx, o.obj, cname, value.val, nil, C.the_go_setter_callback, C.uintN(uint(attrs)))
		} else {
			panic("The getter and setter both nil")
		}

		if r == C.JS_TRUE {
			if getter != nil {
				if o.getters == nil {
					o.getters = make(map[string]JsPropertyGetter)
				}
				o.getters[name] = getter
			}

			if setter != nil {
				if o.setters == nil {
					o.setters = make(map[string]JsPropertySetter)
				}
				o.setters[name] = setter
			}

			result = true
		}
	})

	return result
}