calog/examples/scripts/libraries/export.lua

26 lines
941 B
Lua

-- Showcase the calog export library: publish a function with calogExport, then
-- invoke it two ways -- via calogCall by name, and (a Lua convenience) by its bare
-- name, since exports resolve as unknown globals. Finally calogUnexport it and show
-- a guarded call fails cleanly instead of returning a stale result.
-- Run: bin/calog examples/scripts/libraries/export.lua
calogExport("square", function(x)
return x * x
end)
-- Invoke through the broker by name.
calogPrint("calogCall square 7:", calogCall("square", 7))
-- Invoke by bare name (Lua resolves the unknown global through the export registry).
calogPrint("bare-name square 9:", square(9))
-- Retire the export; the name is no longer resolvable anywhere.
calogUnexport("square")
local ok, err = pcall(calogCall, "square", 7)
calogPrint("call after unexport ok:", ok)
if not ok then
calogPrint("call after unexport failed cleanly:", tostring(err))
end
calogExit(0)