-
Notifications
You must be signed in to change notification settings - Fork 26
/
memory.go
88 lines (69 loc) · 2.02 KB
/
memory.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package wasmer
import (
"fmt"
"reflect"
"unsafe"
)
// MemoryError represents any kind of errors related to a WebAssembly memory. It
// is returned by `Memory` functions only.
type MemoryError struct {
// Error message.
message string
}
// NewMemoryError constructs a new `MemoryError`.
func NewMemoryError(message string) *MemoryError {
return &MemoryError{message}
}
// `MemoryError` is an actual error. The `Error` function returns
// the error message.
func (error *MemoryError) Error() string {
return error.message
}
// Memory represents an exported memory of a WebAssembly instance. To read
// and write data, please see the `Data` function.
type Memory struct {
memory *cWasmerMemoryT
}
// Instantiates a new WebAssembly exported memory.
func newMemory(memory *cWasmerMemoryT) Memory {
return Memory{memory}
}
// Length calculates the memory length (in bytes).
func (memory *Memory) Length() uint32 {
if nil == memory.memory {
return 0
}
return uint32(cWasmerMemoryDataLength(memory.memory))
}
// Data returns a slice of bytes over the WebAssembly memory.
func (memory *Memory) Data() []byte {
if nil == memory.memory {
return make([]byte, 0)
}
var length = memory.Length()
var data = (*uint8)(cWasmerMemoryData(memory.memory))
var header reflect.SliceHeader
header = *(*reflect.SliceHeader)(unsafe.Pointer(&header))
header.Data = uintptr(unsafe.Pointer(data))
header.Len = int(length)
header.Cap = int(length)
return *(*[]byte)(unsafe.Pointer(&header))
}
// Grow the memory by a number of pages (65kb each).
func (memory *Memory) Grow(numberOfPages uint32) error {
if nil == memory.memory {
return nil
}
var growResult = cWasmerMemoryGrow(memory.memory, cUint32T(numberOfPages))
if growResult != cWasmerOk {
var lastError, err = GetLastError()
var errorMessage = "Failed to grow the memory:\n %s"
if err != nil {
errorMessage = fmt.Sprintf(errorMessage, "(unknown details)")
} else {
errorMessage = fmt.Sprintf(errorMessage, lastError)
}
return NewMemoryError(errorMessage)
}
return nil
}