# HG changeset patch # User Kim Alvefur # Date 1666277412 -7200 # Node ID 8815d30909286115e25cbaa4a8d81c58d88aac71 # Parent 22066b02887fc7bf0fbcffee3cd7187236892fac util.mathcompat: Module to ease reuse of math.type() Mostly to ensure it is available during tests, as util.startup is not invoked there diff -r 22066b02887f -r 8815d3090928 teal-src/util/datamapper.tl --- a/teal-src/util/datamapper.tl Wed Oct 19 16:25:05 2022 +0200 +++ b/teal-src/util/datamapper.tl Thu Oct 20 16:50:12 2022 +0200 @@ -19,6 +19,8 @@ -- TODO s/number/integer/ once we have appropriate math.type() compat -- +if not math.type then require "util.mathcompat" end + local st = require "util.stanza"; local json = require"util.json" local pointer = require"util.jsonpointer"; diff -r 22066b02887f -r 8815d3090928 teal-src/util/jsonschema.tl --- a/teal-src/util/jsonschema.tl Wed Oct 19 16:25:05 2022 +0200 +++ b/teal-src/util/jsonschema.tl Thu Oct 20 16:50:12 2022 +0200 @@ -8,6 +8,8 @@ -- https://json-schema.org/draft/2020-12/json-schema-validation.html -- +if not math.type then require "util.mathcompat" end + local json = require"util.json" local null = json.null; diff -r 22066b02887f -r 8815d3090928 teal-src/util/mathcompat.tl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/teal-src/util/mathcompat.tl Thu Oct 20 16:50:12 2022 +0200 @@ -0,0 +1,15 @@ +if not math.type then + local enum number_subtype + "float" "integer" + end + local function math_type(t:any) : number_subtype + if t is number then + if t % 1 == 0 and t ~= t+1 and t ~= t-1 then + return "integer" + else + return "float" + end + end + end + _G.math.type = math_type +end diff -r 22066b02887f -r 8815d3090928 util/datamapper.lua --- a/util/datamapper.lua Wed Oct 19 16:25:05 2022 +0200 +++ b/util/datamapper.lua Thu Oct 20 16:50:12 2022 +0200 @@ -1,5 +1,9 @@ -- This file is generated from teal-src/util/datamapper.lua +if not math.type then + require("util.mathcompat") +end + local st = require("util.stanza"); local pointer = require("util.jsonpointer"); diff -r 22066b02887f -r 8815d3090928 util/mathcompat.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util/mathcompat.lua Thu Oct 20 16:50:12 2022 +0200 @@ -0,0 +1,13 @@ +if not math.type then + + local function math_type(t) + if type(t) == "number" then + if t % 1 == 0 and t ~= t + 1 and t ~= t - 1 then + return "integer" + else + return "float" + end + end + end + _G.math.type = math_type +end diff -r 22066b02887f -r 8815d3090928 util/serialization.lua --- a/util/serialization.lua Wed Oct 19 16:25:05 2022 +0200 +++ b/util/serialization.lua Thu Oct 20 16:50:12 2022 +0200 @@ -21,6 +21,10 @@ local pcall = pcall; local envload = require"util.envload".envload; +if not math.type then + require "util.mathcompat" +end + local pos_inf, neg_inf = math.huge, -math.huge; local m_type = math.type; diff -r 22066b02887f -r 8815d3090928 util/startup.lua --- a/util/startup.lua Wed Oct 19 16:25:05 2022 +0200 +++ b/util/startup.lua Thu Oct 20 16:50:12 2022 +0200 @@ -280,16 +280,7 @@ -- 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 + require "util.mathcompat" end end