71d6fd25add82034df593202e39bcb55369966d3
[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 void print_frame(const wasm::Frame* frame) {
11   std::cout << "> " << frame->instance();
12   std::cout << " @ 0x" << std::hex << frame->module_offset();
13   std::cout << " = " << frame->func_index();
14   std::cout << ".0x" << std::hex << frame->func_offset() << std::endl;
15 }
16
17
18 void run() {
19   // Initialize.
20   std::cout << "Initializing..." << std::endl;
21   auto engine = wasm::Engine::make();
22   auto store_ = wasm::Store::make(engine.get());
23   auto store = store_.get();
24
25   // Load binary.
26   std::cout << "Loading binary..." << std::endl;
27   std::ifstream file("start.wasm");
28   file.seekg(0, std::ios_base::end);
29   auto file_size = file.tellg();
30   file.seekg(0);
31   auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
32   file.read(binary.get(), file_size);
33   file.close();
34   if (file.fail()) {
35     std::cout << "> Error loading module!" << std::endl;
36     exit(1);
37   }
38
39   // Compile.
40   std::cout << "Compiling module..." << std::endl;
41   auto module = wasm::Module::make(store, binary);
42   if (!module) {
43     std::cout << "> Error compiling module!" << std::endl;
44     exit(1);
45   }
46
47   // Instantiate.
48   std::cout << "Instantiating module..." << std::endl;
49   wasm::own<wasm::Trap> trap;
50   auto instance = wasm::Instance::make(store, module.get(), nullptr, &trap);
51   if (instance || !trap) {
52     std::cout << "> Error instantiating module, expected trap!" << std::endl;
53     exit(1);
54   }
55
56   // Print result.
57   std::cout << "Printing message..." << std::endl;
58   std::cout << "> " << trap->message().get() << std::endl;
59
60   std::cout << "Printing origin..." << std::endl;
61   auto frame = trap->origin();
62   if (frame) {
63     print_frame(frame.get());
64   } else {
65     std::cout << "> Empty origin." << std::endl;
66   }
67
68   std::cout << "Printing trace..." << std::endl;
69   auto trace = trap->trace();
70   if (trace.size() > 0) {
71     for (size_t i = 0; i < trace.size(); ++i) {
72       print_frame(trace[i].get());
73     }
74   } else {
75     std::cout << "> Empty trace." << std::endl;
76   }
77
78   // Shut down.
79   std::cout << "Shutting down..." << std::endl;
80 }
81
82
83 int main(int argc, const char* argv[]) {
84   run();
85   std::cout << "Done." << std::endl;
86   return 0;
87 }
88