Skip to content

Commit 134a96c

Browse files
committed
Retire is_lua_array() in favour of the new luaX_is_array()
The new function is better.
1 parent 2e897ba commit 134a96c

File tree

1 file changed

+6
-42
lines changed

1 file changed

+6
-42
lines changed

src/flex-write.cpp

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -132,47 +132,6 @@ static void write_double(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
132132

133133
using table_register_type = std::vector<void const *>;
134134

135-
/**
136-
* Check that the value on the top of the Lua stack is a simple array.
137-
* This means that all keys must be consecutive integers starting from 1.
138-
*/
139-
static bool is_lua_array(lua_State *lua_state)
140-
{
141-
uint32_t n = 1;
142-
lua_pushnil(lua_state);
143-
while (lua_next(lua_state, -2) != 0) {
144-
lua_pop(lua_state, 1); // remove value from stack
145-
#if LUA_VERSION_NUM >= 503
146-
if (!lua_isinteger(lua_state, -1)) {
147-
lua_pop(lua_state, 1);
148-
return false;
149-
}
150-
int okay = 0;
151-
auto const num = lua_tointegerx(lua_state, -1, &okay);
152-
if (!okay || num != n++) {
153-
lua_pop(lua_state, 1);
154-
return false;
155-
}
156-
#else
157-
if (!lua_isnumber(lua_state, -1)) {
158-
lua_pop(lua_state, 1);
159-
return false;
160-
}
161-
double const num = lua_tonumber(lua_state, -1);
162-
double intpart = 0.0;
163-
if (std::modf(num, &intpart) != 0.0 || intpart < 0 ||
164-
static_cast<uint32_t>(num) != n++) {
165-
lua_pop(lua_state, 1);
166-
return false;
167-
}
168-
#endif
169-
}
170-
171-
// An empty lua table could be both, we decide here that it is not stored
172-
// as a JSON array but as a JSON object.
173-
return n != 1;
174-
}
175-
176135
static void write_json(json_writer_t *writer, lua_State *lua_state,
177136
table_register_type *tables);
178137

@@ -187,7 +146,12 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
187146
}
188147
tables->push_back(table_ptr);
189148

190-
if (is_lua_array(lua_state)) {
149+
if (luaX_is_empty_table(lua_state)) {
150+
// An empty lua table could be both, we decide here that it is not
151+
// stored as a JSON array but as a JSON object.
152+
writer->start_object();
153+
writer->end_object();
154+
} else if (luaX_is_array(lua_state)) {
191155
writer->start_array();
192156
luaX_for_each(lua_state, [&]() {
193157
write_json(writer, lua_state, tables);

0 commit comments

Comments
 (0)