diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-05-18 16:17:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-18 16:17:53 +0200 |
commit | 26a64b4edda3c74e26ebec1d1f6af3ffa155012d (patch) | |
tree | 29a5941925e3c7a80f22f41449d3334de9611a00 /cli/napi/threadsafe_functions.rs | |
parent | b0f13560cb5a860df042649fbceb8cd728011b8b (diff) |
refactor(napi): simplify types (#19179)
This commit removes "Error" and "Result" structs from
"ext/napi". In turn all NAPI functions now return "napi_status"
instead of "napi::Result".
Diffstat (limited to 'cli/napi/threadsafe_functions.rs')
-rw-r--r-- | cli/napi/threadsafe_functions.rs | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/cli/napi/threadsafe_functions.rs b/cli/napi/threadsafe_functions.rs index 0168c98d5..c47748758 100644 --- a/cli/napi/threadsafe_functions.rs +++ b/cli/napi/threadsafe_functions.rs @@ -37,33 +37,36 @@ impl Drop for TsFn { } impl TsFn { - pub fn acquire(&mut self) -> Result { + pub fn acquire(&mut self) -> napi_status { self.thread_counter += 1; - Ok(()) + napi_ok } - pub fn release(mut self) -> Result { + pub fn release(mut self) -> napi_status { self.thread_counter -= 1; if self.thread_counter == 0 { - self + if self .tsfn_sender .unbounded_send(ThreadSafeFunctionStatus::Dead) - .map_err(|_| Error::GenericFailure)?; + .is_err() + { + return napi_generic_failure; + } drop(self); } else { forget(self); } - Ok(()) + napi_ok } - pub fn ref_(&mut self) -> Result { + pub fn ref_(&mut self) -> napi_status { self .ref_counter .fetch_add(1, std::sync::atomic::Ordering::SeqCst); - Ok(()) + napi_ok } - pub fn unref(&mut self) -> Result { + pub fn unref(&mut self) -> napi_status { let _ = self.ref_counter.fetch_update( std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst, @@ -76,7 +79,7 @@ impl TsFn { }, ); - Ok(()) + napi_ok } pub fn call(&self, data: *mut c_void, is_blocking: bool) { @@ -138,18 +141,22 @@ fn napi_create_threadsafe_function( context: *mut c_void, maybe_call_js_cb: Option<napi_threadsafe_function_call_js>, result: *mut napi_threadsafe_function, -) -> Result { - let env_ref = env.as_mut().ok_or(Error::GenericFailure)?; +) -> napi_status { + let Some(env_ref) = env.as_mut() else { + return napi_generic_failure; + }; if initial_thread_count == 0 { - return Err(Error::InvalidArg); + return napi_invalid_arg; + } + + let mut maybe_func = None; + + if let Some(value) = *func { + let Ok(func) = v8::Local::<v8::Function>::try_from(value) else { + return napi_function_expected; + }; + maybe_func = Some(v8::Global::new(&mut env_ref.scope(), func)); } - let maybe_func = func - .map(|value| { - let func = v8::Local::<v8::Function>::try_from(value) - .map_err(|_| Error::FunctionExpected)?; - Ok(v8::Global::new(&mut env_ref.scope(), func)) - }) - .transpose()?; let id = TS_FN_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst); @@ -170,35 +177,34 @@ fn napi_create_threadsafe_function( env_ref .add_threadsafe_function_ref_counter(tsfn.id, tsfn.ref_counter.clone()); - env_ref + if env_ref .threadsafe_function_sender .unbounded_send(ThreadSafeFunctionStatus::Alive) - .map_err(|_| Error::GenericFailure)?; + .is_err() + { + return napi_generic_failure; + } *result = transmute::<Box<TsFn>, _>(Box::new(tsfn)); - Ok(()) + napi_ok } #[napi_sym::napi_sym] fn napi_acquire_threadsafe_function( tsfn: napi_threadsafe_function, _mode: napi_threadsafe_function_release_mode, -) -> Result { +) -> napi_status { let tsfn: &mut TsFn = &mut *(tsfn as *mut TsFn); - tsfn.acquire()?; - - Ok(()) + tsfn.acquire() } #[napi_sym::napi_sym] fn napi_unref_threadsafe_function( _env: &mut Env, tsfn: napi_threadsafe_function, -) -> Result { +) -> napi_status { let tsfn: &mut TsFn = &mut *(tsfn as *mut TsFn); - tsfn.unref()?; - - Ok(()) + tsfn.unref() } /// Maybe called from any thread. @@ -206,10 +212,10 @@ fn napi_unref_threadsafe_function( pub fn napi_get_threadsafe_function_context( func: napi_threadsafe_function, result: *mut *const c_void, -) -> Result { +) -> napi_status { let tsfn: &TsFn = &*(func as *const TsFn); *result = tsfn.context; - Ok(()) + napi_ok } #[napi_sym::napi_sym] @@ -217,29 +223,26 @@ fn napi_call_threadsafe_function( func: napi_threadsafe_function, data: *mut c_void, is_blocking: napi_threadsafe_function_call_mode, -) -> Result { +) -> napi_status { let tsfn: &TsFn = &*(func as *const TsFn); tsfn.call(data, is_blocking != 0); - Ok(()) + napi_ok } #[napi_sym::napi_sym] fn napi_ref_threadsafe_function( _env: &mut Env, func: napi_threadsafe_function, -) -> Result { +) -> napi_status { let tsfn: &mut TsFn = &mut *(func as *mut TsFn); - tsfn.ref_()?; - Ok(()) + tsfn.ref_() } #[napi_sym::napi_sym] fn napi_release_threadsafe_function( tsfn: napi_threadsafe_function, _mode: napi_threadsafe_function_release_mode, -) -> Result { +) -> napi_status { let tsfn: Box<TsFn> = Box::from_raw(tsfn as *mut TsFn); - tsfn.release()?; - - Ok(()) + tsfn.release() } |