Skip to content

Commit

Permalink
Add compilation target enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kajacx committed Apr 14, 2024
1 parent 0d1a518 commit 87f4fb2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
50 changes: 36 additions & 14 deletions crates/wasm-bridge-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use std::str::FromStr;
use std::{ops::Deref, str::FromStr};

use original::{Style, VariantStyle};
use quote::ToTokens;
use regex::{Captures, Regex};
use syn::{Attribute, ImplItem, ItemImpl};

mod direct_impl;
mod original;

mod direct_impl;
#[derive(Debug, Clone, PartialEq, Eq)]
enum CompilationTarget {
Sys,
Js,
}

#[proc_macro_derive(Lift, attributes(component))]
pub fn lift(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand All @@ -26,12 +31,12 @@ pub fn component_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream

#[proc_macro]
pub fn flags_sys(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
replace_namespace(original::flags(input, true))
replace_namespace(original::flags(input, CompilationTarget::Sys))
}

#[proc_macro]
pub fn flags_js(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
replace_namespace(original::flags(input, false))
replace_namespace(original::flags(input, CompilationTarget::Js))
}

fn bindgen(input: proc_macro::TokenStream) -> String {
Expand All @@ -53,6 +58,10 @@ fn bindgen(input: proc_macro::TokenStream) -> String {
#[proc_macro]
pub fn bindgen_sys(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let as_string = bindgen(input);

let as_string = add_safe_instantiation(&as_string, CompilationTarget::Sys);

// eprintln!("bindgen SYS IMPL: {as_string}");
proc_macro::TokenStream::from_str(&as_string).unwrap()
}

Expand Down Expand Up @@ -90,26 +99,39 @@ pub fn bindgen_js(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let regex = Regex::new("#\\[derive\\([^)]*Lower\\)\\]").unwrap();
let as_string = regex.replace_all(&as_string, "#[derive(wasm_bridge::component::LowerJs)]");

// Add deprecated warning to [World]::instantiate
let as_string = add_safe_instantiation(&as_string, CompilationTarget::Js);

// eprintln!("bindgen JS IMPL: {as_string}");
proc_macro::TokenStream::from_str(&as_string).unwrap()
}

fn add_safe_instantiation(
as_string: &str,
target: CompilationTarget,
) -> impl Deref<Target = str> + '_ {
let regex = Regex::new("pub\\s+fn\\s+instantiate\\s*<").unwrap();
let as_string = regex.replace_all(&as_string, r#"

regex.replace_all(&as_string, format!(r#"
pub async fn instantiate_safe<T>(
mut store: impl wasm_bridge::AsContextMut<Data = T>,
component: &wasm_bridge::component::Component,
linker: &wasm_bridge::component::Linker<T>,
) -> wasm_bridge::Result<(Self, wasm_bridge::component::Instance)> {
let instance = linker.instantiate_async(&mut store, component).await?;
) -> wasm_bridge::Result<(Self, wasm_bridge::component::Instance)> {{
let instance = linker.{}(&mut store, component){}?;
Ok((Self::new(store, &instance)?, instance))
}
}}
#[deprecated(
since = "0.4.0",
note = "Instantiating a component synchronously can panic, please use `instantiate_safe` instead."
)]
pub fn instantiate<"#);

// eprintln!("bindgen IMPL: {as_string}");
proc_macro::TokenStream::from_str(&as_string).unwrap()
pub fn instantiate<"#, match target {
CompilationTarget::Sys => "instantiate",
CompilationTarget::Js => "instantiate_async",
}, match target {
CompilationTarget::Sys => "",
CompilationTarget::Js => ".await",
}))
}

#[proc_macro_derive(SizeDescription, attributes(component))]
Expand Down
14 changes: 8 additions & 6 deletions crates/wasm-bridge-macros/src/original/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use syn::punctuated::Punctuated;
use syn::{braced, parse_quote, Data, DeriveInput, Error, Result, Token};
use wasmtime_component_util::{DiscriminantSize, FlagsSize};

use crate::CompilationTarget;

mod kw {
syn::custom_keyword!(record);
syn::custom_keyword!(variant);
Expand Down Expand Up @@ -896,7 +898,7 @@ impl Parse for Flags {
}
}

pub fn expand_flags(flags: &Flags, is_sys: bool) -> Result<TokenStream> {
pub fn expand_flags(flags: &Flags, target: CompilationTarget) -> Result<TokenStream> {
let size = FlagsSize::from_count(flags.flags.len());

let ty;
Expand Down Expand Up @@ -1093,7 +1095,7 @@ pub fn expand_flags(flags: &Flags, is_sys: bool) -> Result<TokenStream> {

let fields = fields.iter().collect::<Vec<_>>();

let component_type_impl = if is_sys {
let component_type_impl = if target == CompilationTarget::Sys {
expand_record_for_component_type(
&name,
&generics,
Expand Down Expand Up @@ -1124,13 +1126,13 @@ pub fn expand_flags(flags: &Flags, is_sys: bool) -> Result<TokenStream> {
FlagsSize::Size4Plus(_) => (quote!(#internal::InterfaceType::U32), 4),
};

let extra_derives = if is_sys {
quote!()
} else {
let extra_derives = if target == CompilationTarget::Js {
quote!(, wasm_bridge::component::SizeDescription, wasm_bridge::component::LiftJs, wasm_bridge::component::LowerJs)
} else {
quote!()
};

let lift_and_lower = if is_sys {
let lift_and_lower = if target == CompilationTarget::Sys {
quote!(
unsafe impl wasmtime::component::Lower for #name {
fn lower<T>(
Expand Down
6 changes: 4 additions & 2 deletions crates/wasm-bridge-macros/src/original/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod component;

pub use component::{Style, VariantStyle};

use crate::CompilationTarget;

pub fn lift(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
component::expand(
&component::LiftExpander,
Expand Down Expand Up @@ -32,8 +34,8 @@ pub fn component_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream
.into()
}

pub fn flags(input: proc_macro::TokenStream, is_sys: bool) -> proc_macro::TokenStream {
component::expand_flags(&parse_macro_input!(input as component::Flags), is_sys)
pub fn flags(input: proc_macro::TokenStream, target: CompilationTarget) -> proc_macro::TokenStream {
component::expand_flags(&parse_macro_input!(input as component::Flags), target)
.unwrap_or_else(Error::into_compile_error)
.into()
}
Expand Down

0 comments on commit 87f4fb2

Please sign in to comment.