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 mod config; 15 mod engine; 16 mod error; 17 mod r#extern; 18 mod func; 19 mod global; 20 mod instance; 21 mod linker; 22 mod memory; 23 mod module; 24 mod r#ref; 25 mod store; 26 mod table; 27 mod trap; 28 mod types; 29 mod val; 30 mod vec; 31 32 pub use crate::config::*; 33 pub use crate::engine::*; 34 pub use crate::error::*; 35 pub use crate::func::*; 36 pub use crate::global::*; 37 pub use crate::instance::*; 38 pub use crate::linker::*; 39 pub use crate::memory::*; 40 pub use crate::module::*; 41 pub use crate::r#extern::*; 42 pub use crate::r#ref::*; 43 pub use crate::store::*; 44 pub use crate::table::*; 45 pub use crate::trap::*; 46 pub use crate::types::*; 47 pub use crate::val::*; 48 pub use crate::vec::*; 49 50 #[cfg(feature = "wasi")] 51 mod wasi; 52 #[cfg(feature = "wasi")] 53 pub use crate::wasi::*; 54 55 #[cfg(feature = "wat")] 56 mod wat2wasm; 57 #[cfg(feature = "wat")] 58 pub use crate::wat2wasm::*; 59 60 /// Initialize a `MaybeUninit<T>` 61 /// 62 /// TODO: Replace calls to this function with 63 /// https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write 64 /// once it is stable. 65 pub(crate) fn initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T) { 66 unsafe { 67 std::ptr::write(dst.as_mut_ptr(), val); 68 } 69 } 70 71 /// Helper for running a C-defined finalizer over some data when the Rust 72 /// structure is dropped. 73 pub struct ForeignData { 74 data: *mut std::ffi::c_void, 75 finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>, 76 } 77 78 unsafe impl Send for ForeignData {} 79 unsafe impl Sync for ForeignData {} 80 81 impl Drop for ForeignData { 82 fn drop(&mut self) { 83 if let Some(f) = self.finalizer { 84 f(self.data); 85 } 86 } 87 } 88 89 /// Helper for creating Rust slices from C inputs. 90 /// 91 /// This specifically disregards the `ptr` argument if the length is zero. The 92 /// `ptr` in that case maybe `NULL` or invalid, and it's not valid to have a 93 /// zero-length Rust slice with a `NULL` pointer. 94 unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] { 95 if len == 0 { 96 &[] 97 } else { 98 std::slice::from_raw_parts(ptr, len) 99 } 100 } 101 102 /// Same as above, but for `*_mut` 103 unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] { 104 if len == 0 { 105 &mut [] 106 } else { 107 std::slice::from_raw_parts_mut(ptr, len) 108 } 109 } 110