xref: /wasmtime-44.0.1/examples/hello.cc (revision a045eaa9)
113ebd6f5SMangoPeachGrape #include <fstream>
213ebd6f5SMangoPeachGrape #include <iostream>
313ebd6f5SMangoPeachGrape #include <sstream>
4*a045eaa9SAlex Crichton #include <wasmtime.hh>
513ebd6f5SMangoPeachGrape 
613ebd6f5SMangoPeachGrape using namespace wasmtime;
713ebd6f5SMangoPeachGrape 
readFile(const char * name)813ebd6f5SMangoPeachGrape std::string readFile(const char *name) {
913ebd6f5SMangoPeachGrape   std::ifstream watFile;
1013ebd6f5SMangoPeachGrape   watFile.open(name);
1113ebd6f5SMangoPeachGrape   std::stringstream strStream;
1213ebd6f5SMangoPeachGrape   strStream << watFile.rdbuf();
1313ebd6f5SMangoPeachGrape   return strStream.str();
1413ebd6f5SMangoPeachGrape }
1513ebd6f5SMangoPeachGrape 
main()1613ebd6f5SMangoPeachGrape int main() {
1713ebd6f5SMangoPeachGrape   // First the wasm module needs to be compiled. This is done with a global
1813ebd6f5SMangoPeachGrape   // "compilation environment" within an `Engine`. Note that engines can be
1913ebd6f5SMangoPeachGrape   // further configured through `Config` if desired instead of using the
2013ebd6f5SMangoPeachGrape   // default like this is here.
2113ebd6f5SMangoPeachGrape   std::cout << "Compiling module\n";
2213ebd6f5SMangoPeachGrape   Engine engine;
2313ebd6f5SMangoPeachGrape   auto module =
2413ebd6f5SMangoPeachGrape       Module::compile(engine, readFile("examples/hello.wat")).unwrap();
2513ebd6f5SMangoPeachGrape 
2613ebd6f5SMangoPeachGrape   // After a module is compiled we create a `Store` which will contain
2713ebd6f5SMangoPeachGrape   // instantiated modules and other items like host functions. A Store
2813ebd6f5SMangoPeachGrape   // contains an arbitrary piece of host information, and we use `MyState`
2913ebd6f5SMangoPeachGrape   // here.
3013ebd6f5SMangoPeachGrape   std::cout << "Initializing...\n";
3113ebd6f5SMangoPeachGrape   Store store(engine);
3213ebd6f5SMangoPeachGrape 
3313ebd6f5SMangoPeachGrape   // Our wasm module we'll be instantiating requires one imported function.
3413ebd6f5SMangoPeachGrape   // the function takes no parameters and returns no results. We create a host
3513ebd6f5SMangoPeachGrape   // implementation of that function here.
3613ebd6f5SMangoPeachGrape   std::cout << "Creating callback...\n";
3713ebd6f5SMangoPeachGrape   Func host_func =
3813ebd6f5SMangoPeachGrape       Func::wrap(store, []() { std::cout << "Calling back...\n"; });
3913ebd6f5SMangoPeachGrape 
4013ebd6f5SMangoPeachGrape   // Once we've got that all set up we can then move to the instantiation
4113ebd6f5SMangoPeachGrape   // phase, pairing together a compiled module as well as a set of imports.
4213ebd6f5SMangoPeachGrape   // Note that this is where the wasm `start` function, if any, would run.
4313ebd6f5SMangoPeachGrape   std::cout << "Instantiating module...\n";
4413ebd6f5SMangoPeachGrape   auto instance = Instance::create(store, module, {host_func}).unwrap();
4513ebd6f5SMangoPeachGrape 
4613ebd6f5SMangoPeachGrape   // Next we poke around a bit to extract the `run` function from the module.
4713ebd6f5SMangoPeachGrape   std::cout << "Extracting export...\n";
4813ebd6f5SMangoPeachGrape   auto run = std::get<Func>(*instance.get(store, "run"));
4913ebd6f5SMangoPeachGrape 
5013ebd6f5SMangoPeachGrape   // And last but not least we can call it!
5113ebd6f5SMangoPeachGrape   std::cout << "Calling export...\n";
5213ebd6f5SMangoPeachGrape   run.call(store, {}).unwrap();
5313ebd6f5SMangoPeachGrape 
5413ebd6f5SMangoPeachGrape   std::cout << "Done\n";
5513ebd6f5SMangoPeachGrape }
56