LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

New addConstant allows lua to modify the "constant"

Open kunitoki opened this issue 3 years ago • 3 comments

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 avatar Dec 04 '21 08:12 kunitoki

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

dmitry-t avatar Dec 07 '21 23:12 dmitry-t

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).

kunitoki avatar Dec 08 '21 10:12 kunitoki

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();

Azuna1 avatar Jan 22 '22 09:01 Azuna1