Update ImportObject C API to use iterators

This commit is contained in:
Mark McCaskey
2019-10-29 14:55:14 -07:00
parent aa82df7bc5
commit a104d08c04
5 changed files with 208 additions and 123 deletions

View File

@ -13,7 +13,7 @@ use std::{convert::TryFrom, ffi::c_void, ptr, slice, sync::Arc};
use wasmer_runtime::{Global, Memory, Module, Table}; use wasmer_runtime::{Global, Memory, Module, Table};
use wasmer_runtime_core::{ use wasmer_runtime_core::{
export::{Context, Export, FuncPointer}, export::{Context, Export, FuncPointer},
import::ImportObject, import::{ImportObject, ImportObjectIterator},
module::ImportName, module::ImportName,
types::{FuncSig, Type}, types::{FuncSig, Type},
}; };
@ -41,6 +41,10 @@ pub struct wasmer_import_descriptor_t;
#[derive(Clone)] #[derive(Clone)]
pub struct wasmer_import_descriptors_t; pub struct wasmer_import_descriptors_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_import_object_iter_t;
/// Creates a new empty import object. /// Creates a new empty import object.
/// See also `wasmer_import_object_append` /// See also `wasmer_import_object_append`
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
@ -58,12 +62,11 @@ mod wasi;
pub use self::wasi::*; pub use self::wasi::*;
/// Gets an entry from an ImportObject at the name and namespace. /// Gets an entry from an ImportObject at the name and namespace.
/// Stores an immutable reference to `name` and `namespace` in `import`. /// Stores `name`, `namespace`, and `import_export_value` in `import`.
/// Thus these must remain valid for the lifetime of `import`.
/// ///
/// The caller owns all data involved. /// The caller owns all data involved.
/// `import_export_value` will be written to based on `tag`, `import_export_value` must be /// `import_export_value` will be written to based on `tag`.
/// initialized to point to the type specified by `tag`. Failure to do so may result
/// in data corruption or undefined behavior.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn wasmer_import_object_get_import( pub unsafe extern "C" fn wasmer_import_object_get_import(
import_object: *const wasmer_import_object_t, import_object: *const wasmer_import_object_t,
@ -169,95 +172,157 @@ pub unsafe extern "C" fn wasmer_import_object_get_import(
} }
} }
/// private wrapper data type used for casting
#[repr(C)]
struct WasmerImportObjectIterator(
std::iter::Peekable<Box<dyn Iterator<Item = <ImportObjectIterator as Iterator>::Item>>>,
);
/// Create an iterator over the functions in the import object.
/// Get the next import with `wasmer_import_object_iter_next`
/// Free the iterator with `wasmer_import_object_iter_destroy`
#[no_mangle] #[no_mangle]
/// Get the number of functions that an import object contains. pub unsafe extern "C" fn wasmer_import_object_iterate_functions(
/// The result of this is useful as an argument to `wasmer_import_object_get_functions`.
/// This function returns -1 on error.
pub unsafe extern "C" fn wasmer_import_object_get_num_functions(
import_object: *const wasmer_import_object_t, import_object: *const wasmer_import_object_t,
) -> i32 { ) -> *mut wasmer_import_object_iter_t {
if import_object.is_null() { if import_object.is_null() {
update_last_error(CApiError { update_last_error(CApiError {
msg: "import_object must not be null".to_owned(), msg: "import_object must not be null".to_owned(),
}); });
return -1; return std::ptr::null_mut();
} }
let import_object: &ImportObject = &*(import_object as *const ImportObject); let import_object: &ImportObject = &*(import_object as *const ImportObject);
import_object let iter_inner = Box::new(import_object.clone_ref().into_iter().filter(|(_, _, e)| {
.clone_ref()
.into_iter()
.filter(|(_, _, e)| {
if let Export::Function { .. } = e { if let Export::Function { .. } = e {
true true
} else { } else {
false false
} }
}) })) as Box<dyn Iterator<Item = <ImportObjectIterator as Iterator>::Item>>;
.count() as i32 let iterator = Box::new(WasmerImportObjectIterator(iter_inner.peekable()));
Box::into_raw(iterator) as *mut wasmer_import_object_iter_t
} }
/// Writes the next value to `import`. `WASMER_ERROR` is returned if there
/// was an error or there's nothing left to return.
///
/// To free the memory allocated here, pass the import to `wasmer_import_object_imports_destroy`.
/// To check if the iterator is done, use `wasmer_import_object_iter_at_end`.
#[no_mangle] #[no_mangle]
/// Call `wasmer_import_object_imports_destroy` to free the memory allocated by this function. pub unsafe extern "C" fn wasmer_import_object_iter_next(
/// This function return -1 on error. import_object_iter: *mut wasmer_import_object_iter_t,
pub unsafe extern "C" fn wasmer_import_object_get_functions( import: *mut wasmer_import_t,
import_object: *const wasmer_import_object_t, ) -> wasmer_result_t {
imports: *mut wasmer_import_t, if import_object_iter.is_null() || import.is_null() {
imports_len: u32,
) -> i32 {
if import_object.is_null() || imports.is_null() {
update_last_error(CApiError { update_last_error(CApiError {
msg: "import_object and imports must not be null".to_owned(), msg: "import_object_iter and import must not be null".to_owned(),
}); });
return -1; return wasmer_result_t::WASMER_ERROR;
} }
let import_object: &ImportObject = &*(import_object as *const ImportObject);
let mut i = 0; let iter = &mut *(import_object_iter as *mut WasmerImportObjectIterator);
for (namespace, name, export) in import_object.clone_ref().into_iter() { let out = &mut *import;
if i + 1 > imports_len { // TODO: the copying here can be optimized away, we just need to use a different type of
return i as i32; // iterator internally
} if let Some((namespace, name, export)) = iter.0.next() {
match export { let ns = {
Export::Function { .. } => { let mut n = namespace.clone();
let ns = namespace.clone().into_bytes(); n.shrink_to_fit();
n.into_bytes()
};
let ns_bytes = wasmer_byte_array { let ns_bytes = wasmer_byte_array {
bytes: ns.as_ptr(), bytes: ns.as_ptr(),
bytes_len: ns.len() as u32, bytes_len: ns.len() as u32,
}; };
std::mem::forget(ns);
let name = name.clone().into_bytes(); let name = {
let mut n = name.clone();
n.shrink_to_fit();
n.into_bytes()
};
let name_bytes = wasmer_byte_array { let name_bytes = wasmer_byte_array {
bytes: name.as_ptr(), bytes: name.as_ptr(),
bytes_len: name.len() as u32, bytes_len: name.len() as u32,
}; };
out.module_name = ns_bytes;
out.import_name = name_bytes;
std::mem::forget(ns);
std::mem::forget(name); std::mem::forget(name);
match export {
Export::Function { .. } => {
let func = Box::new(export.clone()); let func = Box::new(export.clone());
let new_entry = wasmer_import_t { out.tag = wasmer_import_export_kind::WASM_FUNCTION;
module_name: ns_bytes, out.value = wasmer_import_export_value {
import_name: name_bytes,
tag: wasmer_import_export_kind::WASM_FUNCTION,
value: wasmer_import_export_value {
func: Box::into_raw(func) as *mut _ as *const _, func: Box::into_raw(func) as *mut _ as *const _,
},
}; };
*imports.add(i as usize) = new_entry;
i += 1;
} }
_ => (), Export::Global(global) => {
let glbl = Box::new(global.clone());
out.tag = wasmer_import_export_kind::WASM_GLOBAL;
out.value = wasmer_import_export_value {
global: Box::into_raw(glbl) as *mut _ as *const _,
};
}
Export::Memory(memory) => {
let mem = Box::new(memory.clone());
out.tag = wasmer_import_export_kind::WASM_MEMORY;
out.value = wasmer_import_export_value {
memory: Box::into_raw(mem) as *mut _ as *const _,
};
}
Export::Table(table) => {
let tbl = Box::new(table.clone());
out.tag = wasmer_import_export_kind::WASM_TABLE;
out.value = wasmer_import_export_value {
memory: Box::into_raw(tbl) as *mut _ as *const _,
};
} }
} }
return i as i32; wasmer_result_t::WASMER_OK
} else {
wasmer_result_t::WASMER_ERROR
}
} }
/// Returns true if further calls to `wasmer_import_object_iter_next` will
/// not return any new data
#[no_mangle] #[no_mangle]
/// Frees the memory acquired in `wasmer_import_object_get_functions` pub unsafe extern "C" fn wasmer_import_object_iter_at_end(
import_object_iter: *mut wasmer_import_object_iter_t,
) -> bool {
if import_object_iter.is_null() {
update_last_error(CApiError {
msg: "import_object_iter must not be null".to_owned(),
});
return true;
}
let iter = &mut *(import_object_iter as *mut WasmerImportObjectIterator);
iter.0.peek().is_none()
}
/// Frees the memory allocated by `wasmer_import_object_iterate_functions`
#[no_mangle]
pub unsafe extern "C" fn wasmer_import_object_iter_destroy(
import_object_iter: *mut wasmer_import_object_iter_t,
) {
let _ = Box::from_raw(import_object_iter as *mut WasmerImportObjectIterator);
}
/// Frees the memory allocated in `wasmer_import_object_iter_next`
/// ///
/// This function does not free the memory in `wasmer_import_object_t`; /// This function does not free the memory in `wasmer_import_object_t`;
/// it only frees memory allocated while querying a `wasmer_import_object_t`. /// it only frees memory allocated while querying a `wasmer_import_object_t`.
#[no_mangle]
pub unsafe extern "C" fn wasmer_import_object_imports_destroy( pub unsafe extern "C" fn wasmer_import_object_imports_destroy(
imports: *mut wasmer_import_t, imports: *mut wasmer_import_t,
imports_len: u32, imports_len: u32,

View File

@ -217,34 +217,26 @@ int main()
assert(call_result == WASMER_OK); assert(call_result == WASMER_OK);
assert(host_print_called); assert(host_print_called);
int32_t num_functions = wasmer_import_object_get_num_functions(import_object); wasmer_import_object_iter_t *func_iter = wasmer_import_object_iterate_functions(import_object);
if (num_functions == -1) {
print_wasmer_error();
return -1;
}
wasmer_import_t *func_array = malloc(sizeof(wasmer_import_t) * num_functions);
assert(func_array);
int32_t num_returned = wasmer_import_object_get_functions(import_object, func_array, num_functions); puts("Functions in import object:");
if (num_functions == -1) { while ( !wasmer_import_object_iter_at_end(func_iter) ) {
print_wasmer_error(); wasmer_import_t import;
return -1; wasmer_result_t result = wasmer_import_object_iter_next(func_iter, &import);
} assert(result == WASMER_OK);
assert(num_functions == num_returned);
printf("Found %d functions in import object:\n", num_returned); print_byte_array(&import.module_name);
for (int i = 0; i < num_returned; ++i) {
print_byte_array(&func_array[i].module_name);
putchar(' '); putchar(' ');
print_byte_array(&func_array[i].import_name); print_byte_array(&import.import_name);
putchar('\n'); putchar('\n');
assert(func_array[i].tag == WASM_FUNCTION);
assert(func_array[i].value.func); assert(import.tag == WASM_FUNCTION);
assert(import.value.func);
wasmer_import_object_imports_destroy(&import, 1);
} }
wasmer_import_object_iter_destroy(func_iter);
// Use *_destroy methods to cleanup as specified in the header documentation // Use *_destroy methods to cleanup as specified in the header documentation
wasmer_import_object_imports_destroy(func_array, num_returned);
free(func_array);
wasmer_import_func_destroy(func); wasmer_import_func_destroy(func);
wasmer_global_destroy(global); wasmer_global_destroy(global);
wasmer_memory_destroy(memory); wasmer_memory_destroy(memory);

View File

@ -138,6 +138,10 @@ typedef struct {
typedef struct { typedef struct {
} wasmer_import_object_iter_t;
typedef struct {
} wasmer_instance_t; } wasmer_instance_t;
typedef struct { typedef struct {
@ -469,22 +473,13 @@ wasmer_result_t wasmer_import_object_extend(wasmer_import_object_t *import_objec
const wasmer_import_t *imports, const wasmer_import_t *imports,
unsigned int imports_len); unsigned int imports_len);
/**
* Call `wasmer_import_object_imports_destroy` to free the memory allocated by this function.
* This function return -1 on error.
*/
int32_t wasmer_import_object_get_functions(const wasmer_import_object_t *import_object,
wasmer_import_t *imports,
uint32_t imports_len);
/** /**
* Gets an entry from an ImportObject at the name and namespace. * Gets an entry from an ImportObject at the name and namespace.
* Stores an immutable reference to `name` and `namespace` in `import`. * Stores `name`, `namespace`, and `import_export_value` in `import`.
* Thus these must remain valid for the lifetime of `import`.
* *
* The caller owns all data involved. * The caller owns all data involved.
* `import_export_value` will be written to based on `tag`, `import_export_value` must be * `import_export_value` will be written to based on `tag`.
* initialized to point to the type specified by `tag`. Failure to do so may result
* in data corruption or undefined behavior.
*/ */
wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *import_object, wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *import_object,
wasmer_byte_array namespace_, wasmer_byte_array namespace_,
@ -494,20 +489,41 @@ wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *im
uint32_t tag); uint32_t tag);
/** /**
* Get the number of functions that an import object contains. * Frees the memory allocated in `wasmer_import_object_iter_next`
* The result of this is useful as an argument to `wasmer_import_object_get_functions`.
* This function returns -1 on error.
*/
int32_t wasmer_import_object_get_num_functions(const wasmer_import_object_t *import_object);
/**
* Frees the memory acquired in `wasmer_import_object_get_functions`
* *
* This function does not free the memory in `wasmer_import_object_t`; * This function does not free the memory in `wasmer_import_object_t`;
* it only frees memory allocated while querying a `wasmer_import_object_t`. * it only frees memory allocated while querying a `wasmer_import_object_t`.
*/ */
void wasmer_import_object_imports_destroy(wasmer_import_t *imports, uint32_t imports_len); void wasmer_import_object_imports_destroy(wasmer_import_t *imports, uint32_t imports_len);
/**
* Returns true if further calls to `wasmer_import_object_iter_next` will
* not return any new data
*/
bool wasmer_import_object_iter_at_end(wasmer_import_object_iter_t *import_object_iter);
/**
* Frees the memory allocated by `wasmer_import_object_iterate_functions`
*/
void wasmer_import_object_iter_destroy(wasmer_import_object_iter_t *import_object_iter);
/**
* Writes the next value to `import`. `WASMER_ERROR` is returned if there
* was an error or there's nothing left to return.
*
* To free the memory allocated here, pass the import to `wasmer_import_object_imports_destroy`.
* To check if the iterator is done, use `wasmer_import_object_iter_at_end`.
*/
wasmer_result_t wasmer_import_object_iter_next(wasmer_import_object_iter_t *import_object_iter,
wasmer_import_t *import);
/**
* Create an iterator over the functions in the import object.
* Get the next import with `wasmer_import_object_iter_next`
* Free the iterator with `wasmer_import_object_iter_destroy`
*/
wasmer_import_object_iter_t *wasmer_import_object_iterate_functions(const wasmer_import_object_t *import_object);
/** /**
* Creates a new empty import object. * Creates a new empty import object.
* See also `wasmer_import_object_append` * See also `wasmer_import_object_append`

View File

@ -120,6 +120,10 @@ struct wasmer_import_t {
wasmer_import_export_value value; wasmer_import_export_value value;
}; };
struct wasmer_import_object_iter_t {
};
struct wasmer_instance_t { struct wasmer_instance_t {
}; };
@ -371,19 +375,12 @@ wasmer_result_t wasmer_import_object_extend(wasmer_import_object_t *import_objec
const wasmer_import_t *imports, const wasmer_import_t *imports,
unsigned int imports_len); unsigned int imports_len);
/// Call `wasmer_import_object_imports_destroy` to free the memory allocated by this function.
/// This function return -1 on error.
int32_t wasmer_import_object_get_functions(const wasmer_import_object_t *import_object,
wasmer_import_t *imports,
uint32_t imports_len);
/// Gets an entry from an ImportObject at the name and namespace. /// Gets an entry from an ImportObject at the name and namespace.
/// Stores an immutable reference to `name` and `namespace` in `import`. /// Stores `name`, `namespace`, and `import_export_value` in `import`.
/// Thus these must remain valid for the lifetime of `import`.
/// ///
/// The caller owns all data involved. /// The caller owns all data involved.
/// `import_export_value` will be written to based on `tag`, `import_export_value` must be /// `import_export_value` will be written to based on `tag`.
/// initialized to point to the type specified by `tag`. Failure to do so may result
/// in data corruption or undefined behavior.
wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *import_object, wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *import_object,
wasmer_byte_array namespace_, wasmer_byte_array namespace_,
wasmer_byte_array name, wasmer_byte_array name,
@ -391,17 +388,32 @@ wasmer_result_t wasmer_import_object_get_import(const wasmer_import_object_t *im
wasmer_import_export_value *import_export_value, wasmer_import_export_value *import_export_value,
uint32_t tag); uint32_t tag);
/// Get the number of functions that an import object contains. /// Frees the memory allocated in `wasmer_import_object_iter_next`
/// The result of this is useful as an argument to `wasmer_import_object_get_functions`.
/// This function returns -1 on error.
int32_t wasmer_import_object_get_num_functions(const wasmer_import_object_t *import_object);
/// Frees the memory acquired in `wasmer_import_object_get_functions`
/// ///
/// This function does not free the memory in `wasmer_import_object_t`; /// This function does not free the memory in `wasmer_import_object_t`;
/// it only frees memory allocated while querying a `wasmer_import_object_t`. /// it only frees memory allocated while querying a `wasmer_import_object_t`.
void wasmer_import_object_imports_destroy(wasmer_import_t *imports, uint32_t imports_len); void wasmer_import_object_imports_destroy(wasmer_import_t *imports, uint32_t imports_len);
/// Returns true if further calls to `wasmer_import_object_iter_next` will
/// not return any new data
bool wasmer_import_object_iter_at_end(wasmer_import_object_iter_t *import_object_iter);
/// Frees the memory allocated by `wasmer_import_object_iterate_functions`
void wasmer_import_object_iter_destroy(wasmer_import_object_iter_t *import_object_iter);
/// Writes the next value to `import`. `WASMER_ERROR` is returned if there
/// was an error or there's nothing left to return.
///
/// To free the memory allocated here, pass the import to `wasmer_import_object_imports_destroy`.
/// To check if the iterator is done, use `wasmer_import_object_iter_at_end`.
wasmer_result_t wasmer_import_object_iter_next(wasmer_import_object_iter_t *import_object_iter,
wasmer_import_t *import);
/// Create an iterator over the functions in the import object.
/// Get the next import with `wasmer_import_object_iter_next`
/// Free the iterator with `wasmer_import_object_iter_destroy`
wasmer_import_object_iter_t *wasmer_import_object_iterate_functions(const wasmer_import_object_t *import_object);
/// Creates a new empty import object. /// Creates a new empty import object.
/// See also `wasmer_import_object_append` /// See also `wasmer_import_object_append`
wasmer_import_object_t *wasmer_import_object_new(); wasmer_import_object_t *wasmer_import_object_new();