-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmain.cc
More file actions
65 lines (51 loc) · 1.82 KB
/
main.cc
File metadata and controls
65 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Example of instantiating a wasm module which uses WASI imports.
You can build the example using CMake:
mkdir build && (cd build && cmake .. && \
cmake --build . --target wasmtime-wasip1-cpp)
And then run it:
build/wasmtime-wasip1-cpp
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <wasmtime.hh>
using namespace wasmtime;
static std::vector<uint8_t> read_binary_file(const char *path) {
std::ifstream file(path, std::ios::in | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error(std::string("failed to open wasm file: ") + path);
}
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return data;
}
int main() {
// Define the WASI functions globally on the `Config`.
Engine engine;
Linker linker(engine);
linker.define_wasi().unwrap();
// Create a WASI context and put it in a Store; all instances in the store
// share this context. `WasiConfig` provides a number of ways to
// configure what the target program will have access to.
WasiConfig wasi;
wasi.inherit_argv();
wasi.inherit_stdin();
wasi.inherit_stdout();
wasi.inherit_stderr();
Store store(engine);
store.context().set_wasi(std::move(wasi)).unwrap();
// Load and compile the wasm module.
auto bytes = read_binary_file("target/wasm32-wasip1/debug/wasi.wasm");
auto module =
Module::compile(engine, Span<uint8_t>(bytes.data(), bytes.size()))
.unwrap();
// Define the module in the linker (anonymous name matches Rust example
// usage).
linker.module(store.context(), "", module).unwrap();
// Get the default export (command entrypoint) and invoke it.
Func default_func = linker.get_default(store.context(), "").unwrap();
default_func.call(store, {}).unwrap();
return 0;
}