Skip to content

Commit

Permalink
feat(c-api) Add wat2wasm to the wasm-c-api module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 18, 2020
1 parent a2e744a commit c1a9c8b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


###########
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ paste = "0.1"

[features]
default = [
"wat",
"cranelift",
"wasi",
]
wat = ["wasmer/wat"]
wasi = ["wasmer-wasi", "typetag", "serde"]
engine = []
jit = [
Expand Down
17 changes: 17 additions & 0 deletions lib/c-api/src/wasm_c_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
5 changes: 5 additions & 0 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */

0 comments on commit c1a9c8b

Please sign in to comment.