1
1
//! The [Neon](https://www.neon-bindings.com/) crate provides bindings for writing Node.js plugins with a safe and fast Rust API.
2
2
3
3
extern crate neon_runtime;
4
- extern crate cfg_if;
5
4
extern crate cslice;
6
5
extern crate semver;
7
6
@@ -25,131 +24,127 @@ pub mod macro_internal;
25
24
#[ cfg( all( feature = "legacy-runtime" , feature = "napi-runtime" ) ) ]
26
25
compile_error ! ( "Cannot enable both `legacy-runtime` and `napi-runtime` features.\n \n To use `napi-runtime`, disable `legacy-runtime` by setting `default-features` to `false` in Cargo.toml\n or with cargo's --no-default-features flag." ) ;
27
26
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
66
60
}
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 _
150
123
} ;
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
+ }
151
144
}
152
- }
145
+
146
+ __load_neon_module
147
+ } ;
153
148
}
154
149
}
155
150
0 commit comments