wasmtime_environ/
component.rs

1//! Support for the component model in Wasmtime.
2//!
3//! This module contains all of the internal type definitions used by Wasmtime
4//! to process the component model. Despite everything being `pub` here this is
5//! not the public interface of Wasmtime to the component model. Instead this is
6//! the internal support to mirror the core wasm support that Wasmtime already
7//! implements.
8//!
9//! Some main items contained within here are:
10//!
11//! * Type hierarchy information for the component model
12//! * Translation of a component into Wasmtime's representation
13//! * Type information about a component used at runtime
14//!
15//! This module also contains a lot of Serialize/Deserialize types which are
16//! encoded in the final compiled image for a component.
17//!
18//! Note that this entire module is gated behind the `component-model` Cargo
19//! feature.
20
21/// Canonical ABI-defined constant for the maximum number of "flat" parameters
22/// to a wasm function, or the maximum number of parameters a core wasm function
23/// will take for just the parameters used. Over this number the heap is used
24/// for transferring parameters.
25pub const MAX_FLAT_PARAMS: usize = 16;
26
27/// Similar to `MAX_FLAT_PARAMS`, but used for async-lowered imports instead of
28/// sync ones.
29pub const MAX_FLAT_ASYNC_PARAMS: usize = 4;
30
31/// Canonical ABI-defined constant for the maximum number of "flat" results.
32/// This number of results are returned directly from wasm and otherwise results
33/// are transferred through memory.
34pub const MAX_FLAT_RESULTS: usize = 1;
35
36/// Sentinel value in `result_count_or_max_if_async` as part of the
37/// `prepare_call` libcall which indicates that preparation is being done for an
38/// async function that produces no result, aka there is no return pointer.
39pub const PREPARE_ASYNC_NO_RESULT: u32 = u32::MAX;
40
41/// Sentinel value in `result_count_or_max_if_async` as part of the
42/// `prepare_call` libcall which indicates that preparation is being done for an
43/// async function that produces at least one result, aka there is a return
44/// pointer.
45pub const PREPARE_ASYNC_WITH_RESULT: u32 = u32::MAX - 1;
46
47/// Bit flag for indicating async-lifted exports
48///
49/// This flag may be passed to the `async-start` built-in function (which is
50/// called from both async->async and async->sync adapters) to indicate that the
51/// callee is an async-lifted export.
52pub const START_FLAG_ASYNC_CALLEE: i32 = 1 << 0;
53
54mod artifacts;
55mod info;
56mod names;
57mod types;
58mod vmcomponent_offsets;
59pub use self::artifacts::*;
60pub use self::info::*;
61pub use self::names::*;
62pub use self::types::*;
63pub use self::vmcomponent_offsets::*;
64
65#[cfg(feature = "compile")]
66mod compiler;
67#[cfg(feature = "compile")]
68pub mod dfg;
69#[cfg(feature = "compile")]
70mod translate;
71#[cfg(feature = "compile")]
72mod types_builder;
73#[cfg(feature = "compile")]
74pub use self::compiler::*;
75#[cfg(feature = "compile")]
76pub use self::translate::*;
77#[cfg(feature = "compile")]
78pub use self::types_builder::*;
79
80/// Helper macro, like `foreach_transcoder`, to iterate over builtins for
81/// components unrelated to transcoding.
82#[macro_export]
83macro_rules! foreach_builtin_component_function {
84    ($mac:ident) => {
85        $mac! {
86            resource_new32(vmctx: vmctx, resource: u32, rep: u32) -> u64;
87            resource_rep32(vmctx: vmctx, resource: u32, idx: u32) -> u64;
88
89            // Returns an `Option<u32>` where `None` is "no destructor needed"
90            // and `Some(val)` is "run the destructor on this rep". The option
91            // is encoded as a 64-bit integer where the low bit is Some/None
92            // and bits 1-33 are the payload.
93            resource_drop(vmctx: vmctx, resource: u32, idx: u32) -> u64;
94
95            resource_transfer_own(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
96            resource_transfer_borrow(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
97            resource_enter_call(vmctx: vmctx);
98            resource_exit_call(vmctx: vmctx) -> bool;
99
100            #[cfg(feature = "component-model-async")]
101            backpressure_set(vmctx: vmctx, caller_instance: u32, enabled: u32) -> bool;
102            #[cfg(feature = "component-model-async")]
103            task_return(vmctx: vmctx, ty: u32, options: u32, storage: ptr_u8, storage_len: size) -> bool;
104            #[cfg(feature = "component-model-async")]
105            task_cancel(vmctx: vmctx, caller_instance: u32) -> bool;
106            #[cfg(feature = "component-model-async")]
107            waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;
108            #[cfg(feature = "component-model-async")]
109            waitable_set_wait(vmctx: vmctx, options: u32, set: u32, payload: u32) -> u64;
110            #[cfg(feature = "component-model-async")]
111            waitable_set_poll(vmctx: vmctx, options: u32, set: u32, payload: u32) -> u64;
112            #[cfg(feature = "component-model-async")]
113            waitable_set_drop(vmctx: vmctx, caller_instance: u32, set: u32) -> bool;
114            #[cfg(feature = "component-model-async")]
115            waitable_join(vmctx: vmctx, caller_instance: u32, set: u32, waitable: u32) -> bool;
116            #[cfg(feature = "component-model-async")]
117            yield_(vmctx: vmctx, async_: u8) -> u32;
118            #[cfg(feature = "component-model-async")]
119            subtask_drop(vmctx: vmctx, caller_instance: u32, task_id: u32) -> bool;
120            #[cfg(feature = "component-model-async")]
121            subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;
122            #[cfg(feature = "component-model-async")]
123            prepare_call(
124                vmctx: vmctx,
125                memory: ptr_u8,
126                start: ptr_u8,
127                return_: ptr_u8,
128                caller_instance: u32,
129                callee_instance: u32,
130                task_return_type: u32,
131                string_encoding: u32,
132                result_count_or_max_if_async: u32,
133                storage: ptr_u8,
134                storage_len: size
135            ) -> bool;
136            #[cfg(feature = "component-model-async")]
137            sync_start(vmctx: vmctx, callback: ptr_u8, storage: ptr_u8, storage_len: size, callee: ptr_u8, param_count: u32) -> bool;
138            #[cfg(feature = "component-model-async")]
139            async_start(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> u64;
140            #[cfg(feature = "component-model-async")]
141            future_new(vmctx: vmctx, ty: u32) -> u64;
142            #[cfg(feature = "component-model-async")]
143            future_write(vmctx: vmctx, ty: u32, options: u32, future: u32, address: u32) -> u64;
144            #[cfg(feature = "component-model-async")]
145            future_read(vmctx: vmctx, ty: u32, options: u32, future: u32, address: u32) -> u64;
146            #[cfg(feature = "component-model-async")]
147            future_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
148            #[cfg(feature = "component-model-async")]
149            future_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
150            #[cfg(feature = "component-model-async")]
151            future_drop_writable(vmctx: vmctx, ty: u32, writer: u32) -> bool;
152            #[cfg(feature = "component-model-async")]
153            future_drop_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
154            #[cfg(feature = "component-model-async")]
155            stream_new(vmctx: vmctx, ty: u32) -> u64;
156            #[cfg(feature = "component-model-async")]
157            stream_write(vmctx: vmctx, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;
158            #[cfg(feature = "component-model-async")]
159            stream_read(vmctx: vmctx, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;
160            #[cfg(feature = "component-model-async")]
161            stream_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
162            #[cfg(feature = "component-model-async")]
163            stream_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
164            #[cfg(feature = "component-model-async")]
165            stream_drop_writable(vmctx: vmctx, ty: u32, writer: u32) -> bool;
166            #[cfg(feature = "component-model-async")]
167            stream_drop_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
168            #[cfg(feature = "component-model-async")]
169            flat_stream_write(vmctx: vmctx, ty: u32, options:u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
170            #[cfg(feature = "component-model-async")]
171            flat_stream_read(vmctx: vmctx, ty: u32, options: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
172            #[cfg(feature = "component-model-async")]
173            error_context_new(vmctx: vmctx, ty: u32, options: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;
174            #[cfg(feature = "component-model-async")]
175            error_context_debug_message(vmctx: vmctx, ty: u32, options: u32, err_ctx_handle: u32, debug_msg_address: u32) -> bool;
176            #[cfg(feature = "component-model-async")]
177            error_context_drop(vmctx: vmctx, ty: u32, err_ctx_handle: u32) -> bool;
178            #[cfg(feature = "component-model-async")]
179            future_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
180            #[cfg(feature = "component-model-async")]
181            stream_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
182            #[cfg(feature = "component-model-async")]
183            error_context_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
184            #[cfg(feature = "component-model-async")]
185            context_get(vmctx: vmctx, slot: u32) -> u64;
186            #[cfg(feature = "component-model-async")]
187            context_set(vmctx: vmctx, slot: u32, val: u32) -> bool;
188
189            trap(vmctx: vmctx, code: u8) -> bool;
190
191            utf8_to_utf8(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
192            utf16_to_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> bool;
193            latin1_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
194            latin1_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> bool;
195            utf8_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> size;
196            utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
197            latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
198            utf16_to_compact_probably_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> size;
199            utf8_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
200            utf16_to_latin1(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
201            utf8_to_compact_utf16(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
202            utf16_to_compact_utf16(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
203        }
204    };
205}
206
207// Define `struct ComponentBuiltinFunctionIndex`
208declare_builtin_index!(
209    ComponentBuiltinFunctionIndex,
210    foreach_builtin_component_function
211);