10 auto get_export_memory(wasm::ownvec<wasm::Extern>& exports, size_t i) -> wasm::Memory* {
11 if (exports.size() <= i || !exports[i]->memory()) {
12 std::cout << "> Error accessing memory export " << i << "!" << std::endl;
15 return exports[i]->memory();
18 auto get_export_func(const wasm::ownvec<wasm::Extern>& exports, size_t i) -> const wasm::Func* {
19 if (exports.size() <= i || !exports[i]->func()) {
20 std::cout << "> Error accessing function export " << i << "!" << std::endl;
23 return exports[i]->func();
26 template<class T, class U>
27 void check(T actual, U expected) {
28 if (actual != expected) {
29 std::cout << "> Error on result, expected " << expected << ", got " << actual << std::endl;
34 template<class... Args>
35 void check_ok(const wasm::Func* func, Args... xs) {
36 wasm::Val args[] = {wasm::Val::i32(xs)...};
37 if (func->call(args)) {
38 std::cout << "> Error on result, expected return" << std::endl;
43 template<class... Args>
44 void check_trap(const wasm::Func* func, Args... xs) {
45 wasm::Val args[] = {wasm::Val::i32(xs)...};
46 if (! func->call(args)) {
47 std::cout << "> Error on result, expected trap" << std::endl;
52 template<class... Args>
53 auto call(const wasm::Func* func, Args... xs) -> int32_t {
54 wasm::Val args[] = {wasm::Val::i32(xs)...};
56 if (func->call(args, results)) {
57 std::cout << "> Error on result, expected return" << std::endl;
60 return results[0].i32();
66 std::cout << "Initializing..." << std::endl;
67 auto engine = wasm::Engine::make();
68 auto store_ = wasm::Store::make(engine.get());
69 auto store = store_.get();
72 std::cout << "Loading binary..." << std::endl;
73 std::ifstream file("memory.wasm");
74 file.seekg(0, std::ios_base::end);
75 auto file_size = file.tellg();
77 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
78 file.read(binary.get(), file_size);
81 std::cout << "> Error loading module!" << std::endl;
86 std::cout << "Compiling module..." << std::endl;
87 auto module = wasm::Module::make(store, binary);
89 std::cout << "> Error compiling module!" << std::endl;
94 std::cout << "Instantiating module..." << std::endl;
95 auto instance = wasm::Instance::make(store, module.get(), nullptr);
97 std::cout << "> Error instantiating module!" << std::endl;
102 std::cout << "Extracting exports..." << std::endl;
103 auto exports = instance->exports();
105 auto memory = get_export_memory(exports, i++);
106 auto size_func = get_export_func(exports, i++);
107 auto load_func = get_export_func(exports, i++);
108 auto store_func = get_export_func(exports, i++);
111 assert(memory->copy()->same(memory));
113 // Check initial memory.
114 std::cout << "Checking memory..." << std::endl;
115 check(memory->size(), 2u);
116 check(memory->data_size(), 0x20000u);
117 check(memory->data()[0], 0);
118 check(memory->data()[0x1000], 1);
119 check(memory->data()[0x1003], 4);
121 check(call(size_func), 2);
122 check(call(load_func, 0), 0);
123 check(call(load_func, 0x1000), 1);
124 check(call(load_func, 0x1003), 4);
125 check(call(load_func, 0x1ffff), 0);
126 check_trap(load_func, 0x20000);
129 std::cout << "Mutating memory..." << std::endl;
130 memory->data()[0x1003] = 5;
131 check_ok(store_func, 0x1002, 6);
132 check_trap(store_func, 0x20000, 0);
134 check(memory->data()[0x1002], 6);
135 check(memory->data()[0x1003], 5);
136 check(call(load_func, 0x1002), 6);
137 check(call(load_func, 0x1003), 5);
140 std::cout << "Growing memory..." << std::endl;
141 check(memory->grow(1), true);
142 check(memory->size(), 3u);
143 check(memory->data_size(), 0x30000u);
145 check(call(load_func, 0x20000), 0);
146 check_ok(store_func, 0x20000, 0);
147 check_trap(load_func, 0x30000);
148 check_trap(store_func, 0x30000, 0);
150 check(memory->grow(1), false);
151 check(memory->grow(0), true);
153 // Create stand-alone memory.
154 // TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
155 std::cout << "Creating stand-alone memory..." << std::endl;
156 auto memorytype = wasm::MemoryType::make(wasm::Limits(5, 5));
157 auto memory2 = wasm::Memory::make(store, memorytype.get());
158 check(memory2->size(), 5u);
159 check(memory2->grow(1), false);
160 check(memory2->grow(0), true);
163 std::cout << "Shutting down..." << std::endl;
167 int main(int argc, const char* argv[]) {
169 std::cout << "Done." << std::endl;