1111#include " flex-write.hpp"
1212#include " geom-functions.hpp"
1313#include " json-writer.hpp"
14+ #include " lua-utils.hpp"
1415#include " wkb.hpp"
1516
1617#include < algorithm>
@@ -188,17 +189,14 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
188189
189190 if (is_lua_array (lua_state)) {
190191 writer->start_array ();
191- lua_pushnil (lua_state);
192- while (lua_next (lua_state, -2 ) != 0 ) {
192+ luaX_for_each (lua_state, [&]() {
193193 write_json (writer, lua_state, tables);
194194 writer->next ();
195- lua_pop (lua_state, 1 );
196- }
195+ });
197196 writer->end_array ();
198197 } else {
199198 writer->start_object ();
200- lua_pushnil (lua_state);
201- while (lua_next (lua_state, -2 ) != 0 ) {
199+ luaX_for_each (lua_state, [&]() {
202200 int const ltype_key = lua_type (lua_state, -2 );
203201 if (ltype_key != LUA_TSTRING) {
204202 throw fmt_error (" Incorrect data type '{}' as key." ,
@@ -208,8 +206,7 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
208206 writer->key (key);
209207 write_json (writer, lua_state, tables);
210208 writer->next ();
211- lua_pop (lua_state, 1 );
212- }
209+ });
213210 writer->end_object ();
214211 }
215212}
@@ -403,8 +400,7 @@ void flex_write_column(lua_State *lua_state,
403400 if (ltype == LUA_TTABLE) {
404401 copy_mgr->new_hash ();
405402
406- lua_pushnil (lua_state);
407- while (lua_next (lua_state, -2 ) != 0 ) {
403+ luaX_for_each (lua_state, [&]() {
408404 char const *const key = lua_tostring (lua_state, -2 );
409405 char const *const val = lua_tostring (lua_state, -1 );
410406 if (key == nullptr ) {
@@ -422,8 +418,7 @@ void flex_write_column(lua_State *lua_state,
422418 lua_typename (lua_state, ltype_value), key);
423419 }
424420 copy_mgr->add_hash_elem (key, val);
425- lua_pop (lua_state, 1 );
426- }
421+ });
427422
428423 copy_mgr->finish_hash ();
429424 } else {
0 commit comments