Skip to content

Commit

Permalink
perf: replace eval func will not run for non-eval matcher (#122)
Browse files Browse the repository at this point in the history
Signed-off-by: Rushikesh Tote <[email protected]>
  • Loading branch information
rushitote authored Aug 23, 2021
1 parent 5f34577 commit 84ade15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/CoreEnforcer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ function CoreEnforcer:enforceEx(...)
end

local expString = self.model.model["m"]["m"].value
local hasEval = Util.hasEval(expString)

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

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

local tExpString = Util.findAndReplaceEval(expString, context)

local tExpString = expString
if hasEval then
tExpString = Util.findAndReplaceEval(expString, context)
end

local res, err
if tExpString == expString then
res, err = luaxp.run(compiledExpression, context)
Expand Down
5 changes: 5 additions & 0 deletions src/util/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ function Util.areTablesSame(a, b)
return true
end

-- checks if the matcher string has eval(...)
function Util.hasEval(str)
return string.find(str, "eval%((.-)%)")
end

-- finds if string has eval and replaces eval(...) with its value so that it can be evaluated by luaxp
function Util.findAndReplaceEval(str, context)
local m = string.gsub(str, "eval%((.-)%)", function (s)
Expand Down

0 comments on commit 84ade15

Please sign in to comment.