Skip to content

Commit 728a837

Browse files
authored
Merge pull request #222 from prometheus-lua/fix/221-lua54-cli
Fix Lua 5.4 CLI crash
2 parents 5db664f + 01b53e1 commit 728a837

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

src/cli.lua

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ local function lines_from(file)
4545
return lines
4646
end
4747

48+
local function load_chunk(content, chunkName, environment)
49+
if type(loadstring) == "function" then
50+
local func, err = loadstring(content, chunkName)
51+
if not func then
52+
return nil, err
53+
end
54+
if environment and type(setfenv) == "function" then
55+
setfenv(func, environment)
56+
elseif environment and type(load) == "function" then
57+
return load(content, chunkName, "t", environment)
58+
end
59+
return func
60+
end
61+
62+
if type(load) ~= "function" then
63+
return nil, "No load function available"
64+
end
65+
66+
return load(content, chunkName, "t", environment)
67+
end
68+
4869
-- CLI
4970
local config, sourceFile, outFile, luaVersion, prettyPrint
5071

@@ -76,9 +97,10 @@ while i <= #arg do
7697

7798
local content = table.concat(lines_from(filename), "\n")
7899
-- Load Config from File
79-
local func = loadstring(content)
80-
-- Sandboxing
81-
setfenv(func, {})
100+
local func, err = load_chunk(content, "@" .. filename, {})
101+
if not func then
102+
Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err)))
103+
end
82104
config = func()
83105
elseif curr == "--out" or curr == "--o" then
84106
i = i + 1

src/prometheus/compiler/expressions/boolean.lua

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
-- This Script contains the expression handler for the BooleanExpression.
66

77
local Ast = require("prometheus.ast");
8+
9+
local expressionEvaluators = {
10+
[Ast.GreaterThanExpression] = function(left, right)
11+
return left > right
12+
end,
13+
[Ast.LessThanExpression] = function(left, right)
14+
return left < right
15+
end,
16+
[Ast.GreaterThanOrEqualsExpression] = function(left, right)
17+
return left >= right
18+
end,
19+
[Ast.LessThanOrEqualsExpression] = function(left, right)
20+
return left <= right
21+
end,
22+
[Ast.NotEqualsExpression] = function(left, right)
23+
return left ~= right
24+
end,
25+
}
26+
827
local function createRandomASTCFlowExpression(resultBool)
928
local expTB = {
1029
Ast.GreaterThanExpression,
@@ -14,21 +33,12 @@ local function createRandomASTCFlowExpression(resultBool)
1433
Ast.NotEqualsExpression
1534
}
1635

17-
local expLookup = {
18-
[Ast.GreaterThanExpression] = ">";
19-
[Ast.LessThanExpression] = "<";
20-
[Ast.GreaterThanOrEqualsExpression] = ">=";
21-
[Ast.LessThanOrEqualsExpression] = "<=";
22-
[Ast.NotEqualsExpression] = "~=";
23-
}
24-
25-
local leftInt, rightInt, boolResult, r3, randomExp
36+
local leftInt, rightInt, boolResult, randomExp
2637
repeat
2738
randomExp = expTB[math.random(1, #expTB)]
2839
leftInt = Ast.NumberExpression(math.random(1, 2^24))
2940
rightInt = Ast.NumberExpression(math.random(1, 2^24))
30-
r3 = "return " .. leftInt.value .. expLookup[randomExp] .. rightInt.value
31-
boolResult = loadstring(r3)()
41+
boolResult = expressionEvaluators[randomExp](leftInt.value, rightInt.value)
3242
until boolResult == resultBool
3343

3444
return randomExp(leftInt, rightInt, false)
@@ -47,4 +57,3 @@ return function(self, expression, _, numReturns)
4757
end
4858
return regs;
4959
end;
50-

tests.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ local Prometheus = require("src.prometheus")
1212

1313
-- Config Variables - Later passed as Parameters
1414
local noColors = false; -- Whether Colors in the Console output should be enabled
15-
local isWindows = true; -- Whether the Test are Performed on a Windows or Linux System
15+
local isWindows = package.config:sub(1, 1) == "\\"; -- Whether the Test are Performed on a Windows or Linux System
1616
local ciMode = false; -- Whether the Test error are ignored or not
1717
local iterationCount = 20; -- How often each test should be executed
1818

1919
for _, currArg in pairs(arg) do
2020
if currArg == "--Linux" then
2121
isWindows = false
2222
end
23+
if currArg == "--Windows" then
24+
isWindows = true
25+
end
2326
if currArg == "--CI" then
2427
ciMode = true
2528
end
@@ -56,6 +59,9 @@ print(string.format(
5659
local function scandir(directory)
5760
local i, t, popen = 0, {}, io.popen
5861
local pfile = popen(isWindows and 'dir "'..directory..'" /b' or 'ls -a "'..directory..'"')
62+
if not pfile then
63+
error("Failed to list files in test directory: " .. tostring(directory))
64+
end
5965
for filename in pfile:lines() do
6066
if string.sub(filename, -4) == ".lua" then
6167
i = i + 1

0 commit comments

Comments
 (0)