Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Am I using getResource incorrectly? #6

Open
Falconerd opened this issue Nov 15, 2022 · 0 comments
Open

Am I using getResource incorrectly? #6

Falconerd opened this issue Nov 15, 2022 · 0 comments

Comments

@Falconerd
Copy link

Falconerd commented Nov 15, 2022

Hi there, thanks for making Zoltan!

I have been using Zoltan in my project but have come across an error under specific circumstances. I'm not entirely sure what the problem is, but I'll post a simple example that reproduces the issue (taken from the readme).

When the loop limit is 501, the code runs, when it's 502, there's an illegal instruction error.

const std = @import("std");
const Lua = @import("lua").Lua;
const assert = std.debug.assert;

pub fn main() !void {
    var lua = try Lua.init(std.testing.allocator);
    defer lua.destroy();

    lua.openLibs();

    var tbl = try lua.createTable();
    defer lua.release(tbl);

    lua.set("tbl", tbl);

    var inTbl = try lua.createTable();

    // Set, integer key
    inTbl.set(1, "string");
    inTbl.set(2, 3.1415);
    inTbl.set(3, 42);

    var tst1 = try inTbl.get([]const u8, 1);
    var tst2 = try inTbl.get(f32, 2);
    var tst3 = try inTbl.get(i32, 3);

    try std.testing.expect(std.mem.eql(u8, tst1, "string"));
    try std.testing.expect(tst2 == 3.1415);
    try std.testing.expect(tst3 == 42);

    // Set, string key
    inTbl.set("bool", true);

    var tst4 = try inTbl.get(bool, "bool");
    try std.testing.expect(tst4 == true);

    // Set table in parent
    tbl.set("inner", inTbl);
    // Now we can release the inTbl directly (tbl refers it)
    lua.release(inTbl);

    // Somewhere else, later.
    {
        var retTbl = try lua.getResource(Lua.Table, "tbl");
        defer lua.release(retTbl);

        var i: usize = 0;
        while (i < 502) : (i += 1) {
            var retInTbl = try retTbl.getResource(Lua.Table, "inner");
            lua.release(retInTbl);
        }
    }

    std.debug.print("Success.\n", .{});
}

The error:

Illegal instruction at address 0x20bdad
zoltan/src/lua-5.4.3/src/lapi.c:0:0: 0x20bdad in finishrawget (/home/falconerd/work/zoltan_test/zoltan/src/lua-5.4.3/src/lapi.c)
zoltan/src/lua-5.4.3/src/lauxlib.c:667:7: 0x21004f in luaL_ref (/home/falconerd/work/zoltan_test/zoltan/src/lua-5.4.3/src/lauxlib.c)
  if (lua_rawgeti(L, t, freelist) == LUA_TNIL) {  /* first access? */
      ^
/home/falconerd/work/zoltan_test/zoltan/src/lua.zig:296:41: 0x253c4e in .lua.Table.init (zoltan_app)
            const _ref = lualib.luaL_ref(_L, lualib.LUA_REGISTRYINDEX);
                                        ^
/home/falconerd/work/zoltan_test/zoltan/src/lua.zig:547:38: 0x253bdd in .lua.Lua.popResource (zoltan_app)
                        return T.init(L);
                                     ^
/home/falconerd/work/zoltan_test/zoltan/src/lua.zig:341:39: 0x24f261 in .lua.Table.getResource (zoltan_app)
            return try Lua.popResource(T, self.L);
                                      ^
/home/falconerd/work/zoltan_test/main.zig:49:50: 0x24e4c6 in main (zoltan_app)
            var retInTbl = try retTbl.getResource(Lua.Table, "inner");
                                                 ^
/home/falconerd/bin/lib/std/start.zig:561:37: 0x259377 in std.start.callMain (zoltan_app)
            const result = root.main() catch |err| {
                                    ^
/home/falconerd/bin/lib/std/start.zig:495:12: 0x24f7e7 in std.start.callMainWithArgs (zoltan_app)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/home/falconerd/bin/lib/std/start.zig:460:12: 0x24f592 in std.start.main (zoltan_app)
    return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
           ^
The following command terminated unexpectedly:
cd /home/falconerd/work/zoltan_test && /home/falconerd/work/zoltan_test/zig-out/bin/zoltan_app
error: the following build command failed with exit code 1:
/home/falconerd/work/zoltan_test/zig-cache/o/86d0b49472420f3a37b88ab0c72eee03/build /home/falconerd/bin/zig /home/falconerd/work/zoltan_test /home/falconerd/work/zoltan_test/zig-cache /home/falconerd/.cache/zig run

While this example is contrived, I ran into this error in my project while looping through a sub-table.

Any help would be greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant