17a1b7cdfSAlex Crichton //! This crate is the implementation of Wasmtime's C API.
27a1b7cdfSAlex Crichton //!
3825494feSMax Brunsfeld //! This crate is normally not intended to be used from Rust itself. For that,
4825494feSMax Brunsfeld //! see the `wasmtime` crate. It is possible to use this crate via Cargo, for
5825494feSMax Brunsfeld //! Rust crates that wrap C libraries that use wasmtime. Most often, this crate
6825494feSMax Brunsfeld //! is compiled as a cdylib or staticlib, via the `wasmtime-c-api` crate.
7825494feSMax Brunsfeld //!
8825494feSMax Brunsfeld //! Documentation for this crate largely lives in the header
97a1b7cdfSAlex Crichton //! files of the `include` directory for this crate.
107a1b7cdfSAlex Crichton //!
117a1b7cdfSAlex Crichton //! At a high level this crate implements the `wasm.h` API with some gymnastics,
127a1b7cdfSAlex Crichton //! but otherwise an accompanying `wasmtime.h` API is provided which is more
137a1b7cdfSAlex Crichton //! specific to Wasmtime and has fewer gymnastics to implement.
14364fa994SAlex Crichton
1545b60bd6SAlex Crichton #![expect(non_camel_case_types, reason = "matching C style, not Rust")]
16073aedabSAlex Crichton #![expect(unsafe_op_in_unsafe_fn, reason = "crate isn't migrated yet")]
17364fa994SAlex Crichton
18f7004c19SAlex Crichton pub use wasmtime;
1990e4daffSSilas Groh
206ef09359SAlex Crichton mod config;
216ef09359SAlex Crichton mod engine;
22bd374fd6SAlex Crichton mod error;
23*0dbb6f3dSChris Fallin mod exn;
246ef09359SAlex Crichton mod r#extern;
256ef09359SAlex Crichton mod func;
266ef09359SAlex Crichton mod global;
276ef09359SAlex Crichton mod instance;
286ef09359SAlex Crichton mod linker;
296ef09359SAlex Crichton mod memory;
306ef09359SAlex Crichton mod module;
31d4eaacd6SMilek7 #[cfg(feature = "profiling")]
32d4eaacd6SMilek7 mod profiling;
336ef09359SAlex Crichton mod r#ref;
3455bd797aSMilek7 mod sharedmemory;
356ef09359SAlex Crichton mod store;
366ef09359SAlex Crichton mod table;
37*0dbb6f3dSChris Fallin mod tag;
386ef09359SAlex Crichton mod trap;
396ef09359SAlex Crichton mod types;
406ef09359SAlex Crichton mod val;
416ef09359SAlex Crichton mod vec;
42ae0b4090SPeter Huene
436ef09359SAlex Crichton pub use crate::config::*;
446ef09359SAlex Crichton pub use crate::engine::*;
45bd374fd6SAlex Crichton pub use crate::error::*;
46*0dbb6f3dSChris Fallin pub use crate::exn::*;
4790ac295eSAlex Crichton pub use crate::r#extern::*;
486ef09359SAlex Crichton pub use crate::func::*;
496ef09359SAlex Crichton pub use crate::global::*;
506ef09359SAlex Crichton pub use crate::instance::*;
516ef09359SAlex Crichton pub use crate::linker::*;
526ef09359SAlex Crichton pub use crate::memory::*;
536ef09359SAlex Crichton pub use crate::module::*;
546ef09359SAlex Crichton pub use crate::r#ref::*;
556ef09359SAlex Crichton pub use crate::store::*;
566ef09359SAlex Crichton pub use crate::table::*;
57*0dbb6f3dSChris Fallin pub use crate::tag::*;
586ef09359SAlex Crichton pub use crate::trap::*;
596ef09359SAlex Crichton pub use crate::types::*;
606ef09359SAlex Crichton pub use crate::val::*;
616ef09359SAlex Crichton pub use crate::vec::*;
624ede98feSAlex Crichton
6337cf8e1eSTyler Rockwood #[cfg(feature = "async")]
6437cf8e1eSTyler Rockwood mod r#async;
6537cf8e1eSTyler Rockwood #[cfg(feature = "async")]
6637cf8e1eSTyler Rockwood pub use crate::r#async::*;
6737cf8e1eSTyler Rockwood
684ede98feSAlex Crichton #[cfg(feature = "wasi")]
694ede98feSAlex Crichton mod wasi;
704ede98feSAlex Crichton #[cfg(feature = "wasi")]
71ae0b4090SPeter Huene pub use crate::wasi::*;
724ede98feSAlex Crichton
734ede98feSAlex Crichton #[cfg(feature = "wat")]
744ede98feSAlex Crichton mod wat2wasm;
754ede98feSAlex Crichton #[cfg(feature = "wat")]
766ef09359SAlex Crichton pub use crate::wat2wasm::*;
77ae0b4090SPeter Huene
786ba6e13bSMangoPeachGrape #[cfg(feature = "component-model")]
796ba6e13bSMangoPeachGrape mod component;
806ba6e13bSMangoPeachGrape #[cfg(feature = "component-model")]
816ba6e13bSMangoPeachGrape pub use crate::component::*;
826ba6e13bSMangoPeachGrape
83d07fdca7SNick Fitzgerald /// Initialize a `MaybeUninit<T>`
84d07fdca7SNick Fitzgerald ///
85d07fdca7SNick Fitzgerald /// TODO: Replace calls to this function with
86d07fdca7SNick Fitzgerald /// https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write
87d07fdca7SNick Fitzgerald /// once it is stable.
initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T)88d07fdca7SNick Fitzgerald pub(crate) fn initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T) {
89d07fdca7SNick Fitzgerald unsafe {
90d07fdca7SNick Fitzgerald std::ptr::write(dst.as_mut_ptr(), val);
91d07fdca7SNick Fitzgerald }
92d07fdca7SNick Fitzgerald }
937a1b7cdfSAlex Crichton
947a1b7cdfSAlex Crichton /// Helper for running a C-defined finalizer over some data when the Rust
957a1b7cdfSAlex Crichton /// structure is dropped.
967a1b7cdfSAlex Crichton pub struct ForeignData {
977a1b7cdfSAlex Crichton data: *mut std::ffi::c_void,
987a1b7cdfSAlex Crichton finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
997a1b7cdfSAlex Crichton }
1007a1b7cdfSAlex Crichton
1017a1b7cdfSAlex Crichton unsafe impl Send for ForeignData {}
1027a1b7cdfSAlex Crichton unsafe impl Sync for ForeignData {}
1037a1b7cdfSAlex Crichton
1047a1b7cdfSAlex Crichton impl Drop for ForeignData {
drop(&mut self)1057a1b7cdfSAlex Crichton fn drop(&mut self) {
1067a1b7cdfSAlex Crichton if let Some(f) = self.finalizer {
1077a1b7cdfSAlex Crichton f(self.data);
1087a1b7cdfSAlex Crichton }
1097a1b7cdfSAlex Crichton }
1107a1b7cdfSAlex Crichton }
1117a1b7cdfSAlex Crichton
1127a1b7cdfSAlex Crichton /// Helper for creating Rust slices from C inputs.
1137a1b7cdfSAlex Crichton ///
1147a1b7cdfSAlex Crichton /// This specifically disregards the `ptr` argument if the length is zero. The
1157a1b7cdfSAlex Crichton /// `ptr` in that case maybe `NULL` or invalid, and it's not valid to have a
1167a1b7cdfSAlex Crichton /// zero-length Rust slice with a `NULL` pointer.
slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T]1177a1b7cdfSAlex Crichton unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
1187a1b7cdfSAlex Crichton if len == 0 {
1197a1b7cdfSAlex Crichton &[]
1207a1b7cdfSAlex Crichton } else {
1217a1b7cdfSAlex Crichton std::slice::from_raw_parts(ptr, len)
1227a1b7cdfSAlex Crichton }
1237a1b7cdfSAlex Crichton }
1247a1b7cdfSAlex Crichton
1257a1b7cdfSAlex Crichton /// Same as above, but for `*_mut`
slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T]1267a1b7cdfSAlex Crichton unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] {
1277a1b7cdfSAlex Crichton if len == 0 {
1287a1b7cdfSAlex Crichton &mut []
1297a1b7cdfSAlex Crichton } else {
1307a1b7cdfSAlex Crichton std::slice::from_raw_parts_mut(ptr, len)
1317a1b7cdfSAlex Crichton }
1327a1b7cdfSAlex Crichton }
133ff93bce0SNick Fitzgerald
abort(name: &str) -> !134ff93bce0SNick Fitzgerald pub(crate) fn abort(name: &str) -> ! {
135a0442ea0SHamir Mahal eprintln!("`{name}` is not implemented");
136ff93bce0SNick Fitzgerald std::process::abort();
137ff93bce0SNick Fitzgerald }
138