4094accd8d07ff0b7ee394814ca8bdd429986020
[platform/framework/web/lwnode.git] /
1 #include <iostream>
2 #include <fstream>
3 #include <cstdlib>
4 #include <string>
5 #include <cinttypes>
6
7 #include "wasm.hh"
8
9
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;
13     exit(1);
14   }
15   return exports[i]->memory();
16 }
17
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;
21     exit(1);
22   }
23   return exports[i]->func();
24 }
25
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;
30     exit(1);
31   }
32 }
33
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;
39     exit(1);
40   }
41 }
42
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;
48     exit(1);
49   }
50 }
51
52 template<class... Args>
53 auto call(const wasm::Func* func, Args... xs) -> int32_t {
54   wasm::Val args[] = {wasm::Val::i32(xs)...};
55   wasm::Val results[1];
56   if (func->call(args, results)) {
57     std::cout << "> Error on result, expected return" << std::endl;
58     exit(1);
59   }
60   return results[0].i32();
61 }
62
63
64 void run() {
65   // Initialize.
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();
70
71   // Load binary.
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();
76   file.seekg(0);
77   auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
78   file.read(binary.get(), file_size);
79   file.close();
80   if (file.fail()) {
81     std::cout << "> Error loading module!" << std::endl;
82     exit(1);
83   }
84
85   // Compile.
86   std::cout << "Compiling module..." << std::endl;
87   auto module = wasm::Module::make(store, binary);
88   if (!module) {
89     std::cout << "> Error compiling module!" << std::endl;
90     exit(1);
91   }
92
93   // Instantiate.
94   std::cout << "Instantiating module..." << std::endl;
95   auto instance = wasm::Instance::make(store, module.get(), nullptr);
96   if (!instance) {
97     std::cout << "> Error instantiating module!" << std::endl;
98     exit(1);
99   }
100
101   // Extract export.
102   std::cout << "Extracting exports..." << std::endl;
103   auto exports = instance->exports();
104   size_t i = 0;
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++);
109
110   // Try cloning.
111   assert(memory->copy()->same(memory));
112
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);
120
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);
127
128   // Mutate memory.
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);
133
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);
138
139   // Grow memory.
140   std::cout << "Growing memory..." << std::endl;
141   check(memory->grow(1), true);
142   check(memory->size(), 3u);
143   check(memory->data_size(), 0x30000u);
144
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);
149
150   check(memory->grow(1), false);
151   check(memory->grow(0), true);
152
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);
161
162   // Shut down.
163   std::cout << "Shutting down..." << std::endl;
164 }
165
166
167 int main(int argc, const char* argv[]) {
168   run();
169   std::cout << "Done." << std::endl;
170   return 0;
171 }
172