Add a wasmtime-specific wasmtime_wat2wasm C API (#1206)
* Add a wasmtime-specific `wasmtime_wat2wasm` C API This commit implements a wasmtime-specific C API for converting the text format to the binary format. An upstream spec issue exists for adding this to the C API, but in the meantime we can experiment with our own version of this API and use it in the C# extension, for example! Closes #1000 * Reorder arguments * Use wasm_byte_vec_t for input `*.wat` * Mark wat input as const * Return an error message and use `fixed` * Actually include the error message * Use `fixed` in `Module.cs` as well
This commit is contained in:
@@ -20,3 +20,4 @@ doctest = false
|
||||
wasmtime = { path = "../api" }
|
||||
wasi-common = { path = "../wasi-common" }
|
||||
wasmtime-wasi = { path = "../wasi" }
|
||||
wat = "1.0"
|
||||
|
||||
@@ -38,6 +38,26 @@ WASMTIME_CONFIG_PROP(cranelift_opt_level, wasmtime_opt_level_t)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Converts from the text format of WebAssembly to to the binary format.
|
||||
//
|
||||
// * `engine` - a previously created engine which will drive allocations and
|
||||
// such
|
||||
// * `wat` - this it the input buffer with the WebAssembly Text Format inside of
|
||||
// it. This will be parsed and converted to the binary format.
|
||||
// * `ret` - if the conversion is successful, this byte vector is filled in with
|
||||
// the WebAssembly binary format.
|
||||
// * `error_message` - if the conversion fails, this is filled in with a
|
||||
// descriptive error message of why parsing failed. This parameter is
|
||||
// optional.
|
||||
//
|
||||
// Returns `true` if conversion succeeded, or `false` if it failed.
|
||||
bool wasmtime_wat2wasm(
|
||||
wasm_engine_t *engine,
|
||||
const wasm_byte_vec_t *wat,
|
||||
own wasm_byte_vec_t *ret,
|
||||
own wasm_byte_vec_t *error_message,
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! This file defines the extern "C" API extension, which are specific
|
||||
//! to the wasmtime implementation.
|
||||
|
||||
use crate::wasm_config_t;
|
||||
use crate::{wasm_byte_vec_t, wasm_config_t, wasm_engine_t};
|
||||
use std::str;
|
||||
use wasmtime::{OptLevel, Strategy};
|
||||
|
||||
#[repr(u8)]
|
||||
@@ -86,3 +87,33 @@ pub unsafe extern "C" fn wasmtime_config_cranelift_opt_level_set(
|
||||
WASMTIME_OPT_LEVEL_SPEED_AND_SIZE => OptLevel::SpeedAndSize,
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_wat2wasm(
|
||||
_engine: *mut wasm_engine_t,
|
||||
wat: *const wasm_byte_vec_t,
|
||||
ret: *mut wasm_byte_vec_t,
|
||||
error: *mut wasm_byte_vec_t,
|
||||
) -> bool {
|
||||
let wat = match str::from_utf8((*wat).as_slice()) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
if !error.is_null() {
|
||||
(*error).set_from_slice(b"input was not valid utf-8");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
match wat::parse_str(wat) {
|
||||
Ok(bytes) => {
|
||||
(*ret).set_from_slice(&bytes);
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
if !error.is_null() {
|
||||
(*error).set_from_slice(e.to_string().as_bytes());
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user