Skip to content

Commit 46529d3

Browse files
committed
Make code work with Lua version < 5.3
The functions lua_isinteger() and lua_tointegerx() are only available in newer Lua versions so we have to work around them.
1 parent 94dae34 commit 46529d3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/output-flex.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ static bool is_lua_array(lua_State *lua_state)
334334
lua_pushnil(lua_state);
335335
while (lua_next(lua_state, -2) != 0) {
336336
lua_pop(lua_state, 1); // remove value from stack
337+
#if LUA_VERSION_NUM >= 503
337338
if (!lua_isinteger(lua_state, -1)) {
338339
lua_pop(lua_state, 1);
339340
return false;
@@ -344,6 +345,19 @@ static bool is_lua_array(lua_State *lua_state)
344345
lua_pop(lua_state, 1);
345346
return false;
346347
}
348+
#else
349+
if (!lua_isnumber(lua_state, -1)) {
350+
lua_pop(lua_state, 1);
351+
return false;
352+
}
353+
double const num = lua_tonumber(lua_state, -1);
354+
double intpart = 0.0;
355+
if (std::modf(num, &intpart) != 0.0 || intpart < 0 ||
356+
static_cast<uint32_t>(num) != n++) {
357+
lua_pop(lua_state, 1);
358+
return false;
359+
}
360+
#endif
347361
}
348362

349363
// An empty lua table could be both, we decide here that it is not stored
@@ -398,13 +412,23 @@ static void write_json_table(json_writer_type *writer, lua_State *lua_state,
398412

399413
static void write_json_number(json_writer_type *writer, lua_State *lua_state)
400414
{
415+
#if LUA_VERSION_NUM >= 503
401416
int okay = 0;
402417
auto const num = lua_tointegerx(lua_state, -1, &okay);
403418
if (okay) {
404419
writer->Int64(num);
405420
} else {
406421
writer->Double(lua_tonumber(lua_state, -1));
407422
}
423+
#else
424+
double const num = lua_tonumber(lua_state, -1);
425+
double intpart = 0.0;
426+
if (std::modf(num, &intpart) == 0.0) {
427+
writer->Int64(static_cast<int64_t>(num));
428+
} else {
429+
writer->Double(num);
430+
}
431+
#endif
408432
}
409433

410434
static void write_json(json_writer_type *writer, lua_State *lua_state,

0 commit comments

Comments
 (0)