Skip to content

Commit

Permalink
feat(c-api) Implement wat2wasm.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Oct 6, 2020
1 parent 80a243f commit ded3132
Show file tree
Hide file tree
Showing 6 changed files with 49 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
1 change: 1 addition & 0 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions lib/c-api/src/wasm_c_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pub mod value;

#[cfg(feature = "wasi")]
pub mod wasi;

pub mod wat;
30 changes: 30 additions & 0 deletions lib/c-api/src/wasm_c_api/wat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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<Box<wasm_byte_vec_t>> {
let wat: &[u8] = wat.into_slice()?;

let result = match wasmer::wat2wasm(wat) {
Ok(result) => result,
Err(error) => {
crate::error::update_last_error(error);

return None;
}
};

let mut result: Vec<u8> = result.into_owned();
result.shrink_to_fit();

let wasm = wasm_byte_vec_t {
size: result.len(),
data: result.as_mut_ptr(),
};

Some(Box::new(wasm))
}
8 changes: 8 additions & 0 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit ded3132

Please sign in to comment.