mirror of
https://github.com/denoland/deno.git
synced 2024-11-05 09:04:41 -05:00
c27234888f
The current location was causing failures during v1.26.1 publication. <!-- Before submitting a PR, please read http://deno.land/manual/contributing 1. Give the PR a descriptive title. Examples of good title: - fix(std/http): Fix race condition in server - docs(console): Update docstrings - feat(doc): Handle nested reexports Examples of bad title: - fix #7123 - update docs - fix bugs 2. Ensure there is a related issue and it is referenced in the PR text. 3. Ensure there are tests that cover the changes. 4. Ensure `cargo test` passes. 5. Ensure `./tools/format.js` passes without changing files. 6. Ensure `./tools/lint.js` passes. -->
46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use proc_macro::TokenStream;
|
|
use quote::quote;
|
|
use serde::Deserialize;
|
|
|
|
static NAPI_EXPORTS: &str = include_str!("./symbol_exports.json");
|
|
|
|
#[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()),
|
|
"tools/napi/symbol_exports.json is out of sync!"
|
|
);
|
|
|
|
let block = &func.block;
|
|
let inputs = &func.sig.inputs;
|
|
let output = &func.sig.output;
|
|
let generics = &func.sig.generics;
|
|
let ret_ty = match output {
|
|
syn::ReturnType::Default => panic!("expected a return type"),
|
|
syn::ReturnType::Type(_, ty) => quote! { #ty },
|
|
};
|
|
TokenStream::from(quote! {
|
|
// SAFETY: it's an NAPI function.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn #name #generics (#inputs) -> napi_status {
|
|
let mut inner = || -> #ret_ty {
|
|
#block
|
|
};
|
|
inner()
|
|
.map(|_| napi_ok)
|
|
.unwrap_or_else(|e| e.into())
|
|
}
|
|
})
|
|
}
|