refactor(napi): scope and Ref
This commit is contained in:
parent
4460f43be8
commit
9c921ffaa3
36 changed files with 619 additions and 474 deletions
bench/src
|
@ -1,17 +1,20 @@
|
|||
use napi::{CallContext, Env, JsBuffer, JsNumber, JsObject, Module, Result, Task};
|
||||
use napi::{
|
||||
CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Module, Ref, Result, Task,
|
||||
};
|
||||
|
||||
#[repr(transparent)]
|
||||
struct BufferLength(&'static [u8]);
|
||||
struct BufferLength(Ref<JsBufferValue>);
|
||||
|
||||
impl Task for BufferLength {
|
||||
type Output = usize;
|
||||
type JsValue = JsNumber;
|
||||
|
||||
fn compute(&mut self) -> Result<Self::Output> {
|
||||
Ok(self.0.len())
|
||||
Ok((&self.0).len())
|
||||
}
|
||||
|
||||
fn resolve(&self, env: &mut Env, output: Self::Output) -> Result<Self::JsValue> {
|
||||
fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
|
||||
self.0.unref(env)?;
|
||||
env.create_uint32(output as u32)
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +22,7 @@ impl Task for BufferLength {
|
|||
#[js_function(1)]
|
||||
fn bench_async_task(ctx: CallContext) -> Result<JsObject> {
|
||||
let n = ctx.get::<JsBuffer>(0)?;
|
||||
let task = BufferLength(n.data);
|
||||
let task = BufferLength(n.into_ref()?);
|
||||
let async_promise = ctx.env.spawn(task)?;
|
||||
Ok(async_promise.promise_object())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue