Rust-Neon: Rust bindings for writing safe and fast native Node.js modules.

neon

Travis Build Status Appveyor Build Status npm

Rust bindings for writing safe and fast native Node.js modules.

Getting started

Once you have the platform dependencies installed, getting started is as simple as:

$ npm install -g neon-cli
$ neon new my-project

Then see the Hello World guide for writing your first Hello World in Neon!

Docs

See our Neon fundamentals docs and our API docs.

Platform Support

Operating Systems

Linux macOS Windows

Node.js

Node 8 Node 10 Node 12

Support for LTS versions of Node and current are expected. If you're using a different version of Node and believe it should be supported, let us know.

Rust

Neon supports Rust stable version 1.18 and higher. We test on the latest stable, beta, and nightly versions of Rust.

A Taste...

fn make_an_array(mut cx: FunctionContext) -> JsResult<JsArray> {
    // Create some values:
    let n = cx.number(9000);
    let s = cx.string("hello");
    let b = cx.boolean(true);

    // Create a new array:
    let array: Handle<JsArray> = cx.empty_array();

    // Push the values into the array:
    array.set(&mut cx, 0, n)?;
    array.set(&mut cx, 1, s)?;
    array.set(&mut cx, 2, b)?;

    // Return the array:
    Ok(array)
}

register_module!(mut cx, {
    cx.export_function("makeAnArray", make_an_array)
})

For more examples, see our examples repo.

Get Involved

The Neon community is just getting started and there's tons of fun to be had. Come play! :)

The Rust Bindings community Slack is open to all; use the Slackin app to receive an invitation.

License

Licensed under either of

at your option.

