26 lines
984 B
Lua
26 lines
984 B
Lua
-- Cross-engine function call: Lua exports a "greet" function through the broker,
|
|
-- then a spawned JavaScript task invokes that Lua function via calogCall and prints
|
|
-- the string it returns. The export happens BEFORE the spawn so the child always
|
|
-- sees the registered name; a short timer gives the JS task time to run, then exits.
|
|
-- Run: bin/calog examples/scripts/polyglot/exportAcrossEngines.lua
|
|
|
|
-- Publish a Lua function into the process-wide export registry.
|
|
calogExport("greet", function(name)
|
|
return "hello, " .. name
|
|
end)
|
|
|
|
-- The child runs on the JavaScript engine and reaches back into the Lua function.
|
|
local jsCode = [[
|
|
var msg = calogCall("greet", "world");
|
|
calogPrint("javascript received:", msg);
|
|
]]
|
|
|
|
local child = taskSpawn("js", jsCode)
|
|
calogPrint("spawned javascript task:", child)
|
|
|
|
-- Give the JS task time to run its call, then tear everything down.
|
|
timerAfter(300, function()
|
|
taskClose(child)
|
|
calogUnexport("greet")
|
|
calogExit(0)
|
|
end)
|