Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "napi-embedding" feature to allow statically linked N-API symbols #691

Closed
wants to merge 2 commits into from

Conversation

branchseer
Copy link
Contributor

This PR adds a new feature napi-embedding. When enabled, it loads N-API symbols using extern "C" { ... } declarations instead of libloading.

Why?

I am working on rust-nodejs which embeds Node.js in Rust. The general idea is to build and link Node.js as a static library, and use N-API to communicate between Rust and Node.js.

Neon fits perfectly for the latter part, expect that it tries to load N-API symbols dynamically (#646). This doesn't work in the case of rust-nodejs, because all N-API symbols are statically linked.

@kjvalencik
Copy link
Member

@patr0nus Can you explain a little bit more about your use case? While the symbols are being loaded dynamically, they are being loaded from the main process and not from a shared library.

Is there possibly a simpler solution to your problem? For example, if the library that the symbols were loaded from was configurable.

The reason I ask is that I'm hesitant to merge this as is because it will not work on Windows. It also might not work on macOS without additional RUSTC flags.

I am also generally curious about the rust-nodejs project. It seems a like a really neat idea! However, I am a little curious why a user might want to embed Node in Rust instead of Rust in Node.js. Is it about packaging a single binary?

@branchseer
Copy link
Contributor Author

branchseer commented Mar 10, 2021

@kjvalencik

While the symbols are being loaded dynamically, they are being loaded from the main process and not from a shared library.

You are right! It turns out it's because all unused N-API symbols are stripped out by the linker. I'm closing this PR in favor of "-rdynamic" linker flag.

However, I am a little curious why a user might want to embed Node in Rust instead of Rust in Node.js. Is it about packaging a single binary?

Here're my two use cases:

  1. Build an Electron-like framework,
  2. Allow user script in a Rust application;

Also I think the neon project itself could benefit: with rust-nodejs some tests can run directly in-process, without the need of setting up npm packages. Just an idea though. I'm no expert.

@branchseer branchseer closed this Mar 10, 2021
@kjvalencik
Copy link
Member

Thanks @patr0nus! It looks like an interesting project. I'll keep an eye on your progress. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants