Centralize the extraction of Lua values and fix bug when trying to extract the first parameter of a Lua parameter pack

This commit is contained in:
Alexander Bock
2023-03-07 01:56:17 +01:00
parent 14cf1d3962
commit 0831b00a77
75 changed files with 43 additions and 438 deletions

View File

@@ -45,47 +45,6 @@ int DoubleListProperty::typeLua() const {
return LUA_TTABLE;
}
std::vector<double> DoubleListProperty::fromLuaConversion(lua_State* state,
bool& success) const
{
if (!lua_istable(state, -1)) {
success = false;
LERRORC(className(), "Conversion from Lua failed. The input was not a table");
return {};
}
std::vector<double> result;
lua_pushnil(state);
while (lua_next(state, -2) != 0) {
if (lua_isnumber(state, -1)) {
result.emplace_back(lua_tonumber(state, -1));
}
else {
success = false;
LERRORC(
className(),
"Conversion from Lua failed. The input table contains non-number values"
);
return {};
}
lua_pop(state, 1);
}
success = true;
return result;
}
void DoubleListProperty::toLuaConversion(lua_State* state) const {
lua_createtable(state, static_cast<int>(_value.size()), 0);
int i = 1;
for (double v : _value) {
ghoul::lua::push(state, i);
ghoul::lua::push(state, v);
lua_settable(state, -3);
++i;
}
}
std::string DoubleListProperty::toStringConversion() const {
nlohmann::json json(_value);
return json.dump();

View File

@@ -44,47 +44,6 @@ int IntListProperty::typeLua() const {
return LUA_TTABLE;
}
std::vector<int> IntListProperty::fromLuaConversion(lua_State* state,
bool& success) const
{
if (!lua_istable(state, -1)) {
success = false;
LERRORC(className(), "Conversion from Lua failed. The input was not a table");
return {};
}
std::vector<int> result;
lua_pushnil(state);
while (lua_next(state, -2) != 0) {
if (lua_isnumber(state, -1)) {
result.emplace_back(static_cast<int>(lua_tonumber(state, -1)));
}
else {
success = false;
LERRORC(
className(),
"Conversion from Lua failed. The input table contains non-number values"
);
return {};
}
lua_pop(state, 1);
}
success = true;
return result;
}
void IntListProperty::toLuaConversion(lua_State* state) const {
lua_createtable(state, static_cast<int>(_value.size()), 0);
int i = 1;
for (int v : _value) {
ghoul::lua::push(state, i);
ghoul::lua::push(state, v);
lua_settable(state, -3);
++i;
}
}
std::string IntListProperty::toStringConversion() const {
nlohmann::json json(_value);
return json.dump();

View File

@@ -45,43 +45,6 @@ int StringListProperty::typeLua() const {
return LUA_TTABLE;
}
std::vector<std::string> StringListProperty::fromLuaConversion(lua_State* state,
bool& success) const
{
if (!lua_istable(state, -1)) {
success = false;
LERRORC(className(), "Conversion from Lua failed. The input was not a table");
return {};
}
std::vector<std::string> result;
lua_pushnil(state);
while (lua_next(state, -2) != 0) {
if (lua_isstring(state, -1)) {
result.emplace_back(lua_tostring(state, -1));
}
else {
success = false;
return {};
}
lua_pop(state, 1);
}
success = true;
return result;
}
void StringListProperty::toLuaConversion(lua_State* state) const {
lua_createtable(state, static_cast<int>(_value.size()), 0);
int i = 1;
for (const std::string& v : _value) {
ghoul::lua::push(state, i);
ghoul::lua::push(state, v.c_str());
lua_settable(state, -3);
++i;
}
}
std::string StringListProperty::toStringConversion() const {
nlohmann::json json(_value);
return json.dump();

View File

@@ -49,8 +49,4 @@ int DMat2Property::typeLua() const {
return LUA_TTABLE;
}
glm::dmat2x2 DMat2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::dmat2x2>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int DMat3Property::typeLua() const {
return LUA_TTABLE;
}
glm::dmat3x3 DMat3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::mat3x3>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int DMat4Property::typeLua() const {
return LUA_TTABLE;
}
glm::dmat4x4 DMat4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::dmat4x4>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int Mat2Property::typeLua() const {
return LUA_TTABLE;
}
glm::mat2x2 Mat2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::mat2x2>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int Mat3Property::typeLua() const {
return LUA_TTABLE;
}
glm::mat3x3 Mat3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::mat3x3>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int Mat4Property::typeLua() const {
return LUA_TTABLE;
}
glm::mat4x4 Mat4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::mat4x4>(state);
}
} // namespace openspace::properties

View File

@@ -41,15 +41,6 @@ int BoolProperty::typeLua() const {
return LUA_TBOOLEAN;
}
bool BoolProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isboolean(state, -1) == 1);
return success ? (lua_toboolean(state, -1) == 1) : false;
}
void BoolProperty::toLuaConversion(lua_State* state) const {
ghoul::lua::push(state, _value);
}
std::string BoolProperty::toStringConversion() const {
return _value ? "true" : "false";
}

