Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(c-api) Add wat2wasm to the wasm-c-api module #1635

Merged
merged 6 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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;
28 changes: 28 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,28 @@
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();

Some(Box::new(wasm_byte_vec_t {
size: result.len(),
data: result.as_mut_ptr(),
}))
}
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 */