File tree Expand file tree Collapse file tree 2 files changed +20
-17
lines changed
bevy_mod_scripting_functions/src
languages/bevy_mod_scripting_lua/tests/data/iter Expand file tree Collapse file tree 2 files changed +20
-17
lines changed Original file line number Diff line number Diff line change @@ -302,29 +302,28 @@ pub fn register_reflect_reference_functions(
302
302
} ) . transpose ( )
303
303
} )
304
304
. register ( "iter" , |w : WorldCallbackAccess , s : ReflectReference | {
305
- let mut infinite_iter = s. into_iter_infinite ( ) ;
306
305
let world = w. try_read ( ) . expect ( "stale world" ) ;
306
+ let mut len = s. len ( world. clone ( ) ) ?. unwrap_or_default ( ) ;
307
+ let mut infinite_iter = s. into_iter_infinite ( ) ;
307
308
let iter_function = move || {
309
+ if len == 0 {
310
+ return Ok ( ScriptValue :: Unit ) ;
311
+ }
312
+
308
313
let ( next_ref, idx) = infinite_iter. next_ref ( ) ;
309
- let allocator = world. allocator ( ) ;
310
- let mut allocator = allocator. write ( ) ;
311
- let reference = ReflectReference :: new_allocated ( next_ref, & mut allocator) ;
314
+ let reference = {
315
+ let allocator = world. allocator ( ) ;
316
+ let mut allocator = allocator. write ( ) ;
317
+ ReflectReference :: new_allocated ( next_ref, & mut allocator)
318
+ } ;
312
319
let converted = ReflectReference :: into_script_ref ( reference, world. clone ( ) ) ;
320
+ // println!("idx: {idx:?}, converted: {converted:?}");
321
+ len -= 1 ;
313
322
// we stop once the reflection path is invalid
314
- match converted {
315
- Ok ( v) => Ok ( v) ,
316
- Err ( e) => {
317
- match e. inner ( ) {
318
- InteropErrorInner :: ReflectionPathError { .. } => {
319
- Ok ( ScriptValue :: Unit )
320
- } ,
321
- _ => Err ( e) ,
322
- }
323
- } ,
324
- }
323
+ converted
325
324
} ;
326
325
327
- iter_function. into_dynamic_script_function_mut ( )
326
+ Ok ( iter_function. into_dynamic_script_function_mut ( ) )
328
327
} ) ;
329
328
330
329
Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -3,9 +3,13 @@ local res = world.get_resource(res_type)
3
3
4
4
iterated_vals = {}
5
5
for i ,v in pairs (res .vec_usize ) do
6
- interated_vals [i ] = v
6
+ iterated_vals [i ] = v
7
7
end
8
8
9
+ print (" Iterated vals:" )
10
+ for i ,v in pairs (iterated_vals ) do
11
+ print (i , v )
12
+ end
9
13
assert (iterated_vals [1 ] == 1 )
10
14
assert (iterated_vals [2 ] == 2 )
11
15
assert (iterated_vals [3 ] == 3 )
You can’t perform that action at this time.
0 commit comments