Skip to content

Commit 9b269c4

Browse files
committed
just lookup length for iteration for now
1 parent 4b7622e commit 9b269c4

File tree

2 files changed

+20
-17
lines changed
  • crates
    • bevy_mod_scripting_functions/src
    • languages/bevy_mod_scripting_lua/tests/data/iter

2 files changed

+20
-17
lines changed

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -302,29 +302,28 @@ pub fn register_reflect_reference_functions(
302302
}).transpose()
303303
})
304304
.register("iter", |w: WorldCallbackAccess, s: ReflectReference| {
305-
let mut infinite_iter = s.into_iter_infinite();
306305
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();
307308
let iter_function = move || {
309+
if len == 0 {
310+
return Ok(ScriptValue::Unit);
311+
}
312+
308313
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+
};
312319
let converted = ReflectReference::into_script_ref(reference, world.clone());
320+
// println!("idx: {idx:?}, converted: {converted:?}");
321+
len -= 1;
313322
// 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
325324
};
326325

327-
iter_function.into_dynamic_script_function_mut()
326+
Ok(iter_function.into_dynamic_script_function_mut())
328327
});
329328

330329
Ok(())

crates/languages/bevy_mod_scripting_lua/tests/data/iter/vec.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ local res = world.get_resource(res_type)
33

44
iterated_vals = {}
55
for i,v in pairs(res.vec_usize) do
6-
interated_vals[i] = v
6+
iterated_vals[i] = v
77
end
88

9+
print("Iterated vals:")
10+
for i,v in pairs(iterated_vals) do
11+
print(i, v)
12+
end
913
assert(iterated_vals[1] == 1)
1014
assert(iterated_vals[2] == 2)
1115
assert(iterated_vals[3] == 3)

0 commit comments

Comments
 (0)