2020-04-21 01:20:35 +09:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use proc_macro2::{Ident, Literal};
|
2020-04-26 19:46:56 +09:00
|
|
|
use quote::{format_ident, quote};
|
|
|
|
use syn::fold::{fold_fn_arg, fold_signature, Fold};
|
2020-04-21 01:20:35 +09:00
|
|
|
use syn::parse::{Parse, ParseStream, Result};
|
|
|
|
use syn::punctuated::Punctuated;
|
2020-06-21 20:10:06 +09:00
|
|
|
use syn::{parse_macro_input, Block, FnArg, ItemFn, Signature, Token, Visibility};
|
2020-04-21 01:20:35 +09:00
|
|
|
|
|
|
|
struct ArgLength {
|
|
|
|
length: Option<Literal>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for ArgLength {
|
|
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
2020-04-26 19:46:56 +09:00
|
|
|
let vars = Punctuated::<Literal, Token![,]>::parse_terminated(input)?;
|
|
|
|
Ok(ArgLength {
|
|
|
|
length: vars.first().map(|i| i.clone()),
|
|
|
|
})
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsFunction {
|
|
|
|
args: Vec<FnArg>,
|
|
|
|
name: Option<Ident>,
|
|
|
|
signature: Option<Signature>,
|
|
|
|
block: Vec<Block>,
|
2020-06-21 20:10:06 +09:00
|
|
|
visibility: Visibility,
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JsFunction {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
JsFunction {
|
|
|
|
args: vec![],
|
|
|
|
name: None,
|
|
|
|
signature: None,
|
2020-06-21 20:10:06 +09:00
|
|
|
visibility: Visibility::Inherited,
|
2020-04-21 01:20:35 +09:00
|
|
|
block: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fold for JsFunction {
|
|
|
|
fn fold_fn_arg(&mut self, arg: FnArg) -> FnArg {
|
|
|
|
self.args.push(arg.clone());
|
|
|
|
fold_fn_arg(self, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_signature(&mut self, signature: Signature) -> Signature {
|
|
|
|
self.name = Some(format_ident!("{}", signature.ident));
|
|
|
|
let mut new_signature = signature.clone();
|
|
|
|
new_signature.ident = format_ident!("_{}", signature.ident);
|
|
|
|
self.signature = Some(new_signature);
|
|
|
|
fold_signature(self, signature)
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
fn fold_visibility(&mut self, v: Visibility) -> Visibility {
|
|
|
|
self.visibility = v.clone();
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2020-04-21 01:20:35 +09:00
|
|
|
fn fold_block(&mut self, node: Block) -> Block {
|
|
|
|
self.block.push(node.clone());
|
|
|
|
node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn js_function(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let arg_len = parse_macro_input!(attr as ArgLength);
|
|
|
|
let arg_len_span = arg_len.length.unwrap_or(Literal::usize_unsuffixed(0));
|
|
|
|
let input = parse_macro_input!(input as ItemFn);
|
|
|
|
let mut js_fn = JsFunction::new();
|
|
|
|
js_fn.fold_item_fn(input);
|
|
|
|
let fn_name = js_fn.name.unwrap();
|
|
|
|
let fn_block = js_fn.block;
|
|
|
|
let signature = js_fn.signature.unwrap();
|
2020-06-21 20:10:06 +09:00
|
|
|
let visibility = js_fn.visibility;
|
2020-04-21 01:20:35 +09:00
|
|
|
let new_fn_name = signature.ident.clone();
|
|
|
|
let expanded = quote! {
|
|
|
|
#signature #(#fn_block)*
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
#visibility extern "C" fn #fn_name(
|
2020-04-21 01:20:35 +09:00
|
|
|
raw_env: napi_rs::sys::napi_env,
|
|
|
|
cb_info: napi_rs::sys::napi_callback_info,
|
|
|
|
) -> napi_rs::sys::napi_value {
|
|
|
|
use std::io::Write;
|
|
|
|
use std::mem;
|
|
|
|
use std::os::raw::c_char;
|
|
|
|
use std::ptr;
|
2020-07-08 01:59:09 +09:00
|
|
|
use std::ffi::CString;
|
2020-06-21 20:10:06 +09:00
|
|
|
use napi_rs::{JsUnknown, Env, Status, NapiValue, CallContext};
|
2020-04-21 01:20:35 +09:00
|
|
|
let mut argc = #arg_len_span as usize;
|
|
|
|
let mut raw_args =
|
2020-06-21 18:50:38 +09:00
|
|
|
unsafe { mem::MaybeUninit::<[napi_rs::sys::napi_value; #arg_len_span as usize]>::uninit().assume_init() };
|
2020-04-21 01:20:35 +09:00
|
|
|
let mut raw_this = ptr::null_mut();
|
|
|
|
|
|
|
|
let mut has_error = false;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let status = napi_rs::sys::napi_get_cb_info(
|
|
|
|
raw_env,
|
|
|
|
cb_info,
|
|
|
|
&mut argc as *mut usize as *mut u64,
|
2020-06-21 18:50:38 +09:00
|
|
|
raw_args.as_mut_ptr(),
|
2020-04-21 01:20:35 +09:00
|
|
|
&mut raw_this,
|
|
|
|
ptr::null_mut(),
|
|
|
|
);
|
|
|
|
has_error = has_error && (Status::from(status) == Status::Ok);
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:14:44 +09:00
|
|
|
let mut env = Env::from_raw(raw_env);
|
2020-06-21 18:50:38 +09:00
|
|
|
let call_ctx = CallContext::new(&mut env, raw_this, &raw_args, #arg_len_span);
|
2020-04-21 01:20:35 +09:00
|
|
|
let result = call_ctx.and_then(|ctx| #new_fn_name(ctx));
|
|
|
|
has_error = has_error && result.is_err();
|
|
|
|
|
|
|
|
match result {
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(result) => result.raw_value(),
|
2020-04-21 01:20:35 +09:00
|
|
|
Err(e) => {
|
2020-06-21 20:10:06 +09:00
|
|
|
let message = format!("{}", e);
|
2020-04-21 01:20:35 +09:00
|
|
|
unsafe {
|
2020-07-08 01:59:09 +09:00
|
|
|
napi_rs::sys::napi_throw_error(raw_env, ptr::null(), CString::from_vec_unchecked(message.into()).as_ptr() as *const c_char);
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
let mut undefined = ptr::null_mut();
|
|
|
|
unsafe { napi_rs::sys::napi_get_undefined(raw_env, &mut undefined) };
|
|
|
|
undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Hand the output tokens back to the compiler
|
|
|
|
TokenStream::from(expanded)
|
|
|
|
}
|