View File

@@ -41,15 +41,4 @@ int DoubleProperty::typeLua() const {
return LUA_TNUMBER;
}
double DoubleProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
double val = lua_tonumber(state, -1);
return val;
}
else {
return 0.0;
}
}
} // namespace openspace::properties

View File

@@ -41,15 +41,4 @@ int FloatProperty::typeLua() const {
return LUA_TNUMBER;
}
float FloatProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
float val = static_cast<float>(lua_tonumber(state, -1));
return val;
}
else {
return 0.f;
}
}
} // namespace openspace::properties

View File

@@ -41,15 +41,4 @@ int IntProperty::typeLua() const {
return LUA_TNUMBER;
}
int IntProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
int val = static_cast<int>(lua_tonumber(state, -1));
return val;
}
else {
return 0;
}
}
} // namespace openspace::properties

View File

@@ -41,15 +41,4 @@ int LongProperty::typeLua() const {
return LUA_TNUMBER;
}
long LongProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
long val = static_cast<long>(lua_tonumber(state, -1));
return val;
}
else {
return 0;
}
}
} // namespace openspace::properties

View File

@@ -41,15 +41,4 @@ int ShortProperty::typeLua() const {
return LUA_TNUMBER;
}
short ShortProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
short val = static_cast<short>(lua_tonumber(state, -1));
return val;
}
else {
return 0;
}
}
} // namespace openspace::properties

View File

@@ -48,15 +48,4 @@ int UIntProperty::typeLua() const {
return LUA_TNUMBER;
}
unsigned int UIntProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
unsigned int val = static_cast<unsigned int>(lua_tonumber(state, -1));
return val;
}
else {
return 0;
}
}
} // namespace openspace::properties

View File

@@ -48,15 +48,4 @@ int ULongProperty::typeLua() const {
return LUA_TNUMBER;
}
unsigned long ULongProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
unsigned long val = static_cast<unsigned long>(lua_tonumber(state, -1));
return val;
}
else {
return 0ul;
}
}
} // namespace openspace::properties

View File

@@ -48,15 +48,4 @@ int UShortProperty::typeLua() const {
return LUA_TNUMBER;
}
unsigned short UShortProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = (lua_isnumber(state, -1) == 1);
if (success) {
unsigned short val = static_cast<unsigned short>(lua_tonumber(state, -1));
return val;
}
else {
return 0;
}
}
} // namespace openspace::properties

View File

