diff --git a/lib/c-api/examples/imports-exports.c b/lib/c-api/examples/imports-exports.c index aaf4186c3..e56a72327 100644 --- a/lib/c-api/examples/imports-exports.c +++ b/lib/c-api/examples/imports-exports.c @@ -63,7 +63,8 @@ int main(int argc, const char* argv[]) { printf("Instantiating module...\n"); wasm_instance_t* instance = wasm_instance_new(store, module, &import_object, NULL); - wasm_extern_vec_delete(&import_object); + wasm_func_delete(host_func); + wasm_global_delete(host_global); if (!instance) { printf("> Error instantiating module!\n"); diff --git a/lib/c-api/src/wasm_c_api/macros.rs b/lib/c-api/src/wasm_c_api/macros.rs index 22a251bb2..04aa90f3d 100644 --- a/lib/c-api/src/wasm_c_api/macros.rs +++ b/lib/c-api/src/wasm_c_api/macros.rs @@ -3,6 +3,7 @@ macro_rules! wasm_declare_vec_inner { name: $name:ident, ty: $elem_ty:ty, c_ty: $c_ty:expr, + c_val: $c_val:expr, new: $new:ident, empty: $empty:ident, uninit: $uninit:ident, @@ -21,10 +22,8 @@ Read the documentation of [`", $c_ty, "`] to see more concrete examples. # (assert_c! { # #include \"tests/wasmer.h\" # -int main() { +void example(", $c_ty, " x, ", $c_ty, " y) { // Create a vector of 2 `", $c_ty, "`. - ", $c_ty, " x; - ", $c_ty, " y; ", $c_ty, " items[2] = {x, y}; ", stringify!($name), " vector; @@ -36,6 +35,8 @@ int main() { // Free it. ", stringify!($delete), "(&vector); } +# +# int main() { example(", $c_val, ", ", $c_val, "); return 0; } # }) # .success(); # } @@ -134,6 +135,8 @@ int main() { // Free it. ", stringify!($delete), "(&vector); + + return 0; } # }) # .success(); @@ -165,6 +168,8 @@ int main() { // Free it. ", stringify!($delete), "(&vector); + + return 0; } # }) # .success(); @@ -215,6 +220,11 @@ macro_rules! wasm_declare_vec { name: [<$prefix _ $name _vec_t>], ty: [<$prefix _ $name _t>], c_ty: stringify!([<$prefix _ $name _t>]), + c_val: concat!("({ ", + stringify!([<$prefix _ $name _t>]), " foo;\n", + "memset(&foo, 0, sizeof(foo));\n", + "foo;\n", + "})"), new: [<$prefix _ $name _vec_new>], empty: [<$prefix _ $name _vec_new_empty>], uninit: [<$prefix _ $name _vec_new_uninitialized>], @@ -236,6 +246,7 @@ macro_rules! wasm_declare_boxed_vec { name: [<$prefix _ $name _vec_t>], ty: Option]>>, c_ty: stringify!([<$prefix _ $name _t>] *), + c_val: "NULL", new: [<$prefix _ $name _vec_new>], empty: [<$prefix _ $name _vec_new_empty>], uninit: [<$prefix _ $name _vec_new_uninitialized>], diff --git a/lib/c-api/src/wasm_c_api/types/export.rs b/lib/c-api/src/wasm_c_api/types/export.rs index 39d5ab674..a60ccf48b 100644 --- a/lib/c-api/src/wasm_c_api/types/export.rs +++ b/lib/c-api/src/wasm_c_api/types/export.rs @@ -5,7 +5,7 @@ use wasmer_api::ExportType; #[derive(Clone)] pub struct wasm_exporttype_t { name: wasm_name_t, - extern_type: Box, + extern_type: wasm_externtype_t, } wasm_declare_boxed_vec!(exporttype); @@ -18,7 +18,7 @@ pub extern "C" fn wasm_exporttype_new( ) -> Box { Box::new(wasm_exporttype_t { name: name.clone(), - extern_type, + extern_type: *extern_type, }) } @@ -29,7 +29,7 @@ pub extern "C" fn wasm_exporttype_name(export_type: &wasm_exporttype_t) -> &wasm #[no_mangle] pub extern "C" fn wasm_exporttype_type(export_type: &wasm_exporttype_t) -> &wasm_externtype_t { - export_type.extern_type.as_ref() + &export_type.extern_type } impl From for wasm_exporttype_t { @@ -41,7 +41,7 @@ impl From for wasm_exporttype_t { impl From<&ExportType> for wasm_exporttype_t { fn from(other: &ExportType) -> Self { let name: wasm_name_t = other.name().to_string().into(); - let extern_type: Box = Box::new(other.ty().into()); + let extern_type: wasm_externtype_t = other.ty().into(); wasm_exporttype_t { name, extern_type } } diff --git a/lib/c-api/src/wasm_c_api/types/global.rs b/lib/c-api/src/wasm_c_api/types/global.rs index bdb9b7eb9..26342e428 100644 --- a/lib/c-api/src/wasm_c_api/types/global.rs +++ b/lib/c-api/src/wasm_c_api/types/global.rs @@ -8,12 +8,12 @@ use wasmer_api::{ExternType, GlobalType}; #[derive(Debug, Clone)] pub(crate) struct WasmGlobalType { pub(crate) global_type: GlobalType, - content: Box, + content: wasm_valtype_t, } impl WasmGlobalType { pub(crate) fn new(global_type: GlobalType) -> Self { - let content = Box::new(global_type.ty.into()); + let content = global_type.ty.into(); Self { global_type, @@ -79,5 +79,5 @@ pub unsafe extern "C" fn wasm_globaltype_mutability( pub unsafe extern "C" fn wasm_globaltype_content( global_type: &wasm_globaltype_t, ) -> &wasm_valtype_t { - global_type.inner().content.as_ref() + &global_type.inner().content } diff --git a/lib/c-api/src/wasm_c_api/types/import.rs b/lib/c-api/src/wasm_c_api/types/import.rs index c2de86aed..5e075ece7 100644 --- a/lib/c-api/src/wasm_c_api/types/import.rs +++ b/lib/c-api/src/wasm_c_api/types/import.rs @@ -7,7 +7,7 @@ use wasmer_api::ImportType; pub struct wasm_importtype_t { module: wasm_name_t, name: wasm_name_t, - extern_type: Box, + extern_type: wasm_externtype_t, } wasm_declare_boxed_vec!(importtype); @@ -21,7 +21,7 @@ pub extern "C" fn wasm_importtype_new( Some(Box::new(wasm_importtype_t { name: *name?, module: *module?, - extern_type: extern_type?, + extern_type: *extern_type?, })) } @@ -37,7 +37,7 @@ pub extern "C" fn wasm_importtype_name(import_type: &wasm_importtype_t) -> &wasm #[no_mangle] pub extern "C" fn wasm_importtype_type(import_type: &wasm_importtype_t) -> &wasm_externtype_t { - import_type.extern_type.as_ref() + &import_type.extern_type } #[no_mangle] @@ -53,7 +53,7 @@ impl From<&ImportType> for wasm_importtype_t { fn from(other: &ImportType) -> Self { let module: wasm_name_t = other.module().to_string().into(); let name: wasm_name_t = other.name().to_string().into(); - let extern_type: Box = Box::new(other.ty().into()); + let extern_type: wasm_externtype_t = other.ty().into(); wasm_importtype_t { module, diff --git a/lib/c-api/src/wasm_c_api/types/memory.rs b/lib/c-api/src/wasm_c_api/types/memory.rs index b98c16494..e60eb8459 100644 --- a/lib/c-api/src/wasm_c_api/types/memory.rs +++ b/lib/c-api/src/wasm_c_api/types/memory.rs @@ -4,18 +4,18 @@ use wasmer_api::{ExternType, MemoryType, Pages}; #[derive(Debug, Clone)] pub(crate) struct WasmMemoryType { pub(crate) memory_type: MemoryType, - limits: Box, + limits: wasm_limits_t, } impl WasmMemoryType { pub(crate) fn new(memory_type: MemoryType) -> Self { - let limits = Box::new(wasm_limits_t { + let limits = wasm_limits_t { min: memory_type.minimum.0 as _, max: memory_type .maximum .map(|max| max.0 as _) .unwrap_or(LIMITS_MAX_SENTINEL), - }); + }; Self { memory_type, @@ -79,5 +79,5 @@ const LIMITS_MAX_SENTINEL: u32 = u32::max_value(); #[no_mangle] pub unsafe extern "C" fn wasm_memorytype_limits(memory_type: &wasm_memorytype_t) -> &wasm_limits_t { - memory_type.inner().limits.as_ref() + &memory_type.inner().limits } diff --git a/lib/c-api/src/wasm_c_api/types/table.rs b/lib/c-api/src/wasm_c_api/types/table.rs index a17c3aef3..a3d74ee79 100644 --- a/lib/c-api/src/wasm_c_api/types/table.rs +++ b/lib/c-api/src/wasm_c_api/types/table.rs @@ -10,21 +10,21 @@ const LIMITS_MAX_SENTINEL: u32 = u32::max_value(); #[derive(Debug, Clone)] pub(crate) struct WasmTableType { - pub(crate) table_type: TableType, - limits: Box, - content: Box, + pub(crate) _table_type: TableType, + limits: wasm_limits_t, + content: wasm_valtype_t, } impl WasmTableType { pub(crate) fn new(table_type: TableType) -> Self { - let limits = Box::new(wasm_limits_t { + let limits = wasm_limits_t { min: table_type.minimum as _, max: table_type.maximum.unwrap_or(LIMITS_MAX_SENTINEL), - }); - let content = Box::new(table_type.ty.into()); + }; + let content = table_type.ty.into(); Self { - table_type, + _table_type: table_type, limits, content, } @@ -79,12 +79,12 @@ pub unsafe extern "C" fn wasm_tabletype_new( #[no_mangle] pub unsafe extern "C" fn wasm_tabletype_limits(table_type: &wasm_tabletype_t) -> &wasm_limits_t { - table_type.inner().limits.as_ref() + &table_type.inner().limits } #[no_mangle] pub unsafe extern "C" fn wasm_tabletype_element(table_type: &wasm_tabletype_t) -> &wasm_valtype_t { - table_type.inner().content.as_ref() + &table_type.inner().content } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/unstable/module.rs b/lib/c-api/src/wasm_c_api/unstable/module.rs index 9e85b2f85..81528369b 100644 --- a/lib/c-api/src/wasm_c_api/unstable/module.rs +++ b/lib/c-api/src/wasm_c_api/unstable/module.rs @@ -70,7 +70,7 @@ pub unsafe extern "C" fn wasmer_module_name( } }; - *out = name.as_bytes().to_vec().into(); + out.set_buffer(name.as_bytes().to_vec()); } /// Unstable non-standard Wasmer-specific API to set the module's