Comments

  • How to parse an array of (structed) JS objects into Vec of rust struct without JSON?
    How to parse an array of (structed) JS objects into Vec of rust struct without JSON?

    Jun 10, 2020

    Is there any way to pass an array of object into a vector of rust struct without using serde?

    I tried work around like this

    
    let object = {
    a:1,
    b:"string",
    c:1.00
    }
    let arrayOfObject =  [object,object,object]
    let arrayOfa = [1,1,1]
    let arrayOfb = ["string","string","string"]
    let arrayOfc=[1.00,1.00,1.00]
    
    some_neon_lib(arrayOfa,arrayOfb,arrayOfc);
    

    and in rust

    struct SampleStruct {
    a:usize;
    b:String;
    c:f32;
    }
    fn create_sample_struct(
    a:usize,
    b:String,
    c:f32
    ) -> SampleStruct{
    a,b,c
    }
    ---------------------------------- neon native/lib.rs
    
    let arrayOfa= cx.argument::<JsArray>(0)?;
    let arrayOfb= cx.argument::<JsArray>(1)?;
    let arrayOfc= cx.argument::<JsArray>(2)?;
    let vec_of_a :Vec<Handle<JsValue>> =arrayOfa.to_vec(&mut cx)?;
    //... so on for b and c
    let sampleVec: Vec<SampleStruct> = vec![]
    for index in 0 .. length-1 {
    let a = vec_of_a [index].downcast::<JsNumber>().unwrap().value() as usize;
    / ... so on for b and c
    let sample_post = create_sample_struct (
    a,b,c
    )
    sampleVec.push(sample_post)
    
    }
    #process something with sample vec
    
    

    The overhead of conversion manually is huge, and using JSON is much worse.

    I can't find document about how to deal with this situation anywhere. Any suggestion? Thanks

    Reply
  • Debugging from Electron
    Debugging from Electron

    Jun 11, 2020

    I have an Electron app that uses Neon to generate bindings for some Rust libraries. I have been trying to debug my application using rust-lldb, but I cannot set breakpoints within the Rust code that LLDB will recognize. All the libraries are built in debug mode. Is there a way to properly debug Rust code being called from Electron via Neon?

    For reference, the command I'm using for LLDB is:

    rust-lldb node -- node_modules/.bin/electron dist/app/main.js
    
    Reply
  • Implement RFC 28
    Implement RFC 28

    Jun 13, 2020

    Implement neon-bindings/rfcs#28.

    Reply
  • Support-matrix automation
    Support-matrix automation

    Jun 13, 2020

    Our support matrix should automatically follow the Node support matrix: we support the Maintenance LTS version of Node and all versions from the Active LTS and newer. We currently have to maintain this manually, which requires:

    • Updating the README
    • Updating the CI matrix This should be automatable with a schedule-based GitHub Action! It could take advantage of publicly available published metadata like the Node release schedule.

    This could be a nice contribution from someone who doesn't yet feel ready to dive into the gnarly guts of the Neon Rust and C++ code (?) but is looking for a way to join in the fun!

    automation beginner friendly 
    Reply
  • Move CI to GitHub Actions
    Move CI to GitHub Actions

    Jun 13, 2020

    GitHub Actions seems like a great fit for this project, since we have tests across Linux, Mac, and Windows, and today that requires maintaining two different CI setups with Travis and Appveyor.

    If someone wants to help us out with this, I'll be happy to work with you to get you the credentials you need to set it up! Hit me up on our community slack (get your auto-invite here)--my Slack handle is dherman.

    automation beginner friendly 
    Reply
  • Neon does not link properly with `lld`
    Neon does not link properly with `lld`

    Jun 16, 2020

    If unable to fix, we should at least document the limitation. It appears to miss the link section in the final library.

    bug docs 
    Reply
  • Fix #463 ; Debug build mode fixing in Windows
    Fix #463 ; Debug build mode fixing in Windows

    Mar 26, 2020

    Related: https://github.com/neon-bindings/neon/issues/463#issuecomment-604243158

    Reply
  • No way to build without --release?
    No way to build without --release?

    Nov 10, 2019

    Hi guys :wave: Is it me or it's only possible to neon build --release? If I run just neon build then I get this message Neon only build with --release.... What is the reason of that?

    There are some conditional attributes in my code that depend on debug_assertions, so those don't work because of this.

    Response would be greatly appreciated, especially if soon.

    Reply
  • 0.3.2 linker error on macOS
    0.3.2 linker error on macOS

    Oct 19, 2019

    When I try to build with neon-cli 0.3.2 on macOS it fails with:

    ld: warning: cannot export hidden symbol compiler_builtins::mem::memcpy::h89bf2c29275db9b6 from /var/folders/09/3ccx9hvj499_767y34mtcr900000gn/T/rustc5SpE5s/libcompiler_builtins-4c0e14a54ecf951d.rlib(compiler_builtins-4c0e14a54ecf951d.compiler_builtins.dcjs62au-cgu.0.rcgu.o)
              ld: warning: cannot export hidden symbol compiler_builtins::mem::memmove::hb03f562604dc3076 from /var/folders/09/3ccx9hvj499_767y34mtcr900000gn/T/rustc5SpE5s/libcompiler_builtins-4c0e14a54ecf951d.rlib(compiler_builtins-4c0e14a54ecf951d.compiler_builtins.dcjs62au-cgu.0.rcgu.o)
              ld: warning: cannot export hidden symbol compiler_builtins::mem::memset::h1f53dc51033b23d1 from /var/folders/09/3ccx9hvj499_767y34mtcr900000gn/T/rustc5SpE5s/libcompiler_builtins-4c0e14a54ecf951d.rlib(compiler_builtins-4c0e14a54ecf951d.compiler_builtins.dcjs62au-cgu.0.rcgu.o)
              ld: warning: cannot export hidden symbol compiler_builtins::mem::memcmp::hc5ebea53cd23af68 from /var/folders/09/3ccx9hvj499_767y34mtcr900000gn/T/rustc5SpE5s/libcompiler_builtins-4c0e14a54ecf951d.rlib(compiler_builtins-4c0e14a54ecf951d.compiler_builtins.dcjs62au-cgu.0.rcgu.o)
    [...]
    Undefined symbols for architecture x86_64:
                "v8::Isolate::ThrowException(v8::Local<v8::Value>)", referenced from:
                    _Neon_Error_Throw in libneon_runtime-0d1b50c0de5edd79.rlib(neon.o)
                    _Neon_Error_ThrowErrorFromUtf8 in libneon_runtime-0d1b50c0de5edd79.rlib(neon.o)
                "v8::Value::IsNumber() const", referenced from:
                    _Neon_Tag_IsNumber in libneon_runtime-0d1b50c0de5edd79.rlib(neon.o)
                "v8::HandleScope::CreateHandle(v8::internal::Isolate*, v8::internal::Object*)", referenced from:
                    _Neon_Class_HasInstance in libneon_runtime-0d1b50c0de5edd79.rlib(neon.o)
    [...]
              ld: symbol(s) not found for architecture x86_64
              clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    with [...] I skipped similar lines, but I can provide full trace.

    Switching back to 0.3.1 (npm install -g [email protected]) fixes the problem.

    Tried with

    • NodeJS v10.15.3, v10.16.3 and v12.12.0.
    • rustc 1.38.0 (625451e37 2019-09-23)
    Reply
  • [docs] Electron apps - undefined symbol __cxa_pure_virtual
    [docs] Electron apps - undefined symbol __cxa_pure_virtual

    Feb 4, 2019

    Hey, I'm learning how to set up neon here, and I'm running into an issue where, upon launching electron, I get the error: undefined symbol: __cxa_pure_virtual.

    FWIW, I followed the documentation here. I found #194, but I've interpreted it as the ticket for integrating with electron-rebuild, and that the electron-build-env process was already functional? :thinking: :smile:

    To reproduce:

    1. git clone https://github.com/electron/electron-quick-start
    2. cd electron-quick-start
    3. npm i neon-hello
    4. npm i electron-build-env neon-cli --save-dev
    5. $(npm bin)/neon build neon-hello
    6. Edit main.js, adding require('neon-hello') to the top
    7. npm start
    $ npm start                                         
    
    > [email protected] start /home/mitch/dev/electron-quick-start
    > electron .
    
    App threw an error during load
    Error: /home/mitch/dev/electron-quick-start/node_modules/neon-hello/native/index.node: undefined symbol: __cxa_pure_virtual
        at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
        at Object.Module._extensions..node (internal/modules/cjs/loader.js:722:18)
        at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
        at Module.load (internal/modules/cjs/loader.js:602:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
        at Function.Module._load (internal/modules/cjs/loader.js:533:3)
        at Module.require (internal/modules/cjs/loader.js:640:17)
        at require (internal/modules/cjs/helpers.js:20:18)
        at Object.<anonymous> (/home/mitch/dev/electron-quick-start/node_modules/neon-hello/lib/index.js:1:168)
        at Object.<anonymous> (/home/mitch/dev/electron-quick-start/node_modules/neon-hello/lib/index.js:5:3)
    A JavaScript error occurred in the main process
    Uncaught Exception:
    Error: /home/mitch/dev/electron-quick-start/node_modules/neon-hello/native/index.node: undefined symbol: __cxa_pure_virtual
        at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
        at Object.Module._extensions..node (internal/modules/cjs/loader.js:722:18)
        at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
        at Module.load (internal/modules/cjs/loader.js:602:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
        at Function.Module._load (internal/modules/cjs/loader.js:533:3)
        at Module.require (internal/modules/cjs/loader.js:640:17)
        at require (internal/modules/cjs/helpers.js:20:18)
        at Object.<anonymous> (/home/mitch/dev/electron-quick-start/node_modules/neon-hello/lib/index.js:1:168)
        at Object.<anonymous> (/home/mitch/dev/electron-quick-start/node_modules/neon-hello/lib/index.js:5:3)
    
    Reply
  • Error building new project (neon-build)
    Error building new project (neon-build)

    Feb 13, 2017

    Hi, i just created a new neon project, but it doesn't seems to build ... Here's the log.

    Updating registry `https://github.com/rust-lang/crates.io-index`
    Compiling nod v0.1.0 (file:///Users/jchaput/dev/rust/nod/native)
    Compiling cslice v0.1.1
    Compiling gcc v0.3.43
    error[E0463]: can't find crate for `neon_build`
     --> build.rs:1:1
      |
    1 | extern crate neon_build;
      | ^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
    
    error: aborting due to previous error
    
    error: Could not compile `nod`.
    Build failed, waiting for other jobs to finish...
    error: build failed
    neon ERR! cargo build failed
    error Command failed with exit code 1.
    

    thanks

    Reply
  • Add ThreadSafeCallback helper to schedule callbacks on the main thread
    Add ThreadSafeCallback helper to schedule callbacks on the main thread

    Nov 29, 2018

    Hi, this adds a little helper to call a JavaScript function from any rust thread as requested in #197. ~~I'm not sure if this will need a RFC, but I can write one if requested.~~ RFC

    Feedback welcome, especially about the v8 scope setup.

    Inspired by mika-fischer/napi-thread-safe-callback

    Reply