Skip to content

Commit

Permalink
add bugfix about to<vector> to<map>
Browse files Browse the repository at this point in the history
  • Loading branch information
peacalm committed Apr 23, 2023
1 parent 5ab32de commit a9bba97
Show file tree
Hide file tree
Showing 2 changed files with 420 additions and 25 deletions.
31 changes: 21 additions & 10 deletions include/peacalm/lua_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,12 @@ class lua_wrapper {
bool disable_log = false,
bool* failed = nullptr,
bool* exists = nullptr) {
if (exists) *exists = !isnoneornil(-1);
if (exists) *exists = !isnoneornil(idx);
T ret;
if (isnoneornil(idx)) {
if (failed) *failed = false;
return ret;
}
if (!lua_istable(L_, idx)) {
if (failed) *failed = true;
if (!disable_log) log_type_convert_error(idx, "vector");
Expand All @@ -511,8 +515,11 @@ class lua_wrapper {
ret.reserve(sz);
for (int i = 1; i <= sz; ++i) {
lua_geti(L_, idx, i);
bool subfailed;
ret.push_back(to<typename T::value_type>(-1, disable_log, &subfailed));
bool subfailed, subexists;
auto subret =
to<typename T::value_type>(-1, disable_log, &subfailed, &subexists);
// Only add elements exist and conversion succeeded
if (!subfailed && subexists) ret.push_back(std::move(subret));
if (subfailed && failed) *failed = true;
pop();
}
Expand Down Expand Up @@ -558,8 +565,12 @@ class lua_wrapper {
bool* failed = nullptr,
bool* exists = nullptr,
const char* tname = "map") {
if (exists) *exists = !isnoneornil(-1);
if (exists) *exists = !isnoneornil(idx);
T ret;
if (isnoneornil(idx)) {
if (failed) *failed = false;
return ret;
}
if (!lua_istable(L_, idx)) {
if (failed) *failed = true;
if (!disable_log) log_type_convert_error(idx, tname);
Expand All @@ -569,12 +580,12 @@ class lua_wrapper {
int absidx = idx > 0 ? idx : gettop() + idx + 1;
lua_pushnil(L_);
while (lua_next(L_, absidx) != 0) {
bool kfailed = false, vfailed = false;
const auto& key = to<typename T::key_type>(-2, disable_log, &kfailed);
if (!kfailed) {
const auto& val =
to<typename T::mapped_type>(-1, disable_log, &vfailed);
if (!vfailed) ret.insert({std::move(key), std::move(val)});
bool kfailed, kexists, vfailed, vexists;
auto key = to<typename T::key_type>(-2, disable_log, &kfailed, &kexists);
if (!kfailed && kexists) {
auto val =
to<typename T::mapped_type>(-1, disable_log, &vfailed, &vexists);
if (!vfailed && vexists) ret.insert({std::move(key), std::move(val)});
}
if ((kfailed || vfailed) && failed) *failed = true;
pop();
Expand Down
Loading

0 comments on commit a9bba97

Please sign in to comment.