10 auto operator<<(std::ostream& out, wasm::Mutability mut) -> std::ostream& {
12 case wasm::VAR: return out << "var";
13 case wasm::CONST: return out << "const";
18 auto operator<<(std::ostream& out, wasm::Limits limits) -> std::ostream& {
20 if (limits.max < wasm::Limits(0).max) out << " " << limits.max;
24 auto operator<<(std::ostream& out, const wasm::ValType& type) -> std::ostream& {
25 switch (type.kind()) {
26 case wasm::I32: return out << "i32";
27 case wasm::I64: return out << "i64";
28 case wasm::F32: return out << "f32";
29 case wasm::F64: return out << "f64";
30 case wasm::ANYREF: return out << "anyref";
31 case wasm::FUNCREF: return out << "funcref";
36 auto operator<<(std::ostream& out, const wasm::ownvec<wasm::ValType>& types) -> std::ostream& {
38 for (size_t i = 0; i < types.size(); ++i) {
44 out << *types[i].get();
49 auto operator<<(std::ostream& out, const wasm::ExternType& type) -> std::ostream& {
50 switch (type.kind()) {
51 case wasm::EXTERN_FUNC: {
52 out << "func " << type.func()->params() << " -> " << type.func()->results();
54 case wasm::EXTERN_GLOBAL: {
55 out << "global " << type.global()->mutability() << " " << *type.global()->content();
57 case wasm::EXTERN_TABLE: {
58 out << "table " << type.table()->limits() << " " << *type.table()->element();
60 case wasm::EXTERN_MEMORY: {
61 out << "memory " << type.memory()->limits();
67 auto operator<<(std::ostream& out, const wasm::Name& name) -> std::ostream& {
68 out << "\"" << std::string(name.get(), name.size()) << "\"";
75 std::cout << "Initializing..." << std::endl;
76 auto engine = wasm::Engine::make();
77 auto store_ = wasm::Store::make(engine.get());
78 auto store = store_.get();
81 std::cout << "Loading binary..." << std::endl;
82 std::ifstream file("reflect.wasm");
83 file.seekg(0, std::ios_base::end);
84 auto file_size = file.tellg();
86 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
87 file.read(binary.get(), file_size);
90 std::cout << "> Error loading module!" << std::endl;
95 std::cout << "Compiling module..." << std::endl;
96 auto module = wasm::Module::make(store, binary);
98 std::cout << "> Error compiling module!" << std::endl;
103 std::cout << "Instantiating module..." << std::endl;
104 auto instance = wasm::Instance::make(store, module.get(), nullptr);
106 std::cout << "> Error instantiating module!" << std::endl;
111 std::cout << "Extracting export..." << std::endl;
112 auto export_types = module->exports();
113 auto exports = instance->exports();
114 assert(exports.size() == export_types.size());
116 for (size_t i = 0; i < exports.size(); ++i) {
117 assert(exports[i]->kind() == export_types[i]->type()->kind());
118 std::cout << "> export " << i << " " << export_types[i]->name() << std::endl;
119 std::cout << ">> initial: " << *export_types[i]->type() << std::endl;
120 std::cout << ">> current: " << *exports[i]->type() << std::endl;
121 if (exports[i]->kind() == wasm::EXTERN_FUNC) {
122 auto func = exports[i]->func();
123 std::cout << ">> in-arity: " << func->param_arity();
124 std::cout << ", out-arity: " << func->result_arity() << std::endl;
129 std::cout << "Shutting down..." << std::endl;
133 int main(int argc, const char* argv[]) {
135 std::cout << "Done." << std::endl;