wasmi_c_api/
instance.rs

1use crate::{
2    wasm_extern_t,
3    wasm_extern_vec_t,
4    wasm_module_t,
5    wasm_store_t,
6    wasm_trap_t,
7    WasmStoreRef,
8};
9use alloc::boxed::Box;
10use wasmi::Instance;
11
12/// A Wasm instance.
13///
14/// Wraps [`Instance`].
15#[derive(Clone)]
16pub struct wasm_instance_t {
17    store: WasmStoreRef,
18    inner: Instance,
19}
20
21wasmi_c_api_macros::declare_ref!(wasm_instance_t);
22
23impl wasm_instance_t {
24    /// Creates a new [`wasm_instance_t`] with the `store` wrapping `instance`.
25    pub(crate) fn new(store: WasmStoreRef, instance: Instance) -> wasm_instance_t {
26        wasm_instance_t {
27            store,
28            inner: instance,
29        }
30    }
31}
32
33/// Instantiates the [`wasm_module_t`] with the given list of `imports`.
34///
35/// - The instantiation process follows the [Wasm core specification].
36/// - Stores a [`wasm_trap_t`] in `out` in case the instantiation failed.
37///
38/// Wraps [`Instance::exports`].
39///
40/// # Safety
41///
42/// It is the caller's responsibility not to alias the [`wasm_instance_t`]
43/// with its underlying, internal [`WasmStoreRef`].
44///
45/// [Wasm core specification]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
46#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
47#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
48pub unsafe extern "C" fn wasm_instance_new(
49    store: &mut wasm_store_t,
50    wasm_module: &wasm_module_t,
51    imports: *const wasm_extern_vec_t,
52    result: Option<&mut *mut wasm_trap_t>,
53) -> Option<Box<wasm_instance_t>> {
54    let imports = (*imports)
55        .as_slice()
56        .iter()
57        .filter_map(|import| import.as_ref().map(|i| i.which))
58        .collect::<Box<[_]>>();
59    match Instance::new(store.inner.context_mut(), &wasm_module.inner, &imports) {
60        Ok(instance) => Some(Box::new(wasm_instance_t::new(
61            store.inner.clone(),
62            instance,
63        ))),
64        Err(e) => {
65            if let Some(ptr) = result {
66                *ptr = Box::into_raw(Box::new(wasm_trap_t::new(e)));
67            }
68            None
69        }
70    }
71}
72
73/// Returns the exports of the [`wasm_instance_t`].
74///
75/// The returned exports are stored in `out`.
76///
77/// Wraps [`Instance::exports`].
78///
79/// # Safety
80///
81/// It is the caller's responsibility not to alias the [`wasm_instance_t`]
82/// with its underlying, internal [`WasmStoreRef`].
83#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
84#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
85pub unsafe extern "C" fn wasm_instance_exports(
86    instance: &mut wasm_instance_t,
87    out: &mut wasm_extern_vec_t,
88) {
89    let store = instance.store.clone();
90    out.set_buffer(
91        instance
92            .inner
93            .exports(&mut instance.store.context_mut())
94            .map(|e| {
95                Some(Box::new(wasm_extern_t {
96                    which: e.into_extern(),
97                    store: store.clone(),
98                }))
99            })
100            .collect(),
101    );
102}