示例#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
}