blob: 68acd17dea2ebd508fba7c49e5e41d591f45c839 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package wasm
import (
"unsafe"
)
type String uint64
func (value String) Address() uint32 {
return uint32(value >> 32)
}
func (value String) Length() uint32 {
return uint32((value << 32) >> 32)
}
func FromString(value string) String {
position := uint32(uintptr(unsafe.Pointer(unsafe.StringData(value))))
bytes := uint32(len(value))
return String(uint64(position)<<32 | uint64(bytes))
}
func (value String) String() string {
return unsafe.String((*byte)(unsafe.Pointer(uintptr(value.Address()))), value.Length())
}
type Buffer uint64
func (buffer Buffer) Address() uint32 {
return uint32(buffer >> 32)
}
func (buffer Buffer) Length() uint32 {
return uint32((buffer << 32) >> 32)
}
func FromSlice(value []byte) Buffer {
if len(value) == 0 {
return 0
}
ptr := uint64(uintptr(unsafe.Pointer(&value[0])))
return Buffer(ptr<<32 | uint64(len(value)))
}
func (buffer Buffer) Slice() []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(buffer.Address()))), buffer.Length())
}
func (buffer Buffer) String() string {
return unsafe.String((*byte)(unsafe.Pointer(uintptr(buffer.Address()))), buffer.Length())
}
|