Skip to content

Commit 1602786

Browse files
committed
Add a lifetime parameter to CallbackInfo
1 parent 8d35ff8 commit 1602786

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/context/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std;
66
use std::cell::RefCell;
77
use std::convert::Into;
88
use std::marker::PhantomData;
9+
use std::os::raw::c_void;
910
use std::panic::UnwindSafe;
1011
use neon_runtime;
1112
use neon_runtime::raw;
@@ -20,15 +21,15 @@ use object::{Object, This};
2021
use object::class::Class;
2122
use result::{NeonResult, JsResult, Throw};
2223
use self::internal::{ContextInternal, Scope, ScopeMetadata};
23-
use std::os::raw::c_void;
2424

2525
#[repr(C)]
26-
pub(crate) struct CallbackInfo {
27-
info: raw::FunctionCallbackInfo
26+
pub(crate) struct CallbackInfo<'a> {
27+
info: raw::FunctionCallbackInfo,
28+
_lifetime: PhantomData<&'a raw::FunctionCallbackInfo>,
2829
}
2930

30-
impl CallbackInfo {
31-
pub fn data<'a>(&self, env: Env) -> *mut c_void {
31+
impl CallbackInfo<'_> {
32+
pub fn data(&self, env: Env) -> *mut c_void {
3233
unsafe {
3334
let mut raw_data: *mut c_void = std::mem::zeroed();
3435
neon_runtime::call::data(env.to_raw(), self.info, &mut raw_data);
@@ -448,7 +449,7 @@ impl<'a, 'b> Context<'a> for ComputeContext<'a, 'b> { }
448449
/// The type parameter `T` is the type of the `this`-binding.
449450
pub struct CallContext<'a, T: This> {
450451
scope: Scope<'a, raw::HandleScope>,
451-
info: &'a CallbackInfo,
452+
info: &'a CallbackInfo<'a>,
452453
phantom_type: PhantomData<T>
453454
}
454455

@@ -458,7 +459,7 @@ impl<'a, T: This> CallContext<'a, T> {
458459
/// Indicates whether the function was called via the JavaScript `[[Call]]` or `[[Construct]]` semantics.
459460
pub fn kind(&self) -> CallKind { self.info.kind() }
460461

461-
pub(crate) fn with<U, F: for<'b> FnOnce(CallContext<'b, T>) -> U>(env: Env, info: &'a CallbackInfo, f: F) -> U {
462+
pub(crate) fn with<U, F: for<'b> FnOnce(CallContext<'b, T>) -> U>(env: Env, info: &'a CallbackInfo<'a>, f: F) -> U {
462463
Scope::with(env, |scope| {
463464
f(CallContext {
464465
scope,

src/object/class/internal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use types::error::convert_panics;
1515
pub struct MethodCallback<T: Class>(pub fn(CallContext<T>) -> JsResult<JsValue>);
1616

1717
impl<T: Class> Callback<()> for MethodCallback<T> {
18-
extern "C" fn invoke(env: Env, info: CallbackInfo) {
18+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) {
1919
unsafe {
2020
info.with_cx::<T, _, _>(env, |mut cx| {
2121
let data = info.data(cx.env());
@@ -65,7 +65,7 @@ impl ConstructorCallCallback {
6565
}
6666

6767
impl Callback<()> for ConstructorCallCallback {
68-
extern "C" fn invoke(env: Env, info: CallbackInfo) {
68+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) {
6969
unsafe {
7070
info.with_cx(env, |cx| {
7171
let data = info.data(cx.env());
@@ -87,7 +87,7 @@ impl Callback<()> for ConstructorCallCallback {
8787
pub struct AllocateCallback<T: Class>(pub fn(CallContext<JsUndefined>) -> NeonResult<T::Internals>);
8888

8989
impl<T: Class> Callback<*mut c_void> for AllocateCallback<T> {
90-
extern "C" fn invoke(env: Env, info: CallbackInfo) -> *mut c_void {
90+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> *mut c_void {
9191
unsafe {
9292
info.with_cx(env, |cx| {
9393
let data = info.data(cx.env());
@@ -112,7 +112,7 @@ impl<T: Class> Callback<*mut c_void> for AllocateCallback<T> {
112112
pub struct ConstructCallback<T: Class>(pub fn(CallContext<T>) -> NeonResult<Option<Handle<JsObject>>>);
113113

114114
impl<T: Class> Callback<bool> for ConstructCallback<T> {
115-
extern "C" fn invoke(env: Env, info: CallbackInfo) -> bool {
115+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> bool {
116116
unsafe {
117117
info.with_cx(env, |cx| {
118118
let data = info.data(cx.env());

src/object/class/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ pub(crate) trait Callback<T: Clone + Copy + Sized>: Sized {
269269
/// Extracts the computed Rust function and invokes it. The Neon runtime
270270
/// ensures that the computed function is provided as the extra data field,
271271
/// wrapped as a V8 External, in the `CallbackInfo` argument.
272-
extern "C" fn invoke(env: Env, info: CallbackInfo) -> T;
272+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> T;
273273

274274
/// See `invoke`. This is used by the non-n-api implementation, so that every impl for this
275275
/// trait doesn't need to provide two versions of `invoke`.
276276
#[cfg(feature = "legacy-runtime")]
277277
#[doc(hidden)]
278-
extern "C" fn invoke_compat(info: CallbackInfo) -> T {
278+
extern "C" fn invoke_compat(info: CallbackInfo<'_>) -> T {
279279
Self::invoke(Env::current(), info)
280280
}
281281

src/types/internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct FunctionCallback<T: Value>(pub fn(FunctionContext) -> JsResult<T>);
3333

3434
#[cfg(feature = "legacy-runtime")]
3535
impl<T: Value> Callback<()> for FunctionCallback<T> {
36-
extern "C" fn invoke(env: Env, info: CallbackInfo) {
36+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) {
3737
unsafe {
3838
info.with_cx::<JsObject, _, _>(env, |cx| {
3939
let data = info.data(env);
@@ -53,7 +53,7 @@ impl<T: Value> Callback<()> for FunctionCallback<T> {
5353

5454
#[cfg(feature = "napi-runtime")]
5555
impl<T: Value> Callback<raw::Local> for FunctionCallback<T> {
56-
extern "C" fn invoke(env: Env, info: CallbackInfo) -> raw::Local {
56+
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> raw::Local {
5757
unsafe {
5858
info.with_cx::<JsObject, _, _>(env, |cx| {
5959
let data = info.data(env);

0 commit comments

Comments
 (0)