2 unstable releases

0.2.0 May 9, 2020
0.1.0 Apr 26, 2020

#11 in #node-js

MIT license

6KB
110 lines

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)
  }
}

Dependencies

~1.5MB
~36K SLoC