|
4 | 4 |
|
5 | 5 | High-level N-API bindings for Node.js addons written in Rust.
|
6 | 6 |
|
7 |
| -This project is covered by a [Code of Conduct](CODE_OF_CONDUCT.md). |
| 7 | +**Warning**: this is a proof-of-concept implementation that's not intended |
| 8 | +for use yet. The project is under initial phase of development, the API is a |
| 9 | +quite sketchy and is going to be refactored heavily. If you are interested in |
| 10 | +contributing, though, it is super welcome! |
| 11 | + |
| 12 | +The project is covered by a [Code of Conduct][coc]. |
8 | 13 |
|
9 | 14 | ## Crates
|
10 | 15 |
|
11 |
| -* `napi-sys`: low-level bindings to N-API generated from |
| 16 | +* [`napi-sys`][napi-sys]: low-level bindings to N-API generated from |
12 | 17 | [`node_api.h`](https://github.com/nodejs/node/blob/master/src/node_api.h)
|
13 | 18 | using [`bindgen`](https://github.com/rust-lang-nursery/rust-bindgen).
|
14 |
| -* `napi`: high-level and rusty wrappers around `napi-sys`. |
15 |
| -* `napi-derive`: contains a procedural macro that allows to construct typesafe |
16 |
| - structures that represent function parameters from JavaScript function call |
17 |
| - arguments and automatically validate them. |
| 19 | +* [`napi`][napi]: high-level and rusty wrappers around `napi-sys`. |
| 20 | +* [`napi-derive`][napi-derive]: contains a procedural macro that allows to |
| 21 | + construct typesafe structures that represent N-API callback parameters and |
| 22 | + automatically validate the arguments that JavaScript code passes in. |
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | +Check out <https://github.com/aqrln/napi-rs/tree/master/example> to see the |
| 27 | +full source code and project structure of this example. |
| 28 | + |
| 29 | +### `lib.rs` |
| 30 | + |
| 31 | +```rust |
| 32 | +#[macro_use] |
| 33 | +extern crate napi; |
| 34 | +#[macro_use] |
| 35 | +extern crate napi_derive; |
| 36 | + |
| 37 | +use napi::{NapiEnv, NapiNumber, NapiResult, NapiUndefined}; |
| 38 | + |
| 39 | +#[derive(NapiArgs)] |
| 40 | +struct HelloArgs; |
| 41 | + |
| 42 | +fn hello<'a>(env: &'a NapiEnv, _: &HelloArgs) -> NapiResult<NapiUndefined<'a>> { |
| 43 | + println!("Hello from the Rust land!"); |
| 44 | + NapiUndefined::new(env) |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(NapiArgs)] |
| 48 | +struct AddArgs<'a> { |
| 49 | + first: NapiNumber<'a>, |
| 50 | + second: NapiNumber<'a>, |
| 51 | +} |
| 52 | + |
| 53 | +fn add<'a>(env: &'a NapiEnv, args: &AddArgs<'a>) -> NapiResult<NapiNumber<'a>> { |
| 54 | + let first = args.first.to_i32()?; |
| 55 | + let second = args.second.to_i32()?; |
| 56 | + NapiNumber::from_i32(env, first + second) |
| 57 | +} |
| 58 | + |
| 59 | +napi_callback!(example_hello, hello); |
| 60 | +napi_callback!(example_add, add); |
| 61 | +``` |
| 62 | + |
| 63 | +### `example.js` |
| 64 | + |
| 65 | +```javascript |
| 66 | +'use strict'; |
| 67 | + |
| 68 | +const addon = require('./build/Release/example.node'); |
| 69 | + |
| 70 | +addon.hello(); |
| 71 | +console.log(addon.add(1, 2)); |
| 72 | +``` |
18 | 73 |
|
| 74 | +[coc]: https://github.com/aqrln/napi-rs/blob/master/CODE_OF_CONDUCT.md |
| 75 | +[napi]: https://crates.io/crates/napi |
| 76 | +[napi-derive]: https://crates.io/crates/napi-derive |
| 77 | +[napi-sys]: https://crates.io/crates/napi-sys |
19 | 78 | [travis-badge]: https://travis-ci.org/aqrln/napi-rs.svg?branch=master
|
20 | 79 | [travis-url]: https://travis-ci.org/aqrln/napi-rs
|
0 commit comments