10 // A function to be called from Wasm code.
12 const wasm::Val args[], wasm::Val results[]
13 ) -> wasm::own<wasm::Trap> {
14 std::cout << "Calling back..." << std::endl;
15 results[0] = wasm::Val(-args[0].i32());
20 auto get_export_table(wasm::ownvec<wasm::Extern>& exports, size_t i) -> wasm::Table* {
21 if (exports.size() <= i || !exports[i]->table()) {
22 std::cout << "> Error accessing table export " << i << "!" << std::endl;
25 return exports[i]->table();
28 auto get_export_func(const wasm::ownvec<wasm::Extern>& exports, size_t i) -> const wasm::Func* {
29 if (exports.size() <= i || !exports[i]->func()) {
30 std::cout << "> Error accessing function export " << i << "!" << std::endl;
33 return exports[i]->func();
36 template<class T, class U>
37 void check(T actual, U expected) {
38 if (actual != expected) {
39 std::cout << "> Error on result, expected " << expected << ", got " << actual << std::endl;
44 void check(bool success) {
46 std::cout << "> Error, expected success" << std::endl;
52 const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2
54 wasm::Val args[2] = {std::move(arg1), std::move(arg2)};
56 if (func->call(args, results)) {
57 std::cout << "> Error on result, expected return" << std::endl;
60 return results[0].copy();
63 void check_trap(const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2) {
64 wasm::Val args[2] = {std::move(arg1), std::move(arg2)};
66 if (! func->call(args, results)) {
67 std::cout << "> Error on result, expected trap" << std::endl;
74 std::cout << "Initializing..." << std::endl;
75 auto engine = wasm::Engine::make();
76 auto store_ = wasm::Store::make(engine.get());
77 auto store = store_.get();
80 std::cout << "Loading binary..." << std::endl;
81 std::ifstream file("table.wasm");
82 file.seekg(0, std::ios_base::end);
83 auto file_size = file.tellg();
85 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
86 file.read(binary.get(), file_size);
89 std::cout << "> Error loading module!" << std::endl;
94 std::cout << "Compiling module..." << std::endl;
95 auto module = wasm::Module::make(store, binary);
97 std::cout << "> Error compiling module!" << std::endl;
102 std::cout << "Instantiating module..." << std::endl;
103 auto instance = wasm::Instance::make(store, module.get(), nullptr);
105 std::cout << "> Error instantiating module!" << std::endl;
110 std::cout << "Extracting exports..." << std::endl;
111 auto exports = instance->exports();
113 auto table = get_export_table(exports, i++);
114 auto call_indirect = get_export_func(exports, i++);
115 auto f = get_export_func(exports, i++);
116 auto g = get_export_func(exports, i++);
118 // Create external function.
119 std::cout << "Creating callback..." << std::endl;
120 auto neg_type = wasm::FuncType::make(
121 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)),
122 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
124 auto h = wasm::Func::make(store, neg_type.get(), neg_callback);
127 assert(table->copy()->same(table));
129 // Check initial table.
130 std::cout << "Checking table..." << std::endl;
131 check(table->size(), 2u);
132 check(table->get(0) == nullptr);
133 check(table->get(1) != nullptr);
134 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(0));
135 check(call(call_indirect, wasm::Val::i32(7), wasm::Val::i32(1)).i32(), 7);
136 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(2));
139 std::cout << "Mutating table..." << std::endl;
140 check(table->set(0, g));
141 check(table->set(1, nullptr));
142 check(! table->set(2, f));
143 check(table->get(0) != nullptr);
144 check(table->get(1) == nullptr);
145 check(call(call_indirect, wasm::Val::i32(7), wasm::Val::i32(0)).i32(), 666);
146 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(1));
147 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(2));
150 std::cout << "Growing table..." << std::endl;
151 check(table->grow(3));
152 check(table->size(), 5u);
153 check(table->set(2, f));
154 check(table->set(3, h.get()));
155 check(! table->set(5, nullptr));
156 check(table->get(2) != nullptr);
157 check(table->get(3) != nullptr);
158 check(table->get(4) == nullptr);
159 check(call(call_indirect, wasm::Val::i32(5), wasm::Val::i32(2)).i32(), 5);
160 check(call(call_indirect, wasm::Val::i32(6), wasm::Val::i32(3)).i32(), -6);
161 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(4));
162 check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(5));
164 check(table->grow(2, f));
165 check(table->size(), 7u);
166 check(table->get(5) != nullptr);
167 check(table->get(6) != nullptr);
169 check(! table->grow(5));
170 check(table->grow(3));
171 check(table->grow(0));
173 // Create stand-alone table.
174 // TODO(wasm+): Once Wasm allows multiple tables, turn this into import.
175 std::cout << "Creating stand-alone table..." << std::endl;
176 auto tabletype = wasm::TableType::make(
177 wasm::ValType::make(wasm::FUNCREF), wasm::Limits(5, 5));
178 auto table2 = wasm::Table::make(store, tabletype.get());
179 check(table2->size() == 5);
180 check(! table2->grow(1));
181 check(table2->grow(0));
184 std::cout << "Shutting down..." << std::endl;
188 int main(int argc, const char* argv[]) {
190 std::cout << "Done." << std::endl;