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

exportFn fails during semantic analysis #7

Closed
Tracked by #32
galli-miro opened this issue Feb 16, 2023 · 7 comments
Closed
Tracked by #32

exportFn fails during semantic analysis #7

galli-miro opened this issue Feb 16, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@galli-miro
Copy link

I've tried to build a simple shared module using the exportFn in a comptime block. Using zig version v0.11.0-dev.1638+7199d7c77 it fails with the following error:

/home/galli/repos/git.sr.ht/~hubidubi/river-lua/zig/lib/ziglua/src/ziglua-5.4/lib.zig:2021:13: error: TODO implement exporting arbitrary Value objects
    @export(declaration, .{ .name = "luaopen_" ++ name, .linkage = .Strong });

Since there is a TODO, I'm assuming this will be fixed at some point. But git blame tells me this TODO has been there for over 2 years, so who knows when that will happen.

build.zig:

const std = @import("std");
const ziglua = @import("lib/ziglua/build.zig");

pub fn build(b: *std.Build) void {
    // Standard release options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addSharedLibrary(.{
        .name = "test",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    lib.addModule("ziglua", ziglua.compileAndCreateModule(b, lib, .{ .shared = true }));
    lib.install();
}

main.zig:

const std = @import("std");
const ziglua = @import("ziglua");

usingnamespace ziglua;

fn hello(lua: *ziglua.Lua) i32 {
    _ = lua;
    std.debug.print("hello world\n", .{});
    return 1;
}

fn module(lua: *ziglua.Lua) i32 {
    var fns = [_]ziglua.FnReg{ .{ .name = "hello", .func = ziglua.wrap(hello) }, .{ .name = "", .func = null } };
    lua.newLib(fns[0..]);
    return 1;
}

comptime {
    ziglua.exportFn("hello", module);
}

My current fix is to do the exporting and wrapping by hand, but maybe I'm missing something:

export fn luaopen_module(L: *ziglua.LuaState) callconv(.C) c_int {
    var fns = [_]ziglua.FnReg{ .{ .name = "hello", .func = ziglua.wrap(hello) }, .{ .name = "", .func = null } };

    var lua = ziglua.Lua{ .state = L };
    lua.newLib(fns[0..]);

    return 1;
}
@natecraddock
Copy link
Owner

Hi! Thanks for reporting. I'll look into this later today. I haven't tested exportFn() with the newest version of Zig, and it has always been a bit messy.

@natecraddock
Copy link
Owner

I looked into this a bit more. This is indeed an issue with Zig. I might try fixing this in Zig (sounds like a small but fun challenge!)

Until then, your solution is the best way. I'll be sure to document this better.

Also, because this is Zig and we can know the size of arrays passed into functions, no need to end the FnReg array with a sentinel .{ .name = "", .func = null }.

@galli-miro
Copy link
Author

Thanks for having a closer look and also for the tip with the array :)

@hryx
Copy link
Contributor

hryx commented Mar 6, 2023

There is now a work-in-progress PR that should address this: ziglang/zig#14796

@natecraddock
Copy link
Owner

The Zig MR is now merged, so I'll look into this in the next couple days

@natecraddock
Copy link
Owner

It looks like the PR only partially fixed this. There is no longer a semantic analysis error, and the code compiles. But the symbol is not found in the output object. I'll look again at this later

@natecraddock natecraddock added the bug Something isn't working label Dec 29, 2023
@natecraddock natecraddock mentioned this issue Dec 29, 2023
10 tasks
@natecraddock
Copy link
Owner

I found another Zig / Lua bindings project that seems to have solved this. Linking here to check on it later: https://github.com/truemedian/lunaro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants