From 361ff81db7f847fc92c20db7808e2673d8abae1c Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 29 Mar 2019 15:50:16 +0100 Subject: [PATCH] feat(runtime-c-api) Extract the `import` module. --- lib/runtime-c-api/src/export.rs | 3 +- lib/runtime-c-api/src/import.rs | 358 ++++++++++++++++++++++++++++++ lib/runtime-c-api/src/instance.rs | 5 +- lib/runtime-c-api/src/lib.rs | 356 +---------------------------- lib/runtime-c-api/src/module.rs | 5 +- 5 files changed, 370 insertions(+), 357 deletions(-) create mode 100644 lib/runtime-c-api/src/import.rs diff --git a/lib/runtime-c-api/src/export.rs b/lib/runtime-c-api/src/export.rs index ea37fdad6..6bcae32f2 100644 --- a/lib/runtime-c-api/src/export.rs +++ b/lib/runtime-c-api/src/export.rs @@ -3,11 +3,12 @@ use crate::{ error::{update_last_error, CApiError}, global::wasmer_global_t, + import::wasmer_import_func_t, memory::wasmer_memory_t, module::wasmer_module_t, table::wasmer_table_t, value::{wasmer_value, wasmer_value_t, wasmer_value_tag}, - wasmer_byte_array, wasmer_import_func_t, wasmer_result_t, + wasmer_byte_array, wasmer_result_t, }; use libc::{c_int, uint32_t}; use std::{ptr, slice}; diff --git a/lib/runtime-c-api/src/import.rs b/lib/runtime-c-api/src/import.rs new file mode 100644 index 000000000..cc3598078 --- /dev/null +++ b/lib/runtime-c-api/src/import.rs @@ -0,0 +1,358 @@ +//! Wasm imports. + +use crate::{ + error::{update_last_error, CApiError}, + export::{wasmer_import_export_kind, wasmer_import_export_value}, + module::wasmer_module_t, + value::wasmer_value_tag, + wasmer_byte_array, wasmer_result_t, +}; +use libc::{c_int, uint32_t}; +use std::{ffi::c_void, ptr, slice, sync::Arc}; +use wasmer_runtime::Module; +use wasmer_runtime_core::{ + export::{Context, Export, FuncPointer}, + module::ImportName, + types::{FuncSig, Type}, +}; + +#[repr(C)] +pub struct wasmer_import_t { + pub module_name: wasmer_byte_array, + pub import_name: wasmer_byte_array, + pub tag: wasmer_import_export_kind, + pub value: wasmer_import_export_value, +} + +#[repr(C)] +#[derive(Clone)] +pub struct wasmer_import_func_t; + +#[repr(C)] +#[derive(Clone)] +pub struct wasmer_import_descriptor_t; + +#[repr(C)] +#[derive(Clone)] +pub struct wasmer_import_descriptors_t; + +/// Gets import descriptors for the given module +/// +/// The caller owns the object and should call `wasmer_import_descriptors_destroy` to free it. +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub unsafe extern "C" fn wasmer_import_descriptors( + module: *const wasmer_module_t, + import_descriptors: *mut *mut wasmer_import_descriptors_t, +) { + let module = &*(module as *const Module); + let total_imports = module.info().imported_functions.len() + + module.info().imported_tables.len() + + module.info().imported_globals.len() + + module.info().imported_memories.len(); + let mut descriptors: Vec = Vec::with_capacity(total_imports); + + for ( + _index, + ImportName { + namespace_index, + name_index, + }, + ) in &module.info().imported_functions + { + let namespace = module.info().namespace_table.get(*namespace_index); + let name = module.info().name_table.get(*name_index); + descriptors.push(NamedImportDescriptor { + module: namespace.to_string(), + name: name.to_string(), + kind: wasmer_import_export_kind::WASM_FUNCTION, + }); + } + + for ( + _index, + ( + ImportName { + namespace_index, + name_index, + }, + _, + ), + ) in &module.info().imported_tables + { + let namespace = module.info().namespace_table.get(*namespace_index); + let name = module.info().name_table.get(*name_index); + descriptors.push(NamedImportDescriptor { + module: namespace.to_string(), + name: name.to_string(), + kind: wasmer_import_export_kind::WASM_TABLE, + }); + } + + for ( + _index, + ( + ImportName { + namespace_index, + name_index, + }, + _, + ), + ) in &module.info().imported_globals + { + let namespace = module.info().namespace_table.get(*namespace_index); + let name = module.info().name_table.get(*name_index); + descriptors.push(NamedImportDescriptor { + module: namespace.to_string(), + name: name.to_string(), + kind: wasmer_import_export_kind::WASM_GLOBAL, + }); + } + + for ( + _index, + ( + ImportName { + namespace_index, + name_index, + }, + _, + ), + ) in &module.info().imported_memories + { + let namespace = module.info().namespace_table.get(*namespace_index); + let name = module.info().name_table.get(*name_index); + descriptors.push(NamedImportDescriptor { + module: namespace.to_string(), + name: name.to_string(), + kind: wasmer_import_export_kind::WASM_MEMORY, + }); + } + + let named_import_descriptors: Box = + Box::new(NamedImportDescriptors(descriptors)); + *import_descriptors = + Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t; +} + +pub struct NamedImportDescriptors(Vec); + +/// Frees the memory for the given import descriptors +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub extern "C" fn wasmer_import_descriptors_destroy( + import_descriptors: *mut wasmer_import_descriptors_t, +) { + if !import_descriptors.is_null() { + unsafe { Box::from_raw(import_descriptors as *mut NamedImportDescriptors) }; + } +} + +/// Gets the length of the import descriptors +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub unsafe extern "C" fn wasmer_import_descriptors_len( + exports: *mut wasmer_import_descriptors_t, +) -> c_int { + if exports.is_null() { + return 0; + } + (*(exports as *mut NamedImportDescriptors)).0.len() as c_int +} + +/// Gets import descriptor by index +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub unsafe extern "C" fn wasmer_import_descriptors_get( + import_descriptors: *mut wasmer_import_descriptors_t, + idx: c_int, +) -> *mut wasmer_import_descriptor_t { + if import_descriptors.is_null() { + return ptr::null_mut(); + } + let named_import_descriptors = &mut *(import_descriptors as *mut NamedImportDescriptors); + &mut (*named_import_descriptors).0[idx as usize] as *mut NamedImportDescriptor + as *mut wasmer_import_descriptor_t +} + +/// Gets name for the import descriptor +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_descriptor_name( + import_descriptor: *mut wasmer_import_descriptor_t, +) -> wasmer_byte_array { + let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); + wasmer_byte_array { + bytes: named_import_descriptor.name.as_ptr(), + bytes_len: named_import_descriptor.name.len() as u32, + } +} + +/// Gets module name for the import descriptor +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_descriptor_module_name( + import_descriptor: *mut wasmer_import_descriptor_t, +) -> wasmer_byte_array { + let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); + wasmer_byte_array { + bytes: named_import_descriptor.module.as_ptr(), + bytes_len: named_import_descriptor.module.len() as u32, + } +} + +/// Gets export descriptor kind +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_descriptor_kind( + export: *mut wasmer_import_descriptor_t, +) -> wasmer_import_export_kind { + let named_import_descriptor = &*(export as *mut NamedImportDescriptor); + named_import_descriptor.kind.clone() +} + +/// Sets the result parameter to the arity of the params of the wasmer_import_func_t +/// +/// Returns `wasmer_result_t::WASMER_OK` upon success. +/// +/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` +/// and `wasmer_last_error_message` to get an error message. +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_func_params_arity( + func: *const wasmer_import_func_t, + result: *mut uint32_t, +) -> wasmer_result_t { + let export = &*(func as *const Export); + if let Export::Function { ref signature, .. } = *export { + *result = signature.params().len() as uint32_t; + wasmer_result_t::WASMER_OK + } else { + update_last_error(CApiError { + msg: "func ptr error in wasmer_import_func_params_arity".to_string(), + }); + wasmer_result_t::WASMER_ERROR + } +} + +/// Creates new func +/// +/// The caller owns the object and should call `wasmer_import_func_destroy` to free it. +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_func_new( + func: extern "C" fn(data: *mut c_void), + params: *const wasmer_value_tag, + params_len: c_int, + returns: *const wasmer_value_tag, + returns_len: c_int, +) -> *mut wasmer_import_func_t { + let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize); + let params: Vec = params.iter().cloned().map(|x| x.into()).collect(); + let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize); + let returns: Vec = returns.iter().cloned().map(|x| x.into()).collect(); + + let export = Box::new(Export::Function { + func: FuncPointer::new(func as _), + ctx: Context::Internal, + signature: Arc::new(FuncSig::new(params, returns)), + }); + Box::into_raw(export) as *mut wasmer_import_func_t +} + +/// Sets the params buffer to the parameter types of the given wasmer_import_func_t +/// +/// Returns `wasmer_result_t::WASMER_OK` upon success. +/// +/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` +/// and `wasmer_last_error_message` to get an error message. +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_func_params( + func: *const wasmer_import_func_t, + params: *mut wasmer_value_tag, + params_len: c_int, +) -> wasmer_result_t { + let export = &*(func as *const Export); + if let Export::Function { ref signature, .. } = *export { + let params: &mut [wasmer_value_tag] = + slice::from_raw_parts_mut(params, params_len as usize); + for (i, item) in signature.params().iter().enumerate() { + params[i] = item.into(); + } + wasmer_result_t::WASMER_OK + } else { + update_last_error(CApiError { + msg: "func ptr error in wasmer_import_func_params".to_string(), + }); + wasmer_result_t::WASMER_ERROR + } +} + +/// Sets the returns buffer to the parameter types of the given wasmer_import_func_t +/// +/// Returns `wasmer_result_t::WASMER_OK` upon success. +/// +/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` +/// and `wasmer_last_error_message` to get an error message. +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_func_returns( + func: *const wasmer_import_func_t, + returns: *mut wasmer_value_tag, + returns_len: c_int, +) -> wasmer_result_t { + let export = &*(func as *const Export); + if let Export::Function { ref signature, .. } = *export { + let returns: &mut [wasmer_value_tag] = + slice::from_raw_parts_mut(returns, returns_len as usize); + for (i, item) in signature.returns().iter().enumerate() { + returns[i] = item.into(); + } + wasmer_result_t::WASMER_OK + } else { + update_last_error(CApiError { + msg: "func ptr error in wasmer_import_func_returns".to_string(), + }); + wasmer_result_t::WASMER_ERROR + } +} + +/// Sets the result parameter to the arity of the returns of the wasmer_import_func_t +/// +/// Returns `wasmer_result_t::WASMER_OK` upon success. +/// +/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` +/// and `wasmer_last_error_message` to get an error message. +#[no_mangle] +#[allow(clippy::cast_ptr_alignment)] +pub unsafe extern "C" fn wasmer_import_func_returns_arity( + func: *const wasmer_import_func_t, + result: *mut uint32_t, +) -> wasmer_result_t { + let export = &*(func as *const Export); + if let Export::Function { ref signature, .. } = *export { + *result = signature.returns().len() as uint32_t; + wasmer_result_t::WASMER_OK + } else { + update_last_error(CApiError { + msg: "func ptr error in wasmer_import_func_results_arity".to_string(), + }); + wasmer_result_t::WASMER_ERROR + } +} + +/// Frees memory for the given Func +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) { + if !func.is_null() { + unsafe { Box::from_raw(func as *mut Export) }; + } +} + +struct NamedImportDescriptor { + module: String, + name: String, + kind: wasmer_import_export_kind, +} diff --git a/lib/runtime-c-api/src/instance.rs b/lib/runtime-c-api/src/instance.rs index 093d381e9..a9d7d5555 100644 --- a/lib/runtime-c-api/src/instance.rs +++ b/lib/runtime-c-api/src/instance.rs @@ -2,10 +2,11 @@ use crate::{ error::{update_last_error, CApiError}, - export::{wasmer_exports_t, NamedExport, NamedExports}, + export::{wasmer_exports_t, wasmer_import_export_kind, NamedExport, NamedExports}, + import::wasmer_import_t, memory::wasmer_memory_t, value::{wasmer_value, wasmer_value_t, wasmer_value_tag}, - wasmer_import_export_kind, wasmer_import_t, wasmer_result_t, + wasmer_result_t, }; use libc::{c_char, c_int, c_void, uint32_t, uint8_t}; use std::{collections::HashMap, ffi::CStr, slice}; diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index b1b6724d0..2baa9b273 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -1,28 +1,21 @@ extern crate wasmer_runtime; extern crate wasmer_runtime_core; -use libc::{c_int, uint32_t, uint8_t}; -use std::slice; -use std::sync::Arc; -use std::{ffi::c_void, ptr}; -use wasmer_runtime::{Module, Value}; -use wasmer_runtime_core::export::{Context, Export, FuncPointer}; -use wasmer_runtime_core::module::ImportName; -use wasmer_runtime_core::types::{FuncSig, Type}; +use libc::{uint32_t, uint8_t}; +use wasmer_runtime::Value; +use wasmer_runtime_core::types::Type; pub mod error; pub mod export; pub mod global; +pub mod import; pub mod instance; pub mod memory; pub mod module; pub mod table; pub mod value; -use error::{update_last_error, CApiError}; -use export::{wasmer_import_export_kind, wasmer_import_export_value}; use instance::wasmer_instance_t; -use module::wasmer_module_t; use value::{wasmer_value, wasmer_value_t, wasmer_value_tag}; #[allow(non_camel_case_types)] @@ -32,10 +25,6 @@ pub enum wasmer_result_t { WASMER_ERROR = 2, } -#[repr(C)] -#[derive(Clone)] -pub struct wasmer_import_func_t; - #[repr(C)] pub struct wasmer_limits_t { pub min: uint32_t, @@ -48,343 +37,12 @@ pub struct wasmer_limit_option_t { pub some: uint32_t, } -#[repr(C)] -pub struct wasmer_import_t { - module_name: wasmer_byte_array, - import_name: wasmer_byte_array, - tag: wasmer_import_export_kind, - value: wasmer_import_export_value, -} - -#[repr(C)] -#[derive(Clone)] -pub struct wasmer_import_descriptor_t; - -#[repr(C)] -#[derive(Clone)] -pub struct wasmer_import_descriptors_t; - #[repr(C)] pub struct wasmer_byte_array { bytes: *const uint8_t, bytes_len: uint32_t, } -/// Gets import descriptors for the given module -/// -/// The caller owns the object and should call `wasmer_import_descriptors_destroy` to free it. -#[allow(clippy::cast_ptr_alignment)] -#[no_mangle] -pub unsafe extern "C" fn wasmer_import_descriptors( - module: *const wasmer_module_t, - import_descriptors: *mut *mut wasmer_import_descriptors_t, -) { - let module = &*(module as *const Module); - let total_imports = module.info().imported_functions.len() - + module.info().imported_tables.len() - + module.info().imported_globals.len() - + module.info().imported_memories.len(); - let mut descriptors: Vec = Vec::with_capacity(total_imports); - - for ( - _index, - ImportName { - namespace_index, - name_index, - }, - ) in &module.info().imported_functions - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_FUNCTION, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_tables - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_TABLE, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_globals - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_GLOBAL, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_memories - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_MEMORY, - }); - } - - let named_import_descriptors: Box = - Box::new(NamedImportDescriptors(descriptors)); - *import_descriptors = - Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t; -} - -pub struct NamedImportDescriptors(Vec); - -/// Frees the memory for the given import descriptors -#[allow(clippy::cast_ptr_alignment)] -#[no_mangle] -pub extern "C" fn wasmer_import_descriptors_destroy( - import_descriptors: *mut wasmer_import_descriptors_t, -) { - if !import_descriptors.is_null() { - unsafe { Box::from_raw(import_descriptors as *mut NamedImportDescriptors) }; - } -} - -/// Gets the length of the import descriptors -#[allow(clippy::cast_ptr_alignment)] -#[no_mangle] -pub unsafe extern "C" fn wasmer_import_descriptors_len( - exports: *mut wasmer_import_descriptors_t, -) -> c_int { - if exports.is_null() { - return 0; - } - (*(exports as *mut NamedImportDescriptors)).0.len() as c_int -} - -/// Gets import descriptor by index -#[allow(clippy::cast_ptr_alignment)] -#[no_mangle] -pub unsafe extern "C" fn wasmer_import_descriptors_get( - import_descriptors: *mut wasmer_import_descriptors_t, - idx: c_int, -) -> *mut wasmer_import_descriptor_t { - if import_descriptors.is_null() { - return ptr::null_mut(); - } - let named_import_descriptors = &mut *(import_descriptors as *mut NamedImportDescriptors); - &mut (*named_import_descriptors).0[idx as usize] as *mut NamedImportDescriptor - as *mut wasmer_import_descriptor_t -} - -/// Gets name for the import descriptor -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_descriptor_name( - import_descriptor: *mut wasmer_import_descriptor_t, -) -> wasmer_byte_array { - let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); - wasmer_byte_array { - bytes: named_import_descriptor.name.as_ptr(), - bytes_len: named_import_descriptor.name.len() as u32, - } -} - -/// Gets module name for the import descriptor -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_descriptor_module_name( - import_descriptor: *mut wasmer_import_descriptor_t, -) -> wasmer_byte_array { - let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); - wasmer_byte_array { - bytes: named_import_descriptor.module.as_ptr(), - bytes_len: named_import_descriptor.module.len() as u32, - } -} - -/// Gets export descriptor kind -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_descriptor_kind( - export: *mut wasmer_import_descriptor_t, -) -> wasmer_import_export_kind { - let named_import_descriptor = &*(export as *mut NamedImportDescriptor); - named_import_descriptor.kind.clone() -} - -/// Sets the result parameter to the arity of the params of the wasmer_import_func_t -/// -/// Returns `wasmer_result_t::WASMER_OK` upon success. -/// -/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` -/// and `wasmer_last_error_message` to get an error message. -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_func_params_arity( - func: *const wasmer_import_func_t, - result: *mut uint32_t, -) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { - *result = signature.params().len() as uint32_t; - wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_params_arity".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } -} - -/// Creates new func -/// -/// The caller owns the object and should call `wasmer_import_func_destroy` to free it. -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_func_new( - func: extern "C" fn(data: *mut c_void), - params: *const wasmer_value_tag, - params_len: c_int, - returns: *const wasmer_value_tag, - returns_len: c_int, -) -> *mut wasmer_import_func_t { - let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize); - let params: Vec = params.iter().cloned().map(|x| x.into()).collect(); - let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize); - let returns: Vec = returns.iter().cloned().map(|x| x.into()).collect(); - - let export = Box::new(Export::Function { - func: FuncPointer::new(func as _), - ctx: Context::Internal, - signature: Arc::new(FuncSig::new(params, returns)), - }); - Box::into_raw(export) as *mut wasmer_import_func_t -} - -/// Sets the params buffer to the parameter types of the given wasmer_import_func_t -/// -/// Returns `wasmer_result_t::WASMER_OK` upon success. -/// -/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` -/// and `wasmer_last_error_message` to get an error message. -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_func_params( - func: *const wasmer_import_func_t, - params: *mut wasmer_value_tag, - params_len: c_int, -) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { - let params: &mut [wasmer_value_tag] = - slice::from_raw_parts_mut(params, params_len as usize); - for (i, item) in signature.params().iter().enumerate() { - params[i] = item.into(); - } - wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_params".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } -} - -/// Sets the returns buffer to the parameter types of the given wasmer_import_func_t -/// -/// Returns `wasmer_result_t::WASMER_OK` upon success. -/// -/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` -/// and `wasmer_last_error_message` to get an error message. -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_func_returns( - func: *const wasmer_import_func_t, - returns: *mut wasmer_value_tag, - returns_len: c_int, -) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { - let returns: &mut [wasmer_value_tag] = - slice::from_raw_parts_mut(returns, returns_len as usize); - for (i, item) in signature.returns().iter().enumerate() { - returns[i] = item.into(); - } - wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_returns".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } -} - -/// Sets the result parameter to the arity of the returns of the wasmer_import_func_t -/// -/// Returns `wasmer_result_t::WASMER_OK` upon success. -/// -/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` -/// and `wasmer_last_error_message` to get an error message. -#[no_mangle] -#[allow(clippy::cast_ptr_alignment)] -pub unsafe extern "C" fn wasmer_import_func_returns_arity( - func: *const wasmer_import_func_t, - result: *mut uint32_t, -) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { - *result = signature.returns().len() as uint32_t; - wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_results_arity".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } -} - -/// Frees memory for the given Func -#[allow(clippy::cast_ptr_alignment)] -#[no_mangle] -pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) { - if !func.is_null() { - unsafe { Box::from_raw(func as *mut Export) }; - } -} - impl From for Value { fn from(v: wasmer_value_t) -> Self { unsafe { @@ -468,9 +126,3 @@ impl From<&wasmer_runtime::wasm::Type> for wasmer_value_tag { } } } - -struct NamedImportDescriptor { - module: String, - name: String, - kind: wasmer_import_export_kind, -} diff --git a/lib/runtime-c-api/src/module.rs b/lib/runtime-c-api/src/module.rs index 18f169621..0e4961ab1 100644 --- a/lib/runtime-c-api/src/module.rs +++ b/lib/runtime-c-api/src/module.rs @@ -2,8 +2,9 @@ use crate::{ error::{update_last_error, CApiError}, - wasmer_byte_array, wasmer_import_export_kind, wasmer_import_t, wasmer_instance_t, - wasmer_result_t, + export::wasmer_import_export_kind, + import::wasmer_import_t, + wasmer_byte_array, wasmer_instance_t, wasmer_result_t, }; use libc::{c_int, uint32_t, uint8_t}; use std::{collections::HashMap, slice};