[oden-js] Ensure callbacks have 'static lifetime

Because we smuggle them through we need to make sure they're not
holding references that will go invalid- we can make no promises about
how long they stay alive. Although maybe bounding to context lifetime
is OK? But anyway.
This commit is contained in:
John Doty 2023-06-21 22:02:07 -07:00
parent 14f9eb655f
commit e36ab17235
3 changed files with 8 additions and 8 deletions

View file

@ -169,14 +169,14 @@ impl ContextRef {
}
/// Construct a new value that wraps a strongly-typed closure.
pub fn new_fn<F>(&self, func: impl RustFunction<F>) -> ValueResult {
self.new_dynamic_fn(|c, _, a| func.call(c, a))
pub fn new_fn<F>(&self, func: impl RustFunction<F> + 'static) -> ValueResult {
self.new_dynamic_fn(move |c, _, a| func.call(c, a))
}
/// Construct a new value that wraps a dynamically-typed closure.
pub fn new_dynamic_fn<F>(&self, func: F) -> ValueResult
where
F: Fn(&ContextRef, &ValueRef, &[&ValueRef]) -> ValueResult,
F: 'static + Fn(&ContextRef, &ValueRef, &[&ValueRef]) -> ValueResult,
{
// Constructing a new function is complicated enough that it needs to
// be out of line.
@ -472,7 +472,7 @@ mod tests {
go.set_property(
&ctx,
"foo",
&ctx.new_dynamic_fn(|c, _, _| Ok(return_value.dup(c)))
&ctx.new_dynamic_fn(move |c, _, _| Ok(return_value.dup(c)))
.unwrap(),
)
.unwrap();