xref: /wasmtime-44.0.1/examples/gcd.cc (revision 13ebd6f5)
1 #include <fstream>
2 #include <iostream>
3 #include <sstream>
4 #include <wasmtime.hh>
5 
6 using namespace wasmtime;
7 
readFile(const char * name)8 std::string readFile(const char *name) {
9   std::ifstream watFile;
10   watFile.open(name);
11   std::stringstream strStream;
12   strStream << watFile.rdbuf();
13   return strStream.str();
14 }
15 
main()16 int main() {
17   // Load our WebAssembly (parsed WAT in our case), and then load it into a
18   // `Module` which is attached to a `Store`. After we've got that we
19   // can instantiate it.
20   Engine engine;
21   Store store(engine);
22   auto module = Module::compile(engine, readFile("examples/gcd.wat")).unwrap();
23   auto instance = Instance::create(store, module, {}).unwrap();
24 
25   // Invoke `gcd` export
26   auto gcd = std::get<Func>(*instance.get(store, "gcd"));
27   auto results = gcd.call(store, {6, 27}).unwrap();
28 
29   std::cout << "gcd(6, 27) = " << results[0].i32() << "\n";
30 }
31