mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-13 22:08:45 +00:00
feat(c-api) Implement wat2wasm.
This commit is contained in:
@@ -29,3 +29,5 @@ pub mod value;
|
||||
|
||||
#[cfg(feature = "wasi")]
|
||||
pub mod wasi;
|
||||
|
||||
pub mod wat;
|
||||
|
||||
30
lib/c-api/src/wasm_c_api/wat.rs
Normal file
30
lib/c-api/src/wasm_c_api/wat.rs
Normal file
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user