1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#![deny(clippy::all)]
#[cfg(feature = "napi8")]
mod async_cleanup_hook;
#[cfg(feature = "napi8")]
pub use async_cleanup_hook::AsyncCleanupHook;
mod async_work;
mod bindgen_runtime;
mod call_context;
#[cfg(feature = "napi3")]
mod cleanup_env;
mod env;
mod error;
mod js_values;
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
mod promise;
mod status;
mod task;
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
mod tokio_runtime;
mod value_type;
#[cfg(feature = "napi3")]
pub use cleanup_env::CleanupEnvHook;
#[cfg(feature = "napi4")]
pub mod threadsafe_function;
mod version;
#[cfg(target_os = "windows")]
mod win_delay_load_hook;
pub use napi_sys as sys;
pub use async_work::AsyncWorkPromise;
pub use call_context::CallContext;
pub use env::*;
pub use error::*;
pub use js_values::*;
pub use status::Status;
pub use task::Task;
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
pub use tokio_runtime::shutdown_tokio_rt;
pub use value_type::*;
pub use version::NodeVersion;
#[cfg(feature = "serde-json")]
#[macro_use]
extern crate serde;
pub type ContextlessResult<T> = Result<Option<T>>;
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! type_of {
($env:expr, $value:expr) => {{
let mut value_type = 0;
check_status!($crate::sys::napi_typeof($env, $value, &mut value_type))
.and_then(|_| Ok($crate::ValueType::from(value_type)))
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! assert_type_of {
($env: expr, $value:expr, $value_ty: expr) => {
$crate::type_of!($env, $value).and_then(|received_type| {
if received_type == $value_ty {
Ok(())
} else {
Err($crate::Error::new(
$crate::Status::InvalidArg,
format!(
"Expect value to be {}, but received {}",
$value_ty, received_type
),
))
}
})
};
}
#[allow(dead_code)]
pub(crate) unsafe fn log_js_value<V: AsRef<[sys::napi_value]>>(
method: &str,
env: sys::napi_env,
values: V,
) {
use std::ffi::CString;
use std::ptr;
let mut g = ptr::null_mut();
sys::napi_get_global(env, &mut g);
let mut console = ptr::null_mut();
let console_c_string = CString::new("console").unwrap();
let method_c_string = CString::new(method).unwrap();
sys::napi_get_named_property(env, g, console_c_string.as_ptr(), &mut console);
let mut method_js_fn = ptr::null_mut();
sys::napi_get_named_property(env, console, method_c_string.as_ptr(), &mut method_js_fn);
sys::napi_call_function(
env,
console,
method_js_fn,
values.as_ref().len(),
values.as_ref().as_ptr(),
ptr::null_mut(),
);
}
pub use crate::bindgen_runtime::ctor as module_init;
pub mod bindgen_prelude {
#[cfg(feature = "compat-mode")]
pub use crate::bindgen_runtime::register_module_exports;
#[cfg(feature = "tokio_rt")]
pub use crate::tokio_runtime::*;
pub use crate::{
assert_type_of, bindgen_runtime::*, check_status, check_status_or_throw, error, error::*, sys,
type_of, JsError, Property, PropertyAttributes, Result, Status, Task, ValueType,
};
}