Merge pull request #232 from Hywan/fix-runtime-c-api-remove-warnings

fix(runtime-c-api) Fix several warnings
This commit is contained in:
Syrus Akbary 2019-03-05 16:38:14 -08:00 committed by GitHub
commit 28bb264266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 179 additions and 185 deletions

View File

@ -17,23 +17,23 @@ use wasmer_runtime_core::module::{ExportIndex, ImportName};
use wasmer_runtime_core::types::{ElementType, FuncSig, MemoryDescriptor, TableDescriptor, Type};
use wasmer_runtime_core::units::{Bytes, Pages};
#[allow(non_camel_case_types)]
pub struct wasmer_module_t();
#[repr(C)]
pub struct wasmer_module_t;
#[repr(C)]
pub struct wasmer_instance_t;
#[repr(C)]
pub struct wasmer_instance_context_t;
#[allow(non_camel_case_types)]
pub struct wasmer_instance_t();
#[allow(non_camel_case_types)]
pub struct wasmer_instance_context_t();
#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_result_t {
WASMER_OK = 1,
WASMER_ERROR = 2,
}
#[allow(non_camel_case_types)]
#[repr(u32)]
#[derive(Clone)]
pub enum wasmer_value_tag {
@ -68,23 +68,23 @@ pub struct wasmer_global_descriptor_t {
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_memory_t();
pub struct wasmer_memory_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_table_t();
pub struct wasmer_table_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_import_func_t();
pub struct wasmer_import_func_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_export_func_t();
pub struct wasmer_export_func_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_global_t();
pub struct wasmer_global_t;
#[repr(C)]
pub struct wasmer_limits_t {
@ -130,6 +130,7 @@ pub struct wasmer_export_descriptor_t;
#[derive(Clone)]
pub struct wasmer_export_descriptors_t;
#[allow(non_camel_case_types)]
#[repr(u32)]
#[derive(Clone)]
pub enum wasmer_import_export_kind {
@ -164,8 +165,8 @@ pub unsafe extern "C" fn wasmer_validate(
if wasm_bytes.is_null() {
return false;
}
let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
let bytes: &[u8] = ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize);
wasmer_runtime_core::validate(bytes)
}
@ -180,7 +181,7 @@ pub unsafe extern "C" fn wasmer_validate(
/// and `wasmer_last_error_message` to get an error message.
#[no_mangle]
pub unsafe extern "C" fn wasmer_memory_new(
mut memory: *mut *mut wasmer_memory_t,
memory: *mut *mut wasmer_memory_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
@ -201,7 +202,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t };
*memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t;
wasmer_result_t::WASMER_OK
}
@ -248,7 +249,7 @@ pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t
/// and `wasmer_last_error_message` to get an error message.
#[no_mangle]
pub unsafe extern "C" fn wasmer_table_new(
mut table: *mut *mut wasmer_table_t,
table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
@ -269,7 +270,7 @@ pub unsafe extern "C" fn wasmer_table_new(
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t };
*table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t;
wasmer_result_t::WASMER_OK
}
@ -301,8 +302,7 @@ pub extern "C" fn wasmer_table_grow(
#[no_mangle]
pub extern "C" fn wasmer_table_length(table: *mut wasmer_table_t) -> uint32_t {
let table = unsafe { &*(table as *mut Table) };
let len = table.size();
len
table.size()
}
/// Frees memory for the given Table
@ -326,7 +326,7 @@ pub unsafe extern "C" fn wasmer_global_new(
} else {
Global::new(value.into())
};
unsafe { Box::into_raw(Box::new(global)) as *mut wasmer_global_t }
Box::into_raw(Box::new(global)) as *mut wasmer_global_t
}
/// Gets the value stored by the given Global
@ -354,11 +354,10 @@ pub extern "C" fn wasmer_global_get_descriptor(
) -> wasmer_global_descriptor_t {
let global = unsafe { &*(global as *mut Global) };
let descriptor = global.descriptor();
let desc = wasmer_global_descriptor_t {
wasmer_global_descriptor_t {
mutable: descriptor.mutable,
kind: descriptor.ty.into(),
};
desc
}
}
/// Frees memory for the given Global
@ -388,12 +387,11 @@ pub extern "C" fn wasmer_memory_destroy(memory: *mut wasmer_memory_t) {
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_compile(
mut module: *mut *mut wasmer_module_t,
module: *mut *mut wasmer_module_t,
wasm_bytes: *mut uint8_t,
wasm_bytes_len: uint32_t,
) -> wasmer_result_t {
let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
let bytes: &[u8] = ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize);
let result = wasmer_runtime::compile(bytes);
let new_module = match result {
Ok(instance) => instance,
@ -402,7 +400,7 @@ pub unsafe extern "C" fn wasmer_compile(
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *module = Box::into_raw(Box::new(new_module)) as *mut wasmer_module_t };
*module = Box::into_raw(Box::new(new_module)) as *mut wasmer_module_t;
wasmer_result_t::WASMER_OK
}
@ -415,8 +413,8 @@ pub unsafe extern "C" fn wasmer_compile(
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_module_instantiate(
module: *mut wasmer_module_t,
mut instance: *mut *mut wasmer_instance_t,
module: *const wasmer_module_t,
instance: *mut *mut wasmer_instance_t,
imports: *mut wasmer_import_t,
imports_len: c_int,
) -> wasmer_result_t {
@ -449,9 +447,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
return wasmer_result_t::WASMER_ERROR;
};
let namespace = namespaces
.entry(module_name)
.or_insert_with(|| Namespace::new());
let namespace = namespaces.entry(module_name).or_insert_with(Namespace::new);
let export = match import.tag {
wasmer_import_export_kind::WASM_MEMORY => {
@ -477,7 +473,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
import_object.register(module_name, namespace);
}
let module = unsafe { &*(module as *mut Module) };
let module = &*(module as *const Module);
let new_instance = if let Ok(res) = module.instantiate(&import_object) {
res
} else {
@ -486,7 +482,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
});
return wasmer_result_t::WASMER_ERROR;
};
unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
*instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t;
wasmer_result_t::WASMER_OK
}
@ -496,18 +492,16 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_export_descriptors(
module: *mut wasmer_module_t,
module: *const wasmer_module_t,
export_descriptors: *mut *mut wasmer_export_descriptors_t,
) {
let mut module = unsafe { &*(module as *mut Module) };
let module = &*(module as *const Module);
let named_export_descriptors: Box<NamedExportDescriptors> = Box::new(NamedExportDescriptors(
module.info().exports.iter().map(|e| e.into()).collect(),
));
unsafe {
*export_descriptors =
Box::into_raw(named_export_descriptors) as *mut wasmer_export_descriptors_t
};
Box::into_raw(named_export_descriptors) as *mut wasmer_export_descriptors_t;
}
pub struct NamedExportDescriptors(Vec<NamedExportDescriptor>);
@ -519,7 +513,9 @@ pub unsafe extern "C" fn wasmer_export_descriptors_destroy(
export_descriptors: *mut wasmer_export_descriptors_t,
) {
if !export_descriptors.is_null() {
drop(unsafe { Box::from_raw(export_descriptors as *mut NamedExportDescriptors) });
drop(Box::from_raw(
export_descriptors as *mut NamedExportDescriptors,
));
}
}
@ -545,11 +541,9 @@ pub unsafe extern "C" fn wasmer_export_descriptors_get(
if export_descriptors.is_null() {
return ptr::null_mut();
}
let mut named_export_descriptors =
unsafe { &mut *(export_descriptors as *mut NamedExportDescriptors) };
let ptr = &mut (*named_export_descriptors).0[idx as usize] as *mut NamedExportDescriptor
as *mut wasmer_export_descriptor_t;
ptr
let named_export_descriptors = &mut *(export_descriptors as *mut NamedExportDescriptors);
&mut (*named_export_descriptors).0[idx as usize] as *mut NamedExportDescriptor
as *mut wasmer_export_descriptor_t
}
/// Gets name for the export descriptor
@ -590,10 +584,10 @@ pub extern "C" fn wasmer_module_destroy(module: *mut wasmer_module_t) {
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_import_descriptors(
module: *mut wasmer_module_t,
module: *const wasmer_module_t,
import_descriptors: *mut *mut wasmer_import_descriptors_t,
) {
let mut module = unsafe { &*(module as *mut Module) };
let module = &*(module as *const Module);
let total_imports = module.info().imported_functions.len()
+ module.info().imported_tables.len()
+ module.info().imported_globals.len()
@ -601,7 +595,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
let mut descriptors: Vec<NamedImportDescriptor> = Vec::with_capacity(total_imports);
for (
index,
_index,
ImportName {
namespace_index,
name_index,
@ -618,7 +612,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
}
for (
index,
_index,
(
ImportName {
namespace_index,
@ -638,7 +632,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
}
for (
index,
_index,
(
ImportName {
namespace_index,
@ -658,7 +652,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
}
for (
index,
_index,
(
ImportName {
namespace_index,
@ -679,10 +673,8 @@ pub unsafe extern "C" fn wasmer_import_descriptors(
let named_import_descriptors: Box<NamedImportDescriptors> =
Box::new(NamedImportDescriptors(descriptors));
unsafe {
*import_descriptors =
Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t
};
Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t;
}
pub struct NamedImportDescriptors(Vec<NamedImportDescriptor>);
@ -694,7 +686,9 @@ pub unsafe extern "C" fn wasmer_import_descriptors_destroy(
import_descriptors: *mut wasmer_import_descriptors_t,
) {
if !import_descriptors.is_null() {
drop(unsafe { Box::from_raw(import_descriptors as *mut NamedImportDescriptors) });
drop(Box::from_raw(
import_descriptors as *mut NamedImportDescriptors,
));
}
}
@ -720,11 +714,9 @@ pub unsafe extern "C" fn wasmer_import_descriptors_get(
if import_descriptors.is_null() {
return ptr::null_mut();
}
let mut named_import_descriptors =
unsafe { &mut *(import_descriptors as *mut NamedImportDescriptors) };
let ptr = &mut (*named_import_descriptors).0[idx as usize] as *mut NamedImportDescriptor
as *mut wasmer_import_descriptor_t;
ptr
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
@ -772,7 +764,7 @@ pub unsafe extern "C" fn wasmer_import_descriptor_kind(
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instantiate(
mut instance: *mut *mut wasmer_instance_t,
instance: *mut *mut wasmer_instance_t,
wasm_bytes: *mut uint8_t,
wasm_bytes_len: uint32_t,
imports: *mut wasmer_import_t,
@ -813,9 +805,7 @@ pub unsafe extern "C" fn wasmer_instantiate(
return wasmer_result_t::WASMER_ERROR;
};
let namespace = namespaces
.entry(module_name)
.or_insert_with(|| Namespace::new());
let namespace = namespaces.entry(module_name).or_insert_with(Namespace::new);
let export = match import.tag {
wasmer_import_export_kind::WASM_MEMORY => {
@ -835,18 +825,17 @@ pub unsafe extern "C" fn wasmer_instantiate(
Export::Table((&*table).clone())
}
};
namespace.insert(import_name, unsafe { export });
namespace.insert(import_name, export);
}
for (module_name, namespace) in namespaces.into_iter() {
import_object.register(module_name, namespace);
}
let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
let bytes: &[u8] = ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize);
let result = wasmer_runtime::instantiate(bytes, &import_object);
let new_instance = match result {
Ok(instance) => instance,
Err(error) => {
Err(_error) => {
// TODO the trait bound `wasmer_runtime::error::Error: std::error::Error` is not satisfied
//update_last_error(error);
update_last_error(CApiError {
@ -855,7 +844,7 @@ pub unsafe extern "C" fn wasmer_instantiate(
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
*instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t;
wasmer_result_t::WASMER_OK
}
@ -898,7 +887,7 @@ pub unsafe extern "C" fn wasmer_instance_call(
let params: &[wasmer_value_t] = slice::from_raw_parts(params, params_len as usize);
let params: Vec<Value> = params.iter().cloned().map(|x| x.into()).collect();
let func_name_c = unsafe { CStr::from_ptr(name) };
let func_name_c = CStr::from_ptr(name);
let func_name_r = func_name_c.to_str().unwrap();
let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize);
@ -906,7 +895,7 @@ pub unsafe extern "C" fn wasmer_instance_call(
match result {
Ok(results_vec) => {
if results_vec.len() > 0 {
if !results_vec.is_empty() {
let ret = match results_vec[0] {
Value::I32(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_I32,
@ -945,7 +934,7 @@ pub unsafe extern "C" fn wasmer_instance_exports(
instance: *mut wasmer_instance_t,
exports: *mut *mut wasmer_exports_t,
) {
let mut instance_ref = unsafe { &mut *(instance as *mut Instance) };
let instance_ref = &mut *(instance as *mut Instance);
let mut exports_vec: Vec<NamedExport> = Vec::with_capacity(instance_ref.exports().count());
for (name, export) in instance_ref.exports() {
exports_vec.push(NamedExport {
@ -955,7 +944,7 @@ pub unsafe extern "C" fn wasmer_instance_exports(
});
}
let named_exports: Box<NamedExports> = Box::new(NamedExports(exports_vec));
unsafe { *exports = Box::into_raw(named_exports) as *mut wasmer_exports_t };
*exports = Box::into_raw(named_exports) as *mut wasmer_exports_t;
}
pub struct NamedExports(Vec<NamedExport>);
@ -965,7 +954,7 @@ pub struct NamedExports(Vec<NamedExport>);
#[no_mangle]
pub unsafe extern "C" fn wasmer_exports_destroy(exports: *mut wasmer_exports_t) {
if !exports.is_null() {
drop(unsafe { Box::from_raw(exports as *mut NamedExports) });
drop(Box::from_raw(exports as *mut NamedExports));
}
}
@ -989,9 +978,8 @@ pub unsafe extern "C" fn wasmer_exports_get(
if exports.is_null() {
return ptr::null_mut();
}
let mut named_exports = unsafe { &mut *(exports as *mut NamedExports) };
let ptr = &mut (*named_exports).0[idx as usize] as *mut NamedExport as *mut wasmer_export_t;
ptr
let named_exports = &mut *(exports as *mut NamedExports);
&mut (*named_exports).0[idx as usize] as *mut NamedExport as *mut wasmer_export_t
}
/// Gets wasmer_export kind
@ -1018,21 +1006,20 @@ pub unsafe extern "C" fn wasmer_export_kind(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_export_func_params_arity(
func: *mut wasmer_export_func_t,
func: *const wasmer_export_func_t,
result: *mut uint32_t,
) -> wasmer_result_t {
let mut named_export = unsafe { &*(func as *mut NamedExport) };
let mut export = &named_export.export;
let result = if let Export::Function { ref signature, .. } = *export {
unsafe { *result = signature.params().len() as uint32_t };
let named_export = &*(func as *const NamedExport);
let export = &named_export.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_export_func_params_arity".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the params buffer to the parameter types of the given wasmer_export_func_t
@ -1044,13 +1031,13 @@ pub unsafe extern "C" fn wasmer_export_func_params_arity(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_export_func_params(
func: *mut wasmer_export_func_t,
func: *const wasmer_export_func_t,
params: *mut wasmer_value_tag,
params_len: c_int,
) -> wasmer_result_t {
let mut named_export = unsafe { &*(func as *mut NamedExport) };
let mut export = &named_export.export;
let result = if let Export::Function { ref signature, .. } = *export {
let named_export = &*(func as *const NamedExport);
let export = &named_export.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() {
@ -1062,8 +1049,7 @@ pub unsafe extern "C" fn wasmer_export_func_params(
msg: "func ptr error in wasmer_export_func_params".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the returns buffer to the parameter types of the given wasmer_export_func_t
@ -1075,13 +1061,13 @@ pub unsafe extern "C" fn wasmer_export_func_params(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_export_func_returns(
func: *mut wasmer_export_func_t,
func: *const wasmer_export_func_t,
returns: *mut wasmer_value_tag,
returns_len: c_int,
) -> wasmer_result_t {
let mut named_export = unsafe { &*(func as *mut NamedExport) };
let mut export = &named_export.export;
let result = if let Export::Function { ref signature, .. } = *export {
let named_export = &*(func as *const NamedExport);
let export = &named_export.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() {
@ -1093,8 +1079,7 @@ pub unsafe extern "C" fn wasmer_export_func_returns(
msg: "func ptr error in wasmer_export_func_returns".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the result parameter to the arity of the returns of the wasmer_export_func_t
@ -1106,21 +1091,20 @@ pub unsafe extern "C" fn wasmer_export_func_returns(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_export_func_returns_arity(
func: *mut wasmer_export_func_t,
func: *const wasmer_export_func_t,
result: *mut uint32_t,
) -> wasmer_result_t {
let mut named_export = unsafe { &*(func as *mut NamedExport) };
let mut export = &named_export.export;
let result = if let Export::Function { ref signature, .. } = *export {
unsafe { *result = signature.returns().len() as uint32_t };
let named_export = &*(func as *const NamedExport);
let export = &named_export.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_export_func_results_arity".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the result parameter to the arity of the params of the wasmer_import_func_t
@ -1132,20 +1116,19 @@ pub unsafe extern "C" fn wasmer_export_func_returns_arity(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_import_func_params_arity(
func: *mut wasmer_import_func_t,
func: *const wasmer_import_func_t,
result: *mut uint32_t,
) -> wasmer_result_t {
let mut export = unsafe { &mut *(func as *mut Export) };
let result = if let Export::Function { ref signature, .. } = *export {
unsafe { *result = signature.params().len() as uint32_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
};
result
}
}
/// Creates new func
@ -1166,7 +1149,7 @@ pub unsafe extern "C" fn wasmer_import_func_new(
let returns: Vec<Type> = returns.iter().cloned().map(|x| x.into()).collect();
let export = Box::new(Export::Function {
func: unsafe { FuncPointer::new(func as _) },
func: FuncPointer::new(func as _),
ctx: Context::Internal,
signature: Arc::new(FuncSig::new(params, returns)),
});
@ -1182,12 +1165,12 @@ pub unsafe extern "C" fn wasmer_import_func_new(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_import_func_params(
func: *mut wasmer_import_func_t,
func: *const wasmer_import_func_t,
params: *mut wasmer_value_tag,
params_len: c_int,
) -> wasmer_result_t {
let mut export = unsafe { &mut *(func as *mut Export) };
let result = if let Export::Function { ref signature, .. } = *export {
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() {
@ -1199,8 +1182,7 @@ pub unsafe extern "C" fn wasmer_import_func_params(
msg: "func ptr error in wasmer_import_func_params".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the returns buffer to the parameter types of the given wasmer_import_func_t
@ -1212,12 +1194,12 @@ pub unsafe extern "C" fn wasmer_import_func_params(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_import_func_returns(
func: *mut wasmer_import_func_t,
func: *const wasmer_import_func_t,
returns: *mut wasmer_value_tag,
returns_len: c_int,
) -> wasmer_result_t {
let mut export = unsafe { &mut *(func as *mut Export) };
let result = if let Export::Function { ref signature, .. } = *export {
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() {
@ -1229,8 +1211,7 @@ pub unsafe extern "C" fn wasmer_import_func_returns(
msg: "func ptr error in wasmer_import_func_returns".to_string(),
});
wasmer_result_t::WASMER_ERROR
};
result
}
}
/// Sets the result parameter to the arity of the returns of the wasmer_import_func_t
@ -1242,20 +1223,19 @@ pub unsafe extern "C" fn wasmer_import_func_returns(
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_import_func_returns_arity(
func: *mut wasmer_import_func_t,
func: *const wasmer_import_func_t,
result: *mut uint32_t,
) -> wasmer_result_t {
let mut export = unsafe { &*(func as *mut Export) };
let result = if let Export::Function { ref signature, .. } = *export {
unsafe { *result = signature.returns().len() as uint32_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
};
result
}
}
/// Frees memory for the given Func
@ -1271,7 +1251,7 @@ pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) {
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_export_to_func(
export: *mut wasmer_export_t,
export: *const wasmer_export_t,
) -> *const wasmer_export_func_t {
export as *const wasmer_export_func_t
}
@ -1297,7 +1277,7 @@ pub unsafe extern "C" fn wasmer_export_name(export: *mut wasmer_export_t) -> was
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_export_func_call(
func: *mut wasmer_export_func_t,
func: *const wasmer_export_func_t,
params: *const wasmer_value_t,
params_len: c_int,
results: *mut wasmer_value_t,
@ -1319,7 +1299,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(
let params: &[wasmer_value_t] = slice::from_raw_parts(params, params_len as usize);
let params: Vec<Value> = params.iter().cloned().map(|x| x.into()).collect();
let named_export = unsafe { &*(func as *mut NamedExport) };
let named_export = &*(func as *mut NamedExport);
let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize);
@ -1327,7 +1307,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(
let result = instance.call(&named_export.name, &params[..]);
match result {
Ok(results_vec) => {
if results_vec.len() > 0 {
if !results_vec.is_empty() {
let ret = match results_vec[0] {
Value::I32(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_I32,
@ -1363,9 +1343,9 @@ pub unsafe extern "C" fn wasmer_export_func_call(
#[no_mangle]
pub extern "C" fn wasmer_instance_context_memory(
ctx: *mut wasmer_instance_context_t,
memory_idx: uint32_t,
_memory_idx: uint32_t,
) -> *const wasmer_memory_t {
let mut ctx = unsafe { &mut *(ctx as *mut Ctx) };
let ctx = unsafe { &*(ctx as *const Ctx) };
let memory = ctx.memory(0);
memory as *const Memory as *const wasmer_memory_t
}
@ -1402,19 +1382,19 @@ impl From<wasmer_value_t> for Value {
unsafe {
match v {
wasmer_value_t {
tag: WASM_I32,
tag: wasmer_value_tag::WASM_I32,
value: wasmer_value { I32 },
} => Value::I32(I32),
wasmer_value_t {
tag: WASM_I64,
tag: wasmer_value_tag::WASM_I64,
value: wasmer_value { I64 },
} => Value::I64(I64),
wasmer_value_t {
tag: WASM_F32,
tag: wasmer_value_tag::WASM_F32,
value: wasmer_value { F32 },
} => Value::F32(F32),
wasmer_value_t {
tag: WASM_F64,
tag: wasmer_value_tag::WASM_F64,
value: wasmer_value { F64 },
} => Value::F64(F64),
_ => panic!("not implemented"),
@ -1460,7 +1440,6 @@ impl From<Type> for wasmer_value_tag {
impl From<wasmer_value_tag> for Type {
fn from(v: wasmer_value_tag) -> Self {
unsafe {
match v {
wasmer_value_tag::WASM_I32 => Type::I32,
wasmer_value_tag::WASM_I64 => Type::I64,
@ -1469,7 +1448,6 @@ impl From<wasmer_value_tag> for Type {
_ => panic!("not implemented"),
}
}
}
}
impl From<&wasmer_runtime::wasm::Type> for wasmer_value_tag {

View File

@ -27,11 +27,9 @@ enum wasmer_value_tag {
};
typedef uint32_t wasmer_value_tag;
typedef struct wasmer_instance_context_t wasmer_instance_context_t;
typedef struct {
typedef struct wasmer_instance_t wasmer_instance_t;
typedef struct wasmer_module_t wasmer_module_t;
} wasmer_module_t;
typedef struct {
@ -93,10 +91,18 @@ typedef struct {
typedef struct {
} wasmer_instance_t;
typedef struct {
} wasmer_memory_t;
typedef struct {
} wasmer_instance_context_t;
typedef struct {
} wasmer_table_t;
typedef union {
@ -147,7 +153,7 @@ wasmer_byte_array wasmer_export_descriptor_name(wasmer_export_descriptor_t *expo
* Gets export descriptors for the given module
* The caller owns the object and should call `wasmer_export_descriptors_destroy` to free it.
*/
void wasmer_export_descriptors(wasmer_module_t *module,
void wasmer_export_descriptors(const wasmer_module_t *module,
wasmer_export_descriptors_t **export_descriptors);
/**
@ -173,7 +179,7 @@ int wasmer_export_descriptors_len(wasmer_export_descriptors_t *exports);
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_export_func_call(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_call(const wasmer_export_func_t *func,
const wasmer_value_t *params,
int params_len,
wasmer_value_t *results,
@ -185,7 +191,7 @@ wasmer_result_t wasmer_export_func_call(wasmer_export_func_t *func,
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_export_func_params(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_params(const wasmer_export_func_t *func,
wasmer_value_tag *params,
int params_len);
@ -195,7 +201,7 @@ wasmer_result_t wasmer_export_func_params(wasmer_export_func_t *func,
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_export_func_params_arity(wasmer_export_func_t *func, uint32_t *result);
wasmer_result_t wasmer_export_func_params_arity(const wasmer_export_func_t *func, uint32_t *result);
/**
* Sets the returns buffer to the parameter types of the given wasmer_export_func_t
@ -203,7 +209,7 @@ wasmer_result_t wasmer_export_func_params_arity(wasmer_export_func_t *func, uint
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_export_func_returns(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_returns(const wasmer_export_func_t *func,
wasmer_value_tag *returns,
int returns_len);
@ -213,7 +219,8 @@ wasmer_result_t wasmer_export_func_returns(wasmer_export_func_t *func,
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_export_func_returns_arity(wasmer_export_func_t *func, uint32_t *result);
wasmer_result_t wasmer_export_func_returns_arity(const wasmer_export_func_t *func,
uint32_t *result);
/**
* Gets wasmer_export kind
@ -228,7 +235,7 @@ wasmer_byte_array wasmer_export_name(wasmer_export_t *export_);
/**
* Gets export func from export
*/
const wasmer_export_func_t *wasmer_export_to_func(wasmer_export_t *export_);
const wasmer_export_func_t *wasmer_export_to_func(const wasmer_export_t *export_);
/**
* Frees the memory for the given exports
@ -290,7 +297,7 @@ wasmer_byte_array wasmer_import_descriptor_name(wasmer_import_descriptor_t *impo
* Gets import descriptors for the given module
* The caller owns the object and should call `wasmer_import_descriptors_destroy` to free it.
*/
void wasmer_import_descriptors(wasmer_module_t *module,
void wasmer_import_descriptors(const wasmer_module_t *module,
wasmer_import_descriptors_t **import_descriptors);
/**
@ -330,7 +337,7 @@ const wasmer_import_func_t *wasmer_import_func_new(void (*func)(void *data),
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_import_func_params(wasmer_import_func_t *func,
wasmer_result_t wasmer_import_func_params(const wasmer_import_func_t *func,
wasmer_value_tag *params,
int params_len);
@ -340,7 +347,7 @@ wasmer_result_t wasmer_import_func_params(wasmer_import_func_t *func,
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_import_func_params_arity(wasmer_import_func_t *func, uint32_t *result);
wasmer_result_t wasmer_import_func_params_arity(const wasmer_import_func_t *func, uint32_t *result);
/**
* Sets the returns buffer to the parameter types of the given wasmer_import_func_t
@ -348,7 +355,7 @@ wasmer_result_t wasmer_import_func_params_arity(wasmer_import_func_t *func, uint
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_import_func_returns(wasmer_import_func_t *func,
wasmer_result_t wasmer_import_func_returns(const wasmer_import_func_t *func,
wasmer_value_tag *returns,
int returns_len);
@ -358,7 +365,8 @@ wasmer_result_t wasmer_import_func_returns(wasmer_import_func_t *func,
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_import_func_returns_arity(wasmer_import_func_t *func, uint32_t *result);
wasmer_result_t wasmer_import_func_returns_arity(const wasmer_import_func_t *func,
uint32_t *result);
/**
* Calls an instances exported function by `name` with the provided parameters.
@ -379,7 +387,7 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance,
* The index is always 0 until multiple memories are supported.
*/
const wasmer_memory_t *wasmer_instance_context_memory(wasmer_instance_context_t *ctx,
uint32_t memory_idx);
uint32_t _memory_idx);
/**
* Frees memory for the given Instance
@ -480,7 +488,7 @@ void wasmer_module_destroy(wasmer_module_t *module);
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_module_instantiate(wasmer_module_t *module,
wasmer_result_t wasmer_module_instantiate(const wasmer_module_t *module,
wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);

View File

@ -24,11 +24,9 @@ enum class wasmer_value_tag : uint32_t {
WASM_F64,
};
struct wasmer_instance_context_t;
struct wasmer_module_t {
struct wasmer_instance_t;
struct wasmer_module_t;
};
struct wasmer_export_descriptor_t {
@ -88,10 +86,18 @@ struct wasmer_import_func_t {
};
struct wasmer_instance_t {
};
struct wasmer_memory_t {
};
struct wasmer_instance_context_t {
};
struct wasmer_table_t {
};
@ -138,7 +144,7 @@ wasmer_byte_array wasmer_export_descriptor_name(wasmer_export_descriptor_t *expo
/// Gets export descriptors for the given module
/// The caller owns the object and should call `wasmer_export_descriptors_destroy` to free it.
void wasmer_export_descriptors(wasmer_module_t *module,
void wasmer_export_descriptors(const wasmer_module_t *module,
wasmer_export_descriptors_t **export_descriptors);
/// Frees the memory for the given export descriptors
@ -156,7 +162,7 @@ int wasmer_export_descriptors_len(wasmer_export_descriptors_t *exports);
/// 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.
wasmer_result_t wasmer_export_func_call(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_call(const wasmer_export_func_t *func,
const wasmer_value_t *params,
int params_len,
wasmer_value_t *results,
@ -166,7 +172,7 @@ wasmer_result_t wasmer_export_func_call(wasmer_export_func_t *func,
/// 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.
wasmer_result_t wasmer_export_func_params(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_params(const wasmer_export_func_t *func,
wasmer_value_tag *params,
int params_len);
@ -174,13 +180,13 @@ wasmer_result_t wasmer_export_func_params(wasmer_export_func_t *func,
/// 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.
wasmer_result_t wasmer_export_func_params_arity(wasmer_export_func_t *func, uint32_t *result);
wasmer_result_t wasmer_export_func_params_arity(const wasmer_export_func_t *func, uint32_t *result);
/// Sets the returns buffer to the parameter types of the given wasmer_export_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.
wasmer_result_t wasmer_export_func_returns(wasmer_export_func_t *func,
wasmer_result_t wasmer_export_func_returns(const wasmer_export_func_t *func,
wasmer_value_tag *returns,
int returns_len);
@ -188,7 +194,8 @@ wasmer_result_t wasmer_export_func_returns(wasmer_export_func_t *func,
/// 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.
wasmer_result_t wasmer_export_func_returns_arity(wasmer_export_func_t *func, uint32_t *result);
wasmer_result_t wasmer_export_func_returns_arity(const wasmer_export_func_t *func,
uint32_t *result);
/// Gets wasmer_export kind
wasmer_import_export_kind wasmer_export_kind(wasmer_export_t *export_);
@ -197,7 +204,7 @@ wasmer_import_export_kind wasmer_export_kind(wasmer_export_t *export_);
wasmer_byte_array wasmer_export_name(wasmer_export_t *export_);
/// Gets export func from export
const wasmer_export_func_t *wasmer_export_to_func(wasmer_export_t *export_);
const wasmer_export_func_t *wasmer_export_to_func(const wasmer_export_t *export_);
/// Frees the memory for the given exports
void wasmer_exports_destroy(wasmer_exports_t *exports);
@ -235,7 +242,7 @@ wasmer_byte_array wasmer_import_descriptor_name(wasmer_import_descriptor_t *impo
/// Gets import descriptors for the given module
/// The caller owns the object and should call `wasmer_import_descriptors_destroy` to free it.
void wasmer_import_descriptors(wasmer_module_t *module,
void wasmer_import_descriptors(const wasmer_module_t *module,
wasmer_import_descriptors_t **import_descriptors);
/// Frees the memory for the given import descriptors
@ -263,7 +270,7 @@ const wasmer_import_func_t *wasmer_import_func_new(void (*func)(void *data),
/// 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.
wasmer_result_t wasmer_import_func_params(wasmer_import_func_t *func,
wasmer_result_t wasmer_import_func_params(const wasmer_import_func_t *func,
wasmer_value_tag *params,
int params_len);
@ -271,13 +278,13 @@ wasmer_result_t wasmer_import_func_params(wasmer_import_func_t *func,
/// 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.
wasmer_result_t wasmer_import_func_params_arity(wasmer_import_func_t *func, uint32_t *result);
wasmer_result_t wasmer_import_func_params_arity(const wasmer_import_func_t *func, uint32_t *result);
/// 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.
wasmer_result_t wasmer_import_func_returns(wasmer_import_func_t *func,
wasmer_result_t wasmer_import_func_returns(const wasmer_import_func_t *func,
wasmer_value_tag *returns,
int returns_len);
@ -285,7 +292,8 @@ wasmer_result_t wasmer_import_func_returns(wasmer_import_func_t *func,
/// 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.
wasmer_result_t wasmer_import_func_returns_arity(wasmer_import_func_t *func, uint32_t *result);
wasmer_result_t wasmer_import_func_returns_arity(const wasmer_import_func_t *func,
uint32_t *result);
/// Calls an instances exported function by `name` with the provided parameters.
/// Results are set using the provided `results` pointer.
@ -302,7 +310,7 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance,
/// Gets the memory within the context at the index `memory_idx`.
/// The index is always 0 until multiple memories are supported.
const wasmer_memory_t *wasmer_instance_context_memory(wasmer_instance_context_t *ctx,
uint32_t memory_idx);
uint32_t _memory_idx);
/// Frees memory for the given Instance
void wasmer_instance_destroy(wasmer_instance_t *instance);
@ -377,7 +385,7 @@ void wasmer_module_destroy(wasmer_module_t *module);
/// 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.
wasmer_result_t wasmer_module_instantiate(wasmer_module_t *module,
wasmer_result_t wasmer_module_instantiate(const wasmer_module_t *module,
wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);