Comments (5)
Hi @ziv, thanks for opening the issue, and for the great reproduction! I think I see two things happening here:
-
You are importing
node_bindgen::derive::node_bindgen
at the top-level module, but you are trying to use it inside yourexample
module. Try putting youruse
statement inside of each module where you use#[node_bindgen]
. -
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.
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.
Seems like this is solved problem. Closing this. Feel to re-open if this doesn't the solve
from node-bindgen.
We should add this to documentation
from node-bindgen.
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)
- Clippy lints for rust 1.53 fails on procmacros HOT 4
- Support for electronjs applications with node-bindgen HOT 4
- Add `impl JsValue` and `impl TryToJs` for `HashMap<String, T>`
- Support M1 as platform HOT 7
- Latest `nj-cli` not published HOT 2
- Generate wrappers for external libraries? HOT 4
- Tokio runtime in node.js? HOT 6
- add donate button
- fetch support? HOT 1
- JS namespaces (modules) HOT 1
- 0.4.2 fails to build. HOT 2
- Expecting compiler error when using &str as parameter
- Build issue starting from rust 1.62.x HOT 5
- update inventory to 0.3
- update nj-sys using latest version of bindgen
- fix CI for windows
- Build on windows failed ( > node 18.x.x) HOT 6
- Create new release 5.1.0 for current version on master HOT 2
- undefined reference to `napi_...` issues HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-bindgen.