sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Cannot modify static `sol::property` from lua

Open Smertig opened this issue 3 years ago • 0 comments

Consider the following:

#include <sol/sol.hpp>

#include <cassert>

struct X {
    static inline int v = 1;

    static void set_var(int value) {
        X::v = value;
    }

    static int get_var() {
        return X::v;
    }
};

int main() {
    std::printf("sol: %s\n", SOL_VERSION_STRING);

    sol::state L;
    L.open_libraries();

    auto x = L.new_usertype<X>("X");
    x["v"] = sol::property(&X::set_var, X::get_var);

    // value == 1?
    L.safe_script("assert(X.v == 1)");
    assert(X::v == 1);

    // modify from cpp..
    X::v = 2;

    // value == 2?
    L.safe_script("assert(X.v == 2)");
    assert(X::v == 2);

    // modify from lua..
    L.safe_script("X.v = 3");

    // value == 3?
    L.safe_script("assert(X.v == 3)");
    assert(X::v == 3); // BOOM
}

Note, that last assertion fails.

  • Compiler: gcc/clang (trunk)
  • sol v3.2.3 (trunk)
  • Demo: https://godbolt.org/z/53eovsfMf

I think it's similar to #1267

Smertig avatar Nov 03 '21 08:11 Smertig