Skip to content

Commit 0dd45bf

Browse files
authored
wasmtime: Remove lifetime and type parameter from exports (#374)
This commit removes the type parameter since it's unused at this time. It was historically used for handles but that's under development again so it's no longer needed. This commit then additionally removes the lifetime parameter from the structure, opting to store `Func` instead of `TypedFunc` to avoid the need to name types and their lifetime parameters. Functions internally then use `TypedFunc::new_unchecked` to re-acquire the typed view with appropriate anonymous lifetimes in parameters.
1 parent 0aa34ce commit 0dd45bf

File tree

1 file changed

+32
-40
lines changed
  • crates/gen-host-wasmtime-rust/src

1 file changed

+32
-40
lines changed

crates/gen-host-wasmtime-rust/src/lib.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::mem;
66
use std::process::{Command, Stdio};
77
use wit_bindgen_core::wit_parser::abi::AbiVariant;
88
use wit_bindgen_core::{
9-
uwrite, wit_parser::*, Direction, Files, Generator, Source, TypeInfo, Types,
9+
uwrite, uwriteln, wit_parser::*, Direction, Files, Generator, Source, TypeInfo, Types,
1010
};
1111
use wit_bindgen_gen_rust_lib::{to_rust_ident, FnSig, RustGenerator, TypeMode};
1212

@@ -352,7 +352,7 @@ impl Generator for Wasmtime {
352352
let prev = mem::take(&mut self.src);
353353
uwrite!(
354354
self.src,
355-
"pub fn {}(&self, mut store: impl wasmtime::AsContextMut<Data = T>, ",
355+
"pub fn {}(&self, mut store: impl wasmtime::AsContextMut, ",
356356
func.name.to_snake_case(),
357357
);
358358
for (i, param) in func.params.iter().enumerate() {
@@ -379,25 +379,34 @@ impl Generator for Wasmtime {
379379
));
380380
}
381381

382-
self.src.push_str("let (");
383-
for (i, _) in func.results.iter_types().enumerate() {
384-
uwrite!(self.src, "ret{},", i);
382+
self.src.push_str("let callee = unsafe {\n");
383+
self.src.push_str("wasmtime::component::TypedFunc::<(");
384+
for (_, ty) in func.params.iter() {
385+
self.print_ty(iface, ty, TypeMode::AllBorrowed("'_"));
386+
self.push_str(", ");
385387
}
386-
uwrite!(
388+
self.src.push_str("), (");
389+
for ty in func.results.iter_types() {
390+
self.print_ty(iface, ty, TypeMode::Owned);
391+
self.push_str(", ");
392+
}
393+
uwriteln!(
387394
self.src,
388-
") = self.{}.call(store.as_context_mut(), (",
395+
")>::new_unchecked(self.{})",
389396
func.name.to_snake_case()
390397
);
398+
self.src.push_str("};\n");
399+
self.src.push_str("let (");
400+
for (i, _) in func.results.iter_types().enumerate() {
401+
uwrite!(self.src, "ret{},", i);
402+
}
403+
uwrite!(self.src, ") = callee.call(store.as_context_mut(), (");
391404
for (i, _) in func.params.iter().enumerate() {
392405
uwrite!(self.src, "arg{}, ", i);
393406
}
394-
uwrite!(self.src, "))?;\n");
407+
uwriteln!(self.src, "))?;");
395408

396-
uwrite!(
397-
self.src,
398-
"self.{}.post_return(store.as_context_mut())?;\n",
399-
func.name.to_snake_case()
400-
);
409+
uwriteln!(self.src, "callee.post_return(store.as_context_mut())?;");
401410

402411
self.src.push_str("Ok(");
403412
if func.results.iter_types().len() == 1 {
@@ -417,23 +426,7 @@ impl Generator for Wasmtime {
417426
let pub_func = mem::replace(&mut self.src, prev).into();
418427
let prev = mem::take(&mut self.src);
419428

420-
self.src.push_str("wasmtime::component::TypedFunc<(");
421-
// ComponentNamedList means using tuple for all:
422-
for (_, ty) in func.params.iter() {
423-
self.print_ty(iface, ty, TypeMode::AllBorrowed("'a"));
424-
self.push_str(", ");
425-
}
426-
self.src.push_str("), (");
427-
for ty in func.results.iter_types() {
428-
self.print_ty(iface, ty, TypeMode::Owned);
429-
self.push_str(", ");
430-
}
431-
self.src.push_str(")>");
432-
433-
let type_sig: String = mem::replace(&mut self.src, prev).into();
434-
let prev = mem::take(&mut self.src);
435-
436-
self.src.push_str("instance.get_typed_func::<(");
429+
self.src.push_str("*instance.get_typed_func::<(");
437430
for (_, ty) in func.params.iter() {
438431
self.print_ty(iface, ty, TypeMode::AllBorrowed("'_"));
439432
self.push_str(", ");
@@ -447,17 +440,18 @@ impl Generator for Wasmtime {
447440

448441
self.src.push_str("), _>(&mut store, \"");
449442
self.src.push_str(&func.name);
450-
self.src.push_str("\")?");
443+
self.src.push_str("\")?.func()");
451444
let getter: String = mem::replace(&mut self.src, prev).into();
452445

453446
let exports = self
454447
.guest_exports
455448
.entry(iface.name.to_string())
456449
.or_insert_with(Exports::default);
457450
exports.funcs.push(pub_func);
458-
exports
459-
.fields
460-
.insert(to_rust_ident(&func.name), (type_sig, getter));
451+
exports.fields.insert(
452+
to_rust_ident(&func.name),
453+
("wasmtime::component::Func".to_string(), getter),
454+
);
461455
}
462456

463457
fn finish_one(&mut self, _iface: &Interface, files: &mut Files) {
@@ -498,16 +492,15 @@ impl Generator for Wasmtime {
498492
for (module, exports) in sorted_iter(&mem::take(&mut self.guest_exports)) {
499493
let name = module.to_upper_camel_case();
500494

501-
uwrite!(self.src, "pub struct {}<'a, T> {{\n", name);
502-
self.push_str("_phantom: std::marker::PhantomData<&'a T>,");
495+
uwrite!(self.src, "pub struct {} {{\n", name);
503496
for (name, (ty, _)) in exports.fields.iter() {
504497
self.push_str(name);
505498
self.push_str(": ");
506499
self.push_str(ty);
507500
self.push_str(",\n");
508501
}
509502
self.push_str("}\n");
510-
uwrite!(self.src, "impl<'a, T> {}<'a, T> {{\n", name);
503+
uwrite!(self.src, "impl {} {{\n", name);
511504

512505
self.push_str(&format!(
513506
"
@@ -521,7 +514,7 @@ impl Generator for Wasmtime {
521514
/// instantiate the `module` otherwise using `linker`, and
522515
/// both an instance of this structure and the underlying
523516
/// `wasmtime::Instance` will be returned.
524-
pub fn instantiate(
517+
pub fn instantiate<T>(
525518
mut store: impl wasmtime::AsContextMut<Data = T>,
526519
component: &wasmtime::component::Component,
527520
linker: &mut wasmtime::component::Linker<T>,
@@ -543,7 +536,7 @@ impl Generator for Wasmtime {
543536
/// returned structure which can be used to interact with
544537
/// the wasm module.
545538
pub fn new(
546-
mut store: impl wasmtime::AsContextMut<Data = T>,
539+
mut store: impl wasmtime::AsContextMut,
547540
instance: &wasmtime::component::Instance,
548541
) -> anyhow::Result<Self> {{
549542
",
@@ -559,7 +552,6 @@ impl Generator for Wasmtime {
559552
self.push_str("Ok(");
560553
self.push_str(&name);
561554
self.push_str("{\n");
562-
self.push_str("_phantom: std::marker::PhantomData,");
563555
for (name, _) in exports.fields.iter() {
564556
self.push_str(name);
565557
self.push_str(",\n");

0 commit comments

Comments
 (0)