robin/maivi

a tiny cute lua web framework

git clone https://git.robinwobin.dev/robin/maivi

robinrefactor: restructure modules3d0ef52

push-srxvuswpkqxx
1.9 KiB63 linesraw
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 {
8    var logger = fern.info().ctx("lua.print");
9    defer logger.log();
10
11    l.pushValue(-1);
12
13    if (l.isTable(-1)) {
14        l.pushNil();
15        // stack now contains: -1 => nil; -2 => table
16        while (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
19            l.pushValue(-2);
20            // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
21            const kz = try l.toString(-1);
22            const vz = l.toString(-2) catch vz: {
23                break :vz l.typeNameIndex(-2);
24            };
25            logger = logger.str(kz, vz);
26            // pop value + copy of key, leaving original key
27            l.pop(2);
28            // stack now contains: -1 => key; -2 => table
29        }
30    } else {
31        const str = try l.toString(-1);
32        l.pop(1);
33        const s = try maivi.allocator.dupe(u8, str);
34        defer maivi.allocator.free(s);
35        logger = logger.str("v", s);
36    }
37
38    l.pop(1);
39
40    return 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// }