Code Monkey home page Code Monkey logo

Comments (5)

nicholastmosher avatar nicholastmosher commented on September 13, 2024 2

Hi @ziv, thanks for opening the issue, and for the great reproduction! I think I see two things happening here:

  1. You are importing node_bindgen::derive::node_bindgen at the top-level module, but you are trying to use it inside your example module. Try putting your use statement inside of each module where you use #[node_bindgen].

  2. I tried fixing 1 and then saw a new error:

error[E0432]: unresolved import `crate::example::sum`
 --> src/testing.rs:1:5                              
  |
1 | use crate::example::sum;
  |     ^^^^^^^^^^^^^^^^^^^ no `sum` in `example`

error: aborting due to previous error

I think this is due to the fact that node_bindgen actually reads your function body and generates a totally different function from it, which is intended to be called from node rather than Rust. For unit testing, I would suggest making an inner Rust function that does your logic and test against that, then create a wrapper that has the #[node_bindgen] annotation and calls the inner function. Something like this:

#[node_bindgen]
pub fn sum(first: i32, second: i32) -> i32 {
    sum_impl(first, second)
}

pub fn sum_impl(first: i32, second: i32) -> i32 {
    first + second
}

// Then unit test `sum_impl`

Let me know if that pattern works for you!

from node-bindgen.

sehz avatar sehz commented on September 13, 2024 2

Test failure happens because actual rust fn sum is hidden:

extern "C" fn napi_sum(
    env: node_bindgen::sys::napi_env,
    cb_info: node_bindgen::sys::napi_callback_info,
) -> node_bindgen::sys::napi_value {
   .....
    fn sum(first: i32, second: i32) -> i32 {

So one way is to move inner fn as peer:

 pub fn sum(first: i32, second: i32) -> i32 {
...
}

extern "C" fn napi_sum(
    env: node_bindgen::sys::napi_env,
    cb_info: node_bindgen::sys::napi_callback_info,
) -> node_bindgen::sys::napi_value {
   .....
   

from node-bindgen.

sehz avatar sehz commented on September 13, 2024

Seems like this is solved problem. Closing this. Feel to re-open if this doesn't the solve

from node-bindgen.

sehz avatar sehz commented on September 13, 2024

We should add this to documentation

from node-bindgen.

ziv avatar ziv commented on September 13, 2024

Hi @nicholastmosher, while your solution is simple to use when my API is minimal, I have a library exports structs and their implementations.

Using this method is practically - copy all my code just to export it.

from node-bindgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.