mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-12 05:18:43 +00:00
feat(c-api) Implement wasm_extern_copy.
This patch also moves `wasm_extern_delete` into the correct Rust module.
This commit is contained in:
78
lib/c-api/src/wasm_c_api/externals/mod.rs
vendored
78
lib/c-api/src/wasm_c_api/externals/mod.rs
vendored
@@ -20,6 +20,84 @@ pub struct wasm_extern_t {
|
|||||||
|
|
||||||
wasm_declare_boxed_vec!(extern);
|
wasm_declare_boxed_vec!(extern);
|
||||||
|
|
||||||
|
/// Copy a `wasm_extern_t`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use inline_c::assert_c;
|
||||||
|
/// # fn main() {
|
||||||
|
/// # (assert_c! {
|
||||||
|
/// # #include "tests/wasmer_wasm.h"
|
||||||
|
/// #
|
||||||
|
/// int main() {
|
||||||
|
/// // Create the engine and the store.
|
||||||
|
/// wasm_engine_t* engine = wasm_engine_new();
|
||||||
|
/// wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
///
|
||||||
|
/// // Create a WebAssembly module from a WAT definition.
|
||||||
|
/// wasm_byte_vec_t wat;
|
||||||
|
/// wasmer_byte_vec_new_from_string(
|
||||||
|
/// &wat,
|
||||||
|
/// "(module\n"
|
||||||
|
/// " (func (export \"function\")))"
|
||||||
|
/// );
|
||||||
|
/// wasm_byte_vec_t wasm;
|
||||||
|
/// wat2wasm(&wat, &wasm);
|
||||||
|
///
|
||||||
|
/// // Create the module.
|
||||||
|
/// wasm_module_t* module = wasm_module_new(store, &wasm);
|
||||||
|
///
|
||||||
|
/// // Instantiate the module.
|
||||||
|
/// wasm_extern_vec_t imports = WASM_EMPTY_VEC;
|
||||||
|
/// wasm_trap_t* traps = NULL;
|
||||||
|
///
|
||||||
|
/// wasm_instance_t* instance = wasm_instance_new(store, module, &imports, &traps);
|
||||||
|
/// assert(instance);
|
||||||
|
///
|
||||||
|
/// // Read the exports.
|
||||||
|
/// wasm_extern_vec_t exports;
|
||||||
|
/// wasm_instance_exports(instance, &exports);
|
||||||
|
///
|
||||||
|
/// // We have 1 of them.
|
||||||
|
/// assert(exports.size == 1);
|
||||||
|
///
|
||||||
|
/// // It is a function.
|
||||||
|
/// wasm_extern_t* function = exports.data[0];
|
||||||
|
/// assert(wasm_extern_kind(function) == WASM_EXTERN_FUNC);
|
||||||
|
///
|
||||||
|
/// // Let's copy the function.
|
||||||
|
/// wasm_extern_t* function_copy = wasm_extern_copy(function);
|
||||||
|
/// assert(wasm_extern_kind(function_copy) == WASM_EXTERN_FUNC);
|
||||||
|
///
|
||||||
|
/// // Free everything.
|
||||||
|
/// wasm_extern_delete(function_copy);
|
||||||
|
/// wasm_instance_delete(instance);
|
||||||
|
/// wasm_module_delete(module);
|
||||||
|
/// wasm_byte_vec_delete(&wasm);
|
||||||
|
/// wasm_byte_vec_delete(&wat);
|
||||||
|
/// wasm_store_delete(store);
|
||||||
|
/// wasm_engine_delete(engine);
|
||||||
|
///
|
||||||
|
/// return 0;
|
||||||
|
/// }
|
||||||
|
/// # })
|
||||||
|
/// # .success();
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasm_extern_copy(r#extern: &wasm_extern_t) -> Box<wasm_extern_t> {
|
||||||
|
Box::new(r#extern.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete an extern.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// See `wasm_extern_copy`.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasm_extern_delete(_extern: Option<Box<wasm_extern_t>>) {}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn wasm_func_as_extern(
|
pub unsafe extern "C" fn wasm_func_as_extern(
|
||||||
func: Option<&wasm_func_t>,
|
func: Option<&wasm_func_t>,
|
||||||
|
|||||||
@@ -524,12 +524,6 @@ pub unsafe extern "C" fn wasi_get_start_function(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete a `wasm_extern_t` allocated by the API.
|
|
||||||
///
|
|
||||||
/// cbindgen:ignore
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn wasm_extern_delete(_item: Option<Box<wasm_extern_t>>) {}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use inline_c::assert_c;
|
use inline_c::assert_c;
|
||||||
|
|||||||
Reference in New Issue
Block a user