@@ -129,44 +129,14 @@ void SelectionProperty::clearOptions() {
clearSelection();
}
std::set<std::string> SelectionProperty::fromLuaConversion(lua_State* state,
bool& success) const
{
constexpr int KEY = -2;
constexpr int VAL = -1;
if (!lua_istable(state, VAL)) {
LERROR("Parameter passed to the property is not a table");
success = false;
return {};
}
std::set<std::string> result;
lua_pushnil(state);
while (lua_next(state, KEY) != 0) {
if (lua_isstring(state, VAL)) {
result.insert(lua_tostring(state, -1));
}
else {
success = false;
return {};
}
lua_pop(state, 1);
}
success = true;
return result;
std::set<std::string> SelectionProperty::fromLuaConversion(lua_State* state) const {
std::vector<std::string> val = ghoul::lua::value<std::vector<std::string>>(state, -1);
return std::set<std::string>(val.begin(), val.end());
}
void SelectionProperty::toLuaConversion(lua_State* state) const {
lua_newtable(state);
int i = 1;
for (const std::string& v : _value) {
lua_pushinteger(state, i);
lua_pushstring(state, v.c_str());
lua_settable(state, -3);
++i;
}
std::vector<std::string> value(_value.begin(), _value.end());
ghoul::lua::push(state, value);
}
std::string SelectionProperty::toStringConversion() const {

View File

@@ -42,9 +42,8 @@ int StringProperty::typeLua() const {
return LUA_TSTRING;
}
std::string StringProperty::fromLuaConversion(lua_State* state, bool& success) const {
success = lua_isstring(state, -1) == 1;
return success ? lua_tostring(state, -1) : "";
std::string StringProperty::fromLuaConversion(lua_State* state) const {
return ghoul::lua::value<std::string>(state);
}
void StringProperty::toLuaConversion(lua_State* state) const {

View File

@@ -49,8 +49,4 @@ int DVec2Property::typeLua() const {
return LUA_TTABLE;
}
glm::dvec2 DVec2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::dvec2>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int DVec3Property::typeLua() const {
return LUA_TTABLE;
}
glm::dvec3 DVec3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::dvec3>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int DVec4Property::typeLua() const {
return LUA_TTABLE;
}
glm::dvec4 DVec4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::dvec4>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int IVec2Property::typeLua() const {
return LUA_TTABLE;
}
glm::ivec2 IVec2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::ivec2>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int IVec3Property::typeLua() const {
return LUA_TTABLE;
}
glm::ivec3 IVec3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::ivec3>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int IVec4Property::typeLua() const {
return LUA_TTABLE;
}
glm::ivec4 IVec4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::ivec4>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int UVec2Property::typeLua() const {
return LUA_TTABLE;
}
glm::uvec2 UVec2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::uvec2>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int UVec3Property::typeLua() const {
return LUA_TTABLE;
}
glm::uvec3 UVec3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::uvec3>(state);
}
} // namespace openspace::properties

View File

@@ -49,8 +49,4 @@ int UVec4Property::typeLua() const {
return LUA_TTABLE;
}
glm::uvec4 UVec4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::uvec4>(state);
}
} // namespace openspace::properties

View File

@@ -48,8 +48,4 @@ int Vec2Property::typeLua() const {
return LUA_TTABLE;
}
glm::vec2 Vec2Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::vec2>(state);
}
} // namespace openspace::properties

View File

@@ -48,8 +48,4 @@ int Vec3Property::typeLua() const {
return LUA_TTABLE;
}
glm::vec3 Vec3Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::vec3>(state);
}
} // namespace openspace::properties

View File

@@ -48,8 +48,4 @@ int Vec4Property::typeLua() const {
return LUA_TTABLE;
}
glm::vec4 Vec4Property::fromLuaConversion(lua_State* state, bool& success) const {
return ghoul::lua::value<glm::vec4>(state);
}
} // namespace openspace::properties

View File

@@ -215,7 +215,6 @@ void applyRegularExpression(lua_State* L, const std::string& regex,
// end of the loop, the property name regex was probably misspelled.
bool foundMatching = false;
for (properties::Property* prop : matchingProps) {
// Check that the types match
if (type != prop->typeLua()) {
LERRORC(
@@ -233,6 +232,10 @@ void applyRegularExpression(lua_State* L, const std::string& regex,
// value change if the types agree
foundMatching = true;
// The setLuaInterpolationTarget and setLuaValue functions will remove the
// value from the stack, so we need to push it to the end
lua_pushvalue(L, -1);
if (global::sessionRecording->isRecording()) {
global::sessionRecording->savePropertyBaseline(*prop);
}