Skip to content

Commit

Permalink
Try out an export macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Mar 22, 2024
1 parent 50c74c0 commit 5731841
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
45 changes: 33 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions crates/neon-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,38 @@ pub fn main(
)
.into()
}

#[proc_macro_attribute]
/// Export a function
pub fn export(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(item as syn_mid::ItemFn);

let attrs = &input.attrs;
let vis = &input.vis;
let sig = &input.sig;
let block = &input.block;
let name = &sig.ident;
let exported_name = quote::format_ident!("__EXPORTED__{name}");

quote::quote!(
#(#attrs)*
#vis #sig {
#[neon::macro_internal::linkme::distributed_slice(neon::macro_internal::EXPORTS)]
#[linkme(crate = neon::macro_internal::linkme)]
fn #exported_name<'cx>(
cx: &mut neon::context::ModuleContext<'cx>,
) -> neon::result::NeonResult<(&'cx str, neon::handle::Handle<'cx, neon::types::JsValue>)> {
neon::types::JsFunction::new(cx, #name).map(|v| (
stringify!(#name),
neon::handle::Handle::upcast(&v),
))
}

#block
}
)
.into()
}
1 change: 1 addition & 0 deletions crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ getrandom = { version = "0.2.11", optional = true }
libloading = "0.8.1"
semver = "1.0.20"
smallvec = "1.11.2"
linkme = "0.3.25"
once_cell = "1.18.0"
neon-macros = { version = "=1.0.0", path = "../neon-macros" }
aquamarine = { version = "0.3.2", optional = true }
Expand Down
8 changes: 7 additions & 1 deletion crates/neon/src/context/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ pub unsafe fn initialize_module(
let env = Env(env);
let exports = Handle::new_internal(JsObject::from_local(env, exports.cast()));

ModuleContext::with(env, exports, |cx| {
ModuleContext::with(env, exports, |mut cx| {
for create in crate::macro_internal::EXPORTS {
if let Ok((k, v)) = create(&mut cx) {
let _ = cx.export_value(k, v);
}
}

let _ = init(cx);
});
}
6 changes: 6 additions & 0 deletions crates/neon/src/macro_internal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Internals needed by macros. These have to be exported for the macros to work

pub use linkme;
pub use crate::context::internal::initialize_module;

use crate::{context::ModuleContext, handle::Handle, result::NeonResult, types::JsValue};

#[linkme::distributed_slice]
pub static EXPORTS: [for<'cx> fn(&mut ModuleContext<'cx>) -> NeonResult<(&'cx str, Handle<'cx, JsValue>)>];
5 changes: 5 additions & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,8 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {

Ok(())
}

#[neon::export]
fn hello_world(mut cx: FunctionContext) -> JsResult<JsString> {
Ok(cx.string("Hello, World!"))
}

0 comments on commit 5731841

Please sign in to comment.