This repository was archived by the owner on Apr 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmemory.cc
79 lines (65 loc) · 2.54 KB
/
memory.cc
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
#undef NDEBUG
#include <fstream>
#include <iostream>
#include <sstream>
#include <wasmtime.hh>
using namespace wasmtime;
std::string readFile(const char *name) {
std::ifstream watFile;
watFile.open(name);
std::stringstream strStream;
strStream << watFile.rdbuf();
return strStream.str();
}
int main() {
// Create our `store` context and then compile a module and create an
// instance from the compiled module all in one go.
Engine engine;
Module module =
Module::compile(engine, readFile("examples/memory.wat")).unwrap();
Store store(engine);
Instance instance = Instance::create(store, module, {}).unwrap();
// load_fn up our exports from the instance
auto memory = std::get<Memory>(*instance.get(store, "memory"));
auto size = std::get<Func>(*instance.get(store, "size"));
auto load_fn = std::get<Func>(*instance.get(store, "load"));
auto store_fn = std::get<Func>(*instance.get(store, "store"));
std::cout << "Checking memory...\n";
assert(memory.size(store) == 2);
auto data = memory.data(store);
assert(data.size() == 0x20000);
assert(data[0] == 0);
assert(data[0x1000] == 1);
assert(data[0x1003] == 4);
assert(size.call(store, {}).unwrap()[0].i32() == 2);
assert(load_fn.call(store, {0}).unwrap()[0].i32() == 0);
assert(load_fn.call(store, {0x1000}).unwrap()[0].i32() == 1);
assert(load_fn.call(store, {0x1003}).unwrap()[0].i32() == 4);
assert(load_fn.call(store, {0x1ffff}).unwrap()[0].i32() == 0);
load_fn.call(store, {0x20000}).err(); // out of bounds trap
std::cout << "Mutating memory...\n";
memory.data(store)[0x1003] = 5;
store_fn.call(store, {0x1002, 6}).unwrap();
store_fn.call(store, {0x20000, 0}).err(); // out of bounds trap
assert(memory.data(store)[0x1002] == 6);
assert(memory.data(store)[0x1003] == 5);
assert(load_fn.call(store, {0x1002}).unwrap()[0].i32() == 6);
assert(load_fn.call(store, {0x1003}).unwrap()[0].i32() == 5);
// Grow memory.
std::cout << "Growing memory...\n";
memory.grow(store, 1).unwrap();
assert(memory.size(store) == 3);
assert(memory.data(store).size() == 0x30000);
assert(load_fn.call(store, {0x20000}).unwrap()[0].i32() == 0);
store_fn.call(store, {0x20000, 0}).unwrap();
load_fn.call(store, {0x30000}).err();
store_fn.call(store, {0x30000, 0}).err();
memory.grow(store, 1).err();
memory.grow(store, 0).ok();
std::cout << "Creating stand-alone memory...\n";
MemoryType ty(5, 5);
Memory memory2 = Memory::create(store, ty).unwrap();
assert(memory2.size(store) == 5);
memory2.grow(store, 1).err();
memory2.grow(store, 0).ok();
}