Skip to content

targeting 8.7.0's node_api.h #4

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: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
[package]
name = "node-api"
version = "0.1.0"
authors = ["Jupp Müller <[email protected]>"]

[lib]
crate-type = ["lib"]

[dependencies]
node-api-sys = {path = "./node-api-sys"}
futures = "0.1"

[workspace]
members = [
"examples/hello-world",
"node-api",
"node-api-sys",
"tests",
]
]
2 changes: 1 addition & 1 deletion examples/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Jupp Müller <[email protected]>"]
crate-type = ["cdylib"]

[dependencies]
node-api = { path = "../.." }
node-api = { path = "../../node-api" }
49 changes: 49 additions & 0 deletions examples/hello-world/npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 verbose lifecycle [email protected]~prestart: unsafe-perm in lifecycle true
7 verbose lifecycle [email protected]~prestart: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/hendrik/code/rust/node-api/examples/hello-world/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:/Applications/Wireshark.app/Contents/MacOS:/Users/hendrik/.cargo/bin:/Users/hendrik/.go/bin
8 verbose lifecycle [email protected]~prestart: CWD: /Users/hendrik/code/rust/node-api/examples/hello-world
9 silly lifecycle [email protected]~prestart: Args: [ '-c', 'npm install' ]
10 silly lifecycle [email protected]~prestart: Returned: code: 0 signal: null
11 info lifecycle [email protected]~start: [email protected]
12 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
13 verbose lifecycle [email protected]~start: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/hendrik/code/rust/node-api/examples/hello-world/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:/Applications/Wireshark.app/Contents/MacOS:/Users/hendrik/.cargo/bin:/Users/hendrik/.go/bin
14 verbose lifecycle [email protected]~start: CWD: /Users/hendrik/code/rust/node-api/examples/hello-world
15 silly lifecycle [email protected]~start: Args: [ '-c', 'node --napi-modules index.js' ]
16 silly lifecycle [email protected]~start: Returned: code: 9 signal: null
17 info lifecycle [email protected]~start: Failed to exec start script
18 verbose stack Error: [email protected] start: `node --napi-modules index.js`
18 verbose stack Exit status 9
18 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
18 verbose stack at emitTwo (events.js:106:13)
18 verbose stack at EventEmitter.emit (events.js:191:7)
18 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
18 verbose stack at emitTwo (events.js:106:13)
18 verbose stack at ChildProcess.emit (events.js:191:7)
18 verbose stack at maybeClose (internal/child_process.js:891:16)
18 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
19 verbose pkgid [email protected]
20 verbose cwd /Users/hendrik/code/rust/node-api/examples/hello-world
21 error Darwin 16.6.0
22 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
23 error node v6.11.0
24 error npm v3.10.10
25 error code ELIFECYCLE
26 error [email protected] start: `node --napi-modules index.js`
26 error Exit status 9
27 error Failed at the [email protected] start script 'node --napi-modules index.js'.
27 error Make sure you have the latest version of node.js and npm installed.
27 error If you do, this is most likely a problem with the hello-world package,
27 error not with npm itself.
27 error Tell the author that this fails on your system:
27 error node --napi-modules index.js
27 error You can get information on how to open an issue for this project with:
27 error npm bugs hello-world
27 error Or if that isn't available, you can get their info via:
27 error npm owner ls hello-world
27 error There is likely additional logging output above.
28 verbose exit [ 1, true ]
4 changes: 2 additions & 2 deletions examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"license": "MIT",
"scripts": {
"preinstall": "cargo build",
"postinstall": "mkdir -p node-api && (cp ./target/debug/libhello_world.dylib node-api/index.node || cp ./target/debug/libhello_world.so node-api/index.node || cp ./target/debug/libhello_world.dll node-api/index.node)",
"postinstall": "mkdir -p node-api && (cp ../../target/debug/libhello_world.dylib node-api/index.node || cp ../../target/debug/libhello_world.so node-api/index.node || cp ../../target/debug/libhello_world.dll node-api/index.node)",
"prestart": "npm install",
"start": "node --napi-modules index.js"
"start": "node index.js"
}
}
6 changes: 4 additions & 2 deletions examples/hello-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use node_api::{create_function, set_named_property, create_object};
use node_api::error::*;

#[no_mangle]
pub extern "C" fn register(env: NapiEnv, exports: NapiValue, _module: NapiValue, _priv: *mut std::os::raw::c_void) {
pub extern "C" fn register(env: NapiEnv, exports: NapiValue) -> NapiValue {
let function = create_function(env, "foo", |_: NapiEnv, _: NapiValue, ()| {
HelloReturn {
foo: "hello".to_string(),
Expand All @@ -18,6 +18,8 @@ pub extern "C" fn register(env: NapiEnv, exports: NapiValue, _module: NapiValue,
})
.expect("error creating function");
set_named_property(env, exports, "hello", function).expect("error attaching function");

exports
}

struct HelloArgs {}
Expand All @@ -29,7 +31,7 @@ impl FromNapiValues for HelloArgs {

struct HelloReturn {
pub foo: String,
pub bar: u64,
pub bar: i64,
}

impl IntoNapiValue for HelloReturn {
Expand Down
2 changes: 2 additions & 0 deletions node-api-sys/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target/
**/*.rs.bk
Cargo.lock
node_api.h
node_api_types.h
21 changes: 21 additions & 0 deletions node-api-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Node-API-sys

This crate contains the bindings to the N-API.
These are generated from [node_api.h](https://github.com/nodejs/node/blob/master/src/node_api.h) [using](https://rust-lang-nursery.github.io/rust-bindgen/) [bindgen](https://github.com/rust-lang-nursery/rust-bindgen).

## Updating the bindings

Run [build.sh](build.sh) to get the latest version of the headers from master and run bindgen on them.
Manually remove a bunch of system constants :(
Manually change the following:

```rust
pub type napi_env = *mut napi_env__;
// to
pub type napi_env = u64;

// and
pub type napi_value = *mut napi_value__;
// to
pub type napi_value = u64;
```
10 changes: 10 additions & 0 deletions node-api-sys/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# curl -O "https://raw.githubusercontent.com/nodejs/node/master/src/node_api.h"
# curl -O "https://raw.githubusercontent.com/nodejs/node/master/src/node_api_types.h"

# nodejs 8.7.0
curl -O "https://raw.githubusercontent.com/nodejs/node/dc4f1b981a888a2f39219d384b6d3eb593203383/src/node_api.h"
curl -O "https://raw.githubusercontent.com/nodejs/node/dc4f1b981a888a2f39219d384b6d3eb593203383/src/node_api_types.h"

bindgen node_api.h > src/lib.rs
Loading