-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
47 lines (37 loc) · 1003 Bytes
/
host.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
//go:build !tinygo.wasm
// This file is designed to be imported by hosts.
package wasm
import (
"context"
"errors"
"fmt"
"github.com/tetratelabs/wazero/api"
)
func ReadMemory(mem api.Memory, offset, size uint32) ([]byte, error) {
buf, ok := mem.Read(offset, size)
if !ok {
return nil, fmt.Errorf("Memory.Read(%d, %d) out of range", offset, size)
}
return buf, nil
}
func WriteMemory(ctx context.Context, m api.Module, data []byte) (uint64, error) {
malloc := m.ExportedFunction("malloc")
if malloc == nil {
return 0, errors.New("malloc is not exported")
}
l := uint64(len(data))
if l == 0 {
return 0, nil
}
results, err := malloc.Call(ctx, l)
if err != nil {
return 0, err
}
dataPtr := results[0]
// The pointer is a linear memory offset, which is where we write the name.
if !m.Memory().Write(uint32(dataPtr), data) {
return 0, fmt.Errorf("Memory.Write(%d, %d) out of range of memory size %d",
dataPtr, len(data), m.Memory().Size())
}
return dataPtr, nil
}