1 //! This crate is the implementation of Wasmtime's C API. 2 //! 3 //! This crate is not intended to be used from Rust itself, for that see the 4 //! `wasmtime` crate. Otherwise this is typically compiled as a 5 //! cdylib/staticlib. Documentation for this crate largely lives in the header 6 //! files of the `include` directory for this crate. 7 //! 8 //! At a high level this crate implements the `wasm.h` API with some gymnastics, 9 //! but otherwise an accompanying `wasmtime.h` API is provided which is more 10 //! specific to Wasmtime and has fewer gymnastics to implement. 11 12 #![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] 13 14 pub use wasmtime::*; 15 16 mod config; 17 mod engine; 18 mod error; 19 mod r#extern; 20 mod func; 21 mod global; 22 mod instance; 23 mod linker; 24 mod memory; 25 mod module; 26 mod r#ref; 27 mod store; 28 mod table; 29 mod trap; 30 mod types; 31 mod val; 32 mod vec; 33 34 pub use crate::config::*; 35 pub use crate::engine::*; 36 pub use crate::error::*; 37 pub use crate::func::*; 38 pub use crate::global::*; 39 pub use crate::instance::*; 40 pub use crate::linker::*; 41 pub use crate::memory::*; 42 pub use crate::module::*; 43 pub use crate::r#extern::*; 44 pub use crate::r#ref::*; 45 pub use crate::store::*; 46 pub use crate::table::*; 47 pub use crate::trap::*; 48 pub use crate::types::*; 49 pub use crate::val::*; 50 pub use crate::vec::*; 51 52 #[cfg(feature = "wasi")] 53 mod wasi; 54 #[cfg(feature = "wasi")] 55 pub use crate::wasi::*; 56 57 #[cfg(feature = "wat")] 58 mod wat2wasm; 59 #[cfg(feature = "wat")] 60 pub use crate::wat2wasm::*; 61 62 /// Initialize a `MaybeUninit<T>` 63 /// 64 /// TODO: Replace calls to this function with 65 /// https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write 66 /// once it is stable. 67 pub(crate) fn initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T) { 68 unsafe { 69 std::ptr::write(dst.as_mut_ptr(), val); 70 } 71 } 72 73 /// Helper for running a C-defined finalizer over some data when the Rust 74 /// structure is dropped. 75 pub struct ForeignData { 76 data: *mut std::ffi::c_void, 77 finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>, 78 } 79 80 unsafe impl Send for ForeignData {} 81 unsafe impl Sync for ForeignData {} 82 83 impl Drop for ForeignData { 84 fn drop(&mut self) { 85 if let Some(f) = self.finalizer { 86 f(self.data); 87 } 88 } 89 } 90 91 /// Helper for creating Rust slices from C inputs. 92 /// 93 /// This specifically disregards the `ptr` argument if the length is zero. The 94 /// `ptr` in that case maybe `NULL` or invalid, and it's not valid to have a 95 /// zero-length Rust slice with a `NULL` pointer. 96 unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] { 97 if len == 0 { 98 &[] 99 } else { 100 std::slice::from_raw_parts(ptr, len) 101 } 102 } 103 104 /// Same as above, but for `*_mut` 105 unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] { 106 if len == 0 { 107 &mut [] 108 } else { 109 std::slice::from_raw_parts_mut(ptr, len) 110 } 111 } 112