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
47mod artifacts;
48mod info;
49mod names;
50mod types;
51mod vmcomponent_offsets;
52pub use self::artifacts::*;
53pub use self::info::*;
54pub use self::names::*;
55pub use self::types::*;
56pub use self::vmcomponent_offsets::*;
57
58#[cfg(feature = "compile")]
59mod compiler;
60#[cfg(feature = "compile")]
61pub mod dfg;
62#[cfg(feature = "compile")]
63mod translate;
64#[cfg(feature = "compile")]
65mod types_builder;
66#[cfg(feature = "compile")]
67pub use self::compiler::*;
68#[cfg(feature = "compile")]
69pub use self::translate::*;
70#[cfg(feature = "compile")]
71pub use self::types_builder::*;
72
73/// Helper macro, like `foreach_transcoder`, to iterate over builtins for
74/// components unrelated to transcoding.
75#[macro_export]
76macro_rules! foreach_builtin_component_function {
77    ($mac:ident) => {
78        $mac! {
79            resource_new32(vmctx: vmctx, resource: u32, rep: u32) -> u64;
80            resource_rep32(vmctx: vmctx, resource: u32, idx: u32) -> u64;
81
82            // Returns an `Option<u32>` where `None` is "no destructor needed"
83            // and `Some(val)` is "run the destructor on this rep". The option
84            // is encoded as a 64-bit integer where the low bit is Some/None
85            // and bits 1-33 are the payload.
86            resource_drop(vmctx: vmctx, resource: u32, idx: u32) -> u64;
87
88            resource_transfer_own(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
89            resource_transfer_borrow(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
90            resource_enter_call(vmctx: vmctx);
91            resource_exit_call(vmctx: vmctx) -> bool;
92
93            #[cfg(feature = "component-model-async")]
94            backpressure_set(vmctx: vmctx, caller_instance: u32, enabled: u32) -> bool;
95            #[cfg(feature = "component-model-async")]
96            task_return(vmctx: vmctx, ty: u32, memory: ptr_u8, string_encoding: u8, storage: ptr_u8, storage_len: size) -> bool;
97            #[cfg(feature = "component-model-async")]
98            task_cancel(vmctx: vmctx, caller_instance: u32) -> bool;
99            #[cfg(feature = "component-model-async")]
100            waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;
101            #[cfg(feature = "component-model-async")]
102            waitable_set_wait(vmctx: vmctx, caller_instance: u32, async_: u8, memory: ptr_u8, set: u32, payload: u32) -> u64;
103            #[cfg(feature = "component-model-async")]
104            waitable_set_poll(vmctx: vmctx, caller_instance: u32, async_: u8, memory: ptr_u8, set: u32, payload: u32) -> u64;
105            #[cfg(feature = "component-model-async")]
106            waitable_set_drop(vmctx: vmctx, caller_instance: u32, set: u32) -> bool;
107            #[cfg(feature = "component-model-async")]
108            waitable_join(vmctx: vmctx, caller_instance: u32, set: u32, waitable: u32) -> bool;
109            #[cfg(feature = "component-model-async")]
110            yield_(vmctx: vmctx, async_: u8) -> u32;
111            #[cfg(feature = "component-model-async")]
112            subtask_drop(vmctx: vmctx, caller_instance: u32, task_id: u32) -> bool;
113            #[cfg(feature = "component-model-async")]
114            subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;
115            #[cfg(feature = "component-model-async")]
116            prepare_call(
117                vmctx: vmctx,
118                memory: ptr_u8,
119                start: ptr_u8,
120                return_: ptr_u8,
121                caller_instance: u32,
122                callee_instance: u32,
123                task_return_type: u32,
124                string_encoding: u32,
125                result_count_or_max_if_async: u32,
126                storage: ptr_u8,
127                torage_len: size
128            ) -> bool;
129            #[cfg(feature = "component-model-async")]
130            sync_start(vmctx: vmctx, callback: ptr_u8, callee: ptr_u8, param_count: u32, storage: ptr_u8, storage_len: size) -> bool;
131            #[cfg(feature = "component-model-async")]
132            async_start(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> u64;
133            #[cfg(feature = "component-model-async")]
134            future_new(vmctx: vmctx, ty: u32) -> u64;
135            #[cfg(feature = "component-model-async")]
136            future_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, future: u32, address: u32) -> u64;
137            #[cfg(feature = "component-model-async")]
138            future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, future: u32, address: u32) -> u64;
139            #[cfg(feature = "component-model-async")]
140            future_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
141            #[cfg(feature = "component-model-async")]
142            future_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
143            #[cfg(feature = "component-model-async")]
144            future_drop_writable(vmctx: vmctx, ty: u32, writer: u32) -> bool;
145            #[cfg(feature = "component-model-async")]
146            future_drop_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
147            #[cfg(feature = "component-model-async")]
148            stream_new(vmctx: vmctx, ty: u32) -> u64;
149            #[cfg(feature = "component-model-async")]
150            stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
151            #[cfg(feature = "component-model-async")]
152            stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
153            #[cfg(feature = "component-model-async")]
154            stream_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
155            #[cfg(feature = "component-model-async")]
156            stream_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
157            #[cfg(feature = "component-model-async")]
158            stream_drop_writable(vmctx: vmctx, ty: u32, writer: u32) -> bool;
159            #[cfg(feature = "component-model-async")]
160            stream_drop_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
161            #[cfg(feature = "component-model-async")]
162            flat_stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, async_: u8, ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
163            #[cfg(feature = "component-model-async")]
164            flat_stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, async_: u8, ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
165            #[cfg(feature = "component-model-async")]
166            error_context_new(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;
167            #[cfg(feature = "component-model-async")]
168            error_context_debug_message(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_handle: u32, debug_msg_address: u32) -> bool;
169            #[cfg(feature = "component-model-async")]
170            error_context_drop(vmctx: vmctx, ty: u32, err_ctx_handle: u32) -> bool;
171            #[cfg(feature = "component-model-async")]
172            future_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
173            #[cfg(feature = "component-model-async")]
174            stream_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
175            #[cfg(feature = "component-model-async")]
176            error_context_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
177            #[cfg(feature = "component-model-async")]
178            context_get(vmctx: vmctx, slot: u32) -> u64;
179            #[cfg(feature = "component-model-async")]
180            context_set(vmctx: vmctx, slot: u32, val: u32) -> bool;
181
182            trap(vmctx: vmctx, code: u8);
183
184            utf8_to_utf8(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
185            utf16_to_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> bool;
186            latin1_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
187            latin1_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> bool;
188            utf8_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> size;
189            utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
190            latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
191            utf16_to_compact_probably_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> size;
192            utf8_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
193            utf16_to_latin1(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
194            utf8_to_compact_utf16(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
195            utf16_to_compact_utf16(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
196        }
197    };
198}
199
200// Define `struct ComponentBuiltinFunctionIndex`
201declare_builtin_index!(
202    ComponentBuiltinFunctionIndex,
203    foreach_builtin_component_function
204);