跳转到内容

动态求值

在运行时动态执行代码,具有沙盒环境和受控模块访问。

Wippy 提供两个求值系统:

系统用途使用场景
expr表达式求值配置、模板、简单计算
eval_runner完整 Lua 执行插件、用户脚本、动态代码

使用 expr-lang 语法的轻量级表达式求值。

local expr = require("expr")
local result, err = expr.eval("x + y * 2", {x = 10, y = 5})
-- result = 20

编译一次,多次运行:

local program, err = expr.compile("price * quantity")
local total1 = program:run({price = 10, quantity = 5})
local total2 = program:run({price = 20, quantity = 3})
-- 算术
expr.eval("1 + 2 * 3") -- 7
expr.eval("10 / 2 - 1") -- 4
expr.eval("10 % 3") -- 1
-- 比较
expr.eval("x > 5", {x = 10}) -- true
expr.eval("x == y", {x = 1, y = 1}) -- true
-- 布尔
expr.eval("a && b", {a = true, b = false}) -- false
expr.eval("a || b", {a = true, b = false}) -- true
expr.eval("!a", {a = false}) -- true
-- 三元
expr.eval("x > 0 ? 'positive' : 'negative'", {x = 5})
-- 函数
expr.eval("max(1, 5, 3)") -- 5
expr.eval("min(1, 5, 3)") -- 1
expr.eval("len([1, 2, 3])") -- 3
-- 数组
expr.eval("[1, 2, 3][0]") -- 1
-- 字符串连接
expr.eval("'hello' + ' ' + 'world'")

具有安全控制的完整 Lua 执行。

local runner = require("eval_runner")
local result, err = runner.run({
source = [[
local function double(x)
return x * 2
end
return double(input)
]],
args = {21}
})
-- result = 42
参数类型描述
sourcestringLua 源代码(必需)
methodstring在返回表中调用的函数
argsany[]传递给函数的参数
modulesstring[]允许的内置模块
importstable要导入的注册表条目
contexttable作为 ctx 可用的值
allow_classesstring[]附加模块类
custom_modulestable作为模块的自定义表

白名单允许的模块:

runner.run({
source = [[
local json = require("json")
return json.encode({hello = "world"})
]],
modules = {"json"}
})

不在列表中的模块无法被 require。

从注册表导入条目:

runner.run({
source = [[
local utils = require("utils")
return utils.format(data)
]],
imports = {
utils = "app.lib:utilities"
},
args = {{key = "value"}}
})

注入自定义表:

runner.run({
source = [[
return sdk.version
]],
custom_modules = {
sdk = {version = "1.0.0", api_key = "xxx"}
}
})

传递可作为 ctx 访问的数据:

runner.run({
source = [[
return "Hello, " .. ctx.user
]],
context = {user = "Alice"}
})

runner.compile 校验源码并报告其入口点和模块,但不运行它:

local program, err = runner.compile([[
local function process(x)
return x * 2
end
return { process = process }
]], "process", {modules = {"json"}})
program:method() -- "process" (string)
program:modules() -- {"json"} (string[])

编译后的程序仅供参考;执行需通过传入源码和方法调用 runner.run

模块按能力分类:

描述默认
deterministic纯函数允许
encoding数据编码允许
time时间操作允许
nondeterministic随机等允许
process进程、注册表阻止
storage文件、数据库阻止
networkHTTP、套接字阻止
runner.run({
source = [[
local http = require("http_client")
return http.get("https://api.example.com")
]],
modules = {"http_client"},
allow_classes = {"network"}
})

系统检查以下权限:

  • eval.compile - 编译前
  • eval.run - 执行前
  • eval.module - 白名单中的每个模块
  • eval.import - 每个注册表导入
  • eval.class - 每个允许的类

在安全策略中配置。

local result, err = runner.run({...})
if err then
if err:kind() == errors.PERMISSION_DENIED then
-- 安全策略拒绝访问
elseif err:kind() == errors.INVALID then
-- 无效的源或配置
elseif err:kind() == errors.INTERNAL then
-- 执行或编译错误
end
end
local plugins = registry.find({meta = {type = "plugin"}})
for _, plugin in ipairs(plugins) do
local source = plugin:data().source
runner.run({
source = source,
method = "init",
modules = {"json", "time"},
context = {config = app_config}
})
end
local template = "Hello, {{name}}! You have {{count}} messages."
local compiled = expr.compile("name")
-- 快速重复求值
for _, user in ipairs(users) do
local greeting = compiled:run({name = user.name})
end
local user_code = request:body()
local result, err = runner.run({
source = user_code,
modules = {"json", "text"}, -- 仅安全模块
context = {data = input_data}
})