Dereference pointers and call the hostcall fn

This commit is contained in:
Jakub Konka
2019-05-14 11:55:50 +02:00
committed by Dan Gohman
parent 4a5910b4a3
commit 7f3c325cdf
2 changed files with 13 additions and 8 deletions

View File

@@ -10,4 +10,3 @@ proc-macro = true
[dependencies]
syn = { version = "0.15.34", features = ["full"] }
quote = "0.6.12"
proc-macro2 = "0.4.30"

View File

@@ -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,
)*)
}
};