Skip to content

Conversation

@nurpax
Copy link
Contributor

@nurpax nurpax commented Sep 14, 2025

No description provided.

@nurpax
Copy link
Contributor Author

nurpax commented Sep 14, 2025

CI failed.. Forced pushed to update.

Imagine if both of these paths worked on all Lua versions lol

    if (zlua.lang == .lua51 or zlua.lang == .luau or zlua.lang == .luajit) {
        lua.newTable();
        lua.registerFns("fnregs", funcs);
    } else {
        lua.newLib(zlua.fnRegsFromStruct(MyLib));
        lua.setGlobal("fnregs");
    }

@robbielyman
Copy link
Collaborator

love this!! really excited to add it :)

@robbielyman robbielyman merged commit 39f8df5 into natecraddock:main Sep 14, 2025
12 checks passed
lua.registerFns("fnregs", funcs);
} else {
lua.newLib(zlua.fnRegsFromStruct(MyLib));
lua.newLib(comptime zlua.fnRegsFromType(MyLib));
Copy link
Contributor Author

@nurpax nurpax Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Is moving the comptime keyword to caller site the more idiomatic way in Zig? I guess I'm used to doing it the way I did it in the original version.

For example, I see comptime used in the library, not at call time, like in the initCompTime function here:

https://ziglang.org/documentation/master/std/#src/std/static_string_map.zig

I don't have a super strong opinion about, but I guess I have a preference for version that doesn't require the callsite to mark the call comptime.

BTW, I intentionally had this working only for structs because I didn't think this sort of "get decls from a namespace" makes so much sense e.g. in enums or unions. Or it may at least be that some public funcs in an enum likely aren't zlua wrappable. In this case, requiring a struct and giving an error may guide the user of this library to the right path. OTOH, if you had some cool uses in mind, then being overly restrictive is not that great idea either!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, i think what you did was right! the reason for comptime here is actually to move it to a testable part of the API contract. sorry for also unrelatedly fiddling with the implementation a little—I liked proving to myself that my code was equivalent to yours even though it doesn't have an explicitly comptime block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be willing to accept a PR that moves comptime back to ziglua so that users of this API don't need to explicitly add comptime? That seems to be the way it's usually done in std library at least.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would, if you insist, but I claim that you are misunderstanding what I am saying.

  • the use of the comptime keyword in the caller here makes no difference and does not need to be done in consumer code.
  • the purpose of the comptime keyword in the test is so that in the future if the function body is altered to include runtime code, CI will fail
  • the code in the body of the function is executed entirely at compile time currently, even though there is no explicit comptime block. please read the function carefully: the main body of work that it does constructs a []const FnReg by modifying a comptime var. All of the code up to const final = funcs; therefore must be run at compile time.

Copy link
Contributor Author

@nurpax nurpax Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! Thanks for clarifying and sorry for the confusion. I had indeed misunderstood what you said before. In this case there's no need to follow up with a PR. :)

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

Successfully merging this pull request may close these issues.

2 participants