-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathlib.rs
More file actions
101 lines (89 loc) · 3.72 KB
/
lib.rs
File metadata and controls
101 lines (89 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use quote::{format_ident, quote};
use syn::{FnArg, GenericArgument, PathSegment};
/// Annotates a function so that it implements
/// the NativeHostFunction trait.
#[proc_macro_attribute]
pub fn host_function(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input: syn::ItemFn = syn::parse_macro_input!(input);
let ident = input.sig.ident.clone();
let gateway_ident = format_ident!("{}_gateway", input.sig.ident);
let struct_ident = format_ident!("{}_struct", input.sig.ident);
let args = input
.sig
.inputs
.iter()
.map(|arg| match arg {
FnArg::Receiver(_) => panic!("self functions are not supported"),
FnArg::Typed(arg) => arg.clone(),
})
.collect::<Vec<_>>();
let args_idents: Vec<_> = args.iter().map(|arg| arg.pat.clone()).collect();
let args_idents_without_cx: Vec<_> = args_idents.iter().skip(1).cloned().collect();
let args_without_cx: Vec<_> = args.iter().skip(1).cloned().collect();
// Extract the inner return type from anyhow::Result<T>.
let ret = match input.sig.output.clone() {
syn::ReturnType::Default => return_type_panic(),
syn::ReturnType::Type(_, ty) => match *ty {
syn::Type::Path(path) => {
let segments: Vec<PathSegment> = path.path.segments.into_iter().collect();
if segments[0].ident != "anyhow" {
return_type_panic();
}
if segments[1].ident != "Result" {
return_type_panic();
}
let arg = segments[1].arguments.clone();
match arg {
syn::PathArguments::AngleBracketed(inner) => {
match inner.args.first().unwrap() {
GenericArgument::Type(typ) => typ.clone(),
_ => return_type_panic(),
}
}
_ => return_type_panic(),
}
}
_ => return_type_panic(),
},
};
let result = quote! {
#input
extern "C" fn #gateway_ident(#(#args),*) -> #ret {
#ident(#(#args_idents),*).expect("host function panicked")
}
#[allow(non_camel_case_types)]
pub struct #struct_ident;
impl crate::host_function::NativeHostFunction for #struct_ident {
fn to_function_pointer(self) -> usize {
// Safety: see the Nomicon: https://rust-lang.github.io/unsafe-code-guidelines/layout/function-pointers.html#representation
// For all targets that Feather compiles to, function pointers
// have the same layout as a usize.
unsafe {
std::mem::transmute(#gateway_ident as *const ())
}
}
}
impl crate::host_function::WasmHostFunction for #struct_ident {
fn to_wasm_function(self, store: &wasmer::Store, env: crate::env::PluginEnv) -> wasmer::Function {
wasmer::Function::new_native_with_env(store, env, |env: &crate::env::PluginEnv, #(#args_without_cx),*| {
let result: anyhow::Result<_> = #ident(&env.context, #(#args_idents_without_cx),*);
match result {
Ok(ret) => ret,
Err(e) => {
unsafe {
wasmer::raise_user_trap(e.into())
}
}
}
})
}
}
};
result.into()
}
fn return_type_panic() -> ! {
panic!("host functions must return an anyhow::Result<T>")
}