Fix some wit-bindgen-related issues with generated bindings (#5692)

* Prefix component-bindgen-generated-functions with `call_`

This fixes clashes between Rust-native methods and the methods
themselves. For example right now `new` is a Rust-generated function for
constructing the wrapper but this can conflict with a world-exported
function called `new`.

Closes #5585

* Fix types being both shared and owned

This refactors some inherited cruft from the original `wit-bindgen`
repository to be more Wasmtime-specific and fixes a codegen case where
a type was used in both a shared and an owned context.

Closes #5688
This commit is contained in:
Alex Crichton
2023-02-02 11:54:35 -06:00
committed by GitHub
parent 63d80fc509
commit 545749b279
7 changed files with 79 additions and 57 deletions

View File

@@ -101,7 +101,7 @@ impl Wasmtime {
fn import(&mut self, resolve: &Resolve, name: &str, item: &WorldItem) {
let snake = name.to_snake_case();
let mut gen = InterfaceGenerator::new(self, resolve, TypeMode::Owned);
let mut gen = InterfaceGenerator::new(self, resolve);
let import = match item {
WorldItem::Function(func) => {
gen.generate_function_trait_sig(TypeOwner::None, &func);
@@ -139,7 +139,7 @@ impl Wasmtime {
fn export(&mut self, resolve: &Resolve, name: &str, item: &WorldItem) {
let snake = name.to_snake_case();
let mut gen = InterfaceGenerator::new(self, resolve, TypeMode::AllBorrowed("'a"));
let mut gen = InterfaceGenerator::new(self, resolve);
let (ty, getter) = match item {
WorldItem::Function(func) => {
gen.define_rust_guest_export(None, func);
@@ -450,21 +450,15 @@ struct InterfaceGenerator<'a> {
src: Source,
gen: &'a mut Wasmtime,
resolve: &'a Resolve,
default_param_mode: TypeMode,
current_interface: Option<InterfaceId>,
}
impl<'a> InterfaceGenerator<'a> {
fn new(
gen: &'a mut Wasmtime,
resolve: &'a Resolve,
default_param_mode: TypeMode,
) -> InterfaceGenerator<'a> {
fn new(gen: &'a mut Wasmtime, resolve: &'a Resolve) -> InterfaceGenerator<'a> {
InterfaceGenerator {
src: Source::default(),
gen,
resolve,
default_param_mode,
current_interface: None,
}
}
@@ -1159,7 +1153,7 @@ impl<'a> InterfaceGenerator<'a> {
self.rustdoc(&func.docs);
uwrite!(
self.src,
"pub {async_} fn {}<S: wasmtime::AsContextMut>(&self, mut store: S, ",
"pub {async_} fn call_{}<S: wasmtime::AsContextMut>(&self, mut store: S, ",
func.name.to_snake_case(),
);
for (i, param) in func.params.iter().enumerate() {
@@ -1351,10 +1345,6 @@ impl<'a> RustGenerator<'a> for InterfaceGenerator<'a> {
self.current_interface
}
fn default_param_mode(&self) -> TypeMode {
self.default_param_mode
}
fn push_str(&mut self, s: &str) {
self.src.push_str(s);
}

View File

@@ -15,7 +15,6 @@ pub trait RustGenerator<'a> {
fn push_str(&mut self, s: &str);
fn info(&self, ty: TypeId) -> TypeInfo;
fn default_param_mode(&self) -> TypeMode;
fn current_interface(&self) -> Option<InterfaceId>;
fn print_ty(&mut self, ty: &Type, mode: TypeMode) {
@@ -209,10 +208,10 @@ pub trait RustGenerator<'a> {
fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
let info = self.info(ty);
let mut result = Vec::new();
if info.param {
result.push((self.param_name(ty), self.default_param_mode()));
if info.borrowed {
result.push((self.param_name(ty), TypeMode::AllBorrowed("'a")));
}
if info.result && (!info.param || self.uses_two_names(&info)) {
if info.owned && (!info.borrowed || self.uses_two_names(&info)) {
result.push((self.result_name(ty), TypeMode::Owned));
}
return result;
@@ -358,13 +357,7 @@ pub trait RustGenerator<'a> {
}
fn uses_two_names(&self, info: &TypeInfo) -> bool {
info.has_list
&& info.param
&& info.result
&& match self.default_param_mode() {
TypeMode::AllBorrowed(_) => true,
TypeMode::Owned => false,
}
info.has_list && info.borrowed && info.owned
}
fn lifetime_for(&self, info: &TypeInfo, mode: TypeMode) -> Option<&'static str> {

View File

@@ -8,13 +8,14 @@ pub struct Types {
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct TypeInfo {
/// Whether or not this type is ever used (transitively) within the
/// parameter of a function.
pub param: bool,
/// Whether or not this type is ever used (transitively) within a borrowed
/// context, or a parameter to an export function.
pub borrowed: bool,
/// Whether or not this type is ever used (transitively) within the
/// result of a function.
pub result: bool,
/// Whether or not this type is ever used (transitively) within an owned
/// context, such as the result of an exported function or in the params or
/// results of an imported function.
pub owned: bool,
/// Whether or not this type is ever used (transitively) within the
/// error case in the result of a function.
@@ -26,8 +27,8 @@ pub struct TypeInfo {
impl std::ops::BitOrAssign for TypeInfo {
fn bitor_assign(&mut self, rhs: Self) {
self.param |= rhs.param;
self.result |= rhs.result;
self.borrowed |= rhs.borrowed;
self.owned |= rhs.owned;
self.error |= rhs.error;
self.has_list |= rhs.has_list;
}
@@ -36,9 +37,14 @@ impl std::ops::BitOrAssign for TypeInfo {
impl Types {
pub fn analyze(&mut self, resolve: &Resolve, world: WorldId) {
let world = &resolve.worlds[world];
for (_, item) in world.imports.iter().chain(world.exports.iter()) {
for (import, (_, item)) in world
.imports
.iter()
.map(|i| (true, i))
.chain(world.exports.iter().map(|i| (false, i)))
{
match item {
WorldItem::Function(f) => self.type_info_func(resolve, f),
WorldItem::Function(f) => self.type_info_func(resolve, f, import),
WorldItem::Interface(id) => {
let iface = &resolve.interfaces[*id];
@@ -46,21 +52,26 @@ impl Types {
self.type_id_info(resolve, *t);
}
for (_, f) in iface.functions.iter() {
self.type_info_func(resolve, f);
self.type_info_func(resolve, f, import);
}
}
}
}
}
fn type_info_func(&mut self, resolve: &Resolve, func: &Function) {
fn type_info_func(&mut self, resolve: &Resolve, func: &Function, import: bool) {
let mut live = LiveTypes::default();
for (_, ty) in func.params.iter() {
self.type_info(resolve, ty);
live.add_type(resolve, ty);
}
for id in live.iter() {
self.type_info.get_mut(&id).unwrap().param = true;
let info = self.type_info.get_mut(&id).unwrap();
if import {
info.owned = true;
} else {
info.borrowed = true;
}
}
let mut live = LiveTypes::default();
for ty in func.results.iter_types() {
@@ -68,7 +79,7 @@ impl Types {
live.add_type(resolve, ty);
}
for id in live.iter() {
self.type_info.get_mut(&id).unwrap().result = true;
self.type_info.get_mut(&id).unwrap().owned = true;
}
for ty in func.results.iter_types() {