11use super :: Value ;
22use crate :: context:: internal:: Env ;
3- use crate :: context:: { CallbackInfo , FunctionContext } ;
3+ #[ cfg( feature = "legacy-runtime" ) ]
4+ use crate :: context:: CallbackInfo ;
5+ #[ cfg( feature = "legacy-runtime" ) ]
6+ use crate :: context:: FunctionContext ;
7+ #[ cfg( feature = "legacy-runtime" ) ]
48use crate :: result:: JsResult ;
5- use crate :: types:: error:: convert_panics;
6- use crate :: types:: { Handle , JsObject , Managed } ;
9+ use crate :: types:: { Handle , Managed } ;
710use neon_runtime;
11+ #[ cfg( feature = "legacy-runtime" ) ]
812use neon_runtime:: call:: CCallback ;
913use neon_runtime:: raw;
10- use std :: mem ;
14+ # [ cfg ( feature = "legacy-runtime" ) ]
1115use std:: os:: raw:: c_void;
1216
1317pub trait ValueInternal : Managed + ' static {
@@ -28,47 +32,24 @@ pub trait ValueInternal: Managed + 'static {
2832 }
2933}
3034
35+ #[ cfg( feature = "legacy-runtime" ) ]
3136#[ repr( C ) ]
3237pub struct FunctionCallback < T : Value > ( pub fn ( FunctionContext ) -> JsResult < T > ) ;
3338
3439#[ cfg( feature = "legacy-runtime" ) ]
3540impl < T : Value > Callback < ( ) > for FunctionCallback < T > {
3641 extern "C" fn invoke ( env : Env , info : CallbackInfo < ' _ > ) {
37- unsafe {
38- info. with_cx :: < JsObject , _ , _ > ( env, |cx| {
39- let data = info. data ( env) ;
40- let dynamic_callback: fn ( FunctionContext ) -> JsResult < T > =
41- mem:: transmute ( neon_runtime:: fun:: get_dynamic_callback ( env. to_raw ( ) , data) ) ;
42- if let Ok ( value) = convert_panics ( env, || dynamic_callback ( cx) ) {
43- info. set_return ( value) ;
44- }
45- } )
46- }
47- }
42+ use crate :: types:: error:: convert_panics;
43+ use crate :: types:: JsObject ;
4844
49- fn into_ptr ( self ) -> * mut c_void {
50- self . 0 as * mut _
51- }
52- }
53-
54- #[ cfg( feature = "napi-1" ) ]
55- impl < T : Value > Callback < raw:: Local > for FunctionCallback < T > {
56- extern "C" fn invoke ( env : Env , info : CallbackInfo < ' _ > ) -> raw:: Local {
5745 unsafe {
5846 info. with_cx :: < JsObject , _ , _ > ( env, |cx| {
5947 let data = info. data ( env) ;
60- let dynamic_callback: fn ( FunctionContext ) -> JsResult < T > =
61- mem:: transmute ( neon_runtime:: fun:: get_dynamic_callback ( env. to_raw ( ) , data) ) ;
48+ let dynamic_callback: fn ( FunctionContext ) -> JsResult < T > = std:: mem:: transmute (
49+ neon_runtime:: fun:: get_dynamic_callback ( env. to_raw ( ) , data) ,
50+ ) ;
6251 if let Ok ( value) = convert_panics ( env, || dynamic_callback ( cx) ) {
63- value. to_raw ( )
64- } else {
65- // We do not have a Js Value to return, most likely due to an exception.
66- // If we are in a throwing state, constructing a Js Value would be invalid.
67- // While not explicitly written, the N-API documentation includes many examples
68- // of returning `NULL` when a native function does not return a value.
69- // Note, `raw::Local` in this context is a type alias for `*mut napi_value` and not a struct
70- // https://nodejs.org/api/n-api.html#n_api_napi_create_function
71- std:: ptr:: null_mut ( )
52+ info. set_return ( value) ;
7253 }
7354 } )
7455 }
@@ -83,6 +64,7 @@ impl<T: Value> Callback<raw::Local> for FunctionCallback<T> {
8364/// This type makes it possible to export a dynamically computed Rust function
8465/// as a pair of 1) a raw pointer to the dynamically computed function, and 2)
8566/// a static function that knows how to transmute that raw pointer and call it.
67+ #[ cfg( feature = "legacy-runtime" ) ]
8668pub ( crate ) trait Callback < T : Clone + Copy + Sized > : Sized {
8769 /// Extracts the computed Rust function and invokes it. The Neon runtime
8870 /// ensures that the computed function is provided as the extra data field,
@@ -91,8 +73,6 @@ pub(crate) trait Callback<T: Clone + Copy + Sized>: Sized {
9173
9274 /// See `invoke`. This is used by the non-n-api implementation, so that every impl for this
9375 /// trait doesn't need to provide two versions of `invoke`.
94- #[ cfg( feature = "legacy-runtime" ) ]
95- #[ doc( hidden) ]
9676 extern "C" fn invoke_compat ( info : CallbackInfo < ' _ > ) -> T {
9777 Self :: invoke ( Env :: current ( ) , info)
9878 }
@@ -103,12 +83,8 @@ pub(crate) trait Callback<T: Clone + Copy + Sized>: Sized {
10383 /// Exports the callback as a pair consisting of the static `Self::invoke`
10484 /// method and the computed callback, both converted to raw void pointers.
10585 fn into_c_callback ( self ) -> CCallback {
106- #[ cfg( feature = "napi-1" ) ]
107- let invoke = Self :: invoke;
108- #[ cfg( feature = "legacy-runtime" ) ]
109- let invoke = Self :: invoke_compat;
11086 CCallback {
111- static_callback : unsafe { mem:: transmute ( invoke as usize ) } ,
87+ static_callback : unsafe { std :: mem:: transmute ( Self :: invoke_compat as usize ) } ,
11288 dynamic_callback : self . into_ptr ( ) ,
11389 }
11490 }
0 commit comments