-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1635: feat(c-api) Add `wat2wasm` to the `wasm-c-api` module r=Hywan a=Hywan This PR adds the `wat2wasm` function to the `wasm-c-api` module. Caveat of this patch: The `wasmer_wasm.h` seems to be written by hand. Consequently, if the `lib/c-api/` crate is compiled without the `wat` feature (which is the default), the `wat2wasm` function will still be defined in `wasmer_wasm.h`. Proposal: We must generate the `wasmer_wasm.h` header file with the same approach than we use for `wasm.h`. It can be done in a next PR. Thoughts @MarkMcCaskey Co-authored-by: Ivan Enderlin <[email protected]>
- Loading branch information
Showing
9 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ pub mod value; | |
|
||
#[cfg(feature = "wasi")] | ||
pub mod wasi; | ||
|
||
pub mod wat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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: wasm_byte_vec_t = c_try!(wasmer::wat2wasm(wat)).into_owned().into(); | ||
|
||
Some(Box::new(result)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <inttypes.h> | ||
|
||
#include "wasmer_wasm.h" | ||
|
||
#define own | ||
|
||
int main(int argc, const char* argv[]) { | ||
// Initialize. | ||
printf("Initializing...\n"); | ||
wasm_engine_t* engine = wasm_engine_new(); | ||
wasm_store_t* store = wasm_store_new(engine); | ||
|
||
// Getting Wasm. | ||
printf("Compiling WAT to Wasm...\n"); | ||
|
||
wasm_byte_vec_t wat = { | ||
.data = "(module)", | ||
.size = 8, | ||
}; | ||
wasm_byte_vec_t *wasm = wat2wasm(&wat); | ||
|
||
if (!wasm) { | ||
printf("> Error compiler WAT to Wasm!\n"); | ||
return 1; | ||
} | ||
|
||
if (wasm->size != 8) { | ||
printf("The Wasm size is incorrect!\n"); | ||
return 1; | ||
} | ||
|
||
if (!(wasm->data[0] == 0 && | ||
wasm->data[1] == 'a' && | ||
wasm->data[2] == 's' && | ||
wasm->data[3] == 'm' && | ||
wasm->data[4] == 1 && | ||
wasm->data[5] == 0 && | ||
wasm->data[6] == 0 && | ||
wasm->data[7] == 0)) { | ||
printf("The Wasm data is incorrect!\n"); | ||
return 1; | ||
} | ||
|
||
wasm_byte_vec_delete(wasm); | ||
|
||
// All done. | ||
printf("Done.\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters