mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Don't expose private event fields to user in choices
This commit is contained in:
parent
7105b445ef
commit
7433d27da5
2 changed files with 16 additions and 22 deletions
|
|
@ -125,16 +125,25 @@ local events = {
|
||||||
state.interpreter.event_type = nil
|
state.interpreter.event_type = nil
|
||||||
state.interpreter.event_buffer = nil
|
state.interpreter.event_buffer = nil
|
||||||
state.interpreter.skip_choices_until_flush = nil
|
state.interpreter.skip_choices_until_flush = nil
|
||||||
-- yield
|
-- extract some needed state data for each choice block
|
||||||
|
local choices
|
||||||
|
if type == "choice" then
|
||||||
|
choices = {}
|
||||||
|
for _, c in ipairs(buffer) do
|
||||||
|
table.insert(choices, c._state)
|
||||||
|
c._state = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- yield event
|
||||||
coroutine.yield(type, buffer)
|
coroutine.yield(type, buffer)
|
||||||
-- run choice
|
-- run choice
|
||||||
if type == "choice" then
|
if type == "choice" then
|
||||||
local sel = state.interpreter.choice_selected
|
local sel = state.interpreter.choice_selected
|
||||||
state.interpreter.choice_selected = nil
|
state.interpreter.choice_selected = nil
|
||||||
if not sel or sel < 1 or sel > #buffer then
|
if not sel or sel < 1 or sel > #choices then
|
||||||
return nil, "invalid choice"
|
return nil, "invalid choice"
|
||||||
else
|
else
|
||||||
local choice = buffer[sel]._d
|
local choice = choices[sel]
|
||||||
-- execute in expected tag & event capture state
|
-- execute in expected tag & event capture state
|
||||||
local capture_state = state.interpreter.event_capture_stack
|
local capture_state = state.interpreter.event_capture_stack
|
||||||
state.interpreter.event_capture_stack = {}
|
state.interpreter.event_capture_stack = {}
|
||||||
|
|
@ -184,14 +193,15 @@ run_line = function(state, line)
|
||||||
local v, e = events:make_space_for(state, "choice")
|
local v, e = events:make_space_for(state, "choice")
|
||||||
if not v then return v, ("%s; in automatic event flush at %s"):format(e, line.source) end
|
if not v then return v, ("%s; in automatic event flush at %s"):format(e, line.source) end
|
||||||
local currentTags = tags:current(state)
|
local currentTags = tags:current(state)
|
||||||
v, e = events:append(state, "choice", { _d = { tags = currentTags, block = line.child }}) -- new choice
|
local choice_block_state = { tags = currentTags, block = line.child }
|
||||||
|
v, e = events:append(state, "choice", { _state = choice_block_state }) -- new choice
|
||||||
if not v then return v, e end
|
if not v then return v, e end
|
||||||
events:push_capture(state, "text", function(event)
|
events:push_capture(state, "text", function(event)
|
||||||
local v2, e2 = events:append_in_last(state, "choice", event, { _d = { tags = currentTags, block = line.child }})
|
local v2, e2 = events:append_in_last(state, "choice", event, { _state = choice_block_state })
|
||||||
if not v2 then return v2, e2 end
|
if not v2 then return v2, e2 end
|
||||||
end)
|
end)
|
||||||
v, e = eval_text_callback(state, line.text, function(text)
|
v, e = eval_text_callback(state, line.text, function(text)
|
||||||
local v2, e2 = events:append_in_last(state, "choice", { text = text, tags = currentTags }, { _d = { tags = currentTags, block = line.child }})
|
local v2, e2 = events:append_in_last(state, "choice", { text = text, tags = currentTags }, { _state = choice_block_state })
|
||||||
if not v2 then return v2, e2 end
|
if not v2 then return v2, e2 end
|
||||||
end)
|
end)
|
||||||
events:pop_capture(state, "text")
|
events:pop_capture(state, "text")
|
||||||
|
|
|
||||||
16
test/run.lua
16
test/run.lua
|
|
@ -21,20 +21,6 @@ local function format_text(t)
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
|
|
||||||
--- remove unneeded things from a result table (namely private fields)
|
|
||||||
local function strip(t, visited)
|
|
||||||
visited = visited or {}
|
|
||||||
for k, v in pairs(t) do
|
|
||||||
if type(k) == "string" and k:match("^_") then
|
|
||||||
t[k] = nil
|
|
||||||
end
|
|
||||||
if type(v) == "table" and not visited[v] then
|
|
||||||
visited[v] = true
|
|
||||||
strip(v, visited)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function compare(a, b)
|
local function compare(a, b)
|
||||||
if type(a) == "table" and type(b) == "table" then
|
if type(a) == "table" and type(b) == "table" then
|
||||||
for k, v in pairs(a) do
|
for k, v in pairs(a) do
|
||||||
|
|
@ -214,8 +200,6 @@ else
|
||||||
table.insert(result, { "error", err })
|
table.insert(result, { "error", err })
|
||||||
end
|
end
|
||||||
|
|
||||||
strip(result)
|
|
||||||
|
|
||||||
if args["write-all"] then
|
if args["write-all"] then
|
||||||
write_result(filebase, result)
|
write_result(filebase, result)
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue