Files
wasmtime/crates/c-api/src/vec.rs
Dan Gohman 9364eb1d98 Refactor (#1524)
* Compute instance exports on demand.

Instead having instances eagerly compute a Vec of Externs, and bumping
the refcount for each Extern, compute Externs on demand.

This also enables `Instance::get_export` to avoid doing a linear search.

This also means that the closure returned by `get0` and friends now
holds an `InstanceHandle` to dynamically hold the instance live rather
than being scoped to a lifetime.

* Compute module imports and exports on demand too.

And compute Extern::ty on demand too.

* Add a utility function for computing an ExternType.

* Add a utility function for looking up a function's signature.

* Add a utility function for computing the ValType of a Global.

* Rename wasmtime_environ::Export to EntityIndex.

This helps differentiate it from other Export types in the tree, and
describes what it is.

* Fix a typo in a comment.

* Simplify module imports and exports.

* Make `Instance::exports` return the export names.

This significantly simplifies the public API, as it's relatively common
to need the names, and this avoids the need to do a zip with
`Module::exports`.

This also changes `ImportType` and `ExportType` to have public members
instead of private members and accessors, as I find that simplifies the
usage particularly in cases where there are temporary instances.

* Remove `Instance::module`.

This doesn't quite remove `Instance`'s `module` member, it gets a step
closer.

* Use a InstanceHandle utility function.

* Don't consume self in the `Func::get*` methods.

Instead, just create a closure containing the instance handle and the
export for them to call.

* Use `ExactSizeIterator` to avoid needing separate `num_*` methods.

* Rename `Extern::func()` etc. to `into_func()` etc.

* Revise examples to avoid using `nth`.

* Add convenience methods to instance for getting specific extern types.

* Use the convenience functions in more tests and examples.

* Avoid cloning strings for `ImportType` and `ExportType`.

* Remove more obviated clone() calls.

* Simplify `Func`'s closure state.

* Make wasmtime::Export's fields private.

This makes them more consistent with ExportType.

* Fix compilation error.

* Make a lifetime parameter explicit, and use better lifetime names.

Instead of 'me, use 'instance and 'module to make it clear what the
lifetime is.

* More lifetime cleanups.
2020-04-20 15:55:33 -05:00

230 lines
6.8 KiB
Rust

use crate::wasm_valtype_t;
use crate::{wasm_exporttype_t, wasm_extern_t, wasm_frame_t, wasm_val_t};
use crate::{wasm_externtype_t, wasm_importtype_t, wasm_memorytype_t};
use crate::{wasm_functype_t, wasm_globaltype_t, wasm_tabletype_t};
use std::mem;
use std::ptr;
use std::slice;
pub type wasm_name_t = wasm_byte_vec_t;
impl wasm_name_t {
pub(crate) fn from_name(name: String) -> wasm_name_t {
name.into_bytes().into()
}
}
macro_rules! declare_vecs {
(
$((
name: $name:ident,
ty: $elem_ty:ty,
new: $new:ident,
empty: $empty:ident,
uninit: $uninit:ident,
copy: $copy:ident,
delete: $delete:ident,
))*
) => {$(
#[repr(C)]
#[derive(Clone)]
pub struct $name {
size: usize,
data: *mut $elem_ty,
}
impl $name {
pub fn set_buffer(&mut self, buffer: Vec<$elem_ty>) {
let mut vec = buffer.into_boxed_slice();
self.size = vec.len();
self.data = vec.as_mut_ptr();
mem::forget(vec);
}
pub fn as_slice(&self) -> &[$elem_ty] {
// Note that we're careful to not create a slice with a null
// pointer as the data pointer, since that isn't defined
// behavior in Rust.
if self.size == 0 {
&[]
} else {
assert!(!self.data.is_null());
unsafe { slice::from_raw_parts(self.data, self.size) }
}
}
pub fn take(&mut self) -> Vec<$elem_ty> {
if self.data.is_null() {
return Vec::new();
}
let vec = unsafe {
Vec::from_raw_parts(self.data, self.size, self.size)
};
self.data = ptr::null_mut();
self.size = 0;
return vec;
}
}
impl From<Vec<$elem_ty>> for $name {
fn from(mut vec: Vec<$elem_ty>) -> Self {
assert_eq!(vec.len(), vec.capacity());
let result = $name {
size: vec.len(),
data: vec.as_mut_ptr(),
};
mem::forget(vec);
result
}
}
impl Drop for $name {
fn drop(&mut self) {
drop(self.take());
}
}
#[no_mangle]
pub extern "C" fn $empty(out: &mut $name) {
out.size = 0;
out.data = ptr::null_mut();
}
#[no_mangle]
pub extern "C" fn $uninit(out: &mut $name, size: usize) {
out.set_buffer(vec![Default::default(); size]);
}
#[no_mangle]
pub unsafe extern "C" fn $new(
out: &mut $name,
size: usize,
ptr: *const $elem_ty,
) {
let slice = slice::from_raw_parts(ptr, size);
out.set_buffer(slice.to_vec());
}
#[no_mangle]
pub extern "C" fn $copy(out: &mut $name, src: &$name) {
out.set_buffer(src.as_slice().to_vec());
}
#[no_mangle]
pub extern "C" fn $delete(out: &mut $name) {
out.take();
}
)*};
}
declare_vecs! {
(
name: wasm_byte_vec_t,
ty: u8,
new: wasm_byte_vec_new,
empty: wasm_byte_vec_new_empty,
uninit: wasm_byte_vec_new_uninitialized,
copy: wasm_byte_vec_copy,
delete: wasm_byte_vec_delete,
)
(
name: wasm_valtype_vec_t,
ty: Option<Box<wasm_valtype_t>>,
new: wasm_valtype_vec_new,
empty: wasm_valtype_vec_new_empty,
uninit: wasm_valtype_vec_new_uninitialized,
copy: wasm_valtype_vec_copy,
delete: wasm_valtype_vec_delete,
)
(
name: wasm_functype_vec_t,
ty: Option<Box<wasm_functype_t>>,
new: wasm_functype_vec_new,
empty: wasm_functype_vec_new_empty,
uninit: wasm_functype_vec_new_uninitialized,
copy: wasm_functype_vec_copy,
delete: wasm_functype_vec_delete,
)
(
name: wasm_globaltype_vec_t,
ty: Option<Box<wasm_globaltype_t>>,
new: wasm_globaltype_vec_new,
empty: wasm_globaltype_vec_new_empty,
uninit: wasm_globaltype_vec_new_uninitialized,
copy: wasm_globaltype_vec_copy,
delete: wasm_globaltype_vec_delete,
)
(
name: wasm_tabletype_vec_t,
ty: Option<Box<wasm_tabletype_t>>,
new: wasm_tabletype_vec_new,
empty: wasm_tabletype_vec_new_empty,
uninit: wasm_tabletype_vec_new_uninitialized,
copy: wasm_tabletype_vec_copy,
delete: wasm_tabletype_vec_delete,
)
(
name: wasm_memorytype_vec_t,
ty: Option<Box<wasm_memorytype_t>>,
new: wasm_memorytype_vec_new,
empty: wasm_memorytype_vec_new_empty,
uninit: wasm_memorytype_vec_new_uninitialized,
copy: wasm_memorytype_vec_copy,
delete: wasm_memorytype_vec_delete,
)
(
name: wasm_externtype_vec_t,
ty: Option<Box<wasm_externtype_t>>,
new: wasm_externtype_vec_new,
empty: wasm_externtype_vec_new_empty,
uninit: wasm_externtype_vec_new_uninitialized,
copy: wasm_externtype_vec_copy,
delete: wasm_externtype_vec_delete,
)
(
name: wasm_importtype_vec_t,
ty: Option<Box<wasm_importtype_t>>,
new: wasm_importtype_vec_new,
empty: wasm_importtype_vec_new_empty,
uninit: wasm_importtype_vec_new_uninitialized,
copy: wasm_importtype_vec_copy,
delete: wasm_importtype_vec_delete,
)
(
name: wasm_exporttype_vec_t,
ty: Option<Box<wasm_exporttype_t>>,
new: wasm_exporttype_vec_new,
empty: wasm_exporttype_vec_new_empty,
uninit: wasm_exporttype_vec_new_uninitialized,
copy: wasm_exporttype_vec_copy,
delete: wasm_exporttype_vec_delete,
)
(
name: wasm_val_vec_t,
ty: wasm_val_t,
new: wasm_val_vec_new,
empty: wasm_val_vec_new_empty,
uninit: wasm_val_vec_new_uninitialized,
copy: wasm_val_vec_copy,
delete: wasm_val_vec_delete,
)
(
name: wasm_frame_vec_t,
ty: Option<Box<wasm_frame_t>>,
new: wasm_frame_vec_new,
empty: wasm_frame_vec_new_empty,
uninit: wasm_frame_vec_new_uninitialized,
copy: wasm_frame_vec_copy,
delete: wasm_frame_vec_delete,
)
(
name: wasm_extern_vec_t,
ty: Option<Box<wasm_extern_t>>,
new: wasm_extern_vec_new,
empty: wasm_extern_vec_new_empty,
uninit: wasm_extern_vec_new_uninitialized,
copy: wasm_extern_vec_copy,
delete: wasm_extern_vec_delete,
)
}