mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-25 01:34:02 +00:00
Add functions with multiple arguments to import macro, add test cases
This commit is contained in:
parent
28f071e50d
commit
f110945fd6
3 changed files with 100 additions and 69 deletions
|
@ -2,7 +2,9 @@ use core::panic;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
use syn::{parse_macro_input, FnArg, ForeignItemFn, ItemFn, Type, Visibility};
|
use syn::{
|
||||||
|
parse_macro_input, Block, FnArg, ForeignItemFn, Ident, ItemFn, Pat, PatIdent, Type, Visibility,
|
||||||
|
};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
|
@ -32,7 +34,7 @@ pub fn export(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| match x {
|
.map(|x| match x {
|
||||||
FnArg::Receiver(_) => {
|
FnArg::Receiver(_) => {
|
||||||
panic!("all arguments must have specified types, no `self` allowed")
|
panic!("All arguments must have specified types, no `self` allowed")
|
||||||
}
|
}
|
||||||
FnArg::Typed(item) => *item.ty.clone(),
|
FnArg::Typed(item) => *item.ty.clone(),
|
||||||
})
|
})
|
||||||
|
@ -91,70 +93,69 @@ pub fn import(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
panic!("Exported functions can not take generic parameters");
|
panic!("Exported functions can not take generic parameters");
|
||||||
}
|
}
|
||||||
|
|
||||||
dbg!(&fn_declare.sig);
|
// let inner_fn_name = format_ident!("{}", fn_declare.sig.ident);
|
||||||
|
let extern_fn_name = format_ident!("__{}", fn_declare.sig.ident);
|
||||||
|
|
||||||
// let inner_fn = ItemFn {
|
let (args, tys): (Vec<Ident>, Vec<Type>) = fn_declare
|
||||||
// attrs: fn_declare.attrs,
|
.sig
|
||||||
// vis: fn_declare.vis,
|
.inputs
|
||||||
// sig: fn_declare.sig,
|
.clone()
|
||||||
// block: todo!(),
|
.into_iter()
|
||||||
// };
|
.map(|x| match x {
|
||||||
|
FnArg::Receiver(_) => {
|
||||||
let outer_fn_name = format_ident!("{}", fn_declare.sig.ident);
|
panic!("All arguments must have specified types, no `self` allowed")
|
||||||
let inner_fn_name = format_ident!("__{}", outer_fn_name);
|
}
|
||||||
|
FnArg::Typed(t) => {
|
||||||
// let variadic = inner_fn.sig.inputs.len();
|
if let Pat::Ident(i) = *t.pat {
|
||||||
// let i = (0..variadic).map(syn::Index::from);
|
(i.ident, *t.ty)
|
||||||
// let t: Vec<Type> = inner_fn
|
} else {
|
||||||
// .sig
|
panic!("All function arguments must be identifiers");
|
||||||
// .inputs
|
}
|
||||||
// .iter()
|
}
|
||||||
// .map(|x| match x {
|
})
|
||||||
// FnArg::Receiver(_) => {
|
.unzip();
|
||||||
// panic!("all arguments must have specified types, no `self` allowed")
|
|
||||||
// }
|
dbg!("hello");
|
||||||
// FnArg::Typed(item) => *item.ty.clone(),
|
|
||||||
// })
|
let body = TokenStream::from(quote! {
|
||||||
// .collect();
|
{
|
||||||
|
// dbg!("executing imported function");
|
||||||
// // this is cursed...
|
// setup
|
||||||
// let (args, ty) = if variadic != 1 {
|
let data: (#( #tys ),*) = (#( #args ),*);
|
||||||
// (
|
let data = ::plugin::bincode::serialize(&data).unwrap();
|
||||||
// quote! {
|
let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
|
||||||
// #( data.#i ),*
|
|
||||||
// },
|
// operation
|
||||||
// quote! {
|
let new_buffer = unsafe { #extern_fn_name(buffer.into_u64()) };
|
||||||
// ( #( #t ),* )
|
let new_data = unsafe { ::plugin::__Buffer::from_u64(new_buffer).to_vec() };
|
||||||
// },
|
|
||||||
// )
|
// teardown
|
||||||
// } else {
|
match ::plugin::bincode::deserialize(&new_data) {
|
||||||
// let ty = &t[0];
|
Ok(d) => d,
|
||||||
// (quote! { data }, quote! { #ty })
|
Err(e) => panic!("Data returned from function not deserializable."),
|
||||||
// };
|
}
|
||||||
|
}
|
||||||
// TokenStream::from(quote! {
|
});
|
||||||
// extern "C" {
|
|
||||||
// fn #inner_fn_name(buffer: u64) -> u64;
|
dbg!("hello2");
|
||||||
// }
|
|
||||||
|
let block = parse_macro_input!(body as Block);
|
||||||
// #[no_mangle]
|
|
||||||
// fn #outer_fn_name #args /* (string: &str) */ -> #return_type /* Option<Vec<u8>> */ {
|
dbg!("hello {:?}", &block);
|
||||||
// dbg!("executing command: {}", string);
|
|
||||||
// // setup
|
let inner_fn = ItemFn {
|
||||||
// let data = #args_collect;
|
attrs: fn_declare.attrs,
|
||||||
// let data = ::plugin::bincode::serialize(&data).unwrap();
|
vis: fn_declare.vis,
|
||||||
// let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
|
sig: fn_declare.sig,
|
||||||
|
block: Box::new(block),
|
||||||
// // operation
|
};
|
||||||
// let new_buffer = unsafe { #inner_fn_name(buffer.into_u64()) };
|
|
||||||
// let new_data = unsafe { ::plugin::__Buffer::from_u64(new_buffer).to_vec() };
|
TokenStream::from(quote! {
|
||||||
|
extern "C" {
|
||||||
// // teardown
|
fn #extern_fn_name(buffer: u64) -> u64;
|
||||||
// match ::plugin::bincode::deserialize(&new_data) {
|
}
|
||||||
// Ok(d) => d,
|
|
||||||
// Err(e) => panic!("Data returned from function not deserializable."),
|
#[no_mangle]
|
||||||
// }
|
#inner_fn
|
||||||
// }
|
})
|
||||||
// })
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,8 @@ mod tests {
|
||||||
swap: WasiFn<(u32, u32), (u32, u32)>,
|
swap: WasiFn<(u32, u32), (u32, u32)>,
|
||||||
sort: WasiFn<Vec<u32>, Vec<u32>>,
|
sort: WasiFn<Vec<u32>, Vec<u32>>,
|
||||||
print: WasiFn<String, ()>,
|
print: WasiFn<String, ()>,
|
||||||
// and_back: WasiFn<u32, u32>,
|
and_back: WasiFn<u32, u32>,
|
||||||
|
imports: WasiFn<u32, u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async {
|
async {
|
||||||
|
@ -24,6 +25,12 @@ mod tests {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.host_function("mystery_number", |input: u32| input + 7)
|
.host_function("mystery_number", |input: u32| input + 7)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
.host_function("import_noop", |_: ()| ())
|
||||||
|
.unwrap()
|
||||||
|
.host_function("import_identity", |input: u32| input)
|
||||||
|
.unwrap()
|
||||||
|
.host_function("import_swap", |(a, b): (u32, u32)| (b, a))
|
||||||
|
.unwrap()
|
||||||
.init(include_bytes!("../../../plugins/bin/test_plugin.wasm"))
|
.init(include_bytes!("../../../plugins/bin/test_plugin.wasm"))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -36,7 +43,8 @@ mod tests {
|
||||||
swap: runtime.function("swap").unwrap(),
|
swap: runtime.function("swap").unwrap(),
|
||||||
sort: runtime.function("sort").unwrap(),
|
sort: runtime.function("sort").unwrap(),
|
||||||
print: runtime.function("print").unwrap(),
|
print: runtime.function("print").unwrap(),
|
||||||
// and_back: runtime.function("and_back").unwrap(),
|
and_back: runtime.function("and_back").unwrap(),
|
||||||
|
imports: runtime.function("imports").unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let unsorted = vec![1, 3, 4, 2, 5];
|
let unsorted = vec![1, 3, 4, 2, 5];
|
||||||
|
@ -49,7 +57,10 @@ mod tests {
|
||||||
assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
|
assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
|
||||||
assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
|
assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
|
||||||
assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
|
assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
|
||||||
// assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
|
assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
|
||||||
|
assert_eq!(runtime.call(&plugin.imports, 1).await.unwrap(), 8);
|
||||||
|
|
||||||
|
// dbg!("{}", runtime.call(&plugin.and_back, 1).await.unwrap());
|
||||||
}
|
}
|
||||||
.block_on()
|
.block_on()
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,10 +35,29 @@ pub fn print(string: String) {
|
||||||
eprintln!("to stderr: {}", string);
|
eprintln!("to stderr: {}", string);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[import]
|
#[import]
|
||||||
// fn mystery_number(input: u32) -> u32;
|
fn mystery_number(input: u32) -> u32;
|
||||||
|
|
||||||
// #[export]
|
#[export]
|
||||||
// pub fn and_back(secret: u32) -> u32 {
|
pub fn and_back(secret: u32) -> u32 {
|
||||||
// mystery_number(secret)
|
mystery_number(secret)
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
#[import]
|
||||||
|
fn import_noop() -> ();
|
||||||
|
|
||||||
|
#[import]
|
||||||
|
fn import_identity(i: u32) -> u32;
|
||||||
|
|
||||||
|
#[import]
|
||||||
|
fn import_swap(a: u32, b: u32) -> (u32, u32);
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn imports(x: u32) -> u32 {
|
||||||
|
let a = import_identity(7);
|
||||||
|
import_noop();
|
||||||
|
let (b, c) = import_swap(a, x);
|
||||||
|
assert_eq!(a, c);
|
||||||
|
assert_eq!(x, b);
|
||||||
|
a + b // should be 7 + x
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue