Skip to content

libnode: Added C FFI compatible function to act as an entrypoint for embedded consumers #57846

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alshdavid
Copy link

Motivation

Hi all,

I have been working on embedding Nodejs in my Rust application to act as a plugin runtime however, while libnode exposes a C++ API, it does not work with consumers that only have access to C FFI calls (like Rust).

Changes

  • Added node_start() function in node.cc to proxy node::Start()
    • Enables C bindings to be written for Nodejs

Examples

I created a repo that patches nodejs to add this function, generate static binaries (under GitHub releases) and offers a Rust crate that provides bindings. So far, all I need is this one function added to node.cc.

Usage:

use libnode_rs;

pub fn main() -> std::io::Result<()> {
  // Register a napi module and inject it into the JavaScript runtime
  libnode_rs::napi_module_register("my_native_extension", |env, exports| exports);

  libnode_rs::eval_blocking(r"
    console.log('Hello World')
    console.log(process._linkedBinding('my_native_extension'))
  ")
}

CC: #52289

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Apr 12, 2025
@alshdavid alshdavid changed the title Added C function to act as an entrypoint for embedded consumers libnode: Added C FFI compatible function to act as an entrypoint for embedded consumers Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants