#include <fstream>1#include <iostream>2#include <sstream>3#include <wasmtime.hh>45using namespace wasmtime;67std::string readFile(const char *name) {8std::ifstream watFile;9watFile.open(name);10std::stringstream strStream;11strStream << watFile.rdbuf();12return strStream.str();13}1415int main() {16// First the wasm module needs to be compiled. This is done with a global17// "compilation environment" within an `Engine`. Note that engines can be18// further configured through `Config` if desired instead of using the19// default like this is here.20std::cout << "Compiling module\n";21Engine engine;22auto module =23Module::compile(engine, readFile("examples/hello.wat")).unwrap();2425// After a module is compiled we create a `Store` which will contain26// instantiated modules and other items like host functions. A Store27// contains an arbitrary piece of host information, and we use `MyState`28// here.29std::cout << "Initializing...\n";30Store store(engine);3132// Our wasm module we'll be instantiating requires one imported function.33// the function takes no parameters and returns no results. We create a host34// implementation of that function here.35std::cout << "Creating callback...\n";36Func host_func =37Func::wrap(store, []() { std::cout << "Calling back...\n"; });3839// Once we've got that all set up we can then move to the instantiation40// phase, pairing together a compiled module as well as a set of imports.41// Note that this is where the wasm `start` function, if any, would run.42std::cout << "Instantiating module...\n";43auto instance = Instance::create(store, module, {host_func}).unwrap();4445// Next we poke around a bit to extract the `run` function from the module.46std::cout << "Extracting export...\n";47auto run = std::get<Func>(*instance.get(store, "run"));4849// And last but not least we can call it!50std::cout << "Calling export...\n";51run.call(store, {}).unwrap();5253std::cout << "Done\n";54}555657