@@ -392,6 +392,50 @@ impl UserData for LuaReflectReference {
392392 }
393393 } ) ;
394394
395+ m. add_method ( "ipairs_clone" , |lua, s : & LuaReflectReference , _args : ( ) | {
396+ profiling:: function_scope!( "ipairs_clone" ) ;
397+ let world = ThreadWorldContainer
398+ . try_get_world ( )
399+ . map_err ( IntoMluaError :: to_lua_error) ?;
400+
401+ let iter_func = world
402+ . lookup_function ( [ TypeId :: of :: < ReflectReference > ( ) ] , "iter_clone" )
403+ . map_err ( |f| {
404+ InteropError :: missing_function ( f, TypeId :: of :: < ReflectReference > ( ) . into ( ) )
405+ } )
406+ . map_err ( IntoMluaError :: to_lua_error) ?;
407+
408+ let result = iter_func
409+ . call ( vec ! [ ScriptValue :: Reference ( s. clone( ) . into( ) ) ] , LUA_CALLER_CONTEXT )
410+ . map_err ( IntoMluaError :: to_lua_error) ?;
411+
412+ match result {
413+ ScriptValue :: FunctionMut ( func) => {
414+ let mut index = 0i64 ;
415+ lua. create_function_mut ( move |lua, _args : ( ) | {
416+ let result = func
417+ . call ( vec ! [ ] , LUA_CALLER_CONTEXT )
418+ . map_err ( IntoMluaError :: to_lua_error) ?;
419+
420+ match result {
421+ ScriptValue :: Unit => {
422+ // End of iteration
423+ Ok ( ( mlua:: Value :: Nil , mlua:: Value :: Nil ) )
424+ }
425+ _ => {
426+ // Return (index, value) tuple for ipairs
427+ index += 1 ;
428+ let idx = mlua:: Value :: Integer ( index) ;
429+ let val = LuaScriptValue ( result) . into_lua ( lua) ?;
430+ Ok ( ( idx, val) )
431+ }
432+ }
433+ } )
434+ }
435+ _ => Err ( mlua:: Error :: RuntimeError ( "iter_clone function did not return a FunctionMut" . to_string ( ) ) )
436+ }
437+ } ) ;
438+
395439 m. add_meta_function ( MetaMethod :: ToString , |_, self_ : LuaReflectReference | {
396440 profiling:: function_scope!( "MetaMethod::ToString" ) ;
397441 let world = ThreadWorldContainer
0 commit comments