sol2
sol2 copied to clipboard
Crash on metatable method error
I'm using Lua 5.4.4 and Sol 3.3.0. The following program crashes with an uncaught exception, despite my handler.
sol::state lua;
lua.open_libraries (sol::lib::base);
lua.script (R"(
t = setmetatable({}, {
__newindex = function() error('ouch!') end
})
)");
try {
lua["t"]["x"] = 10;
} catch (const std::exception& e) {
std::cout << "The error is: " << e.what() << "\n";
}
The exception is attempt to call a nil value, and it seems to originate in basic_table_core::traverse_set when the push_popper_n goes out of scope, i.e. while the stack is unwinding.
Catching a Lua error as an exception is always tricky, and exception-related mechanisms have been broken for a long time now in sol2 due to the many different issues with triggering unwinding through a C-style library.
As a first question, did you compile Lua-as-C++, and did you enable SOL_USING_CXX_LUA as a preprocessor define for the whole build?
Version in branch develop seems to be ok with this code.