Skip to content

Commit

Permalink
Add a lifetime parameter to CallbackInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed May 24, 2020
1 parent d9e3324 commit d25fe61
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std;
use std::cell::RefCell;
use std::convert::Into;
use std::marker::PhantomData;
use std::os::raw::c_void;
use std::panic::UnwindSafe;
use neon_runtime;
use neon_runtime::raw;
Expand All @@ -20,15 +21,15 @@ use object::{Object, This};
use object::class::Class;
use result::{NeonResult, JsResult, Throw};
use self::internal::{ContextInternal, Scope, ScopeMetadata};
use std::os::raw::c_void;

#[repr(C)]
pub(crate) struct CallbackInfo {
info: raw::FunctionCallbackInfo
pub(crate) struct CallbackInfo<'a> {
info: raw::FunctionCallbackInfo,
_lifetime: PhantomData<&'a raw::FunctionCallbackInfo>,
}

impl CallbackInfo {
pub fn data<'a>(&self, env: Env) -> *mut c_void {
impl CallbackInfo<'_> {
pub fn data(&self, env: Env) -> *mut c_void {
unsafe {
let mut raw_data: *mut c_void = std::mem::zeroed();
neon_runtime::call::data(env.to_raw(), self.info, &mut raw_data);
Expand Down Expand Up @@ -448,7 +449,7 @@ impl<'a, 'b> Context<'a> for ComputeContext<'a, 'b> { }
/// The type parameter `T` is the type of the `this`-binding.
pub struct CallContext<'a, T: This> {
scope: Scope<'a, raw::HandleScope>,
info: &'a CallbackInfo,
info: &'a CallbackInfo<'a>,
phantom_type: PhantomData<T>
}

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

pub(crate) fn with<U, F: for<'b> FnOnce(CallContext<'b, T>) -> U>(env: Env, info: &'a CallbackInfo, f: F) -> U {
pub(crate) fn with<U, F: for<'b> FnOnce(CallContext<'b, T>) -> U>(env: Env, info: &'a CallbackInfo<'a>, f: F) -> U {
Scope::with(env, |scope| {
f(CallContext {
scope,
Expand Down
8 changes: 4 additions & 4 deletions src/object/class/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use types::error::convert_panics;
pub struct MethodCallback<T: Class>(pub fn(CallContext<T>) -> JsResult<JsValue>);

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

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

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

impl<T: Class> Callback<bool> for ConstructCallback<T> {
extern "C" fn invoke(env: Env, info: CallbackInfo) -> bool {
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> bool {
unsafe {
info.with_cx(env, |cx| {
let data = info.data(cx.env());
Expand Down
4 changes: 2 additions & 2 deletions src/object/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ pub(crate) trait Callback<T: Clone + Copy + Sized>: Sized {
/// Extracts the computed Rust function and invokes it. The Neon runtime
/// ensures that the computed function is provided as the extra data field,
/// wrapped as a V8 External, in the `CallbackInfo` argument.
extern "C" fn invoke(env: Env, info: CallbackInfo) -> T;
extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> T;

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

Expand Down
4 changes: 2 additions & 2 deletions src/types/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct FunctionCallback<T: Value>(pub fn(FunctionContext) -> JsResult<T>);

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

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

0 comments on commit d25fe61

Please sign in to comment.