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

New addConstant allows lua to modify the "constant" #273

Open
kunitoki opened this issue Dec 4, 2021 · 3 comments
Open

New addConstant allows lua to modify the "constant" #273

kunitoki opened this issue Dec 4, 2021 · 3 comments

Comments

@kunitoki
Copy link

kunitoki commented Dec 4, 2021

Is this intended? In addConstant what you are pushing is just a lua variable, not a constant as you are not registering a setter function erroring if called. You need to use addProperty with isWritable = false. I could take your example with the enum, registering it with the addConstant then in lua you could modify the enum:

    enum class A { a, b, c, d };

    luabridge::getGlobalNamespace(lua_state)
        .beginNamespace("A")
            .addConstant("a", A::a)
            .addConstant("b", A::b)
            .addConstant("c", A::c)
            .addConstant("d", A::d)
        .endNamespace();

    runLua("A.a = 2; result = A.a");
    ASSERT_EQ(A::c, static_cast<A>(result<int>())); // Passes !!!!
@kunitoki kunitoki changed the title New addConstant allows lua to modify the constant New addConstant allows lua to modify the "constant" Dec 4, 2021
@dmitry-t
Copy link
Collaborator

dmitry-t commented Dec 7, 2021

My bad. I had to ask for unit tests at least.

@kunitoki
Copy link
Author

kunitoki commented Dec 8, 2021

This seems to me more of a addVariable function, where the argument is stored as lua variable by copy directly, instead of being registered as property with getters and setters (and their corresponding metatables) so it is still useful and should produce leaner and faster lua access. Just a rename should be enough i think (plus unit tests of course).

@Azuna1
Copy link

Azuna1 commented Jan 22, 2022

So to binding Enums we could instead do smth like this i suppose:

 enum class A { a, b, c, d };

    luabridge::getGlobalNamespace(lua_state)
        .beginNamespace("A")
            .addProperty("a", []() {return static_cast<uint8_t>(A::a))
            .addProperty("b", []() {return static_cast<uint8_t>(A::b))
            .addProperty("c", []() {return static_cast<uint8_t>(A::c))
            .addProperty("d", []() {return static_cast<uint8_t>(A::d))
        .endNamespace();

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

3 participants