1a3621b727
1. inline everything 2. change `check_status` and `type_of` to macro 3. provide #[module_exports] macro 4. remove debug and repr[transparent] for ffi struct
32 lines
768 B
Rust
32 lines
768 B
Rust
#[macro_use]
|
|
extern crate napi_derive;
|
|
|
|
use napi::{CallContext, Error, JsNumber, JsObject, JsUnknown, Result, Status};
|
|
use std::convert::TryInto;
|
|
|
|
#[module_exports]
|
|
fn init(mut exports: JsObject) -> Result<()> {
|
|
exports.create_named_method("testThrow", test_throw)?;
|
|
|
|
exports.create_named_method("fibonacci", fibonacci)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[js_function]
|
|
fn test_throw(_ctx: CallContext) -> Result<JsUnknown> {
|
|
Err(Error::from_status(Status::GenericFailure))
|
|
}
|
|
|
|
#[js_function(1)]
|
|
fn fibonacci(ctx: CallContext) -> Result<JsNumber> {
|
|
let n = ctx.get::<JsNumber>(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),
|
|
}
|
|
}
|