#include <fstream>
#include <iostream>
#include <sstream>
using namespace wasmtime;
template <
typename T,
typename E> T unwrap(
Result<T, E> result) {
if (result) {
}
std::cerr <<
"error: " << result.
err().message() <<
"\n";
std::abort();
}
std::string readFile(const char *name) {
std::ifstream watFile;
watFile.open(name);
std::stringstream strStream;
strStream << watFile.rdbuf();
return strStream.str();
}
int main() {
std::string linking1_wat = readFile("examples/linking1.wat");
std::string linking2_wat = readFile("examples/linking2.wat");
Module linking1_module = Module::compile(engine, linking1_wat).unwrap();
Module linking2_module = Module::compile(engine, linking2_wat).unwrap();
store.context().set_wasi(std::move(wasi)).unwrap();
linker.define_wasi().unwrap();
Instance linking2 = linker.instantiate(store, linking2_module).unwrap();
linker.define_instance(store, "linking2", linking2).unwrap();
Instance linking1 = linker.instantiate(store, linking1_module).unwrap();
Func f = std::get<Func>(*linking1.
get(store,
"run"));
f.
call(store, {}).unwrap();
}
Global compilation state in Wasmtime.
Definition: wasmtime.hh:645
Representation of a WebAssembly function.
Definition: wasmtime.hh:2505
TrapResult< std::vector< Val > > call(Store::Context cx, const I &begin, const I &end) const
Invoke a WebAssembly function.
Definition: wasmtime.hh:2670
A WebAssembly instance.
Definition: wasmtime.hh:3074
std::optional< Extern > get(Store::Context cx, std::string_view name)
Load an instance's export by name.
Definition: wasmtime.hh:3159
Helper class for linking modules together with name-based resolution.
Definition: wasmtime.hh:3206
Representation of a compiled WebAssembly module.
Definition: wasmtime.hh:1559
Fallible result type used for Wasmtime.
Definition: wasmtime.hh:194
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: wasmtime.hh:208
T && ok()
Returns the success, if present, aborts if this is an error.
Definition: wasmtime.hh:213
Owner of all WebAssembly objects.
Definition: wasmtime.hh:1820
Configuration for an instance of WASI.
Definition: wasmtime.hh:1721
void inherit_stderr()
Definition: wasmtime.hh:1794
void inherit_env()
Definition: wasmtime.hh:1764
void inherit_stdout()
Definition: wasmtime.hh:1784
void inherit_argv()
Configures the argv for wasm to be inherited from this process itself.
Definition: wasmtime.hh:1746
void inherit_stdin()
Definition: wasmtime.hh:1774