mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
62 lines
1.4 KiB
Rust
62 lines
1.4 KiB
Rust
#[macro_use]
|
|
extern crate napi_rs as napi;
|
|
#[macro_use]
|
|
extern crate napi_rs_derive;
|
|
|
|
use napi::{Any, CallContext, Env, Error, Number, Object, Result, Status, Task, Value};
|
|
use std::convert::TryInto;
|
|
|
|
register_module!(test_module, init);
|
|
|
|
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
|
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
|
exports.set_named_property(
|
|
"testSpawnThread",
|
|
env.create_function("testSpawnThread", test_spawn_thread)?,
|
|
)?;
|
|
Ok(())
|
|
}
|
|
|
|
struct ComputeFib {
|
|
n: u32,
|
|
}
|
|
|
|
impl ComputeFib {
|
|
pub fn new(n: u32) -> ComputeFib {
|
|
ComputeFib { n }
|
|
}
|
|
}
|
|
|
|
impl Task for ComputeFib {
|
|
type Output = u32;
|
|
type JsValue = Number;
|
|
|
|
fn compute(&self) -> Result<Self::Output> {
|
|
Ok(fibonacci_native(self.n))
|
|
}
|
|
|
|
fn resolve(&self, env: &mut Env, output: Self::Output) -> Result<Value<Self::JsValue>> {
|
|
env.create_uint32(output)
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn fibonacci_native(n: u32) -> u32 {
|
|
match n {
|
|
1 | 2 => 1,
|
|
_ => fibonacci_native(n - 1) + fibonacci_native(n - 2),
|
|
}
|
|
}
|
|
|
|
#[js_function(1)]
|
|
fn test_spawn_thread(ctx: CallContext) -> Result<Value<Object>> {
|
|
let n = ctx.get::<Number>(0)?;
|
|
let task = ComputeFib::new(n.try_into()?);
|
|
ctx.env.spawn(task)
|
|
}
|
|
|
|
#[js_function]
|
|
fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
|
|
Err(Error::from_status(Status::GenericFailure))
|
|
}
|