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