1
1
use super :: { MetacallNull , MetacallValue } ;
2
2
use crate :: {
3
3
bindings:: { metacall_await_future, metacall_value_destroy, metacall_value_to_future} ,
4
- helpers , parsers,
4
+ parsers,
5
5
} ;
6
6
use std:: {
7
7
ffi:: c_void,
@@ -13,7 +13,7 @@ use std::{
13
13
/// and the second argument is the data that you may want to access when the function gets called.
14
14
/// Checkout [MetacallFuture resolve](MetacallFuture#method.then) or
15
15
/// [MetacallFuture reject](MetacallFuture#method.catch) for usage.
16
- pub type MetacallFutureHandler < T > = fn ( Box < dyn MetacallValue > , T ) ;
16
+ pub type MetacallFutureHandler < T > = fn ( Box < dyn MetacallValue > , T ) -> Box < dyn MetacallValue > ;
17
17
18
18
/// Represents MetacallFuture. Keep in mind that it's not supported to pass a future as an argument.
19
19
///
@@ -125,11 +125,12 @@ unsafe extern "C" fn resolver<T: 'static + Debug>(
125
125
) -> * mut c_void {
126
126
let ( resolve, _, data) = * Box :: from_raw ( upper_data as * mut MetacallFutureFFIData < T > ) ;
127
127
let user_data = std:: ptr:: read_unaligned ( data) ;
128
+ let result = ( resolve. unwrap ( ) ) (
128
129
parsers:: raw_to_metacallobj_untyped_leak :: < T > ( resolve_data) ,
129
- * user_data,
130
+ user_data,
130
131
) ;
131
132
132
- ptr :: null_mut ( )
133
+ Box :: into_raw ( result ) as * mut c_void
133
134
}
134
135
unsafe extern "C" fn rejecter < T : ' static + Debug > (
135
136
reject_data : * mut c_void ,
@@ -279,12 +280,13 @@ impl<T: 'static + Debug> MetacallFuture<T> {
279
280
///}
280
281
/// ```
281
282
pub fn data ( mut self , data : T ) -> Self {
282
- pub fn data ( mut self , data : impl MetacallValue ) -> Self {
283
+ dbg ! ( & data) ;
283
284
unsafe { drop ( Box :: from_raw ( self . data ) ) } ;
285
+ dbg ! ( & self . data) ;
284
286
self . data = Box :: into_raw ( Box :: new ( Some ( data) ) ) ;
285
-
286
- self . data = Box :: into_raw ( Box :: new ( data ) as Box < dyn MetacallValue > ) ;
287
-
287
+ unsafe {
288
+ dbg ! ( & * self . data ) ;
289
+ }
288
290
self
289
291
}
290
292
@@ -293,6 +295,9 @@ impl<T: 'static + Debug> MetacallFuture<T> {
293
295
let resolve_is_some = self . resolve . is_some ( ) ;
294
296
let reject_is_some = self . reject . is_some ( ) ;
295
297
298
+ unsafe {
299
+ dbg ! ( & * self . data) ;
300
+ }
296
301
unsafe {
297
302
metacall_value_destroy ( metacall_await_future (
298
303
metacall_value_to_future ( self . value ) ,
@@ -359,7 +364,9 @@ impl<T> Drop for MetacallFuture<T> {
359
364
mod tests {
360
365
use std:: fmt:: Debug ;
361
366
362
- use crate :: { loaders, metacall, metacall_no_arg, switch, MetacallFuture , MetacallValue } ;
367
+ use crate :: {
368
+ loaders, metacall, metacall_no_arg, switch, MetacallFuture , MetacallNull , MetacallValue ,
369
+ } ;
363
370
#[ test]
364
371
fn test_metacall_future ( ) {
365
372
let script = r#"
@@ -382,29 +389,36 @@ module.exports = {
382
389
383
390
let _metacall = switch:: initialize ( ) . unwrap ( ) ;
384
391
loaders:: from_memory ( "node" , script) . unwrap ( ) ;
385
-
386
- fn resolve < T : PartialEq < i16 > + Debug > ( result : Box < dyn MetacallValue > , data : T ) {
387
- let result = result. downcast :: < f64 > ( ) . unwrap ( ) ;
388
-
392
+ for _ in 0 ..1000000 {
393
+ fn resolve < T : PartialEq < i64 > + Debug > (
394
+ result : Box < dyn MetacallValue > ,
395
+ data : T ,
396
+ ) -> Box < dyn MetacallValue > {
397
+ let result_f64 = result. clone ( ) . downcast :: < f64 > ( ) . unwrap ( ) ;
398
+ println ! ( "data = {data:?}" ) ;
389
399
assert_eq ! (
390
- result , 2.0 ,
400
+ result_f64 , 2.0 ,
391
401
"the result should be double of the passed value"
392
402
) ;
393
403
assert_eq ! ( data, 100 , "data should be passed without change" ) ;
404
+ result
394
405
}
395
- fn reject < T > ( _: Box < dyn MetacallValue > , _: T ) {
406
+ fn reject < T > ( _: Box < dyn MetacallValue > , _: T ) -> Box < dyn MetacallValue > {
396
407
panic ! ( "It shouldnt be rejected" ) ;
397
408
}
398
- let future = metacall :: < MetacallFuture < i16 > > ( "doubleValueAfterTime" , [ 1 , 2000 ] ) . unwrap ( ) ;
409
+ let future =
410
+ metacall :: < MetacallFuture < i64 > > ( "doubleValueAfterTime" , [ 1 , 2000 ] ) . unwrap ( ) ;
399
411
future
400
- . then ( resolve :: < i16 > )
401
- . catch ( reject :: < i16 > )
412
+ . then ( resolve :: < i64 > )
413
+ . catch ( reject :: < i64 > )
402
414
. data ( 100 )
403
415
. await_fut ( ) ;
416
+ }
404
417
}
405
418
406
419
#[ test]
407
420
fn test_metacall_future_data ( ) {
421
+ let _metacall = switch:: initialize ( ) . unwrap ( ) ;
408
422
let script = r#"
409
423
function func(value, delay) {
410
424
return new Promise((resolve) => {
@@ -415,23 +429,26 @@ function func(value, delay) {
415
429
module.exports = {
416
430
func
417
431
}
418
-
419
432
"# ;
420
- let _metacall = switch:: initialize ( ) . unwrap ( ) ;
421
433
loaders:: from_memory ( "node" , script) . unwrap ( ) ;
422
-
423
- fn resolve < T : PartialEq < String > + Debug > ( _: Box < dyn MetacallValue > , data : T ) {
434
+ for _ in 0 ..1000000 {
435
+ fn resolve < T : PartialEq < String > + Debug > (
436
+ _: Box < dyn MetacallValue > ,
437
+ data : T ,
438
+ ) -> Box < dyn MetacallValue > {
424
439
assert_eq ! (
425
440
data,
426
441
String :: from( "USER_DATA" ) ,
427
442
"data should be passed without change"
428
443
) ;
444
+ Box :: new ( MetacallNull ( ) )
429
445
}
430
446
431
447
let future = metacall_no_arg :: < MetacallFuture < String > > ( "func" ) . unwrap ( ) ;
432
448
future
433
449
. then ( resolve :: < String > )
434
450
. data ( String :: from ( "USER_DATA" ) )
435
451
. await_fut ( ) ;
452
+ }
436
453
}
437
454
}
0 commit comments