Changeset

12782:8815d3090928

util.mathcompat: Module to ease reuse of math.type() Mostly to ensure it is available during tests, as util.startup is not invoked there
author Kim Alvefur <zash@zash.se>
date Thu, 20 Oct 2022 16:50:12 +0200
parents 12781:22066b02887f
children 12783:d513e4bd4928
files teal-src/util/datamapper.tl teal-src/util/jsonschema.tl teal-src/util/mathcompat.tl util/datamapper.lua util/mathcompat.lua util/serialization.lua util/startup.lua
diffstat 7 files changed, 41 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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";
--- 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;
 
--- /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
--- 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");
 
--- /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
--- 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;
 
--- 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