napi-rs/napi-derive
2020-05-02 00:21:38 +08:00
..
src refactor(napi-rs): impl TryFrom instead of TryInto 2020-04-26 18:46:56 +08:00
Cargo.toml chore: rename napi-derive to napi-rs-derive 2020-04-26 20:21:45 +08:00
README.md chore: remove lifetime param in example 2020-05-02 00:21:38 +08:00

napi-derive

js_function

#[macro_use]
extern crate napi_rs_derive;

use napi_rs::{Result, Value, CallContext, Number};
use std::convert::TryInto;

#[js_function(1)]
fn fibonacci(ctx: CallContext) -> Result<Value<Number>> {
  let n = ctx.get::<Number>(0)?.try_into()?;
  ctx.env.create_int64(fibonacci_native(n))
}

#[inline]
fn fibonacci_native(n: i64) -> i64 {
  match n {
    1 | 2 => 1,
    _ => fibonacci_native(n - 1) + fibonacci_native(n - 2)
  }
}