1 #include <iostream>
2 #include <fstream>
3 #include <cstdlib>
4 #include <string>
5 #include <cinttypes>
6
7 #include "wasm.hh"
8
9
10 // A function to be called from Wasm code.
hello_callback(const wasm::Val args[],wasm::Val results[])11 auto hello_callback(
12 const wasm::Val args[], wasm::Val results[]
13 ) -> wasm::own<wasm::Trap> {
14 std::cout << "Calling back..." << std::endl;
15 std::cout << "> Hello world!" << std::endl;
16 return nullptr;
17 }
18
19
run()20 void run() {
21 // Initialize.
22 std::cout << "Initializing..." << std::endl;
23 auto engine = wasm::Engine::make();
24 auto store_ = wasm::Store::make(engine.get());
25 auto store = store_.get();
26
27 // Load binary.
28 std::cout << "Loading binary..." << std::endl;
29 std::ifstream file("serialize.wasm");
30 file.seekg(0, std::ios_base::end);
31 auto file_size = file.tellg();
32 file.seekg(0);
33 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
34 file.read(binary.get(), file_size);
35 file.close();
36 if (file.fail()) {
37 std::cout << "> Error loading module!" << std::endl;
38 exit(1);
39 }
40
41 // Compile.
42 std::cout << "Compiling module..." << std::endl;
43 auto module = wasm::Module::make(store, binary);
44 if (!module) {
45 std::cout << "> Error compiling module!" << std::endl;
46 exit(1);
47 }
48
49 // Serialize module.
50 std::cout << "Serializing module..." << std::endl;
51 auto serialized = module->serialize();
52
53 // Deserialize module.
54 std::cout << "Deserializing module..." << std::endl;
55 auto deserialized = wasm::Module::deserialize(store, serialized);
56 if (!deserialized) {
57 std::cout << "> Error deserializing module!" << std::endl;
58 exit(1);
59 }
60
61 // Create external print functions.
62 std::cout << "Creating callback..." << std::endl;
63 auto hello_type = wasm::FuncType::make(
64 wasm::ownvec<wasm::ValType>::make(), wasm::ownvec<wasm::ValType>::make()
65 );
66 auto hello_func = wasm::Func::make(store, hello_type.get(), hello_callback);
67
68 // Instantiate.
69 std::cout << "Instantiating deserialized module..." << std::endl;
70 wasm::Extern* imports[] = {hello_func.get()};
71 auto instance = wasm::Instance::make(store, deserialized.get(), imports);
72 if (!instance) {
73 std::cout << "> Error instantiating module!" << std::endl;
74 exit(1);
75 }
76
77 // Extract export.
78 std::cout << "Extracting export..." << std::endl;
79 auto exports = instance->exports();
80 if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
81 std::cout << "> Error accessing export!" << std::endl;
82 exit(1);
83 }
84 auto run_func = exports[0]->func();
85
86 // Call.
87 std::cout << "Calling export..." << std::endl;
88 if (run_func->call()) {
89 std::cout << "> Error calling function!" << std::endl;
90 exit(1);
91 }
92
93 // Shut down.
94 std::cout << "Shutting down..." << std::endl;
95 }
96
97
main(int argc,const char * argv[])98 int main(int argc, const char* argv[]) {
99 run();
100 std::cout << "Done." << std::endl;
101 return 0;
102 }
103
104