@@ -577,13 +577,11 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
577577 table->name ());
578578 }
579579
580+ if (!luaX_is_array (lua_state ())) {
581+ throw std::runtime_error{" The 'columns' field must contain an array." };
582+ }
580583 std::size_t num_columns = 0 ;
581- lua_pushnil (lua_state ());
582- while (lua_next (lua_state (), -2 ) != 0 ) {
583- if (!lua_isnumber (lua_state (), -2 )) {
584- throw std::runtime_error{
585- " The 'columns' field must contain an array." };
586- }
584+ luaX_for_each (lua_state (), [&]() {
587585 if (!lua_istable (lua_state (), -1 )) {
588586 throw std::runtime_error{
589587 " The entries in the 'columns' array must be tables." };
@@ -611,16 +609,15 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
611609 column.type () == table_column_type::area) {
612610 column.set_projection (lua_tostring (lua_state (), -1 ));
613611 } else {
614- throw std::runtime_error{
615- " Projection can only be set on geometry and area columns." };
612+ throw std::runtime_error{" Projection can only be set on "
613+ " geometry and area columns." };
616614 }
617615 }
618616
619- // stack has: projection, create_only, not_null, sql_type, column,
620- // type, table
621- lua_pop (lua_state (), 7 );
617+ // stack has: projection, create_only, not_null, sql_type, column, type
618+ lua_pop (lua_state (), 6 );
622619 ++num_columns;
623- }
620+ });
624621
625622 if (num_columns == 0 && !table->has_id_column ()) {
626623 throw fmt_error (" No columns defined for table '{}'." , table->name ());
@@ -655,21 +652,18 @@ void output_flex_t::setup_indexes(flex_table_t *table)
655652 table->name ());
656653 }
657654
658- lua_pushnil (lua_state ());
659- while (lua_next (lua_state (), -2 ) != 0 ) {
660- if (!lua_isnumber (lua_state (), -2 )) {
661- throw std::runtime_error{
662- " The 'indexes' field must contain an array." };
663- }
655+ if (!luaX_is_array (lua_state ())) {
656+ throw std::runtime_error{" The 'indexes' field must contain an array." };
657+ }
658+
659+ luaX_for_each (lua_state (), [&]() {
664660 if (!lua_istable (lua_state (), -1 )) {
665661 throw std::runtime_error{
666662 " The entries in the 'indexes' array must be Lua tables." };
667663 }
668664
669665 flex_lua_setup_index (lua_state (), table);
670-
671- lua_pop (lua_state (), 1 );
672- }
666+ });
673667
674668 lua_pop (lua_state (), 1 ); // "indexes"
675669}
@@ -1179,25 +1173,24 @@ void output_flex_t::select_relation_members()
11791173 }
11801174
11811175 // Iterate over the 'ways' table to get all ids...
1182- lua_pushnil (lua_state ());
1183- while (lua_next (lua_state (), -2 ) != 0 ) {
1184- if (!lua_isnumber (lua_state (), -2 )) {
1185- throw std::runtime_error{
1186- " Table returned from select_relation_members() contains 'ways' "
1187- " field, but it isn't an array table." };
1188- }
1176+ if (!luaX_is_array (lua_state ())) {
1177+ throw std::runtime_error{
1178+ " Table returned from select_relation_members() contains 'ways' "
1179+ " field, but it isn't an array table." };
1180+ }
11891181
1190- osmid_t const id = lua_tointeger (lua_state (), -1 );
1191- if (id == 0 ) {
1192- throw std::runtime_error{
1193- " Table returned from select_relation_members() contains 'ways' "
1194- " field, which must contain an array of non-zero integer way "
1195- " ids." };
1196- }
1182+ luaX_for_each (
1183+ lua_state (), [&]() {
1184+ osmid_t const id = lua_tointeger (lua_state (), -1 );
1185+ if (id == 0 ) {
1186+ throw std::runtime_error{
1187+ " Table returned from select_relation_members() contains "
1188+ " 'ways' field, which must contain an array of non-zero "
1189+ " integer way ids." };
1190+ }
11971191
1198- m_stage2_way_ids->set (id);
1199- lua_pop (lua_state (), 1 ); // value pushed by lua_next()
1200- }
1192+ m_stage2_way_ids->set (id);
1193+ });
12011194
12021195 lua_pop (lua_state (), 2 ); // return value (a table), ways field (a table)
12031196}
0 commit comments