Path: blob/main/crates/environ/src/component.rs
3067 views
//! Support for the component model in Wasmtime.1//!2//! This module contains all of the internal type definitions used by Wasmtime3//! to process the component model. Despite everything being `pub` here this is4//! not the public interface of Wasmtime to the component model. Instead this is5//! the internal support to mirror the core wasm support that Wasmtime already6//! implements.7//!8//! Some main items contained within here are:9//!10//! * Type hierarchy information for the component model11//! * Translation of a component into Wasmtime's representation12//! * Type information about a component used at runtime13//!14//! This module also contains a lot of Serialize/Deserialize types which are15//! encoded in the final compiled image for a component.16//!17//! Note that this entire module is gated behind the `component-model` Cargo18//! feature.1920/// Canonical ABI-defined constant for the maximum number of "flat" parameters21/// to a wasm function, or the maximum number of parameters a core wasm function22/// will take for just the parameters used. Over this number the heap is used23/// for transferring parameters.24pub const MAX_FLAT_PARAMS: usize = 16;2526/// Similar to `MAX_FLAT_PARAMS`, but used for async-lowered imports instead of27/// sync ones.28pub const MAX_FLAT_ASYNC_PARAMS: usize = 4;2930/// Canonical ABI-defined constant for the maximum number of "flat" results.31/// This number of results are returned directly from wasm and otherwise results32/// are transferred through memory.33pub const MAX_FLAT_RESULTS: usize = 1;3435/// Sentinel value in `result_count_or_max_if_async` as part of the36/// `prepare_call` libcall which indicates that preparation is being done for an37/// async function that produces no result, aka there is no return pointer.38pub const PREPARE_ASYNC_NO_RESULT: u32 = u32::MAX;3940/// Sentinel value in `result_count_or_max_if_async` as part of the41/// `prepare_call` libcall which indicates that preparation is being done for an42/// async function that produces at least one result, aka there is a return43/// pointer.44pub const PREPARE_ASYNC_WITH_RESULT: u32 = u32::MAX - 1;4546/// Bit flag for indicating async-lifted exports47///48/// This flag may be passed to the `async-start` built-in function (which is49/// called from both async->async and async->sync adapters) to indicate that the50/// callee is an async-lifted export.51pub const START_FLAG_ASYNC_CALLEE: i32 = 1 << 0;5253mod artifacts;54mod info;55mod intrinsic;56mod names;57mod types;58mod vmcomponent_offsets;59pub use self::artifacts::*;60pub use self::info::*;61pub use self::intrinsic::*;62pub use self::names::*;63pub use self::types::*;64pub use self::vmcomponent_offsets::*;6566#[cfg(feature = "compile")]67mod compiler;68#[cfg(feature = "compile")]69pub mod dfg;70#[cfg(feature = "compile")]71mod translate;72#[cfg(feature = "compile")]73mod types_builder;74#[cfg(feature = "compile")]75pub use self::compiler::*;76#[cfg(feature = "compile")]77pub use self::translate::*;78#[cfg(feature = "compile")]79pub use self::types_builder::*;8081/// Helper macro, like `foreach_transcoder`, to iterate over builtins for82/// components unrelated to transcoding.83#[macro_export]84macro_rules! foreach_builtin_component_function {85($mac:ident) => {86$mac! {87resource_new32(vmctx: vmctx, caller_instance: u32, resource: u32, rep: u32) -> u64;88resource_rep32(vmctx: vmctx, caller_instance: u32, resource: u32, idx: u32) -> u64;8990// Returns an `Option<u32>` where `None` is "no destructor needed"91// and `Some(val)` is "run the destructor on this rep". The option92// is encoded as a 64-bit integer where the low bit is Some/None93// and bits 1-33 are the payload.94resource_drop(vmctx: vmctx, caller_instance: u32, resource: u32, idx: u32) -> u64;9596resource_transfer_own(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;97resource_transfer_borrow(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;98resource_enter_call(vmctx: vmctx);99resource_exit_call(vmctx: vmctx) -> bool;100101#[cfg(feature = "component-model-async")]102enter_sync_call(vmctx: vmctx, caller_instance: u32, callee_async: u32, callee_instance: u32) -> bool;103#[cfg(feature = "component-model-async")]104exit_sync_call(vmctx: vmctx) -> bool;105106#[cfg(feature = "component-model-async")]107backpressure_modify(vmctx: vmctx, caller_instance: u32, increment: u8) -> bool;108#[cfg(feature = "component-model-async")]109task_return(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, storage: ptr_u8, storage_len: size) -> bool;110#[cfg(feature = "component-model-async")]111task_cancel(vmctx: vmctx, caller_instance: u32) -> bool;112#[cfg(feature = "component-model-async")]113waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;114#[cfg(feature = "component-model-async")]115waitable_set_wait(vmctx: vmctx, caller_instance: u32, options: u32, set: u32, payload: u32) -> u64;116#[cfg(feature = "component-model-async")]117waitable_set_poll(vmctx: vmctx, caller_instance: u32, options: u32, set: u32, payload: u32) -> u64;118#[cfg(feature = "component-model-async")]119waitable_set_drop(vmctx: vmctx, caller_instance: u32, set: u32) -> bool;120#[cfg(feature = "component-model-async")]121waitable_join(vmctx: vmctx, caller_instance: u32, set: u32, waitable: u32) -> bool;122#[cfg(feature = "component-model-async")]123thread_yield(vmctx: vmctx, caller_instance: u32, cancellable: u8) -> u32;124#[cfg(feature = "component-model-async")]125subtask_drop(vmctx: vmctx, caller_instance: u32, task_id: u32) -> bool;126#[cfg(feature = "component-model-async")]127subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;128#[cfg(feature = "component-model-async")]129prepare_call(130vmctx: vmctx,131memory: ptr_u8,132start: ptr_u8,133return_: ptr_u8,134caller_instance: u32,135callee_instance: u32,136task_return_type: u32,137callee_async: u32,138string_encoding: u32,139result_count_or_max_if_async: u32,140storage: ptr_u8,141storage_len: size142) -> bool;143#[cfg(feature = "component-model-async")]144sync_start(vmctx: vmctx, callback: ptr_u8, storage: ptr_u8, storage_len: size, callee: ptr_u8, param_count: u32) -> bool;145#[cfg(feature = "component-model-async")]146async_start(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> u64;147#[cfg(feature = "component-model-async")]148future_new(vmctx: vmctx, caller_instance: u32, ty: u32) -> u64;149#[cfg(feature = "component-model-async")]150future_write(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, future: u32, address: u32) -> u64;151#[cfg(feature = "component-model-async")]152future_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, future: u32, address: u32) -> u64;153#[cfg(feature = "component-model-async")]154future_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;155#[cfg(feature = "component-model-async")]156future_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;157#[cfg(feature = "component-model-async")]158future_drop_writable(vmctx: vmctx, caller_instance: u32, ty: u32, writer: u32) -> bool;159#[cfg(feature = "component-model-async")]160future_drop_readable(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32) -> bool;161#[cfg(feature = "component-model-async")]162stream_new(vmctx: vmctx, caller_instance: u32, ty: u32) -> u64;163#[cfg(feature = "component-model-async")]164stream_write(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;165#[cfg(feature = "component-model-async")]166stream_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;167#[cfg(feature = "component-model-async")]168stream_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;169#[cfg(feature = "component-model-async")]170stream_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;171#[cfg(feature = "component-model-async")]172stream_drop_writable(vmctx: vmctx, caller_instance: u32, ty: u32, writer: u32) -> bool;173#[cfg(feature = "component-model-async")]174stream_drop_readable(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32) -> bool;175#[cfg(feature = "component-model-async")]176flat_stream_write(vmctx: vmctx, caller_instance: u32, ty: u32, options:u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;177#[cfg(feature = "component-model-async")]178flat_stream_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;179#[cfg(feature = "component-model-async")]180error_context_new(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;181#[cfg(feature = "component-model-async")]182error_context_debug_message(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, err_ctx_handle: u32, debug_msg_address: u32) -> bool;183#[cfg(feature = "component-model-async")]184error_context_drop(vmctx: vmctx, caller_instance: u32, ty: u32, err_ctx_handle: u32) -> bool;185#[cfg(feature = "component-model-async")]186future_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;187#[cfg(feature = "component-model-async")]188stream_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;189#[cfg(feature = "component-model-async")]190error_context_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;191#[cfg(feature = "component-model-async")]192context_get(vmctx: vmctx, caller_instance: u32, slot: u32) -> u64;193#[cfg(feature = "component-model-async")]194context_set(vmctx: vmctx, caller_instance: u32, slot: u32, val: u32) -> bool;195#[cfg(feature = "component-model-async")]196thread_index(vmctx: vmctx) -> u64;197#[cfg(feature = "component-model-async")]198thread_new_indirect(vmctx: vmctx, caller_instance: u32, func_ty_id: u32, func_table_idx: u32, func_idx: u32, context: u32) -> u64;199#[cfg(feature = "component-model-async")]200thread_switch_to(vmctx: vmctx, caller_instance: u32, cancellable: u8, thread_idx: u32) -> u32;201#[cfg(feature = "component-model-async")]202thread_suspend(vmctx: vmctx, caller_instance: u32, cancellable: u8) -> u32;203#[cfg(feature = "component-model-async")]204thread_resume_later(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> bool;205#[cfg(feature = "component-model-async")]206thread_yield_to(vmctx: vmctx, caller_instance: u32, cancellable: u8, thread_idx: u32) -> u32;207208trap(vmctx: vmctx, code: u32) -> bool;209210utf8_to_utf8(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;211utf16_to_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> bool;212latin1_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;213latin1_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> bool;214utf8_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> size;215utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;216latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;217utf16_to_compact_probably_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> size;218utf8_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8, ret2: ptr_size) -> size;219utf16_to_latin1(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u8, ret2: ptr_size) -> size;220utf8_to_compact_utf16(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;221utf16_to_compact_utf16(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;222}223};224}225226// Define `struct ComponentBuiltinFunctionIndex`227declare_builtin_index! {228/// An index type for component builtin functions.229pub struct ComponentBuiltinFunctionIndex: foreach_builtin_component_function;230}231232233