# HG changeset patch # User Kim Alvefur # Date 1666189505 -7200 # Node ID 22066b02887fc7bf0fbcffee3cd7187236892fac # Parent 2c0c1b18a02babfbd955e2b2c49eb7265e21d396 util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2 Code deduplication diff -r 2c0c1b18a02b -r 22066b02887f util/format.lua --- a/util/format.lua Thu Oct 20 16:56:45 2022 +0200 +++ b/util/format.lua Wed Oct 19 16:25:05 2022 +0200 @@ -11,9 +11,7 @@ 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 -r 2c0c1b18a02b -r 22066b02887f util/jsonpointer.lua --- a/util/jsonpointer.lua Thu Oct 20 16:56:45 2022 +0200 +++ b/util/jsonpointer.lua Wed Oct 19 16:25:05 2022 +0200 @@ -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 -r 2c0c1b18a02b -r 22066b02887f util/serialization.lua --- a/util/serialization.lua Thu Oct 20 16:56:45 2022 +0200 +++ b/util/serialization.lua Wed Oct 19 16:25:05 2022 +0200 @@ -22,9 +22,7 @@ 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 -r 2c0c1b18a02b -r 22066b02887f util/startup.lua --- a/util/startup.lua Thu Oct 20 16:56:45 2022 +0200 +++ b/util/startup.lua Wed Oct 19 16:25:05 2022 +0200 @@ -277,6 +277,20 @@ 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()