Skip to content

Commit

Permalink
Wasmtime component bindgen: opt-in trappable error types (#5397)
Browse files Browse the repository at this point in the history
* wip

* start trying to write a runtime test

* cut out all the more complex test cases until i get this one working

* add macro parsing for the trappable error type config

* runtime result tests works for an empty and a string error type

* debugging: macro is broken because interfaces dont have names???

* thats how you name interfaces

* record error and variant error work

* show a concrete trap type, remove debug

* delete clap annotations from wit-bindgen crate

these are not used - clap isnt even an optional dep here - but were a holdover from the old home
  • Loading branch information
Pat Hickey authored Dec 14, 2022
1 parent f0af622 commit 2e0bc7d
Show file tree
Hide file tree
Showing 8 changed files with 714 additions and 176 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions crates/component-macro/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use proc_macro2::{Span, TokenStream};
use std::path::{Path, PathBuf};
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::token;
use syn::Token;
use syn::{braced, token, Ident, Token};
use wasmtime_wit_bindgen::Opts;
use wit_parser::{Document, World};

Expand Down Expand Up @@ -68,6 +67,7 @@ impl Parse for Config {
}
Opt::Tracing(val) => ret.opts.tracing = val,
Opt::Async(val) => ret.opts.async_ = val,
Opt::TrappableErrorType(val) => ret.opts.trappable_error_type = val,
}
}
} else {
Expand Down Expand Up @@ -99,13 +99,15 @@ mod kw {
syn::custom_keyword!(path);
syn::custom_keyword!(inline);
syn::custom_keyword!(tracing);
syn::custom_keyword!(trappable_error_type);
}

enum Opt {
Path(syn::LitStr),
Inline(Span, World),
Tracing(bool),
Async(bool),
TrappableErrorType(Vec<(String, String, String)>),
}

impl Parse for Opt {
Expand All @@ -132,8 +134,25 @@ impl Parse for Opt {
input.parse::<Token![async]>()?;
input.parse::<Token![:]>()?;
Ok(Opt::Async(input.parse::<syn::LitBool>()?.value))
} else if l.peek(kw::trappable_error_type) {
input.parse::<kw::trappable_error_type>()?;
input.parse::<Token![:]>()?;
let contents;
let _lbrace = braced!(contents in input);
let fields: Punctuated<(String, String, String), Token![,]> =
contents.parse_terminated(trappable_error_field_parse)?;
Ok(Opt::TrappableErrorType(fields.into_iter().collect()))
} else {
Err(l.error())
}
}
}

fn trappable_error_field_parse(input: ParseStream<'_>) -> Result<(String, String, String)> {
let interface = input.parse::<Ident>()?.to_string();
input.parse::<Token![::]>()?;
let type_ = input.parse::<Ident>()?.to_string();
input.parse::<Token![:]>()?;
let rust_type = input.parse::<Ident>()?.to_string();
Ok((interface, type_, rust_type))
}
126 changes: 0 additions & 126 deletions crates/wasmtime/src/component/error.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/wasmtime/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! probably buggy implementation of the component model.

mod component;
mod error;
mod func;
mod instance;
mod linker;
Expand All @@ -14,7 +13,6 @@ mod store;
pub mod types;
mod values;
pub use self::component::Component;
pub use self::error::{Error, Result};
pub use self::func::{
ComponentNamedList, ComponentType, Func, Lift, Lower, TypedFunc, WasmList, WasmStr,
};
Expand Down
1 change: 1 addition & 0 deletions crates/wit-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ documentation = "https://docs.rs/wasmtime-wit-bindgen/"
edition.workspace = true

[dependencies]
anyhow = { workspace = true }
heck = { workspace = true }
wit-parser = { workspace = true }
Loading

0 comments on commit 2e0bc7d

Please sign in to comment.