2020-04-26 17:58:11 +09:00
|
|
|
# napi-derive
|
|
|
|
|
|
|
|
## js_function
|
|
|
|
|
|
|
|
```rust
|
2020-04-26 21:21:45 +09:00
|
|
|
#[macro_use]
|
|
|
|
extern crate napi_rs_derive;
|
|
|
|
|
2020-04-26 17:58:11 +09:00
|
|
|
use napi_rs::{Result, Value, CallContext, Number};
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2020-04-27 11:35:48 +09:00
|
|
|
#[js_function(1)]
|
2020-04-26 17:58:11 +09:00
|
|
|
fn fibonacci<'env>(ctx: CallContext<'env>) -> Result<Value<'env, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|