Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/environ/src/hostcall.rs
1691 views
1
use crate::BuiltinFunctionIndex;
2
#[cfg(feature = "component-model")]
3
use crate::component::ComponentBuiltinFunctionIndex;
4
5
/// Enumeration of all possible ways that wasm may execute code in the
6
/// host.
7
///
8
/// This type is intended to be serialized into a 32-bit index (or smaller) and
9
/// is used by Pulley for example to identify how transitions from the guest to
10
/// the host are performed.
11
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12
pub enum HostCall {
13
/// An "array call" is being done which means that the wasm is calling the
14
/// host using the array calling convention (e.g. `VMArrayCallNative`).
15
ArrayCall,
16
17
/// A builtin function, e.g. for `memory.grow`, is being called. Each
18
/// builtin has its own ABI.
19
Builtin(BuiltinFunctionIndex),
20
21
/// A lowered component function is being called. This is done by a
22
/// trampoline generated by Cranelift and is distinct from the array calling
23
/// convention.
24
///
25
/// This correspond to `VMLoweringCallee`.
26
#[cfg(feature = "component-model")]
27
ComponentLowerImport,
28
29
/// A builtin function, but specifically for components. For example string
30
/// transcoders.
31
#[cfg(feature = "component-model")]
32
ComponentBuiltin(ComponentBuiltinFunctionIndex),
33
}
34
35
impl HostCall {
36
/// Returns a 32-bit index for this hostcall.
37
pub const fn index(&self) -> u32 {
38
match self {
39
HostCall::ArrayCall => 0,
40
HostCall::Builtin(i) => 1 + i.index(),
41
#[cfg(feature = "component-model")]
42
HostCall::ComponentLowerImport => 1 + BuiltinFunctionIndex::len(),
43
#[cfg(feature = "component-model")]
44
HostCall::ComponentBuiltin(i) => 2 + BuiltinFunctionIndex::len() + i.index(),
45
}
46
}
47
48
/// Create a `HostCall` from the result of an earlier call to
49
/// `HostCall::index`.
50
pub fn from_index(index: u32) -> Self {
51
let host_call = match index {
52
0 => Self::ArrayCall,
53
_ if index < 1 + BuiltinFunctionIndex::len() => {
54
Self::Builtin(BuiltinFunctionIndex::from_u32(index - 1))
55
}
56
#[cfg(feature = "component-model")]
57
_ if index == 1 + BuiltinFunctionIndex::len() => Self::ComponentLowerImport,
58
#[cfg(feature = "component-model")]
59
_ if index < 2 + BuiltinFunctionIndex::len() + ComponentBuiltinFunctionIndex::len() => {
60
Self::ComponentBuiltin(ComponentBuiltinFunctionIndex::from_u32(
61
index - 2 - BuiltinFunctionIndex::len(),
62
))
63
}
64
_ => panic!("bad host call index: {index}"),
65
};
66
debug_assert_eq!(index, host_call.index());
67
host_call
68
}
69
}
70
71
impl From<BuiltinFunctionIndex> for HostCall {
72
fn from(idx: BuiltinFunctionIndex) -> HostCall {
73
HostCall::Builtin(idx)
74
}
75
}
76
77
#[cfg(feature = "component-model")]
78
impl From<ComponentBuiltinFunctionIndex> for HostCall {
79
fn from(idx: ComponentBuiltinFunctionIndex) -> HostCall {
80
HostCall::ComponentBuiltin(idx)
81
}
82
}
83
84