sol2
sol2 copied to clipboard
Loss of idempotence of user type declarationd post v3.2.3
I am working on Fedora 37 using gcc 12.3.1 and lua 5.4.6. The following code works as expected with sol v 3.2.3.
#include "TestClass.h"
#define SOL_ALL_SAFETIES_ON 1 #include "sol/sol.hpp"
#include
int main() { std::string script { R"( print(testClass) testClass:greet() )" };
try
{
sol::state lua;
lua.open_libraries();
for(int i{0}; i<16; ++i)
{
sol::usertype<TestClass> luaUserType{lua.new_usertype<TestClass>("TestClass")};
luaUserType.set("TestClass", sol::constructors<TestClass()>());
luaUserType.set("greet", &TestClass::greet);
TestClass testClass{};
lua["testClass"] = &testClass;
auto result{lua.safe_script(script)};
}
}
catch( const sol::error& e )
{
std::cout << "An unexpected error has occurred: " << e.what() << std::endl;
}
}
The output from this code is as follows: sol.TestClass*: 0x2167dc8 Hello World!!! userdata: 0x2169f98 Hello World!!! userdata: 0x216b188 Hello World!!! userdata: 0x216c378 Hello World!!! userdata: 0x216d568 Hello World!!! userdata: 0x21678d8 Hello World!!! userdata: 0x2169a18 Hello World!!! userdata: 0x216adf8 Hello World!!! userdata: 0x216df18 Hello World!!! userdata: 0x216eff8 Hello World!!! userdata: 0x2170188 Hello World!!! userdata: 0x2171378 Hello World!!! userdata: 0x2172568 Hello World!!! userdata: 0x216d088 Hello World!!! userdata: 0x216c378 Hello World!!! userdata: 0x2161cd8 Hello World!!!
If I change to using sol 3.3.x or 4.x the out put from the program is:
/home/ian/projects/lua/sol/build/source/idempotence> /home/ian/projects/lua/sol/build/source/idempotence/indempotence sol.TestClass*: 0x7e2cd8 Hello World!!! sol.TestClass*: 0x7e5718 Hello World!!! sol.TestClass*: 0x7e7178 Hello World!!! sol.TestClass*: 0x7e2998 An unexpected error has occurred: sol: runtime error: [string "..."]:3: attempt to index a sol.TestClass* value (global 'testClass') stack traceback: [string "..."]:3: in main chunk [sol2] An error occurred and has been passed to an error handler: sol: runtime error: [string "..."]:3: attempt to index a sol.TestClass* value (global 'testClass') stack traceback: [string "..."]:3: in main chunk
I have seen similar code failing the first time the user type was re-declared. An other instance it was after 10 re-declarations.
My expectations is that the example code should work regardless of the number of re-declarations. I.e. it should be idempotent.
The same results are seen in Fedora 38 with gcc 13.2.1
Creating multiple non-shared environments (i.e., without a parent) and then creating new usertypes within them results in quite similar behavior
Duplicate of #1492