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 = "async")] 53 mod r#async; 54 #[cfg(feature = "async")] 55 pub use crate::r#async::*; 56 57 #[cfg(feature = "wasi")] 58 mod wasi; 59 #[cfg(feature = "wasi")] 60 pub use crate::wasi::*; 61 62 #[cfg(feature = "wat")] 63 mod wat2wasm; 64 #[cfg(feature = "wat")] 65 pub use crate::wat2wasm::*; 66 67 /// Initialize a `MaybeUninit<T>` 68 /// 69 /// TODO: Replace calls to this function with 70 /// https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write 71 /// once it is stable. 72 pub(crate) fn initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T) { 73 unsafe { 74 std::ptr::write(dst.as_mut_ptr(), val); 75 } 76 } 77 78 /// Helper for running a C-defined finalizer over some data when the Rust 79 /// structure is dropped. 80 pub struct ForeignData { 81 data: *mut std::ffi::c_void, 82 finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>, 83 } 84 85 unsafe impl Send for ForeignData {} 86 unsafe impl Sync for ForeignData {} 87 88 impl Drop for ForeignData { 89 fn drop(&mut self) { 90 if let Some(f) = self.finalizer { 91 f(self.data); 92 } 93 } 94 } 95 96 /// Helper for creating Rust slices from C inputs. 97 /// 98 /// This specifically disregards the `ptr` argument if the length is zero. The 99 /// `ptr` in that case maybe `NULL` or invalid, and it's not valid to have a 100 /// zero-length Rust slice with a `NULL` pointer. 101 unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] { 102 if len == 0 { 103 &[] 104 } else { 105 std::slice::from_raw_parts(ptr, len) 106 } 107 } 108 109 /// Same as above, but for `*_mut` 110 unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] { 111 if len == 0 { 112 &mut [] 113 } else { 114 std::slice::from_raw_parts_mut(ptr, len) 115 } 116 } 117