1*13ebd6f5SMangoPeachGrape #include <fstream>
2*13ebd6f5SMangoPeachGrape #include <iostream>
3*13ebd6f5SMangoPeachGrape #include <sstream>
4*13ebd6f5SMangoPeachGrape #include <wasmtime.hh>
5*13ebd6f5SMangoPeachGrape
6*13ebd6f5SMangoPeachGrape using namespace wasmtime;
7*13ebd6f5SMangoPeachGrape
unwrap(Result<T,E> result)8*13ebd6f5SMangoPeachGrape template <typename T, typename E> T unwrap(Result<T, E> result) {
9*13ebd6f5SMangoPeachGrape if (result) {
10*13ebd6f5SMangoPeachGrape return result.ok();
11*13ebd6f5SMangoPeachGrape }
12*13ebd6f5SMangoPeachGrape std::cerr << "error: " << result.err().message() << "\n";
13*13ebd6f5SMangoPeachGrape std::abort();
14*13ebd6f5SMangoPeachGrape }
15*13ebd6f5SMangoPeachGrape
readFile(const char * name)16*13ebd6f5SMangoPeachGrape std::string readFile(const char *name) {
17*13ebd6f5SMangoPeachGrape std::ifstream watFile;
18*13ebd6f5SMangoPeachGrape watFile.open(name);
19*13ebd6f5SMangoPeachGrape std::stringstream strStream;
20*13ebd6f5SMangoPeachGrape strStream << watFile.rdbuf();
21*13ebd6f5SMangoPeachGrape return strStream.str();
22*13ebd6f5SMangoPeachGrape }
23*13ebd6f5SMangoPeachGrape
main()24*13ebd6f5SMangoPeachGrape int main() {
25*13ebd6f5SMangoPeachGrape Engine engine;
26*13ebd6f5SMangoPeachGrape Store store(engine);
27*13ebd6f5SMangoPeachGrape
28*13ebd6f5SMangoPeachGrape // Read our input `*.wat` files into `std::string`s
29*13ebd6f5SMangoPeachGrape std::string linking1_wat = readFile("examples/linking1.wat");
30*13ebd6f5SMangoPeachGrape std::string linking2_wat = readFile("examples/linking2.wat");
31*13ebd6f5SMangoPeachGrape
32*13ebd6f5SMangoPeachGrape // Compile our two modules
33*13ebd6f5SMangoPeachGrape Module linking1_module = Module::compile(engine, linking1_wat).unwrap();
34*13ebd6f5SMangoPeachGrape Module linking2_module = Module::compile(engine, linking2_wat).unwrap();
35*13ebd6f5SMangoPeachGrape
36*13ebd6f5SMangoPeachGrape // Configure WASI and store it within our `wasmtime_store_t`
37*13ebd6f5SMangoPeachGrape WasiConfig wasi;
38*13ebd6f5SMangoPeachGrape wasi.inherit_argv();
39*13ebd6f5SMangoPeachGrape wasi.inherit_env();
40*13ebd6f5SMangoPeachGrape wasi.inherit_stdin();
41*13ebd6f5SMangoPeachGrape wasi.inherit_stdout();
42*13ebd6f5SMangoPeachGrape wasi.inherit_stderr();
43*13ebd6f5SMangoPeachGrape store.context().set_wasi(std::move(wasi)).unwrap();
44*13ebd6f5SMangoPeachGrape
45*13ebd6f5SMangoPeachGrape // Create our linker which will be linking our modules together, and then add
46*13ebd6f5SMangoPeachGrape // our WASI instance to it.
47*13ebd6f5SMangoPeachGrape Linker linker(engine);
48*13ebd6f5SMangoPeachGrape linker.define_wasi().unwrap();
49*13ebd6f5SMangoPeachGrape
50*13ebd6f5SMangoPeachGrape // Instantiate our first module which only uses WASI, then register that
51*13ebd6f5SMangoPeachGrape // instance with the linker since the next linking will use it.
52*13ebd6f5SMangoPeachGrape Instance linking2 = linker.instantiate(store, linking2_module).unwrap();
53*13ebd6f5SMangoPeachGrape linker.define_instance(store, "linking2", linking2).unwrap();
54*13ebd6f5SMangoPeachGrape
55*13ebd6f5SMangoPeachGrape // And with that we can perform the final link and the execute the module.
56*13ebd6f5SMangoPeachGrape Instance linking1 = linker.instantiate(store, linking1_module).unwrap();
57*13ebd6f5SMangoPeachGrape Func f = std::get<Func>(*linking1.get(store, "run"));
58*13ebd6f5SMangoPeachGrape f.call(store, {}).unwrap();
59*13ebd6f5SMangoPeachGrape }
60