Example #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
}
Example #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
}
Example #3
0
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
}