Skip to content

Commit 84ade15

Browse files
authored
perf: replace eval func will not run for non-eval matcher (#122)
Signed-off-by: Rushikesh Tote <[email protected]>
1 parent 5f34577 commit 84ade15

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/main/CoreEnforcer.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ function CoreEnforcer:enforceEx(...)
404404
end
405405

406406
local expString = self.model.model["m"]["m"].value
407+
local hasEval = Util.hasEval(expString)
407408

408409
local policyLen = #self.model.model["p"]["p"].policy
409410

@@ -429,8 +430,11 @@ function CoreEnforcer:enforceEx(...)
429430
context[v] = pvals[k]
430431
end
431432

432-
local tExpString = Util.findAndReplaceEval(expString, context)
433-
433+
local tExpString = expString
434+
if hasEval then
435+
tExpString = Util.findAndReplaceEval(expString, context)
436+
end
437+
434438
local res, err
435439
if tExpString == expString then
436440
res, err = luaxp.run(compiledExpression, context)

src/util/Util.lua

+5
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ function Util.areTablesSame(a, b)
166166
return true
167167
end
168168

169+
-- checks if the matcher string has eval(...)
170+
function Util.hasEval(str)
171+
return string.find(str, "eval%((.-)%)")
172+
end
173+
169174
-- finds if string has eval and replaces eval(...) with its value so that it can be evaluated by luaxp
170175
function Util.findAndReplaceEval(str, context)
171176
local m = string.gsub(str, "eval%((.-)%)", function (s)

0 commit comments

Comments
 (0)