@@ -2,10 +2,10 @@ use super::{from::FromScript, into::IntoScript, namespace::Namespace};
2
2
use crate :: {
3
3
bindings:: {
4
4
function:: from:: { Mut , Ref , Val } ,
5
- ReflectReference , WorldGuard ,
5
+ ReflectReference , ThreadWorldContainer , WorldContainer , WorldGuard ,
6
6
} ,
7
7
error:: InteropError ,
8
- ScriptValue , WorldCallbackAccess ,
8
+ ScriptValue ,
9
9
} ;
10
10
use bevy:: {
11
11
prelude:: { Reflect , Resource } ,
@@ -96,7 +96,8 @@ macro_rules! register_tuple_dependencies {
96
96
}
97
97
98
98
no_type_dependencies ! ( InteropError ) ;
99
- self_type_dependency_only ! ( WorldCallbackAccess , CallerContext , ReflectReference ) ;
99
+ no_type_dependencies ! ( WorldGuard <' static >) ;
100
+ self_type_dependency_only ! ( FunctionCallContext , ReflectReference ) ;
100
101
101
102
recursive_type_dependencies ! (
102
103
( Val <T > where T : GetTypeRegistration ) ,
@@ -118,9 +119,21 @@ pub trait GetFunctionTypeDependencies<Marker> {
118
119
/// Functions can choose to react to caller preferences such as converting 1-indexed numbers to 0-indexed numbers
119
120
#[ derive( Clone , Copy , Debug , Reflect , Default ) ]
120
121
#[ reflect( opaque) ]
121
- pub struct CallerContext {
122
+ pub struct FunctionCallContext {
122
123
pub convert_to_0_indexed : bool ,
123
124
}
125
+ impl FunctionCallContext {
126
+ pub fn new ( convert_to_0_indexed : bool ) -> Self {
127
+ Self {
128
+ convert_to_0_indexed,
129
+ }
130
+ }
131
+
132
+ /// Tries to access the world, returning an error if the world is not available
133
+ pub fn world ( & self ) -> Result < WorldGuard < ' static > , InteropError > {
134
+ ThreadWorldContainer . try_get_world ( )
135
+ }
136
+ }
124
137
125
138
#[ derive( Clone , Debug , PartialEq , Default ) ]
126
139
pub struct FunctionInfo {
@@ -146,12 +159,7 @@ impl FunctionInfo {
146
159
pub struct DynamicScriptFunction {
147
160
pub info : FunctionInfo ,
148
161
// TODO: info about the function, this is hard right now because of non 'static lifetimes in wrappers, we can't use TypePath etc
149
- func : Arc <
150
- dyn Fn ( CallerContext , WorldCallbackAccess , Vec < ScriptValue > ) -> ScriptValue
151
- + Send
152
- + Sync
153
- + ' static ,
154
- > ,
162
+ func : Arc < dyn Fn ( FunctionCallContext , Vec < ScriptValue > ) -> ScriptValue + Send + Sync + ' static > ,
155
163
}
156
164
157
165
impl PartialEq for DynamicScriptFunction {
@@ -167,10 +175,7 @@ pub struct DynamicScriptFunctionMut {
167
175
func : Arc <
168
176
RwLock <
169
177
// I'd rather consume an option or something instead of having the RWLock but I just wanna get this release out
170
- dyn FnMut ( CallerContext , WorldCallbackAccess , Vec < ScriptValue > ) -> ScriptValue
171
- + Send
172
- + Sync
173
- + ' static ,
178
+ dyn FnMut ( FunctionCallContext , Vec < ScriptValue > ) -> ScriptValue + Send + Sync + ' static ,
174
179
> ,
175
180
> ,
176
181
}
@@ -188,13 +193,11 @@ impl DynamicScriptFunction {
188
193
pub fn call < I : IntoIterator < Item = ScriptValue > > (
189
194
& self ,
190
195
args : I ,
191
- world : WorldGuard ,
192
- context : CallerContext ,
196
+ context : FunctionCallContext ,
193
197
) -> Result < ScriptValue , InteropError > {
194
198
let args = args. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
195
- let world_callback_access = WorldCallbackAccess :: from_guard ( world. clone ( ) ) ;
196
199
// should we be inlining call errors into the return value?
197
- let return_val = ( self . func ) ( context, world_callback_access , args) ;
200
+ let return_val = ( self . func ) ( context, args) ;
198
201
match return_val {
199
202
ScriptValue :: Error ( e) => Err ( InteropError :: function_interop_error (
200
203
self . name ( ) ,
@@ -237,14 +240,12 @@ impl DynamicScriptFunctionMut {
237
240
pub fn call < I : IntoIterator < Item = ScriptValue > > (
238
241
& self ,
239
242
args : I ,
240
- world : WorldGuard ,
241
- context : CallerContext ,
243
+ context : FunctionCallContext ,
242
244
) -> Result < ScriptValue , InteropError > {
243
245
let args = args. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
244
- let world_callback_access = WorldCallbackAccess :: from_guard ( world. clone ( ) ) ;
245
246
// should we be inlining call errors into the return value?
246
247
let mut write = self . func . write ( ) ;
247
- let return_val = ( write) ( context, world_callback_access , args) ;
248
+ let return_val = ( write) ( context, args) ;
248
249
match return_val {
249
250
ScriptValue :: Error ( e) => Err ( InteropError :: function_interop_error (
250
251
self . name ( ) ,
@@ -297,10 +298,7 @@ impl std::fmt::Debug for DynamicScriptFunctionMut {
297
298
298
299
impl < F > From < F > for DynamicScriptFunction
299
300
where
300
- F : Fn ( CallerContext , WorldCallbackAccess , Vec < ScriptValue > ) -> ScriptValue
301
- + Send
302
- + Sync
303
- + ' static ,
301
+ F : Fn ( FunctionCallContext , Vec < ScriptValue > ) -> ScriptValue + Send + Sync + ' static ,
304
302
{
305
303
fn from ( fn_ : F ) -> Self {
306
304
DynamicScriptFunction {
@@ -313,10 +311,7 @@ where
313
311
314
312
impl < F > From < F > for DynamicScriptFunctionMut
315
313
where
316
- F : FnMut ( CallerContext , WorldCallbackAccess , Vec < ScriptValue > ) -> ScriptValue
317
- + Send
318
- + Sync
319
- + ' static ,
314
+ F : FnMut ( FunctionCallContext , Vec < ScriptValue > ) -> ScriptValue + Send + Sync + ' static ,
320
315
{
321
316
fn from ( fn_ : F ) -> Self {
322
317
DynamicScriptFunctionMut {
@@ -527,52 +522,43 @@ macro_rules! impl_script_function {
527
522
// FnMut(T1...Tn) -> O
528
523
impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : -> O => O ) ;
529
524
530
- // Fn(WorldCallbackAccess, T1...Tn) -> O
531
- impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : , ( callback: WorldCallbackAccess ) -> O => O ) ;
532
- // FnMut(WorldCallbackAccess, T1...Tn) -> O
533
- impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : , ( callback: WorldCallbackAccess ) -> O => O ) ;
534
-
535
- // Fn(CallerContext, WorldCallbackAccess, T1...Tn) -> O
536
- impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : ( context: CallerContext ) , ( callback: WorldCallbackAccess ) -> O => O ) ;
537
- // FnMut(CallerContext, WorldCallbackAccess, T1...Tn) -> O
538
- impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : ( context: CallerContext ) , ( callback: WorldCallbackAccess ) -> O => O ) ;
525
+ // Fn(CallerContext, T1...Tn) -> O
526
+ impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : ( context: FunctionCallContext ) -> O => O ) ;
527
+ // FnMut(FunctionCallContext, T1...Tn) -> O
528
+ impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : ( context: FunctionCallContext ) -> O => O ) ;
539
529
540
530
// Fn(T1...Tn) -> Result<O, InteropError>
541
531
impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : -> O => Result <O , InteropError > where s) ;
542
532
// FnMut(T1...Tn) -> Result<O, InteropError>
543
533
impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : -> O => Result <O , InteropError > where s) ;
544
534
545
- // Fn(WorldCallbackAccess, T1...Tn) -> Result<O, InteropError>
546
- impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : , ( callback: WorldCallbackAccess ) -> O => Result <O , InteropError > where s) ;
547
- // FnMut(WorldCallbackAccess, T1...Tn) -> Result<O, InteropError>
548
- impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : , ( callback: WorldCallbackAccess ) -> O => Result <O , InteropError > where s) ;
549
-
550
- // Fn(CallerContext, WorldCallbackAccess, T1...Tn) -> Result<O, InteropError>
551
- impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : ( context: CallerContext ) , ( callback: WorldCallbackAccess ) -> O => Result <O , InteropError > where s) ;
552
- // FnMut(CallerContext, WorldCallbackAccess, T1...Tn) -> Result<O, InteropError>
553
- impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : ( context: CallerContext ) , ( callback: WorldCallbackAccess ) -> O => Result <O , InteropError > where s) ;
535
+ // Fn(FunctionCallContext, WorldGuard<'w>, T1...Tn) -> Result<O, InteropError>
536
+ impl_script_function!( @ ScriptFunction Fn DynamicScriptFunction into_dynamic_script_function $( $param ) ,* : ( context: FunctionCallContext ) -> O => Result <O , InteropError > where s) ;
537
+ // FnMut(FunctionCallContext, WorldGuard<'w>, T1...Tn) -> Result<O, InteropError>
538
+ impl_script_function!( @ ScriptFunctionMut FnMut DynamicScriptFunctionMut into_dynamic_script_function_mut $( $param ) ,* : ( context: FunctionCallContext ) -> O => Result <O , InteropError > where s) ;
554
539
555
540
556
541
} ;
557
542
558
- ( @ $trait_type: ident $fn_type: ident $dynamic_type: ident $trait_fn_name: ident $( $param: ident ) ,* : $( ( $context: ident: $contextty: ty) ) ? $ ( , ( $callback : ident : $callbackty : ty ) ) ? -> O => $res: ty $( where $out: ident) ?) => {
543
+ ( @ $trait_type: ident $fn_type: ident $dynamic_type: ident $trait_fn_name: ident $( $param: ident ) ,* : $( ( $context: ident: $contextty: ty) ) ? -> O => $res: ty $( where $out: ident) ?) => {
559
544
#[ allow( non_snake_case) ]
560
545
impl <
561
546
' env,
547
+ ' w : ' static ,
562
548
$( $param: FromScript , ) *
563
549
O ,
564
550
F
565
551
> $trait_type<' env,
566
- fn ( $( $contextty, ) ? $( $callbackty , ) ? $ ( $param ) ,* ) -> $res
552
+ fn ( $( $contextty, ) ? $( $param ) ,* ) -> $res
567
553
> for F
568
554
where
569
555
O : IntoScript + TypePath + GetOwnership ,
570
- F : $fn_type( $( $contextty, ) ? $( $callbackty , ) ? $ ( $ param ) ,* ) -> $res + Send + Sync + ' static ,
571
- $( $param:: This <' env >: Into <$param>, ) *
556
+ F : $fn_type( $( $contextty, ) ? $( $ param ) ,* ) -> $res + Send + Sync + ' static ,
557
+ $( $param:: This <' w >: Into <$param>, ) *
572
558
{
573
559
#[ allow( unused_mut, unused_variables) ]
574
560
fn $trait_fn_name( mut self ) -> $dynamic_type {
575
- let func = ( move |caller_context: CallerContext , world : WorldCallbackAccess , args: Vec <ScriptValue > | {
561
+ let func = ( move |caller_context: FunctionCallContext , args: Vec <ScriptValue > | {
576
562
let res: Result <ScriptValue , InteropError > = ( || {
577
563
let expected_arg_count = count!( $( $param ) * ) ;
578
564
if args. len( ) < expected_arg_count {
@@ -582,8 +568,7 @@ macro_rules! impl_script_function {
582
568
} ) ) ;
583
569
}
584
570
$( let $context = caller_context; ) ?
585
- $( let $callback = world. clone( ) ; ) ?
586
- let world = world. try_read( ) ?;
571
+ let world = caller_context. world( ) ?;
587
572
world. begin_access_scope( ) ?;
588
573
let ret = {
589
574
let mut current_arg = 0 ;
@@ -593,7 +578,7 @@ macro_rules! impl_script_function {
593
578
let $param = <$param>:: from_script( arg_iter. next( ) . expect( "invariant" ) , world. clone( ) )
594
579
. map_err( |e| InteropError :: function_arg_conversion_error( current_arg. to_string( ) , e) ) ?;
595
580
) *
596
- let out = self ( $( $context, ) ? $ ( $callback , ) ? $( $param. into( ) , ) * ) ;
581
+ let out = self ( $( $context, ) ? $( $param. into( ) , ) * ) ;
597
582
$(
598
583
let $out = out?;
599
584
let out = $out;
0 commit comments