예제 #1
0
func (o *Object) Keys() []string {
	var result []string

	o.cx.rt.Use(func() {
		ids := C.JS_Enumerate(o.cx.jscx, o.obj)
		if ids == nil {
			panic("enumerate failed")
		}
		defer C.JS_free(o.cx.jscx, unsafe.Pointer(ids))

		keys := make([]string, ids.length)
		head := unsafe.Pointer(&ids.vector[0])

		sl := &reflect.SliceHeader{
			uintptr(head), len(keys), len(keys),
		}
		vector := *(*[]C.jsid)(unsafe.Pointer(sl))
		for i := 0; i < len(keys); i++ {
			id := vector[i]
			ckey := C.JS_EncodeString(o.cx.jscx, C.JSID_TO_STRING(id))
			gkey := C.GoString(ckey)
			C.JS_free(o.cx.jscx, unsafe.Pointer(ckey))
			keys[i] = gkey
		}

		result = keys
	})

	return result
}
예제 #2
0
// Convert a value to String.
func (v *Value) ToString() string {
	var result string

	v.cx.rt.Use(func() {
		cstring := C.JS_EncodeString(v.cx.jscx, C.JS_ValueToString(v.cx.jscx, v.val))
		gostring := C.GoString(cstring)
		C.JS_free(v.cx.jscx, unsafe.Pointer(cstring))

		result = gostring
	})

	return result
}
예제 #3
0
파일: gomonkey.go 프로젝트: zond/gomonkey
func (self *JS) jsval2goval(val C.jsval) interface{} {
	t := C.JS_TypeOfValue(self.context, val)
	if t == C.JSTYPE_VOID {
		return nil
	} else if t == C.JSTYPE_OBJECT {
		return &JSObject{self, C.Jsval2JSObject(self.context, val)}
	} else if t == C.JSTYPE_FUNCTION {
		return NewJSFunction(self, val)
	} else if t == C.JSTYPE_STRING {
		c_string := C.JS_EncodeString(self.context, C.Jsval2JSString(self.context, val))
		defer C.free(unsafe.Pointer(c_string))
		return C.GoString(c_string)
	} else if t == C.JSTYPE_NUMBER {
		return float64(C.Jsval2jsdouble(self.context, val))
	} else if t == C.JSTYPE_BOOLEAN {
		return C.Jsval2JSBool(self.context, val) == C.JS_TRUE
	}
	return nil
}