Skip to content

Commit

Permalink
Add inventory-0-3-1 optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhenrymantilla committed Aug 17, 2022
1 parent acbf214 commit 48b50c1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ js = [
"std",
]

inventory-0-3-1 = [
"dep:inventory-0-3-1",
"safer_ffi-proc_macros/inventory-0-3-1",
]

[dev-dependencies]
macro_rules_attribute = "0.1.0"
safer-ffi.path = "."
Expand All @@ -95,6 +100,12 @@ unwind_safe.version = "0.1.0"
with_builtin_macros.version = "0.0.3"
macro_rules_attribute = "0.1.0"


[dependencies.inventory-0-3-1]
package = "inventory"
version = "0.3.1"
optional = true

[dependencies.napi]
package = "napi-dispatcher"
path = "napi-dispatcher"
Expand Down
5 changes: 4 additions & 1 deletion napi-dispatcher/nodejs-derive/src/proc_macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ fn js_export (
)
};
let stmts = &fun.block.stmts;
let krate_annotation = cfg!(not(feature = "inventory-0-3-1")).then(|| {
quote!( #![crate = ::safer_ffi::js::registering] )
});
let ret = quote!(
const _: () = {
#napi_import
Expand All @@ -106,7 +109,7 @@ fn js_export (
}

::safer_ffi::js::registering::submit! {
#![crate = ::safer_ffi::js::registering]
#krate_annotation

::safer_ffi::js::registering::NapiRegistryEntry::NamedMethod {
name: ::core::stringify!(#js_name),
Expand Down
16 changes: 12 additions & 4 deletions src/_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,16 @@ pub use ::safer_ffi_proc_macros::derive_ReprC;
pub mod layout;

__cfg_headers__! {
#[doc(hidden)] pub
use ::inventory;
cfg_match! {
feature = "inventory-0-3-1" => {
#[doc(hidden)] pub
use ::inventory_0_3_1 as inventory;
},
_ => {
#[doc(hidden)] pub
use ::inventory;
},
}

#[cfg_attr(feature = "nightly",
doc(cfg(feature = "headers")),
Expand All @@ -246,7 +254,7 @@ __cfg_headers__! {
,
}

::inventory::collect!(FfiExport);
self::inventory::collect!(FfiExport);
}

cfg_alloc! {
Expand Down Expand Up @@ -478,7 +486,6 @@ mod __ {

#[cfg(feature = "headers")]
pub use {
::inventory,
crate::{
headers::{
Definer,
Expand All @@ -491,6 +498,7 @@ mod __ {
StructField,
},
},
inventory,
FfiExport,
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/js/registering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ enum NapiRegistryEntry {
},
}

::inventory::collect!(NapiRegistryEntry);
self::inventory::collect!(NapiRegistryEntry);

pub use ::inventory::{self, submit};
pub use crate::inventory::{self, submit};

#[cold]
pub
Expand All @@ -41,7 +41,7 @@ fn napi_register_module_v1 (
// let env = ::napi::Env::from_raw(raw_env);
let mut exports: ::napi::JsObject = ::napi::NapiValue::from_raw_unchecked(raw_env, raw_exports);
match (|| ::napi::Result::<_>::Ok({
for entry in ::inventory::iter::<NapiRegistryEntry> {
for entry in crate::inventory::iter::<NapiRegistryEntry> {
match entry {
| &NapiRegistryEntry::NamedMethod { name, method } => {
let _ = exports.create_named_method(name, method);
Expand Down
2 changes: 2 additions & 0 deletions src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ js = [
verbose-expansions = [
# "prettyplease",
]

inventory-0-3-1 = []
6 changes: 5 additions & 1 deletion src/proc_macro/ffi_export/const_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ fn handle (
let Ty @ _ = &input.ty;
let ref each_doc = utils::extract_docs(&input.attrs)?;

let inventory_krate = cfg!(not(feature = "inventory-0-3-1")).then(|| {
quote!( #![crate = #ඞ] )
});
Ok(quote!(
#input

#[cfg(not(target_arch = "wasm32"))]
#ඞ::inventory::submit! {
#![crate = #ඞ]
#inventory_krate

#ඞ::FfiExport {
name: #VAR_str,
gen_def: |
Expand Down
6 changes: 5 additions & 1 deletion src/proc_macro/ffi_export/fn_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,14 @@ fn handle (
let ref EachArgTy @ _ = arg_tys(&fun).vec();
let each_doc = utils::extract_docs(&fun.attrs)?;
let (generics, _, where_clause) = fun.sig.generics.split_for_impl();
let inventory_krate = cfg!(not(feature = "inventory-0-3-1")).then(|| {
quote!( #![crate = #ඞ] )
});
ret.extend(quote!(
#[cfg(not(target_arch = "wasm32"))]
#ඞ::inventory::submit! {
#![crate = #ඞ]
#inventory_krate

#ඞ::FfiExport {
name: #export_name_str,
gen_def: {
Expand Down
5 changes: 4 additions & 1 deletion src/proc_macro/ffi_export/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ fn handle (
}
}
let ref Ty_str @ _ = Ty.to_string();
let inventory_krate = cfg!(not(feature = "inventory-0-3-1")).then(|| {
quote!( #![crate = ::safer_ffi] )
});
Ok(quote!(
#input

#[cfg(not(target_arch = "wasm32"))]
::safer_ffi::__cfg_headers__! {
::safer_ffi::inventory::submit! {
#![crate = ::safer_ffi]
#inventory_krate

::safer_ffi::FfiExport {
name: #Ty_str,
Expand Down

0 comments on commit 48b50c1

Please sign in to comment.