Skip to main content

wasm_link/
interface.rs

1use std::sync::{ Arc, Mutex };
2use std::collections::{ HashMap, HashSet };
3use wasmtime::component::{ Linker, ResourceType, Val };
4
5use crate::{ Binding, PluginContext };
6use crate::cardinality::Cardinality ;
7use crate::linker::{ dispatch_all, dispatch_method };
8use crate::resource_wrapper::ResourceWrapper ;
9
10/// A single WIT interface within a [`Binding`].
11///
12/// Each interface declares functions and resources that implementers must export.
13/// Note that the interface name is not a part of the struct but rather a key in
14/// a hash map provided to the Binding constructor. This is to prevent duplicate
15/// interface names.
16///
17/// ```
18/// # use std::collections::{ HashMap, HashSet };
19/// # use wasm_link::{ Binding, Interface, PluginContext, PluginInstance, ResourceTable };
20/// # use wasm_link::cardinality::AtMostOne ;
21/// # struct Ctx { resource_table: ResourceTable }
22/// # impl PluginContext for Ctx {
23/// # 	fn resource_table( &mut self ) -> &mut ResourceTable { &mut self.resource_table }
24/// # }
25/// let binding: Binding<String, Ctx, AtMostOne<String, PluginInstance<Ctx>>> = Binding::new(
26/// 	"my:package",
27/// 	HashMap::from([
28/// 		( "interface-a".to_string(), Interface::new( HashMap::new(), HashSet::new() )),
29/// 		( "interface-b".to_string(), Interface::new( HashMap::new(), HashSet::new() )),
30/// 	]),
31/// 	AtMostOne( None ),
32/// );
33/// # let _ = binding;
34/// ```
35#[derive( Debug, Clone, Default )]
36pub struct Interface {
37	/// Functions exported by this interface
38	functions: HashMap<String, Function>,
39	/// Resource types defined by this interface
40	resources: HashSet<String>,
41}
42
43impl Interface {
44	/// Creates a new interface declaration.
45	pub fn new(
46		functions: HashMap<String, Function>,
47		resources: HashSet<String>,
48	) -> Self {
49		Self { functions, resources }
50	}
51
52	#[inline]
53	pub(crate) fn function( &self, name: &str ) -> Option<&Function> {
54		self.functions.get( name )
55	}
56
57	#[inline]
58	pub(crate) fn add_to_linker<PluginId, Ctx, Plugins>(
59		&self,
60		linker: &mut Linker<Ctx>,
61		interface_ident: &str,
62		binding: &Binding<PluginId, Ctx, Plugins>,
63	) -> Result<(), wasmtime::Error>
64	where
65		PluginId: std::hash::Hash + Eq + Clone + Send + Sync + Into<Val> + 'static,
66		Ctx: PluginContext,
67		Plugins: Cardinality<PluginId, crate::PluginInstance<Ctx>> + 'static,
68		<Plugins as Cardinality<PluginId, crate::PluginInstance<Ctx>>>::Rebind<Mutex<crate::PluginInstance<Ctx>>>: Send + Sync,
69		<Plugins as Cardinality<PluginId, crate::PluginInstance<Ctx>>>::Rebind<Mutex<crate::PluginInstance<Ctx>>>: Cardinality<PluginId, Mutex<crate::PluginInstance<Ctx>>>,
70		<<Plugins as Cardinality<PluginId, crate::PluginInstance<Ctx>>>::Rebind<Mutex<crate::PluginInstance<Ctx>>> as Cardinality<PluginId, Mutex<crate::PluginInstance<Ctx>>>>::Rebind<Val>: Into<Val>,
71	{
72		let mut linker_root = linker.root();
73		let mut linker_instance = linker_root.instance( interface_ident )?;
74
75		self.functions.iter().try_for_each(|( name, metadata )| {
76
77			let interface_ident_clone = interface_ident.to_string();
78			let binding_clone = binding.clone();
79			let name_clone = name.clone();
80			let metadata_clone = metadata.clone();
81
82			macro_rules! link {( $dispatch: expr ) => {
83				linker_instance.func_new( name, move | ctx, _ty, args, results | Ok(
84					results[0] = $dispatch( &binding_clone, ctx, &interface_ident_clone, &name_clone, &metadata_clone, args )
85				))
86			}}
87
88			match metadata.kind() {
89				FunctionKind::Freestanding => link!( dispatch_all ),
90				FunctionKind::Method => link!( dispatch_method ),
91			}
92
93		})?;
94
95		self.resources.iter().try_for_each(| resource | linker_instance
96			.resource( resource.as_str(), ResourceType::host::<Arc<ResourceWrapper<PluginId>>>(), ResourceWrapper::<PluginId>::drop )
97		)?;
98
99		Ok(())
100
101	}
102
103}
104
105/// Denotes whether a function is freestanding or a resource method.
106/// Constructors are treated as freestanding functions.
107///
108/// Determines how dispatch is routed during cross-plugin calls:
109/// freestanding functions broadcast to all plugins, while methods
110/// route to the specific plugin that owns the resource.
111#[derive( Debug, Clone, Copy, Eq, PartialEq )]
112pub enum FunctionKind {
113	/// A freestanding function — dispatched to all plugins.
114	Freestanding,
115	/// A resource method (has a `self` parameter) — routed to the plugin that owns the resource.
116	Method,
117}
118
119/// Metadata about a function declared by an interface.
120///
121/// Provides information needed during linking to wire up cross-plugin dispatch.
122#[derive( Debug, Clone )]
123pub struct Function {
124	/// Whether this function is freestanding or a resource method.
125	kind: FunctionKind,
126	/// The function's return kind for dispatch handling
127	return_kind: ReturnKind,
128}
129
130impl Function {
131	/// Creates a new function metadata entry.
132	pub fn new(
133		kind: FunctionKind,
134		return_kind: ReturnKind,
135	) -> Self {
136		Self { kind, return_kind }
137	}
138
139	/// The function's return kind for dispatch handling.
140	pub fn return_kind( &self ) -> ReturnKind { self.return_kind }
141
142	/// Whether this function is freestanding or a resource method.
143	pub fn kind( &self ) -> FunctionKind { self.kind }
144
145}
146
147/// Categorizes a function's return for dispatch handling.
148///
149/// Determines how return values are processed during cross-plugin dispatch.
150/// Resources require special wrapping to track ownership across plugin
151/// boundaries, while plain data can be passed through directly.
152///
153/// # Choosing the Right Variant
154///
155/// **When uncertain, use [`MayContainResources`]( Self::MayContainResources ).**
156/// Using [`AssumeNoResources`]( Self::AssumeNoResources ) when resources are
157/// actually present will cause resource handles to be passed through unwrapped
158/// causing runtime exceptions.
159///
160/// [`AssumeNoResources`]( Self::AssumeNoResources ) is a performance optimization
161/// that skips the wrapping step. Only use it when you are certain the return type
162/// contains no resource handles anywhere in its structure (including nested within
163/// records, variants, lists, etc.).
164#[derive( Copy, Clone, Eq, PartialEq, Hash, Debug, Default )]
165pub enum ReturnKind {
166	/// Function returns nothing (void).
167	#[default] Void,
168	/// Function may return resource handles - always wraps safely.
169	///
170	/// Use this variant whenever resources might be present in the return value,
171	/// or when you're unsure. The performance overhead of wrapping is preferable
172	/// to the undefined behavior caused by unwrapped resource handles.
173	MayContainResources,
174	/// Assumes no resource handles are present - skips wrapping for performance.
175	///
176	/// **Warning:** Only use this if you are certain no resources are present.
177	/// If resources are returned but this variant is used, resource handles will
178	/// not be wrapped correctly, potentially causing undefined behavior in plugins.
179	/// When in doubt, use [`MayContainResources`](Self::MayContainResources) instead.
180	AssumeNoResources,
181}
182
183impl std::fmt::Display for ReturnKind {
184	fn fmt( &self, f: &mut std::fmt::Formatter ) -> Result<(), std::fmt::Error> {
185		match self {
186			Self::Void => write!( f, "Function returns no data" ),
187			Self::MayContainResources => write!( f, "Return type may contain resources" ),
188			Self::AssumeNoResources => write!( f, "Function is assumed to not return any resources" ),
189		}
190	}
191}