Skip to content

Commit c79700a

Browse files
committed
Remove use of cfg_if, which doesn't work with re-exported macro definitions.
See: rust-lang/rust#53270 (comment)
1 parent 6d7cd7d commit c79700a

File tree

2 files changed

+118
-124
lines changed

2 files changed

+118
-124
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ neon-build = { version = "=0.3.1", path = "crates/neon-build" }
1818
lazy_static = "1.4.0"
1919

2020
[dependencies]
21-
cfg-if = "0.1.9"
2221
cslice = "0.2"
2322
semver = "0.9.0"
2423
neon-runtime = { version = "=0.3.1", path = "crates/neon-runtime" }

src/lib.rs

Lines changed: 118 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! The [Neon](https://www.neon-bindings.com/) crate provides bindings for writing Node.js plugins with a safe and fast Rust API.
22
33
extern crate neon_runtime;
4-
extern crate cfg_if;
54
extern crate cslice;
65
extern crate semver;
76

@@ -25,131 +24,127 @@ pub mod macro_internal;
2524
#[cfg(all(feature = "legacy-runtime", feature = "napi-runtime"))]
2625
compile_error!("Cannot enable both `legacy-runtime` and `napi-runtime` features.\n\nTo use `napi-runtime`, disable `legacy-runtime` by setting `default-features` to `false` in Cargo.toml\nor with cargo's --no-default-features flag.");
2726

28-
use cfg_if::cfg_if;
29-
30-
cfg_if! {
31-
if #[cfg(feature = "napi-runtime")] {
32-
/// Register the current crate as a Node module, providing startup
33-
/// logic for initializing the module object at runtime.
34-
///
35-
/// The first argument is a pattern bound to a `neon::context::ModuleContext`. This
36-
/// is usually bound to a mutable variable `mut cx`, which can then be used to
37-
/// pass to Neon APIs that require mutable access to an execution context.
38-
///
39-
/// Example:
40-
///
41-
/// ```rust,ignore
42-
/// register_module!(mut cx, {
43-
/// cx.export_function("foo", foo)?;
44-
/// cx.export_function("bar", bar)?;
45-
/// cx.export_function("baz", baz)?;
46-
/// Ok(())
47-
/// });
48-
/// ```
49-
#[macro_export]
50-
macro_rules! register_module {
51-
($module:pat, $init:block) => {
52-
#[no_mangle]
53-
pub unsafe extern "C" fn napi_register_module_v1(
54-
_env: $crate::macro_internal::runtime::nodejs_sys::napi_env,
55-
exports: $crate::macro_internal::runtime::nodejs_sys::napi_value
56-
) -> $crate::macro_internal::runtime::nodejs_sys::napi_value
57-
{
58-
// Suppress the default Rust panic hook, which prints diagnostics to stderr.
59-
::std::panic::set_hook(::std::boxed::Box::new(|_| { }));
60-
61-
$init
62-
63-
exports
64-
}
65-
}
27+
#[cfg(feature = "napi-runtime")]
28+
/// Register the current crate as a Node module, providing startup
29+
/// logic for initializing the module object at runtime.
30+
///
31+
/// The first argument is a pattern bound to a `neon::context::ModuleContext`. This
32+
/// is usually bound to a mutable variable `mut cx`, which can then be used to
33+
/// pass to Neon APIs that require mutable access to an execution context.
34+
///
35+
/// Example:
36+
///
37+
/// ```rust,ignore
38+
/// register_module!(mut cx, {
39+
/// cx.export_function("foo", foo)?;
40+
/// cx.export_function("bar", bar)?;
41+
/// cx.export_function("baz", baz)?;
42+
/// Ok(())
43+
/// });
44+
/// ```
45+
#[macro_export]
46+
macro_rules! register_module {
47+
($module:pat, $init:block) => {
48+
#[no_mangle]
49+
pub unsafe extern "C" fn napi_register_module_v1(
50+
_env: $crate::macro_internal::runtime::nodejs_sys::napi_env,
51+
exports: $crate::macro_internal::runtime::nodejs_sys::napi_value
52+
) -> $crate::macro_internal::runtime::nodejs_sys::napi_value
53+
{
54+
// Suppress the default Rust panic hook, which prints diagnostics to stderr.
55+
::std::panic::set_hook(::std::boxed::Box::new(|_| { }));
56+
57+
$init
58+
59+
exports
6660
}
67-
} else {
68-
/// Register the current crate as a Node module, providing startup
69-
/// logic for initializing the module object at runtime.
70-
///
71-
/// The first argument is a pattern bound to a `neon::context::ModuleContext`. This
72-
/// is usually bound to a mutable variable `mut cx`, which can then be used to
73-
/// pass to Neon APIs that require mutable access to an execution context.
74-
///
75-
/// Example:
76-
///
77-
/// ```rust,ignore
78-
/// register_module!(mut cx, {
79-
/// cx.export_function("foo", foo)?;
80-
/// cx.export_function("bar", bar)?;
81-
/// cx.export_function("baz", baz)?;
82-
/// Ok(())
83-
/// });
84-
/// ```
85-
#[macro_export]
86-
macro_rules! register_module {
87-
($module:pat, $init:block) => {
88-
// Mark this function as a global constructor (like C++).
89-
#[allow(improper_ctypes)]
90-
#[cfg_attr(target_os = "linux", link_section = ".ctors")]
91-
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
92-
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
93-
#[used]
94-
pub static __LOAD_NEON_MODULE: extern "C" fn() = {
95-
fn __init_neon_module($module: $crate::context::ModuleContext) -> $crate::result::NeonResult<()> $init
96-
97-
extern "C" fn __load_neon_module() {
98-
// Put everything else in the ctor fn so the user fn can't see it.
99-
#[repr(C)]
100-
struct __NodeModule {
101-
version: i32,
102-
flags: u32,
103-
dso_handle: *mut u8,
104-
filename: *const u8,
105-
register_func: Option<extern "C" fn(
106-
$crate::handle::Handle<$crate::types::JsObject>, *mut u8, *mut u8)>,
107-
context_register_func: Option<extern "C" fn(
108-
$crate::handle::Handle<$crate::types::JsObject>, *mut u8, *mut u8, *mut u8)>,
109-
modname: *const u8,
110-
priv_data: *mut u8,
111-
link: *mut __NodeModule
112-
}
113-
114-
// Mark as used during tests to suppress warnings
115-
#[cfg_attr(test, used)]
116-
static mut __NODE_MODULE: __NodeModule = __NodeModule {
117-
version: 0,
118-
flags: 0,
119-
dso_handle: 0 as *mut _,
120-
filename: b"neon_source.rs\0" as *const u8,
121-
register_func: Some(__register_neon_module),
122-
context_register_func: None,
123-
modname: b"neon_module\0" as *const u8,
124-
priv_data: 0 as *mut _,
125-
link: 0 as *mut _
126-
};
127-
128-
extern "C" fn __register_neon_module(
129-
m: $crate::handle::Handle<$crate::types::JsObject>, _: *mut u8, _: *mut u8) {
130-
$crate::macro_internal::initialize_module(m, __init_neon_module);
131-
}
132-
133-
extern "C" {
134-
fn node_module_register(module: *mut __NodeModule);
135-
}
136-
137-
// Suppress the default Rust panic hook, which prints diagnostics to stderr.
138-
::std::panic::set_hook(::std::boxed::Box::new(|_| { }));
139-
140-
// During tests, node is not available. Skip module registration.
141-
#[cfg(not(test))]
142-
unsafe {
143-
// Set the ABI version based on the NODE_MODULE_VERSION constant provided by the current node headers.
144-
__NODE_MODULE.version = $crate::macro_internal::runtime::module::get_version();
145-
node_module_register(&mut __NODE_MODULE);
146-
}
147-
}
148-
149-
__load_neon_module
61+
}
62+
}
63+
64+
#[cfg(feature = "legacy-runtime")]
65+
/// Register the current crate as a Node module, providing startup
66+
/// logic for initializing the module object at runtime.
67+
///
68+
/// The first argument is a pattern bound to a `neon::context::ModuleContext`. This
69+
/// is usually bound to a mutable variable `mut cx`, which can then be used to
70+
/// pass to Neon APIs that require mutable access to an execution context.
71+
///
72+
/// Example:
73+
///
74+
/// ```rust,ignore
75+
/// register_module!(mut cx, {
76+
/// cx.export_function("foo", foo)?;
77+
/// cx.export_function("bar", bar)?;
78+
/// cx.export_function("baz", baz)?;
79+
/// Ok(())
80+
/// });
81+
/// ```
82+
#[macro_export]
83+
macro_rules! register_module {
84+
($module:pat, $init:block) => {
85+
// Mark this function as a global constructor (like C++).
86+
#[allow(improper_ctypes)]
87+
#[cfg_attr(target_os = "linux", link_section = ".ctors")]
88+
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
89+
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
90+
#[used]
91+
pub static __LOAD_NEON_MODULE: extern "C" fn() = {
92+
fn __init_neon_module($module: $crate::context::ModuleContext) -> $crate::result::NeonResult<()> $init
93+
94+
extern "C" fn __load_neon_module() {
95+
// Put everything else in the ctor fn so the user fn can't see it.
96+
#[repr(C)]
97+
struct __NodeModule {
98+
version: i32,
99+
flags: u32,
100+
dso_handle: *mut u8,
101+
filename: *const u8,
102+
register_func: Option<extern "C" fn(
103+
$crate::handle::Handle<$crate::types::JsObject>, *mut u8, *mut u8)>,
104+
context_register_func: Option<extern "C" fn(
105+
$crate::handle::Handle<$crate::types::JsObject>, *mut u8, *mut u8, *mut u8)>,
106+
modname: *const u8,
107+
priv_data: *mut u8,
108+
link: *mut __NodeModule
109+
}
110+
111+
// Mark as used during tests to suppress warnings
112+
#[cfg_attr(test, used)]
113+
static mut __NODE_MODULE: __NodeModule = __NodeModule {
114+
version: 0,
115+
flags: 0,
116+
dso_handle: 0 as *mut _,
117+
filename: b"neon_source.rs\0" as *const u8,
118+
register_func: Some(__register_neon_module),
119+
context_register_func: None,
120+
modname: b"neon_module\0" as *const u8,
121+
priv_data: 0 as *mut _,
122+
link: 0 as *mut _
150123
};
124+
125+
extern "C" fn __register_neon_module(
126+
m: $crate::handle::Handle<$crate::types::JsObject>, _: *mut u8, _: *mut u8) {
127+
$crate::macro_internal::initialize_module(m, __init_neon_module);
128+
}
129+
130+
extern "C" {
131+
fn node_module_register(module: *mut __NodeModule);
132+
}
133+
134+
// Suppress the default Rust panic hook, which prints diagnostics to stderr.
135+
::std::panic::set_hook(::std::boxed::Box::new(|_| { }));
136+
137+
// During tests, node is not available. Skip module registration.
138+
#[cfg(not(test))]
139+
unsafe {
140+
// Set the ABI version based on the NODE_MODULE_VERSION constant provided by the current node headers.
141+
__NODE_MODULE.version = $crate::macro_internal::runtime::module::get_version();
142+
node_module_register(&mut __NODE_MODULE);
143+
}
151144
}
152-
}
145+
146+
__load_neon_module
147+
};
153148
}
154149
}
155150

0 commit comments

Comments
 (0)