From d0edd60d74218b583bb6e6a335707c33d0957115 Mon Sep 17 00:00:00 2001 From: Pinya <800pin.ru@gmail.com> Date: Sun, 22 Jul 2018 16:46:04 +0300 Subject: [PATCH] update select & math.modf functions --- !Compatibility/api/luaAPI.lua | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/!Compatibility/api/luaAPI.lua b/!Compatibility/api/luaAPI.lua index 9468b4b..4ee88f5 100644 --- a/!Compatibility/api/luaAPI.lua +++ b/!Compatibility/api/luaAPI.lua @@ -18,6 +18,18 @@ math.huge = 1/0 string.gmatch = string.gfind gmatch = string.gfind +local MAXN = 2147483647 +local function toInt(x) + if x == floor(x) then return x end + + if x > 0 then + x = floor(x + 0.5) + else + x = ceil(x - 0.5) + end + + return x +end function select(n, ...) if not (type(n) == "number" or (type(n) == "string" and n == "#")) then error(format("bad argument #1 to 'select' (number expected, got %s)", n and type(n) or "no value"), 2) @@ -25,18 +37,22 @@ function select(n, ...) if n == "#" then return arg.n + elseif n == 0 or n > MAXN then + error("bad argument #1 to 'select' (index out of range)", 2) elseif n == 1 then return unpack(arg) end - local args = {} - args.n = arg.n - n + 1 + if n < 0 then + n = arg.n + n + 1 + end + n = toInt(n) for i = n, arg.n do - args[i-n+1] = arg[i] + tremove(arg, 1) end - return unpack(args) + return unpack(arg) end local huge = math.huge @@ -53,7 +69,7 @@ function math.modf(i) return i, i > 0 and 0 or -0 end - local int = i >= 0 and floor(i) or ceil(i) + local int = i > 0 and floor(i) or ceil(i) return int, i - int end