Skip to content

Commit

Permalink
fix lifetimes in ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
Protryon committed Apr 6, 2020
1 parent eaa7a1e commit 0a4b6f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/function.rs
Expand Up @@ -240,8 +240,9 @@ pub type FunctionCallback = extern "C" fn(*const FunctionCallbackInfo);

impl<F> MapFnFrom<F> for FunctionCallback
where
for<'sc>
F: UnitType
+ Fn(FunctionCallbackScope, FunctionCallbackArguments, ReturnValue),
+ Fn(FunctionCallbackScope<'sc>, FunctionCallbackArguments<'sc>, ReturnValue<'sc>),
{
fn mapping() -> Self {
let f = |info: *const FunctionCallbackInfo| {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_api.rs
Expand Up @@ -1370,6 +1370,18 @@ fn data_is_true_callback(
assert!(data.is_true());
}

fn fn_callback_lifetimes_proxy<'sc>(scope: &mut impl v8::ToLocal<'sc>) -> v8::Local<'sc, v8::Value> {
v8::String::new(scope, "Hello lifetimed callback!").unwrap().into()
}

fn fn_callback_lifetimes<'sc>(
scope: v8::FunctionCallbackScope<'sc>,
args: v8::FunctionCallbackArguments<'sc>,
mut rv: v8::ReturnValue<'sc>,
) {
rv.set(fn_callback_lifetimes_proxy(scope));
}

#[test]
fn function() {
let _setup_guard = setup();
Expand Down Expand Up @@ -1419,6 +1431,14 @@ fn function() {
function
.call(scope, context, recv, &[])
.expect("Function call failed");
let function = v8::Function::new(scope, context, fn_callback_lifetimes)
.expect("Unable to create function");
let value = function
.call(scope, context, recv, &[])
.expect("Function call failed");
let value_str: v8::Local<v8::String> = value.try_into().unwrap();
let value_str = value_str.to_rust_string_lossy(scope);
assert_eq!(value_str, "Hello lifetimed callback!");
}
}

Expand Down

0 comments on commit 0a4b6f2

Please sign in to comment.