|
|
|
@ -21,14 +21,14 @@ int main(void)
|
|
|
|
|
|
|
|
|
|
/* Wasm module can be loaded from a file */
|
|
|
|
|
try {
|
|
|
|
|
wasm3::environment env;
|
|
|
|
|
wasm3::runtime runtime = env.new_runtime(1024);
|
|
|
|
|
wasm3::wasm_environment env;
|
|
|
|
|
wasm3::wasm_runtime runtime = env.new_runtime(1024);
|
|
|
|
|
const char* file_name = "wasm/test_prog.wasm";
|
|
|
|
|
std::ifstream wasm_file(file_name, std::ios::binary | std::ios::in);
|
|
|
|
|
if (!wasm_file.is_open()) {
|
|
|
|
|
throw std::runtime_error("Failed to open wasm file");
|
|
|
|
|
}
|
|
|
|
|
wasm3::module mod = env.parse_module(wasm_file);
|
|
|
|
|
wasm3::wasm_module mod = env.parse_module(wasm_file);
|
|
|
|
|
runtime.load(mod);
|
|
|
|
|
}
|
|
|
|
|
catch(std::runtime_error &e) {
|
|
|
|
@ -38,21 +38,21 @@ int main(void)
|
|
|
|
|
|
|
|
|
|
/* Wasm module can also be loaded from an array */
|
|
|
|
|
try {
|
|
|
|
|
wasm3::environment env;
|
|
|
|
|
wasm3::runtime runtime = env.new_runtime(1024);
|
|
|
|
|
wasm3::module mod = env.parse_module(test_prog_wasm, test_prog_wasm_len);
|
|
|
|
|
wasm3::wasm_environment env;
|
|
|
|
|
wasm3::wasm_runtime runtime = env.new_runtime(1024);
|
|
|
|
|
wasm3::wasm_module mod = env.parse_module(test_prog_wasm, test_prog_wasm_len);
|
|
|
|
|
runtime.load(mod);
|
|
|
|
|
|
|
|
|
|
mod.link("*", "sum", sum);
|
|
|
|
|
mod.link("*", "ext_memcpy", ext_memcpy);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
wasm3::function test_fn = runtime.find_function("test");
|
|
|
|
|
wasm3::wasm_function test_fn = runtime.find_function("test");
|
|
|
|
|
auto res = test_fn.call<int>(20, 10);
|
|
|
|
|
std::cout << "result: " << res << std::endl;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
wasm3::function memcpy_test_fn = runtime.find_function("test_memcpy");
|
|
|
|
|
wasm3::wasm_function memcpy_test_fn = runtime.find_function("test_memcpy");
|
|
|
|
|
auto res = memcpy_test_fn.call<int64_t>();
|
|
|
|
|
std::cout << "result: 0x" << std::hex << res << std::dec << std::endl;
|
|
|
|
|
}
|
|
|
|
@ -61,9 +61,9 @@ int main(void)
|
|
|
|
|
* Calling functions that modify an internal state, with mixed argument / return types
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
wasm3::function counter_get_fn = runtime.find_function("test_counter_get");
|
|
|
|
|
wasm3::function counter_inc_fn = runtime.find_function("test_counter_inc");
|
|
|
|
|
wasm3::function counter_add_fn = runtime.find_function("test_counter_add");
|
|
|
|
|
wasm3::wasm_function counter_get_fn = runtime.find_function("test_counter_get");
|
|
|
|
|
wasm3::wasm_function counter_inc_fn = runtime.find_function("test_counter_inc");
|
|
|
|
|
wasm3::wasm_function counter_add_fn = runtime.find_function("test_counter_add");
|
|
|
|
|
|
|
|
|
|
// call with no arguments and a return value
|
|
|
|
|
auto value = counter_get_fn.call<int32_t>();
|
|
|
|
|