Skip to content

Commit

Permalink
Add name to JsCallable for debugging (#9880)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak authored Jul 30, 2024
1 parent 5d11f17 commit 2aac813
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions crates/parcel_napi_helpers/src/js_callable/js_callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ use super::JsValue;
pub type MapJsParams = Box<dyn FnOnce(&Env) -> napi::Result<Vec<JsUnknown>> + 'static>;
pub type MapJsReturn<Return> = Box<dyn Fn(&Env, JsUnknown) -> napi::Result<Return> + 'static>;

/// JsCallable provides a Send + Sync wrapper around callable JavaScript functions.
/// Functions can be called from threads or the main thread.
/// Parameters and return types can be mapped by the caller.
/// JsCallable provides a Send + Sync wrapper around callable JavaScript functions
///
/// Functions can be called from threads or the main thread, while parameters and return types can
/// be mapped by the caller.
pub struct JsCallable {
#[cfg(debug_assertions)]
initial_thread: ThreadId,
name: String,
threadsafe_function: ThreadsafeFunction<MapJsParams, ErrorStrategy::Fatal>,
}

impl JsCallable {
pub fn new(callback: JsFunction) -> napi::Result<Self> {
pub fn new(callback: JsFunction, name: String) -> napi::Result<Self> {
// Store the threadsafe function on the struct
let tsfn: ThreadsafeFunction<MapJsParams, ErrorStrategy::Fatal> = callback
.create_threadsafe_function(0, |ctx: ThreadSafeCallContext<MapJsParams>| {
Expand All @@ -40,13 +42,17 @@ impl JsCallable {
Ok(Self {
#[cfg(debug_assertions)]
initial_thread: std::thread::current().id(),
name,
threadsafe_function: tsfn,
})
}

/// Construct a JsCallable from an object property
pub fn new_from_object_prop(method_name: &str, obj: &JsObject) -> napi::Result<Self> {
Self::new(obj.get_named_property(method_name)?)
Self::new(
obj.get_named_property(method_name)?,
method_name.to_string(),
)
}

/// Construct a JsCallable from an object property, binding it to the source object
Expand All @@ -55,7 +61,7 @@ impl JsCallable {
let fn_obj = jsfn.coerce_to_object()?;
let bind: JsFunction = fn_obj.get_named_property("bind")?;
let jsfn: JsFunction = bind.call(Some(&fn_obj), &[obj])?.try_into()?;
Self::new(jsfn)
Self::new(jsfn, method_name.to_string())
}

pub fn into_unref(mut self, env: &Env) -> napi::Result<Self> {
Expand All @@ -70,9 +76,10 @@ impl JsCallable {
) -> napi::Result<()> {
#[cfg(debug_assertions)]
if self.initial_thread == std::thread::current().id() {
return Err(napi::Error::from_reason(
"Cannot run threadsafe function on main thread",
));
return Err(napi::Error::from_reason(format!(
"Cannot run threadsafe function {} on main thread",
self.name
)));
}

self
Expand Down Expand Up @@ -100,9 +107,10 @@ impl JsCallable {
{
#[cfg(debug_assertions)]
if self.initial_thread == std::thread::current().id() {
return Err(napi::Error::from_reason(
"Cannot run threadsafe function on main thread",
));
return Err(napi::Error::from_reason(format!(
"Cannot run threadsafe function {} on main thread",
self.name
)));
}

let (tx, rx) = channel();
Expand Down

0 comments on commit 2aac813

Please sign in to comment.