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 {
|
2020-09-30 15:34:26 +09:00
|
|
|
length: Literal,
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-09-30 15:34:26 +09:00
|
|
|
length: vars
|
|
|
|
.first()
|
2020-12-01 15:55:19 +09:00
|
|
|
.cloned()
|
|
|
|
.unwrap_or_else(|| Literal::usize_unsuffixed(0)),
|
2020-04-26 19:46:56 +09:00
|
|
|
})
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsFunction {
|
|
|
|
args: Vec<FnArg>,
|
2020-11-25 18:42:14 +09:00
|
|
|
name: Option<Ident>,
|
|
|
|
signature: Option<Signature>,
|
|
|
|
signature_raw: Option<Signature>,
|
2020-04-21 01:20:35 +09:00
|
|
|
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![],
|
2020-11-25 18:42:14 +09:00
|
|
|
name: None,
|
|
|
|
signature: None,
|
|
|
|
signature_raw: 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 {
|
2020-11-25 18:42:14 +09:00
|
|
|
self.name = Some(format_ident!("{}", signature.ident));
|
2020-04-21 01:20:35 +09:00
|
|
|
let mut new_signature = signature.clone();
|
2020-11-25 18:42:14 +09:00
|
|
|
new_signature.ident = format_ident!("_generated_{}_generated_", signature.ident);
|
|
|
|
self.signature = Some(new_signature);
|
|
|
|
self.signature_raw = Some(signature.clone());
|
2020-04-21 01:20:35 +09:00
|
|
|
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);
|
2020-09-30 15:34:26 +09:00
|
|
|
let arg_len_span = arg_len.length;
|
2020-04-21 01:20:35 +09:00
|
|
|
let input = parse_macro_input!(input as ItemFn);
|
|
|
|
let mut js_fn = JsFunction::new();
|
|
|
|
js_fn.fold_item_fn(input);
|
2020-11-25 18:42:14 +09:00
|
|
|
let fn_name = js_fn.name.unwrap();
|
2020-04-21 01:20:35 +09:00
|
|
|
let fn_block = js_fn.block;
|
2020-11-25 18:42:14 +09:00
|
|
|
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();
|
2020-09-30 15:34:26 +09:00
|
|
|
let execute_js_function = get_execute_js_code(new_fn_name, FunctionKind::JsFunction);
|
2020-04-21 01:20:35 +09:00
|
|
|
let expanded = quote! {
|
2020-12-02 15:10:48 +09:00
|
|
|
#[inline(always)]
|
2020-04-21 01:20:35 +09:00
|
|
|
#signature #(#fn_block)*
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
#visibility extern "C" fn #fn_name(
|
2020-07-15 01:43:20 +09:00
|
|
|
raw_env: napi::sys::napi_env,
|
|
|
|
cb_info: napi::sys::napi_callback_info,
|
|
|
|
) -> napi::sys::napi_value {
|
2020-04-21 01:20:35 +09:00
|
|
|
use std::ptr;
|
2020-07-19 17:54:10 +09:00
|
|
|
use std::panic::{self, AssertUnwindSafe};
|
2020-07-08 01:59:09 +09:00
|
|
|
use std::ffi::CString;
|
2020-11-25 18:42:14 +09:00
|
|
|
use napi::{Env, Error, Status, NapiValue, CallContext};
|
2020-04-21 01:20:35 +09:00
|
|
|
let mut argc = #arg_len_span as usize;
|
2020-09-26 18:49:30 +09:00
|
|
|
let mut raw_args: [napi::sys::napi_value; #arg_len_span] = [ptr::null_mut(); #arg_len_span];
|
2020-04-21 01:20:35 +09:00
|
|
|
let mut raw_this = ptr::null_mut();
|
|
|
|
|
|
|
|
unsafe {
|
2020-07-15 01:43:20 +09:00
|
|
|
let status = napi::sys::napi_get_cb_info(
|
2020-04-21 01:20:35 +09:00
|
|
|
raw_env,
|
|
|
|
cb_info,
|
2020-11-25 18:42:14 +09:00
|
|
|
&mut argc,
|
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(),
|
|
|
|
);
|
2020-09-07 18:26:28 +09:00
|
|
|
debug_assert!(Status::from(status) == Status::Ok, "napi_get_cb_info failed");
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
2020-11-20 01:07:13 +09:00
|
|
|
let mut env = unsafe { Env::from_raw(raw_env) };
|
2020-11-25 18:42:14 +09:00
|
|
|
let ctx = CallContext::new(&mut env, cb_info, raw_this, &raw_args, #arg_len_span, argc);
|
2020-09-30 15:34:26 +09:00
|
|
|
#execute_js_function
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Hand the output tokens back to the compiler
|
|
|
|
TokenStream::from(expanded)
|
|
|
|
}
|
2020-09-30 15:34:26 +09:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn contextless_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(input as ItemFn);
|
|
|
|
let mut js_fn = JsFunction::new();
|
|
|
|
js_fn.fold_item_fn(input);
|
2020-11-25 18:42:14 +09:00
|
|
|
let fn_name = js_fn.name.unwrap();
|
2020-09-30 15:34:26 +09:00
|
|
|
let fn_block = js_fn.block;
|
2020-11-25 18:42:14 +09:00
|
|
|
let signature = js_fn.signature.unwrap();
|
2020-09-30 15:34:26 +09:00
|
|
|
let visibility = js_fn.visibility;
|
|
|
|
let new_fn_name = signature.ident.clone();
|
|
|
|
let execute_js_function = get_execute_js_code(new_fn_name, FunctionKind::Contextless);
|
|
|
|
|
|
|
|
let expanded = quote! {
|
2020-12-02 15:10:48 +09:00
|
|
|
#[inline(always)]
|
2020-09-30 15:34:26 +09:00
|
|
|
#signature #(#fn_block)*
|
|
|
|
|
|
|
|
#visibility extern "C" fn #fn_name(
|
|
|
|
raw_env: napi::sys::napi_env,
|
|
|
|
cb_info: napi::sys::napi_callback_info,
|
|
|
|
) -> napi::sys::napi_value {
|
|
|
|
use std::ptr;
|
|
|
|
use std::panic::{self, AssertUnwindSafe};
|
|
|
|
use std::ffi::CString;
|
|
|
|
use napi::{Env, NapiValue, Error, Status};
|
|
|
|
|
2020-11-20 01:07:13 +09:00
|
|
|
let ctx = unsafe { Env::from_raw(raw_env) };
|
2020-09-30 15:34:26 +09:00
|
|
|
#execute_js_function
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Hand the output tokens back to the compiler
|
|
|
|
TokenStream::from(expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
enum FunctionKind {
|
|
|
|
Contextless,
|
|
|
|
JsFunction,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_execute_js_code(
|
|
|
|
new_fn_name: Ident,
|
|
|
|
function_kind: FunctionKind,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let return_token_stream = match function_kind {
|
|
|
|
FunctionKind::Contextless => {
|
|
|
|
quote! {
|
2020-11-20 01:07:13 +09:00
|
|
|
Ok(Some(v)) => unsafe { v.raw() },
|
2020-09-30 15:34:26 +09:00
|
|
|
Ok(None) => ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FunctionKind::JsFunction => {
|
|
|
|
quote! {
|
2020-11-20 01:07:13 +09:00
|
|
|
Ok(v) => unsafe { v.raw() },
|
2020-09-30 15:34:26 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
match panic::catch_unwind(AssertUnwindSafe(move || #new_fn_name(ctx))).map_err(|e| {
|
|
|
|
let message = {
|
|
|
|
if let Some(string) = e.downcast_ref::<String>() {
|
|
|
|
string.clone()
|
|
|
|
} else if let Some(string) = e.downcast_ref::<&str>() {
|
|
|
|
string.to_string()
|
|
|
|
} else {
|
|
|
|
format!("panic from Rust code: {:?}", e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Error::from_reason(message)
|
|
|
|
}).and_then(|v| v) {
|
|
|
|
#return_token_stream
|
|
|
|
Err(e) => {
|
2020-12-02 15:10:48 +09:00
|
|
|
unsafe { napi::JsError::from(e).throw_into(raw_env) };
|
2020-09-30 15:34:26 +09:00
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:42:14 +09:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn module_exports(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(input as ItemFn);
|
|
|
|
let mut js_fn = JsFunction::new();
|
|
|
|
js_fn.fold_item_fn(input);
|
|
|
|
let fn_block = js_fn.block;
|
|
|
|
let fn_name = js_fn.name.unwrap();
|
|
|
|
let signature = js_fn.signature_raw.unwrap();
|
|
|
|
let args_len = js_fn.args.len();
|
|
|
|
let call_expr = if args_len == 1 {
|
|
|
|
quote! { #fn_name(exports) }
|
|
|
|
} else if args_len == 2 {
|
|
|
|
quote! { #fn_name(exports, env) }
|
|
|
|
} else {
|
|
|
|
panic!("Arguments length of #[module_exports] function must be 1 or 2");
|
|
|
|
};
|
|
|
|
let expanded = quote! {
|
|
|
|
#[inline]
|
|
|
|
#signature #(#fn_block)*
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn napi_register_module_v1(
|
|
|
|
raw_env: napi::sys::napi_env,
|
|
|
|
raw_exports: napi::sys::napi_value,
|
|
|
|
) -> napi::sys::napi_value {
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::ptr;
|
|
|
|
use napi::{Env, JsObject, NapiValue};
|
|
|
|
|
|
|
|
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
|
|
|
|
use napi::shutdown_tokio_rt;
|
|
|
|
let env = Env::from_raw(raw_env);
|
|
|
|
let exports = JsObject::from_raw_unchecked(raw_env, raw_exports);
|
|
|
|
let result: napi::Result<()> = #call_expr;
|
|
|
|
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
|
|
|
|
let hook_result = napi::check_status!(unsafe {
|
|
|
|
napi::sys::napi_add_env_cleanup_hook(raw_env, Some(shutdown_tokio_rt), ptr::null_mut())
|
|
|
|
});
|
|
|
|
#[cfg(not(all(feature = "tokio_rt", feature = "napi4")))]
|
|
|
|
let hook_result = Ok(());
|
|
|
|
match hook_result.and_then(move |_| result) {
|
|
|
|
Ok(_) => raw_exports,
|
|
|
|
Err(e) => {
|
|
|
|
unsafe {
|
|
|
|
napi::sys::napi_throw_error(
|
|
|
|
raw_env,
|
|
|
|
ptr::null(),
|
|
|
|
CString::from_vec_unchecked(format!("Error initializing module: {}", e).into())
|
|
|
|
.as_ptr(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Hand the output tokens back to the compiler
|
|
|
|
TokenStream::from(expanded)
|
|
|
|
}
|