sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Cannot modify `sol::var` with `std::ref` from lua

Open Smertig opened this issue 3 years ago • 0 comments

Consider the following:

#include <sol/sol.hpp>

#include <cassert>

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

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

    struct X {};
    auto x = L.new_usertype<X>("X");
    int v = 1;
    x["v"] = sol::var(std::ref(v));

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

    // modify from cpp..
    v = 2;

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

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

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

Note, that last assertion fails.

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

BTW, there are tests for sol::var, but they were disabled. I hope they'll be fixed before v4.0.0 release.

Smertig avatar Nov 03 '21 07:11 Smertig