Skip to content

Commit

Permalink
fix: Make native modules built with Neon work in Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Jul 8, 2022
1 parent f21146d commit 562f401
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Neon projects to 0.10!
Support for [LTS versions of Node](https://github.com/nodejs/LTS#release-schedule) and current are expected. If you're
using a different version of Node and believe it should be supported, let us know.

### Bun (experimental)

[Bun](https://bun.sh/) is an alternate JavaScript runtime that targets Node compatibility. In many cases Neon modules will work in bun; however, at the time of this writing, some Node-API functions are [not implemented](https://github.com/Jarred-Sumner/bun/issues/158).

### Rust

Neon supports Rust stable version 1.18 and higher. We test on the latest stable, beta, and nightly versions of Rust.
Expand Down
10 changes: 5 additions & 5 deletions crates/neon/src/sys/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,19 @@ pub(super) unsafe fn load(env: Env) -> Result<(), libloading::Error> {
// with `Error: Module did not self-register` if N-API does not exist.
let version = get_version(&host, env).expect("Failed to find N-API version");

napi1::load(&host, version, 1)?;
napi1::load(&host, version, 1);

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

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

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

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

Ok(())
}
22 changes: 16 additions & 6 deletions crates/neon/src/sys/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ macro_rules! generate {

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

static mut NAPI: Napi = {
Expand All @@ -139,21 +139,31 @@ macro_rules! generate {
host: &libloading::Library,
actual_napi_version: u32,
expected_napi_version: u32,
) -> Result<(), libloading::Error> {
) {
assert!(
actual_napi_version >= expected_napi_version,
"Minimum required N-API version {}, found {}.",
"Minimum required Node-API version {}, found {}.",
expected_napi_version,
actual_napi_version,
);

let print_warn = |err| eprintln!("WARN: {}", err);

NAPI = Napi {
$(
$name: *host.get(napi_name!($name).as_bytes())?,
$name: match host.get(napi_name!($name).as_bytes()) {
Ok(f) => *f,
// Node compatible runtimes may not have full coverage of Node-API
// (e.g., bun). Instead of failing to start, warn on start and
// panic when the API is called.
// https://github.com/Jarred-Sumner/bun/issues/158
Err(err) => {
print_warn(err);
NAPI.$name
},
},
)*
};

Ok(())
}

$(
Expand Down

0 comments on commit 562f401

Please sign in to comment.