robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
3d0ef52
main
1const fern =@import ( "fern" ); 2const zlua =@import ( "zlua" ); 3pub const Lua =zlua . Lua ; 4 5const maivi =@import ( "../root.zig" ); 6 7pub fn lua_print ( l : * Lua ) ! i32 { 8var logger =fern . info (). ctx ( "lua.print" ); 9defer logger . log (); 10 11l . pushValue ( - 1 ); 12 13if ( l . isTable ( - 1 )) { 14l . pushNil (); 15// stack now contains: -1 => nil; -2 => table 16while ( l . next ( - 2 )) { 17// stack now contains: -1 => value; -2 => key; -3 => table 18// copy the key so that toString does not modify the original 19l . pushValue ( - 2 ); 20// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table 21const kz =try l . toString ( - 1 ); 22const vz =l . toString ( - 2 ) catch vz : { 23break : vz l . typeNameIndex ( - 2 ); 24}; 25logger = logger . str ( kz , vz ); 26// pop value + copy of key, leaving original key 27l . pop ( 2 ); 28// stack now contains: -1 => key; -2 => table 29} 30} else { 31const str =try l . toString ( - 1 ); 32l . pop ( 1 ); 33const s =try maivi . allocator . dupe ( u8 , str ); 34defer maivi . allocator . free ( s ); 35logger = logger . str ( "v" , s ); 36} 37 38l . pop ( 1 ); 39 40return 0 ; 41} 42 43// fn get_keys(l: *zlua.Lua, keys: *std.ArrayList([]const u8)) !void { 44// l.pushValue(-1); 45// 46// if (!l.isTable(-1)) return error.NotATable; 47// 48// l.pushNil(); 49// // stack now contains: -1 => nil; -2 => table 50// while (l.next(-2)) { 51// // stack now contains: -1 => value; -2 => key; -3 => table 52// // copy the key so that toString does not modify the original 53// l.pushValue(-2); 54// // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table 55// const kz = try l.toString(-1); 56// keys.appendBounded(kz) catch continue; 57// // pop value + copy of key, leaving original key 58// l.pop(2); 59// // stack now contains: -1 => key; -2 => table 60// } 61// 62// l.pop(1); 63// }