aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-05-30 13:54:11 +0200
committerKim Alvefur <zash@zash.se>2019-05-30 13:54:11 +0200
commit2661a6f5a32731100287df9564c45cbe2406e0f0 (patch)
tree21e8655ccdf297cc318dd8288ef97debf7100fc1
parent236abc4afed5321e0da406e369a8b23dac6fef83 (diff)
downloadprosody-2661a6f5a32731100287df9564c45cbe2406e0f0.tar.gz
prosody-2661a6f5a32731100287df9564c45cbe2406e0f0.zip
util.format: Handle integer formats the same way on Lua versions without integer support
-rw-r--r--spec/util_format_spec.lua1
-rw-r--r--util/format.lua7
2 files changed, 6 insertions, 2 deletions
diff --git a/spec/util_format_spec.lua b/spec/util_format_spec.lua
index 82b70205..50509630 100644
--- a/spec/util_format_spec.lua
+++ b/spec/util_format_spec.lua
@@ -13,6 +13,7 @@ describe("util.format", function()
assert.equal("% [true]", format("%%", true));
assert.equal("{ }", format("%q", { }));
assert.equal("[1.5]", format("%d", 1.5));
+ assert.equal("[7.3786976294838e+19]", format("%d", 73786976294838206464));
end);
end);
end);
diff --git a/util/format.lua b/util/format.lua
index 857bb694..1ce670f3 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -7,9 +7,12 @@ local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack
local pack = require "util.table".pack; -- TODO table.pack in 5.2+
local type = type;
local dump = require "util.serialization".new("debug");
-local num_type = math.type;
+local num_type = math.type or function (n)
+ return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
+end
-local expects_integer = num_type and { c = true, d = true, i = true, o = true, u = true, X = true, x = true, } or {};
+-- In Lua 5.3+ these formats throw an error if given a float
+local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, };
local function format(formatstring, ...)
local args = pack(...);