Skip to content

Commit 8bb1f8a

Browse files
author
Pat Hickey
authored
wasmtime-wiggle: make it possible to add host funcs to a config under an alias (#2761)
* wasmtime-wiggle: make it possible to add host funcs to a config under an alias * remove debugging printlns
1 parent 81c4403 commit 8bb1f8a

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

  • crates/wiggle/wasmtime/macro/src

crates/wiggle/wasmtime/macro/src/lib.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ contained in the `cx` parameter.",
135135
module_conf.name.to_string()
136136
);
137137

138+
let config_adder_definitions = host_funcs.iter().map(|(func_name, body)| {
139+
let adder_func = format_ident!("add_{}_to_config", names.func(&func_name));
140+
let docs = format!(
141+
"Add the host function for `{}` to a config under a given module and field name.",
142+
func_name.as_str()
143+
);
144+
quote! {
145+
#[doc = #docs]
146+
pub fn #adder_func(config: &mut wasmtime::Config, module: &str, field: &str) {
147+
#body
148+
}
149+
}
150+
});
151+
let config_adder_invocations = host_funcs.iter().map(|(func_name, _body)| {
152+
let adder_func = format_ident!("add_{}_to_config", names.func(&func_name));
153+
let module = module.name.as_str();
154+
let field = func_name.as_str();
155+
quote! {
156+
Self::#adder_func(config, #module, #field);
157+
}
158+
});
159+
138160
quote! {
139161
#type_docs
140162
pub struct #type_name {
@@ -151,6 +173,7 @@ contained in the `cx` parameter.",
151173
}
152174
}
153175

176+
154177
/// Looks up a field called `name` in this structure, returning it
155178
/// if found.
156179
///
@@ -175,9 +198,11 @@ contained in the `cx` parameter.",
175198
///
176199
/// Host functions will trap if the context is not set in the calling [`wasmtime::Store`].
177200
pub fn add_to_config(config: &mut wasmtime::Config) {
178-
#(#host_funcs)*
201+
#(#config_adder_invocations)*
179202
}
180203

204+
#(#config_adder_definitions)*
205+
181206
/// Sets the context in the given store.
182207
///
183208
/// Context must be set in the store when using [`add_to_config`] and prior to any
@@ -207,7 +232,7 @@ fn generate_func(
207232
is_async: bool,
208233
fns: &mut Vec<TokenStream2>,
209234
ctors: &mut Vec<TokenStream2>,
210-
host_funcs: &mut Vec<TokenStream2>,
235+
host_funcs: &mut Vec<(witx::Id, TokenStream2)>,
211236
) {
212237
let name_ident = names.func(&func.name);
213238

@@ -281,12 +306,12 @@ fn generate_func(
281306
});
282307
}
283308

284-
if is_async {
309+
let host_wrapper = if is_async {
285310
let wrapper = format_ident!("wrap{}_host_func_async", params.len());
286-
host_funcs.push(quote! {
311+
quote! {
287312
config.#wrapper(
288-
stringify!(#module_ident),
289-
stringify!(#name_ident),
313+
module,
314+
field,
290315
move |caller #(,#arg_decls)*|
291316
-> Box<dyn std::future::Future<Output = Result<#ret_ty, wasmtime::Trap>>> {
292317
Box::new(async move {
@@ -298,12 +323,12 @@ fn generate_func(
298323
})
299324
}
300325
);
301-
});
326+
}
302327
} else {
303-
host_funcs.push(quote! {
328+
quote! {
304329
config.wrap_host_func(
305-
stringify!(#module_ident),
306-
stringify!(#name_ident),
330+
module,
331+
field,
307332
move |caller: wasmtime::Caller #(, #arg_decls)*| -> Result<#ret_ty, wasmtime::Trap> {
308333
let ctx = caller
309334
.store()
@@ -313,6 +338,7 @@ fn generate_func(
313338
result
314339
},
315340
);
316-
});
317-
}
341+
}
342+
};
343+
host_funcs.push((func.name.clone(), host_wrapper));
318344
}

0 commit comments

Comments
 (0)