2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2022-10-06 23:51:43 -04:00
|
|
|
static NAPI_EXPORTS: &str = include_str!("./symbol_exports.json");
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SymbolExports {
|
|
|
|
pub symbols: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn napi_sym(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let func = syn::parse::<syn::ItemFn>(item).expect("expected a function");
|
|
|
|
|
|
|
|
let exports: SymbolExports =
|
|
|
|
serde_json::from_str(NAPI_EXPORTS).expect("failed to parse exports");
|
|
|
|
let name = &func.sig.ident;
|
|
|
|
assert!(
|
|
|
|
exports.symbols.contains(&name.to_string()),
|
2022-11-28 17:28:54 -05:00
|
|
|
"tools/napi/sym/symbol_exports.json is out of sync!"
|
2022-10-05 10:06:44 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
let block = &func.block;
|
|
|
|
let inputs = &func.sig.inputs;
|
2022-10-06 23:51:08 -04:00
|
|
|
let generics = &func.sig.generics;
|
2022-10-05 10:06:44 -04:00
|
|
|
TokenStream::from(quote! {
|
|
|
|
// SAFETY: it's an NAPI function.
|
|
|
|
#[no_mangle]
|
2022-10-06 23:51:08 -04:00
|
|
|
pub unsafe extern "C" fn #name #generics (#inputs) -> napi_status {
|
2023-05-18 10:17:53 -04:00
|
|
|
#block
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|