From c1a9c8bcff5191ad8c3dcff4209cbcd6af6633ba Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 18 Sep 2020 14:36:46 +0200 Subject: [PATCH] feat(c-api) Add `wat2wasm` to the `wasm-c-api` module. --- Makefile | 12 ++++++------ lib/c-api/Cargo.toml | 2 ++ lib/c-api/src/wasm_c_api/mod.rs | 17 +++++++++++++++++ lib/c-api/wasmer_wasm.h | 5 +++++ 4 files changed, 30 insertions(+), 6 deletions(-) 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 bc525a83199..e438cd36986 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/src/wasm_c_api/mod.rs b/lib/c-api/src/wasm_c_api/mod.rs index 8e14c34cd2f..673d65f702b 100644 --- a/lib/c-api/src/wasm_c_api/mod.rs +++ b/lib/c-api/src/wasm_c_api/mod.rs @@ -24,6 +24,23 @@ use wasmer::{ #[cfg(feature = "jit")] use wasmer_engine_jit::JIT; +/// Parses in-memory bytes as either the WAT format, or a binary Wasm +/// module. This is wasmer-specific. +#[cfg(feature = "wat")] +#[no_mangle] +pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, wasm: &mut wasm_byte_vec_t) -> Option<()> { + let wat: &[u8] = slice::from_raw_parts_mut(wat.data, wat.size); + + let result = c_try!(wasmer::wat2wasm(wat)); + let mut result = result.into_owned(); + wasm.size = result.len(); + wasm.data = result.as_mut_ptr(); + + mem::forget(result); + + Some(()) +} + /// this can be a wasmer-specific type with wasmer-specific functions for manipulating it #[repr(C)] pub struct wasm_config_t {} diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index 56260c91fc4..7374c998245 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -21,6 +21,7 @@ // Used to build a `wasi_env_t`. typedef struct wasi_config_t wasi_config_t; + // This type is passed to the WASI host functions owns the data core to the // functioning of WASI. typedef struct wasi_env_t wasi_env_t; @@ -142,4 +143,8 @@ int wasmer_last_error_length(); */ 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. +void wat2wasm(wasm_byte_vec_t* wat, wasm_byte_vec_t* wasm); + #endif /* WASMER_WASM_H */