diff --git a/CHANGELOG.md b/CHANGELOG.md index d195b62b70b..0b0573bd984 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## **[Unreleased]** +- [#1635](https://github.com/wasmerio/wasmer/pull/1635) Implement `wat2wasm` in the Wasm C API. +- [#1636](https://github.com/wasmerio/wasmer/pull/1636) Implement `wasm_module_validate` in the Wasm C API. - [#1671](https://github.com/wasmerio/wasmer/pull/1671) Fix probestack firing inappropriately, and sometimes over/under allocating stack. - [#1657](https://github.com/wasmerio/wasmer/pull/1657) Implement `wasm_trap_t` and `wasm_frame_t` for Wasm C API; add examples in Rust and C of exiting early with a host function. - [#1645](https://github.com/wasmerio/wasmer/pull/1645) Move the install script to https://github.com/wasmerio/wasmer-install diff --git a/Makefile b/Makefile index 3cb273b8abb..b4217370ae7 100644 --- a/Makefile +++ b/Makefile @@ -76,15 +76,15 @@ build-capi: build-capi-cranelift build-capi-singlepass: cargo build --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,singlepass,wasi + --no-default-features --features wat,jit,singlepass,wasi build-capi-cranelift: cargo build --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,cranelift,wasi + --no-default-features --features wat,jit,cranelift,wasi build-capi-llvm: cargo build --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,llvm,wasi + --no-default-features --features wat,jit,llvm,wasi ########### @@ -112,15 +112,15 @@ test-packages: test-capi-singlepass: build-capi-singlepass cargo test --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,singlepass,wasi -- --nocapture + --no-default-features --features wat,jit,singlepass,wasi -- --nocapture test-capi-cranelift: build-capi-cranelift cargo test --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,cranelift,wasi -- --nocapture + --no-default-features --features wat,jit,cranelift,wasi -- --nocapture test-capi-llvm: build-capi-llvm cargo test --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features jit,llvm,wasi -- --nocapture + --no-default-features --features wat,jit,llvm,wasi -- --nocapture test-capi: test-capi-singlepass test-capi-cranelift test-capi-llvm diff --git a/lib/c-api/Cargo.toml b/lib/c-api/Cargo.toml index 4bc32e47fdb..d33425c466e 100644 --- a/lib/c-api/Cargo.toml +++ b/lib/c-api/Cargo.toml @@ -40,9 +40,11 @@ paste = "0.1" [features] default = [ + "wat", "cranelift", "wasi", ] +wat = ["wasmer/wat"] wasi = ["wasmer-wasi", "typetag", "serde"] engine = [] jit = [ diff --git a/lib/c-api/build.rs b/lib/c-api/build.rs index 683c62f85c6..3f7c96ef8be 100644 --- a/lib/c-api/build.rs +++ b/lib/c-api/build.rs @@ -374,4 +374,5 @@ fn exclude_items_from_wasm_c_api(builder: Builder) -> Builder { .exclude_item("wasi_get_start_function") .exclude_item("wasi_get_wasi_version") .exclude_item("wasi_version_t") + .exclude_item("wat2wasm") } diff --git a/lib/c-api/src/wasm_c_api/mod.rs b/lib/c-api/src/wasm_c_api/mod.rs index 9d9d26485d5..de528d8bce8 100644 --- a/lib/c-api/src/wasm_c_api/mod.rs +++ b/lib/c-api/src/wasm_c_api/mod.rs @@ -29,3 +29,5 @@ pub mod value; #[cfg(feature = "wasi")] pub mod wasi; + +pub mod wat; diff --git a/lib/c-api/src/wasm_c_api/wat.rs b/lib/c-api/src/wasm_c_api/wat.rs new file mode 100644 index 00000000000..0ff73d69ce0 --- /dev/null +++ b/lib/c-api/src/wasm_c_api/wat.rs @@ -0,0 +1,14 @@ +use super::types::wasm_byte_vec_t; + +/// Parses in-memory bytes as either the WAT format, or a binary Wasm +/// module. This is wasmer-specific. +/// +/// In case of failure, `wat2wasm` returns `NULL`. +#[cfg(feature = "wat")] +#[no_mangle] +pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t) -> Option> { + let wat: &[u8] = wat.into_slice()?; + let result: wasm_byte_vec_t = c_try!(wasmer::wat2wasm(wat)).into_owned().into(); + + Some(Box::new(result)) +} diff --git a/lib/c-api/tests/wasm_c_api/CMakeLists.txt b/lib/c-api/tests/wasm_c_api/CMakeLists.txt index 4ee4291b543..1ac9b3eaf53 100644 --- a/lib/c-api/tests/wasm_c_api/CMakeLists.txt +++ b/lib/c-api/tests/wasm_c_api/CMakeLists.txt @@ -17,9 +17,10 @@ add_executable(wasm-c-api-serialize wasm-c-api/example/serialize.c) add_executable(wasm-c-api-trap wasm-c-api/example/trap.c) # Our additional tests. -add_executable(test-wasi test-wasi.c) add_executable(test-early-exit test-early-exit.c) add_executable(test-memory test-memory.c) +add_executable(test-wasi test-wasi.c) +add_executable(test-wat2wasm test-wat2wasm.c) include_directories(wasm-c-api/include) include_directories(../../) @@ -133,11 +134,6 @@ add_test(NAME wasm-c-api-trap WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/example/ ) -set_property(TARGET test-wasi PROPERTY C_STANDARD 11) -target_link_libraries(test-wasi general ${WASMER_LIB}) -target_compile_options(test-wasi PRIVATE ${COMPILER_OPTIONS}) -add_test(test-wasi test-wasi) - set_property(TARGET test-early-exit PROPERTY C_STANDARD 11) target_link_libraries(test-early-exit general ${WASMER_LIB}) target_compile_options(test-early-exit PRIVATE ${COMPILER_OPTIONS}) @@ -147,3 +143,13 @@ set_property(TARGET test-memory PROPERTY C_STANDARD 11) target_link_libraries(test-memory general ${WASMER_LIB}) target_compile_options(test-memory PRIVATE ${COMPILER_OPTIONS}) add_test(test-memory test-memory) + +set_property(TARGET test-wasi PROPERTY C_STANDARD 11) +target_link_libraries(test-wasi general ${WASMER_LIB}) +target_compile_options(test-wasi PRIVATE ${COMPILER_OPTIONS}) +add_test(test-wasi test-wasi) + +set_property(TARGET test-wat2wasm PROPERTY C_STANDARD 11) +target_link_libraries(test-wat2wasm general ${WASMER_LIB}) +target_compile_options(test-wat2wasm PRIVATE ${COMPILER_OPTIONS}) +add_test(test-wat2wasm test-wat2wasm) diff --git a/lib/c-api/tests/wasm_c_api/test-wat2wasm.c b/lib/c-api/tests/wasm_c_api/test-wat2wasm.c new file mode 100644 index 00000000000..5897cadadbe --- /dev/null +++ b/lib/c-api/tests/wasm_c_api/test-wat2wasm.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include "wasmer_wasm.h" + +#define own + +int main(int argc, const char* argv[]) { + // Initialize. + printf("Initializing...\n"); + wasm_engine_t* engine = wasm_engine_new(); + wasm_store_t* store = wasm_store_new(engine); + + // Getting Wasm. + printf("Compiling WAT to Wasm...\n"); + + wasm_byte_vec_t wat = { + .data = "(module)", + .size = 8, + }; + wasm_byte_vec_t *wasm = wat2wasm(&wat); + + if (!wasm) { + printf("> Error compiler WAT to Wasm!\n"); + return 1; + } + + if (wasm->size != 8) { + printf("The Wasm size is incorrect!\n"); + return 1; + } + + if (!(wasm->data[0] == 0 && + wasm->data[1] == 'a' && + wasm->data[2] == 's' && + wasm->data[3] == 'm' && + wasm->data[4] == 1 && + wasm->data[5] == 0 && + wasm->data[6] == 0 && + wasm->data[7] == 0)) { + printf("The Wasm data is incorrect!\n"); + return 1; + } + + wasm_byte_vec_delete(wasm); + + // All done. + printf("Done.\n"); + return 0; +} diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index 4201bdd54ac..8e44602c9d8 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -177,4 +177,12 @@ int wasmer_last_error_length(void); */ int wasmer_last_error_message(char *buffer, int length); +/** + * Parses in-memory bytes as either the WAT format, or a binary Wasm + * module. This is wasmer-specific. + * + * In case of failure, `wat2wasm` returns `NULL`. + */ +wasm_byte_vec_t *wat2wasm(const wasm_byte_vec_t *wat); + #endif /* WASMER_WASM_H */