Skip to content

Commit

Permalink
Export the script environment template as orch.env
Browse files Browse the repository at this point in the history
We don't need to be that strict about what callers do with the environment,
just export it wholesale.  The only real rule is that they can't modify it
post-run_script(), because those changes won't show up to the running
script.

Fixes #4

Signed-off-by: Kyle Evans <[email protected]>
  • Loading branch information
kevans91 committed Jan 22, 2024
1 parent 698b344 commit e60e2e4
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions orch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--

local impl = require("orch_impl")
local orch = {}
local orch = {env = {}}

local CTX_QUEUE = 1
local CTX_FAIL = 2
Expand Down Expand Up @@ -376,8 +376,6 @@ function match_ctx_stack:dump()
end


local orch_env = {}

-- Execute a chunk; may either be a callback from a match block, or it may be
-- an entire included file. Either way, each execution gets a new match context
-- that we may or may not use. We'll act upon the latest in the stack no matter
Expand Down Expand Up @@ -431,7 +429,7 @@ local function grab_caller(level)
return info.short_src, info.currentline
end

-- Bits available to the sandbox; orch_env functions are directly exposed, the
-- Bits available to the sandbox; orch.env functions are directly exposed, the
-- below do_*() implementations are the callbacks we use when the main loop goes
-- to process them.
local function do_debug(action)
Expand Down Expand Up @@ -516,7 +514,7 @@ local function do_write(action)
return true
end

function orch_env.debug(str)
function orch.env.debug(str)
local debug_action = MatchAction:new("debug", do_debug)
debug_action.message = str
if ctx() ~= CTX_QUEUE then
Expand All @@ -527,7 +525,7 @@ function orch_env.debug(str)
return true
end

function orch_env.enqueue(callback)
function orch.env.enqueue(callback)
local enqueue_action = MatchAction:new("enqueue", do_enqueue)
enqueue_action.callback = callback

Expand All @@ -540,7 +538,7 @@ function orch_env.enqueue(callback)
return true
end

function orch_env.eof(timeout)
function orch.env.eof(timeout)
local eof_action = MatchAction:new("eof", do_eof)
eof_action.timeout = timeout or current_timeout

Expand All @@ -555,7 +553,7 @@ function orch_env.eof(timeout)
return true
end

function orch_env.exit(code)
function orch.env.exit(code)
local exit_action = MatchAction:new("exit", do_exit)
exit_action.code = code

Expand All @@ -572,14 +570,14 @@ function orch_env.exit(code)
return true
end

function orch_env.fail(func)
function orch.env.fail(func)
local fail_action = MatchAction:new("fail", do_fail_handler)
fail_action.callback = func
match_ctx:push(fail_action)
return true
end

function orch_env.match(pattern)
function orch.env.match(pattern)
local match_action = MatchAction:new("match")
match_action.pattern = pattern
match_action.timeout = current_timeout
Expand Down Expand Up @@ -608,7 +606,7 @@ function orch_env.match(pattern)
return set_cfg
end

function orch_env.matcher(val)
function orch.env.matcher(val)
local matcher_obj

for k, v in pairs(available_matchers) do
Expand All @@ -627,7 +625,7 @@ function orch_env.matcher(val)
return true
end

function orch_env.one(func)
function orch.env.one(func)
local action_obj = MatchAction:new("one", do_one)
local parent_ctx = match_ctx
local src, line = grab_caller(2)
Expand Down Expand Up @@ -662,20 +660,20 @@ function orch_env.one(func)
return true
end

function orch_env.raw(val)
function orch.env.raw(val)
local action_obj = MatchAction:new("raw", do_raw)
action_obj.value = val
match_ctx:push(action_obj)
return true
end

function orch_env.release()
function orch.env.release()
local action_obj = MatchAction:new("release", do_release)
match_ctx:push(action_obj)
return true
end

function orch_env.sleep(duration)
function orch.env.sleep(duration)
local action_obj = MatchAction:new("sleep", do_sleep)
action_obj.duration = duration
if ctx() ~= CTX_QUEUE then
Expand All @@ -686,7 +684,7 @@ function orch_env.sleep(duration)
return true
end

function orch_env.spawn(...)
function orch.env.spawn(...)
local action_obj = MatchAction:new("spawn", do_spawn)
action_obj.cmd = { ... }
if type(action_obj.cmd[1]) == "table" then
Expand All @@ -699,14 +697,14 @@ function orch_env.spawn(...)
return true
end

function orch_env.timeout(val)
function orch.env.timeout(val)
if val == nil or val < 0 then
error("Timeout must be >= 0")
end
current_timeout = val
end

function orch_env.write(str)
function orch.env.write(str)
local action_obj = MatchAction:new("write", do_write)
action_obj.data = str
match_ctx:push(action_obj)
Expand All @@ -719,12 +717,12 @@ end
function orch.run_script(scriptfile, config)
local done

-- Make a copy of orch_env at the time of script execution. The
-- Make a copy of orch.env at the time of script execution. The
-- environment is effectively immutable from the driver's perspective
-- after execution starts, and we want to avoid a script from corrupting
-- future executions when we eventually support that.
local current_env = {}
for k, v in pairs(orch_env) do
for k, v in pairs(orch.env) do
current_env[k] = v
end

Expand Down Expand Up @@ -758,9 +756,9 @@ function orch.run_script(scriptfile, config)
end

-- Inherited from our environment
orch_env.assert = assert
orch_env.string = string
orch_env.table = table
orch_env.type = type
orch.env.assert = assert
orch.env.string = string
orch.env.table = table
orch.env.type = type

return orch

0 comments on commit e60e2e4

Please sign in to comment.