1use crate::{
2 wasm_byte_vec_t,
3 wasm_exporttype_t,
4 wasm_exporttype_vec_t,
5 wasm_importtype_t,
6 wasm_importtype_vec_t,
7 wasm_store_t,
8 CExternType,
9};
10use alloc::{boxed::Box, string::String};
11use wasmi::{Engine, Module};
12
13#[derive(Clone)]
17pub struct wasm_module_t {
18 pub(crate) inner: Module,
19}
20
21wasmi_c_api_macros::declare_ref!(wasm_module_t);
22
23impl wasm_module_t {
24 pub(crate) fn new(module: Module) -> wasm_module_t {
25 wasm_module_t { inner: module }
26 }
27}
28
29#[repr(C)]
35#[derive(Clone)]
36pub struct wasm_shared_module_t {
37 inner: Module,
38}
39
40wasmi_c_api_macros::declare_own!(wasm_shared_module_t);
41
42#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
53#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
54pub unsafe extern "C" fn wasm_module_new(
55 store: &mut wasm_store_t,
56 binary: &wasm_byte_vec_t,
57) -> Option<Box<wasm_module_t>> {
58 match Module::new(store.inner.context().engine(), binary.as_slice()) {
59 Ok(module) => Some(Box::new(wasm_module_t::new(module))),
60 Err(_) => None,
61 }
62}
63
64#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
73#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
74pub unsafe extern "C" fn wasm_module_validate(
75 store: &mut wasm_store_t,
76 binary: &wasm_byte_vec_t,
77) -> bool {
78 Module::validate(store.inner.context().engine(), binary.as_slice()).is_ok()
79}
80
81fn fill_exports(module: &Module, out: &mut wasm_exporttype_vec_t) {
83 let exports = module
84 .exports()
85 .map(|e| {
86 Some(Box::new(wasm_exporttype_t::new(
87 String::from(e.name()),
88 CExternType::new(e.ty().clone()),
89 )))
90 })
91 .collect::<Box<[_]>>();
92 out.set_buffer(exports);
93}
94
95#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
101#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
102pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exporttype_vec_t) {
103 fill_exports(&module.inner, out);
104}
105
106fn fill_imports(module: &Module, out: &mut wasm_importtype_vec_t) {
108 let imports = module
109 .imports()
110 .map(|i| {
111 Some(Box::new(wasm_importtype_t::new(
112 String::from(i.module()),
113 String::from(i.name()),
114 CExternType::new(i.ty().clone()),
115 )))
116 })
117 .collect::<Box<[_]>>();
118 out.set_buffer(imports);
119}
120
121#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
127#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
128pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_importtype_vec_t) {
129 fill_imports(&module.inner, out);
130}
131
132#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
139#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
140pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_module_t> {
141 Box::new(wasm_shared_module_t {
142 inner: module.inner.clone(),
143 })
144}
145
146#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
157#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
158pub unsafe extern "C" fn wasm_module_obtain(
159 store: &mut wasm_store_t,
160 shared_module: &wasm_shared_module_t,
161) -> Option<Box<wasm_module_t>> {
162 let module = shared_module.inner.clone();
163 if Engine::same(store.inner.context().engine(), module.engine()) {
164 Some(Box::new(wasm_module_t::new(module)))
165 } else {
166 None
167 }
168}
169
170#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
178#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
179pub extern "C" fn wasm_module_serialize(_module: &wasm_module_t, _ret: &mut wasm_byte_vec_t) {
180 unimplemented!("wasm_module_serialize")
181}
182
183#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
198#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
199pub unsafe extern "C" fn wasm_module_deserialize(
200 _store: &mut wasm_store_t,
201 _binary: &wasm_byte_vec_t,
202) -> Option<Box<wasm_module_t>> {
203 unimplemented!("wasm_module_deserialize")
204}