MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / run

Function run

example/table.cc:72–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70}
71
72void run() {
73 // Initialize.
74 std::cout << "Initializing..." << std::endl;
75 auto engine = wasm::Engine::make();
76 auto store_ = wasm::Store::make(engine.get());
77 auto store = store_.get();
78
79 // Load binary.
80 std::cout << "Loading binary..." << std::endl;
81 std::ifstream file("table.wasm");
82 file.seekg(0, std::ios_base::end);
83 auto file_size = file.tellg();
84 file.seekg(0);
85 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
86 file.read(binary.get(), file_size);
87 file.close();
88 if (file.fail()) {
89 std::cout << "> Error loading module!" << std::endl;
90 exit(1);
91 }
92
93 // Compile.
94 std::cout << "Compiling module..." << std::endl;
95 auto module = wasm::Module::make(store, binary);
96 if (!module) {
97 std::cout << "> Error compiling module!" << std::endl;
98 exit(1);
99 }
100
101 // Instantiate.
102 std::cout << "Instantiating module..." << std::endl;
103 auto imports = wasm::vec<wasm::Extern*>::make();
104 auto instance = wasm::Instance::make(store, module.get(), imports);
105 if (!instance) {
106 std::cout << "> Error instantiating module!" << std::endl;
107 exit(1);
108 }
109
110 // Extract export.
111 std::cout << "Extracting exports..." << std::endl;
112 auto exports = instance->exports();
113 size_t i = 0;
114 auto table = get_export_table(exports, i++);
115 auto call_indirect = get_export_func(exports, i++);
116 auto f = get_export_func(exports, i++);
117 auto g = get_export_func(exports, i++);
118
119 // Create external function.
120 std::cout << "Creating callback..." << std::endl;
121 auto neg_type = wasm::FuncType::make(
122 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32)),
123 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
124 );
125 auto h = wasm::Func::make(store, neg_type.get(), neg_callback);
126
127 // Try cloning.
128 assert(table->copy()->same(table));
129

Callers 1

mainFunction · 0.70

Calls 14

LimitsClass · 0.85
exportsMethod · 0.80
sameMethod · 0.80
i32Method · 0.80
setMethod · 0.80
growMethod · 0.80
get_export_tableFunction · 0.70
get_export_funcFunction · 0.70
checkFunction · 0.70
check_trapFunction · 0.70
callFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected