Skip to content
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 <jupp0r@gmail.com>"]

[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 <jupp0r@gmail.com>"]
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 npm@3.10.10
3 info using node@v6.11.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle hello-world@1.0.0~prestart: hello-world@1.0.0
6 verbose lifecycle hello-world@1.0.0~prestart: unsafe-perm in lifecycle true
7 verbose lifecycle hello-world@1.0.0~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 hello-world@1.0.0~prestart: CWD: /Users/hendrik/code/rust/node-api/examples/hello-world
9 silly lifecycle hello-world@1.0.0~prestart: Args: [ '-c', 'npm install' ]
10 silly lifecycle hello-world@1.0.0~prestart: Returned: code: 0 signal: null
11 info lifecycle hello-world@1.0.0~start: hello-world@1.0.0
12 verbose lifecycle hello-world@1.0.0~start: unsafe-perm in lifecycle true
13 verbose lifecycle hello-world@1.0.0~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 hello-world@1.0.0~start: CWD: /Users/hendrik/code/rust/node-api/examples/hello-world
15 silly lifecycle hello-world@1.0.0~start: Args: [ '-c', 'node --napi-modules index.js' ]
16 silly lifecycle hello-world@1.0.0~start: Returned: code: 9 signal: null
17 info lifecycle hello-world@1.0.0~start: Failed to exec start script
18 verbose stack Error: hello-world@1.0.0 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 hello-world@1.0.0
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 hello-world@1.0.0 start: `node --napi-modules index.js`
26 error Exit status 9
27 error Failed at the hello-world@1.0.0 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