Skip to content

Commit

Permalink
add PropOptions::set_with
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Sep 26, 2024
1 parent 0e2ba9e commit 02cebcf
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/neon/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::{
function::{BindOptions, CallOptions},
private::ValueInternal,
utf8::Utf8,
JsFunction, JsObject, JsUndefined, JsValue, Value,
JsFunction, JsUndefined, JsValue, Value,
},
};

Expand Down Expand Up @@ -229,6 +229,20 @@ where
Ok(self)
}

/// Sets the property on the object to a value computed from a closure.
/// Equivalent to calling `obj.set(cx, f(cx).try_into_js(cx)?)`.
///
/// May throw an exception either during converting the value or setting the property.
pub fn set_with<R, F>(&mut self, f: F) -> NeonResult<&mut Self>
where
R: TryIntoJs<'cx>,
F: FnOnce(&mut Cx<'cx>) -> R,
{
let v = f(self.cx).try_into_js(self.cx)?;
self.this.set(self.cx, self.key, v)?;
Ok(self)
}

/// Gets the property from the object as a method and binds `this` to the object.
///
/// May throw an exception either during accessing the property or downcasting it
Expand Down

0 comments on commit 02cebcf

Please sign in to comment.