Skip to content

Commit

Permalink
chore(napi-derive): make_ref tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 22, 2022
1 parent 618d0f8 commit d3b2d4a
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 34 deletions.
14 changes: 8 additions & 6 deletions crates/backend/src/codegen/fn.rs
Expand Up @@ -61,13 +61,12 @@ impl TryToTokens for NapiFn {
unsafe impl Sync for NapiRefContainer {}
let _make_ref = |a: ::std::ptr::NonNull<napi::bindgen_prelude::sys::napi_value__>| {
let mut node_ref = ::std::mem::MaybeUninit::uninit();
assert_eq!(unsafe {
napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 1, node_ref.as_mut_ptr())
napi::bindgen_prelude::check_status!(unsafe {
napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 0, node_ref.as_mut_ptr())
},
napi::bindgen_prelude::sys::Status::napi_ok,
"failed to create napi ref"
);
unsafe { node_ref.assume_init() }
)?;
Ok::<napi::sys::napi_ref, napi::Error>(unsafe { node_ref.assume_init() })
};
let mut _args_array = [::std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_ref__>(); #arg_ref_count];
let mut _arg_write_index = 0;
Expand Down Expand Up @@ -184,7 +183,10 @@ impl NapiFn {
let mut mut_ref_spans = vec![];
let make_ref = |input| {
quote! {
_args_array[_arg_write_index] = _make_ref(::std::ptr::NonNull::new(#input).expect("ref ptr was null"));
_args_array[_arg_write_index] = _make_ref(
::std::ptr::NonNull::new(#input)
.ok_or_else(|| napi::Error::new(napi::Status::InvalidArg, "referenced ptr is null".to_owned()))?
)?;
_arg_write_index += 1;
}
};
Expand Down
1 change: 0 additions & 1 deletion examples/napi/__test__/typegen.spec.ts.md
Expand Up @@ -195,7 +195,6 @@ Generated by [AVA](https://avajs.dev).
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void␊
export function useTokioWithoutAsync(): void␊
export function getBuffer(): Buffer␊
export function appendBuffer(buf: Buffer): Buffer␊
export function getEmptyBuffer(): Buffer␊
Expand Down
Binary file modified examples/napi/__test__/typegen.spec.ts.snap
Binary file not shown.
7 changes: 0 additions & 7 deletions examples/napi/__test__/values.spec.ts
Expand Up @@ -106,7 +106,6 @@ import {
receiveObjectWithClassField,
AnotherClassForEither,
receiveDifferentClass,
useTokioWithoutAsync,
getNumArr,
getNestedNumArr,
CustomFinalize,
Expand Down Expand Up @@ -754,12 +753,6 @@ Napi4Test('await Promise in rust', async (t) => {
t.is(result, fx + 100)
})

Napi4Test('Run function which uses tokio internally but is not async', (t) => {
useTokioWithoutAsync()
// The prior didn't throw an exception, so it worked.
t.assert(true)
})

Napi4Test('Promise should reject raw error in rust', async (t) => {
const fxError = new Error('What is Happy Planet')
const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError)))
Expand Down
1 change: 0 additions & 1 deletion examples/napi/index.d.ts
Expand Up @@ -185,7 +185,6 @@ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void
export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void
export function useTokioWithoutAsync(): void
export function getBuffer(): Buffer
export function appendBuffer(buf: Buffer): Buffer
export function getEmptyBuffer(): Buffer
Expand Down
1 change: 0 additions & 1 deletion examples/napi/src/lib.rs
Expand Up @@ -40,5 +40,4 @@ mod string;
mod symbol;
mod task;
mod threadsafe_function;
mod tokio_outside_async;
mod typed_array;
18 changes: 0 additions & 18 deletions examples/napi/src/tokio_outside_async.rs

This file was deleted.

1 comment on commit d3b2d4a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: d3b2d4a Previous: 618d0f8 Ratio
noop#napi-rs 60655818 ops/sec (±0.27%) 45447890 ops/sec (±0.28%) 0.75
noop#JavaScript 591261192 ops/sec (±0.4%) 590595038 ops/sec (±0.23%) 1.00
Plus number#napi-rs 21526750 ops/sec (±0.3%) 16263518 ops/sec (±1.15%) 0.76
Plus number#JavaScript 591219796 ops/sec (±0.34%) 579851743 ops/sec (±0.92%) 0.98
Create buffer#napi-rs 454738 ops/sec (±6.87%) 336864 ops/sec (±10.45%) 0.74
Create buffer#JavaScript 1953351 ops/sec (±5.59%) 1515749 ops/sec (±4.34%) 0.78
createArray#createArrayJson 43472 ops/sec (±0.11%) 31565 ops/sec (±0.47%) 0.73
createArray#create array for loop 8245 ops/sec (±0.09%) 6354 ops/sec (±0.15%) 0.77
createArray#create array with serde trait 8238 ops/sec (±0.09%) 6415 ops/sec (±0.15%) 0.78
getArrayFromJs#get array from json string 17963 ops/sec (±0.11%) 13919 ops/sec (±0.4%) 0.77
getArrayFromJs#get array from serde 10796 ops/sec (±0.11%) 8343 ops/sec (±0.1%) 0.77
getArrayFromJs#get array with for loop 12403 ops/sec (±0.33%) 9978 ops/sec (±0.25%) 0.80
Get Set property#Get Set from native#u32 368469 ops/sec (±4.37%) 336441 ops/sec (±5.89%) 0.91
Get Set property#Get Set from JavaScript#u32 344705 ops/sec (±4%) 304214 ops/sec (±5.45%) 0.88
Get Set property#Get Set from native#string 354188 ops/sec (±4.35%) 319101 ops/sec (±5.48%) 0.90
Get Set property#Get Set from JavaScript#string 312187 ops/sec (±4.24%) 292832 ops/sec (±5.28%) 0.94
Async task#spawn task 36428 ops/sec (±0.92%) 28072 ops/sec (±2.74%) 0.77
Async task#ThreadSafeFunction 3052 ops/sec (±3.1%) 1909 ops/sec (±17.73%) 0.63
Async task#Tokio future to Promise 30898 ops/sec (±3.18%) 24707 ops/sec (±3.07%) 0.80
Query#query * 100 2015 ops/sec (±6%) 1664 ops/sec (±4.26%) 0.83
Query#query * 1 32563 ops/sec (±0.43%) 24647 ops/sec (±1.72%) 0.76

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.