diff --git a/wasi-common-cbindgen/Cargo.toml b/wasi-common-cbindgen/Cargo.toml index ddb546170b..75bb538d46 100644 --- a/wasi-common-cbindgen/Cargo.toml +++ b/wasi-common-cbindgen/Cargo.toml @@ -10,4 +10,3 @@ proc-macro = true [dependencies] syn = { version = "0.15.34", features = ["full"] } quote = "0.6.12" -proc-macro2 = "0.4.30" diff --git a/wasi-common-cbindgen/src/lib.rs b/wasi-common-cbindgen/src/lib.rs index 310c2cf441..65977ad2b9 100644 --- a/wasi-common-cbindgen/src/lib.rs +++ b/wasi-common-cbindgen/src/lib.rs @@ -2,8 +2,7 @@ extern crate proc_macro; use proc_macro::TokenStream; use quote::quote; -use std::collections::HashMap; -use syn::{ArgCaptured, FnArg, Pat, PatIdent, Token, Type, TypeReference}; +use syn::{ArgCaptured, FnArg, Pat, PatIdent, Type, TypeReference}; #[proc_macro_attribute] pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenStream { @@ -12,13 +11,14 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt let function = syn::parse_macro_input!(function as syn::ItemFn); // generate C fn name prefixed with __wasi_ - let ident = &function.ident; - let concatenated = format!("__wasi_{}", ident); - let c_fn_ident = syn::Ident::new(&concatenated, ident.span()); + let fn_ident = &function.ident; + let concatenated = format!("__wasi_{}", fn_ident); + let c_fn_ident = syn::Ident::new(&concatenated, fn_ident.span()); // capture input args let mut arg_ident = Vec::new(); let mut arg_type = Vec::new(); + let mut call_arg_ident = Vec::new(); for input in &function.decl.inputs { match input { FnArg::Captured(ArgCaptured { @@ -35,8 +35,13 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt // so substitute it for *mut since we're exporting to C let elem = &ty.elem; arg_type.push(quote!(*mut #elem)); + // we need to properly dereference the substituted raw + // pointer if we are to properly call the hostcall fn + call_arg_ident.push(quote!(&mut *#ident)); } else { arg_type.push(quote!(#ty)); + // non-&-ref type, so preserve whatever the arg was + call_arg_ident.push(quote!(#ident)); } } _ => {} @@ -54,8 +59,9 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt #arg_ident: #arg_type, )* ) #output { - - + #fn_ident(#( + #call_arg_ident, + )*) } };