calog/examples/scripts/polyglot/foreignCallable.lua

22 lines
924 B
Lua

-- A function value can cross INTO any engine and be invoked there. Here Lua exports a
-- higher-order function; a my-basic script receives the returned closure as a value and calls
-- it with calogInvoke(fn, ...). (Every other engine invokes such a value directly, e.g. fn(x).)
-- run: bin/calog examples/scripts/polyglot/foreignCallable.lua
local helper = taskSpawn("lua", [[
calogExport("makeGreeter", function()
return function(name) return "hello, " .. tostring(name) .. "!" end
end)
kvSet("helper_ready", "1")
]])
while not kvHas("helper_ready") do timeSleep(10) end
local mb = taskSpawn("mybasic", [[
greet = calogCall("makeGreeter") ' a Lua closure, held as a my-basic value
kvSet("greeting", calogInvoke(greet, "my-basic"))
]])
while not kvHas("greeting") do timeSleep(20) end
calogPrint("my-basic invoked the Lua closure ->", kvGet("greeting"))
taskClose(mb); taskClose(helper)
calogExit(0)