summaryrefslogtreecommitdiff
path: root/cli/napi/sym/lib.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-11-28 17:28:54 -0500
committerGitHub <noreply@github.com>2022-11-28 17:28:54 -0500
commit2d4c46c975eb916dc622cc729a1a8d397582a76f (patch)
tree445e819117acd2f94ffc9d7da7ed8e3e604435d0 /cli/napi/sym/lib.rs
parentf526513d74d34ac254aa40ef9b73238cb21c395b (diff)
refactor: create util folder, move nap_sym to napi/sym, move http_cache to cache folder (#16857)
Diffstat (limited to 'cli/napi/sym/lib.rs')
-rw-r--r--cli/napi/sym/lib.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/cli/napi/sym/lib.rs b/cli/napi/sym/lib.rs
new file mode 100644
index 000000000..984d7f4bc
--- /dev/null
+++ b/cli/napi/sym/lib.rs
@@ -0,0 +1,46 @@
+// 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/sym/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())
+ }
+ })
+}