diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-10-05 07:06:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-05 19:36:44 +0530 |
commit | 0b016a7fb8639ce49603c8c339539174b191a4b1 (patch) | |
tree | c511060d701db60ede0214b7280e89c5749bbe62 /cli/napi_sym/lib.rs | |
parent | 3a3a8484069c9c6955fcb83ea761f9f74638175a (diff) |
feat(npm): implement Node API (#13633)
This PR implements the NAPI for loading native modules into Deno.
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Co-authored-by: DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com>
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/napi_sym/lib.rs')
-rw-r--r-- | cli/napi_sym/lib.rs | 46 |
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..caef3da65 --- /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!("../../tools/napi/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 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(#inputs) -> napi_status { + let mut inner = || -> #ret_ty { + #block + }; + inner() + .map(|_| napi_ok) + .unwrap_or_else(|e| e.into()) + } + }) +} |