コード例 #1
0
ファイル: js_value.go プロジェクト: jueebushe/monkey
func (v *Value) IsArray() bool {
	var result bool
	v.cx.rt.Use(func() {
		result = v.IsObject() && C.JS_IsArrayObject(
			v.cx.jscx, C.JSVAL_TO_OBJECT(v.val),
		) == C.JS_TRUE
	})
	return result
}
コード例 #2
0
ファイル: js_value.go プロジェクト: jueebushe/monkey
// Convert a value to Array.
func (v *Value) ToArray() *Array {
	var result *Array

	v.cx.rt.Use(func() {
		var obj *C.JSObject
		if C.JS_ValueToObject(v.cx.jscx, v.val, &obj) == C.JS_TRUE {
			if C.JS_IsArrayObject(v.cx.jscx, obj) == C.JS_TRUE {
				result = newArray(v.cx, obj)
			}
		}
	})

	return result
}
コード例 #3
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
}