Skip to main content

wasmtime_c_api/
lib.rs

1//! This crate is the implementation of Wasmtime's C API.
2//!
3//! This crate is normally not intended to be used from Rust itself. For that,
4//! see the `wasmtime` crate. It is possible to use this crate via Cargo, for
5//! Rust crates that wrap C libraries that use wasmtime. Most often, this crate
6//! is compiled as a cdylib or staticlib, via the `wasmtime-c-api` crate.
7//!
8//! Documentation for this crate largely lives in the header
9//! files of the `include` directory for this crate.
10//!
11//! At a high level this crate implements the `wasm.h` API with some gymnastics,
12//! but otherwise an accompanying `wasmtime.h` API is provided which is more
13//! specific to Wasmtime and has fewer gymnastics to implement.
14
15#![expect(non_camel_case_types, reason = "matching C style, not Rust")]
16#![expect(unsafe_op_in_unsafe_fn, reason = "crate isn't migrated yet")]
17
18pub use wasmtime;
19
20// `tracing` is only a dependency to support the `disable-logging` feature, so
21// fake a use of it to quiet Cargo's `unused_dependencies` lint.
22#[cfg(feature = "disable-logging")]
23use tracing as _;
24
25mod config;
26mod engine;
27mod error;
28mod r#extern;
29mod func;
30mod global;
31mod instance;
32mod linker;
33mod memory;
34mod module;
35#[cfg(feature = "profiling")]
36mod profiling;
37mod sharedmemory;
38mod store;
39mod table;
40mod tag;
41mod trap;
42mod types;
43mod val;
44mod vec;
45
46pub use crate::config::*;
47pub use crate::engine::*;
48pub use crate::error::*;
49pub use crate::r#extern::*;
50pub use crate::func::*;
51pub use crate::global::*;
52pub use crate::instance::*;
53pub use crate::linker::*;
54pub use crate::memory::*;
55pub use crate::module::*;
56pub use crate::store::*;
57pub use crate::table::*;
58pub use crate::tag::*;
59pub use crate::trap::*;
60pub use crate::types::*;
61pub use crate::val::*;
62pub use crate::vec::*;
63
64#[cfg(feature = "gc")]
65mod anyref;
66#[cfg(feature = "gc")]
67mod arrayref;
68#[cfg(feature = "gc")]
69mod eqref;
70#[cfg(feature = "gc")]
71mod exnref;
72#[cfg(feature = "gc")]
73mod externref;
74#[cfg(feature = "gc")]
75mod r#ref;
76#[cfg(feature = "gc")]
77mod structref;
78
79#[cfg(feature = "gc")]
80pub use crate::anyref::*;
81#[cfg(feature = "gc")]
82pub use crate::arrayref::*;
83#[cfg(feature = "gc")]
84pub use crate::eqref::*;
85#[cfg(feature = "gc")]
86pub use crate::exnref::*;
87#[cfg(feature = "gc")]
88pub use crate::externref::*;
89#[cfg(feature = "gc")]
90pub use crate::r#ref::*;
91#[cfg(feature = "gc")]
92pub use crate::structref::*;
93
94#[cfg(feature = "async")]
95mod r#async;
96#[cfg(feature = "async")]
97pub use crate::r#async::*;
98
99#[cfg(feature = "wasi")]
100mod wasi;
101#[cfg(feature = "wasi")]
102pub use crate::wasi::*;
103
104#[cfg(feature = "wat")]
105mod wat2wasm;
106#[cfg(feature = "wat")]
107pub use crate::wat2wasm::*;
108
109#[cfg(feature = "component-model")]
110mod component;
111#[cfg(feature = "component-model")]
112pub use crate::component::*;
113
114/// Helper for running a C-defined finalizer over some data when the Rust
115/// structure is dropped.
116pub struct ForeignData {
117    data: *mut std::ffi::c_void,
118    finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
119}
120
121unsafe impl Send for ForeignData {}
122unsafe impl Sync for ForeignData {}
123
124impl Drop for ForeignData {
125    fn drop(&mut self) {
126        if let Some(f) = self.finalizer {
127            f(self.data);
128        }
129    }
130}
131
132/// Helper for creating Rust slices from C inputs.
133///
134/// This specifically disregards the `ptr` argument if the length is zero. The
135/// `ptr` in that case maybe `NULL` or invalid, and it's not valid to have a
136/// zero-length Rust slice with a `NULL` pointer.
137unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
138    if len == 0 {
139        &[]
140    } else {
141        std::slice::from_raw_parts(ptr, len)
142    }
143}
144
145/// Same as above, but for `*_mut`
146unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] {
147    if len == 0 {
148        &mut []
149    } else {
150        std::slice::from_raw_parts_mut(ptr, len)
151    }
152}
153
154pub(crate) fn abort(name: &str) -> ! {
155    eprintln!("`{name}` is not implemented");
156    std::process::abort();
157}
158
159/// Aborts the process after printing `message` to stderr.
160///
161/// Unlike [`abort`], which reports a function that is not implemented, this is
162/// for functions that are implemented but cannot represent their input.
163pub(crate) fn abort_unsupported(message: &str) -> ! {
164    eprintln!("{message}");
165    std::process::abort();
166}