项目作者: napi-rs

项目描述 :
High-level Node.js N-API bindings for Rust [WIP] ✨🦀🚀✨
高级语言: Rust
项目地址: git://github.com/napi-rs/napi.git
创建时间: 2017-10-06T12:48:51Z
项目社区:https://github.com/napi-rs/napi

开源协议:MIT License

下载


Node.js N-API for Rust! [work in progress]

Travis Build Status
AppVeyor Build Status

High-level N-API bindings for Node.js addons written in Rust.

Warning: this is a proof-of-concept implementation that’s not intended
for use yet. The project is under initial phase of development, the API is a
quite sketchy and is going to be refactored heavily. If you are interested in
contributing, though, it is super welcome!

The project is covered by a Code of Conduct.

Crates

  • napi-sys: low-level bindings to N-API generated from
    node_api.h
    using bindgen.
  • napi: high-level and rusty wrappers around napi-sys.
  • napi-derive: contains a procedural macro that allows to
    construct typesafe structures that represent N-API callback parameters and
    automatically validate the arguments that JavaScript code passes in.

Example

Check out the example directory to see the full source code and
project structure of this example. (TODO: initialize the module from Rust too).

lib.rs

  1. #[macro_use]
  2. extern crate napi;
  3. #[macro_use]
  4. extern crate napi_derive;
  5. use napi::{NapiEnv, NapiNumber, NapiResult, NapiUndefined};
  6. #[derive(NapiArgs)]
  7. struct HelloArgs;
  8. fn hello<'a>(env: &'a NapiEnv, _: &HelloArgs) -> NapiResult<NapiUndefined<'a>> {
  9. println!("Hello from the Rust land!");
  10. NapiUndefined::new(env)
  11. }
  12. #[derive(NapiArgs)]
  13. struct AddArgs<'a> {
  14. first: NapiNumber<'a>,
  15. second: NapiNumber<'a>,
  16. }
  17. fn add<'a>(env: &'a NapiEnv, args: &AddArgs<'a>) -> NapiResult<NapiNumber<'a>> {
  18. let first = args.first.to_i32()?;
  19. let second = args.second.to_i32()?;
  20. NapiNumber::from_i32(env, first + second)
  21. }
  22. napi_callback!(example_hello, hello);
  23. napi_callback!(example_add, add);

example.js

  1. 'use strict';
  2. const addon = require('./build/Release/example.node');
  3. addon.hello();
  4. console.log(addon.add(1, 2));