Skip to content

Commit 553de18

Browse files
committed
fix: Make native modules built with Neon work in Bun
1 parent f21146d commit 553de18

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

crates/neon/src/sys/bindings/functions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,19 @@ pub(super) unsafe fn load(env: Env) -> Result<(), libloading::Error> {
371371
// with `Error: Module did not self-register` if N-API does not exist.
372372
let version = get_version(&host, env).expect("Failed to find N-API version");
373373

374-
napi1::load(&host, version, 1)?;
374+
napi1::load(&host, version, 1);
375375

376376
#[cfg(feature = "napi-4")]
377-
napi4::load(&host, version, 4)?;
377+
napi4::load(&host, version, 4);
378378

379379
#[cfg(feature = "napi-5")]
380-
napi5::load(&host, version, 5)?;
380+
napi5::load(&host, version, 5);
381381

382382
#[cfg(feature = "napi-6")]
383-
napi6::load(&host, version, 6)?;
383+
napi6::load(&host, version, 6);
384384

385385
#[cfg(feature = "napi-8")]
386-
napi8::load(&host, version, 8)?;
386+
napi8::load(&host, version, 8);
387387

388388
Ok(())
389389
}

crates/neon/src/sys/bindings/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ macro_rules! generate {
118118

119119
#[inline(never)]
120120
fn panic_load<T>() -> T {
121-
panic!("Must load N-API bindings")
121+
panic!("Node-API symbol has not been loaded")
122122
}
123123

124124
static mut NAPI: Napi = {
@@ -139,21 +139,29 @@ macro_rules! generate {
139139
host: &libloading::Library,
140140
actual_napi_version: u32,
141141
expected_napi_version: u32,
142-
) -> Result<(), libloading::Error> {
142+
) {
143143
assert!(
144144
actual_napi_version >= expected_napi_version,
145-
"Minimum required N-API version {}, found {}.",
145+
"Minimum required Node-API version {}, found {}.",
146146
expected_napi_version,
147147
actual_napi_version,
148148
);
149149

150150
NAPI = Napi {
151151
$(
152-
$name: *host.get(napi_name!($name).as_bytes())?,
152+
$name: match host.get(napi_name!($name).as_bytes()) {
153+
Ok(f) => *f,
154+
// Node compatible runtimes may not have full coverage of Node-API
155+
// (e.g., bun). Instead of failing to start, warn on start and
156+
// panic when the API is called.
157+
// https://github.com/Jarred-Sumner/bun/issues/158
158+
Err(err) => {
159+
eprintln!("WARN: {}", err);
160+
NAPI.$name
161+
},
162+
},
153163
)*
154164
};
155-
156-
Ok(())
157165
}
158166

159167
$(

0 commit comments

Comments
 (0)