aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2022-10-19 16:25:05 +0200
committerKim Alvefur <zash@zash.se>2022-10-19 16:25:05 +0200
commite64c5e30c2ab1f59c5051f2bd66f053d34a6eb25 (patch)
treed5a7cf2782f2c6335eaa221de6eb67cf0c338f63
parent8fc457681e6dacf2272f4a8e3c1994623c2eab5a (diff)
downloadprosody-e64c5e30c2ab1f59c5051f2bd66f053d34a6eb25.tar.gz
prosody-e64c5e30c2ab1f59c5051f2bd66f053d34a6eb25.zip
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Code deduplication
-rw-r--r--util/format.lua4
-rw-r--r--util/jsonpointer.lua4
-rw-r--r--util/serialization.lua4
-rw-r--r--util/startup.lua14
4 files changed, 17 insertions, 9 deletions
diff --git a/util/format.lua b/util/format.lua
index 203bdeab..0631f423 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -11,9 +11,7 @@ local pack = table.pack;
local valid_utf8 = require "util.encodings".utf8.valid;
local type = type;
local dump = require "util.serialization".new("debug");
-local num_type = math.type or function (n)
- return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
-end
+local num_type = math.type;
-- 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, };
diff --git a/util/jsonpointer.lua b/util/jsonpointer.lua
index 9b871ae7..f1c354a4 100644
--- a/util/jsonpointer.lua
+++ b/util/jsonpointer.lua
@@ -1,6 +1,4 @@
-local m_type = math.type or function (n)
- return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
-end;
+local m_type = math.type;
local function unescape_token(escaped_token)
local unescaped = escaped_token:gsub("~1", "/"):gsub("~0", "~")
diff --git a/util/serialization.lua b/util/serialization.lua
index d310a3e8..6552d53b 100644
--- a/util/serialization.lua
+++ b/util/serialization.lua
@@ -22,9 +22,7 @@ local pcall = pcall;
local envload = require"util.envload".envload;
local pos_inf, neg_inf = math.huge, -math.huge;
-local m_type = math.type or function (n)
- return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
-end;
+local m_type = math.type;
local function rawpairs(t)
return next, t, nil;
diff --git a/util/startup.lua b/util/startup.lua
index 10ff1875..9e512cb3 100644
--- a/util/startup.lua
+++ b/util/startup.lua
@@ -277,6 +277,20 @@ function startup.init_global_state()
startup.detect_platform();
startup.detect_installed();
_G.prosody = prosody;
+
+ -- COMPAT Lua < 5.3
+ if not math.type then
+ -- luacheck: ignore 122/math
+ function math.type(n)
+ if type(n) == "number" then
+ if n % 1 == 0 and (n + 1 ~= n and n - 1 ~= n) then
+ return "integer"
+ else
+ return "float"
+ end
+ end
+ end
+ end
end
function startup.setup_datadir()