示例工程 30-embedding-and-cpp
30-embedding-and-cpp/01-embed-config/main.lua
-- @runtimes lua54 luajit
--- 演示:宿主加载 Lua 配置并校验(模拟嵌入)。
local M = {}
local TEXT = [[
host = "db"
port = 5432
debug = false
]]
--- @return table { host = string, port = integer, debug = boolean }
function M.run(log)
log = log or print
local env = {}
local chunk = assert(load(TEXT, "=config", "t", env))
chunk()
assert(type(env.host) == "string", "host required")
assert(math.type(env.port) == "integer", "port must be integer")
local debug = env.debug == true
log(("host=%s port=%d debug=%s"):format(env.host, env.port, tostring(debug)))
return { host = env.host, port = env.port, debug = debug }
end
if not ... then
M.run()
end
return M