platforms/cpp: demonstrate loading module from file, ensure no `skipws` (#235)

* cpp: env.parse_module: ensure the file is opened without skipws

Closes https://github.com/wasm3/wasm3/issues/124

* cpp: demonstrate loading a module from file

* cpp: remove leftover note about function::call doing string conversion

* cpp: add missing return value checks for m3_GetResults
opam-2.0.0
Ivan Grokhotkov 3 years ago committed by GitHub
parent a6b61a8a71
commit c618f7b7d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,2 @@
build
cmake-build-debug
wasm/test_prog.wasm

@ -14,3 +14,12 @@ target_link_libraries(${target} PRIVATE wasm3_cpp)
target_compile_options(${target} PUBLIC -g)
target_compile_options(m3 PUBLIC -g)
# Copy the 'wasm' directory into the build directory, so that
# wasm/test_prog.wasm is found even if wasm3_cpp_example is executed
# from the build directory.
add_custom_target(copy_wasm ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_LIST_DIR}/wasm
${CMAKE_BINARY_DIR}/wasm
)
add_dependencies(${CMAKE_PROJECT_NAME} copy_wasm)

@ -54,7 +54,7 @@ If the module doesn't reference an imported function named `func`, an exception
`template <typename Ret> Ret function::call()` — call a WebAssembly function which doesn't take any arguments. The return value of the function is automatically converted to the type `Ret`. Note that you need to specify the return type when using this template function, and the type has to match the type returned by the WebAssembly function.
`template <typename Ret, typename ...Args> Ret function::call(Args...)` — same as above, but also allows passing arguments to the WebAssembly function. Note that due to a limitation of WASM3 API, the arguments are first converted to strings, and then passed to WASM3. The strings are then converted to the appropriate types based on the WebAssembly function signature. This conversion is limited to the following types: `int32_t`, `int64_t`, `float`, `double`.
`template <typename Ret, typename ...Args> Ret function::call(Args...)` — same as above, but also allows passing arguments to the WebAssembly function.
`template <typename Ret, typename ...Args> Ret function::call_argv(Args...)` — same as above, except that this function takes arguments as C strings (`const char*`).

@ -1,4 +1,5 @@
#include <cstdio>
#include <fstream>
#include "wasm3_cpp.h"
#include "wasm/test_prog.wasm.h"
@ -16,13 +17,35 @@ int main(void)
{
std::cout << "Loading WebAssembly..." << std::endl;
/* Wasm module can be loaded from a file */
try {
wasm3::environment env;
wasm3::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);
runtime.load(mod);
}
catch(std::runtime_error &e) {
std::cerr << "WASM3 error: " << e.what() << std::endl;
return 1;
}
/* 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);
runtime.load(mod);
/* link C++ functions "sum" and "ext_memcpy" to the module */
mod.link<sum>("*", "sum");
mod.link<ext_memcpy>("*", "ext_memcpy");
/* find and call functions defined in a wasm module */
{
wasm3::function test_fn = runtime.find_function("test");
auto res = test_fn.call<int>(20, 10);
@ -34,7 +57,6 @@ int main(void)
std::cout << "result: 0x" << std::hex << res << std::dec << std::endl;
}
}
catch(wasm3::error &e) {
std::cerr << "WASM3 error: " << e.what() << std::endl;
return 1;

Binary file not shown.

@ -256,6 +256,7 @@ namespace wasm3 {
friend class runtime;
module(const std::shared_ptr<M3Environment> &env, std::istream &in_wasm) {
in_wasm.unsetf(std::ios::skipws);
std::vector<uint8_t> in_bytes;
std::copy(std::istream_iterator<uint8_t>(in_wasm),
std::istream_iterator<uint8_t>(),
@ -315,6 +316,7 @@ namespace wasm3 {
detail::check_error(res);
Ret ret;
res = m3_GetResults(m_func, 1, &ret);
detail::check_error(res);
return ret;
}
@ -333,6 +335,7 @@ namespace wasm3 {
Ret ret;
const void* ret_ptrs[] = { &ret };
res = m3_GetResults(m_func, 1, ret_ptrs);
detail::check_error(res);
return ret;
}

Loading…
Cancel
Save