Example #1
0
// Bytes returns a byte slice with the bytes of this string.
func (ref *String) Bytes() []byte {
	// Conversion 1, null-terminate UTF-8 string
	len := C.JSStringGetMaximumUTF8CStringSize((C.JSStringRef)(unsafe.Pointer(ref)))
	buffer := C.malloc(len)
	if buffer == nil {
		panic(syscall.ENOMEM)
	}
	defer C.free(buffer)
	strlenWithNul := C.JSStringGetUTF8CString((C.JSStringRef)(unsafe.Pointer(ref)), (*C.char)(buffer), len)

	// Conversion 2, Go []byte
	return C.GoBytes(buffer, C.int(strlenWithNul-1))
}
Example #2
0
func (ref *String) String() string {
	// Conversion 1, null-terminate UTF-8 string
	len := C.JSStringGetMaximumUTF8CStringSize((C.JSStringRef)(unsafe.Pointer(ref)))
	buffer := C.malloc(len)
	if buffer == nil {
		panic(syscall.ENOMEM)
	}
	defer C.free(buffer)
	C.JSStringGetUTF8CString((C.JSStringRef)(unsafe.Pointer(ref)), (*C.char)(buffer), len)

	// Conversion 2, Go string
	ret := C.GoString((*C.char)(buffer))
	return ret
}