Rework rounding and building lua script
To allow user script to specify what was modified where
This commit is contained in:
@@ -129,7 +129,10 @@ func InitLuaHelpers(L *lua.LState) error {
|
||||
-- Custom Lua helpers for math operations
|
||||
function min(a, b) return math.min(a, b) end
|
||||
function max(a, b) return math.max(a, b) end
|
||||
function round(x) return math.floor(x + 0.5) end
|
||||
function round(x, n)
|
||||
if n == nil then n = 0 end
|
||||
return math.floor(x * 10^n + 0.5) / 10^n
|
||||
end
|
||||
function floor(x) return math.floor(x) end
|
||||
function ceil(x) return math.ceil(x) end
|
||||
function upper(s) return string.upper(s) end
|
||||
@@ -149,6 +152,8 @@ end
|
||||
function is_number(str)
|
||||
return tonumber(str) ~= nil
|
||||
end
|
||||
|
||||
modified = false
|
||||
`
|
||||
if err := L.DoString(helperScript); err != nil {
|
||||
return fmt.Errorf("error loading helper functions: %v", err)
|
||||
@@ -187,7 +192,21 @@ func BuildLuaScript(luaExpr string) string {
|
||||
luaExpr = "v1 = " + luaExpr
|
||||
}
|
||||
|
||||
return luaExpr
|
||||
// This allows the user to specify whether or not they modified a value
|
||||
// If they do nothing we assume they did modify (no return at all)
|
||||
// If they return before our return then they themselves specify what they did
|
||||
// If nothing is returned lua assumes nil
|
||||
// So we can say our value was modified if the return value is either nil or true
|
||||
// If the return value is false then the user wants to keep the original
|
||||
fullScript := fmt.Sprintf(`
|
||||
function run()
|
||||
%s
|
||||
end
|
||||
local res = run()
|
||||
modified = res == nil or res
|
||||
`, luaExpr)
|
||||
|
||||
return fullScript
|
||||
}
|
||||
|
||||
// Max returns the maximum of two integers
|
||||
|
Reference in New Issue
Block a user