---
source: framec/tests/lua_snapshots.rs
expression: "compile_fixture(\"05_pushpop\", \"lua\")"
---
local PushPopFrameEvent = {}
PushPopFrameEvent.__index = PushPopFrameEvent

function PushPopFrameEvent.new(message, parameters)
    local self = setmetatable({}, PushPopFrameEvent)
    self._message = message
    self._parameters = parameters or {}
    return self
end


local PushPopFrameContext = {}
PushPopFrameContext.__index = PushPopFrameContext

function PushPopFrameContext.new(event, default_return)
    local self = setmetatable({}, PushPopFrameContext)
    self._event = event
    self._return = default_return
    self._data = {}
    self._transitioned = false
    return self
end


local PushPopCompartment = {}
PushPopCompartment.__index = PushPopCompartment

function PushPopCompartment.new(state, parent_compartment)
    local self = setmetatable({}, PushPopCompartment)
    self.state = state
    self.state_args = {}
    self.state_vars = {}
    self.enter_args = {}
    self.exit_args = {}
    self.forward_event = nil
    self.parent_compartment = parent_compartment
    return self
end

function PushPopCompartment:copy()
    local c = PushPopCompartment.new(self.state, self.parent_compartment)
    c.state_args = {}
    for k, v in pairs(self.state_args) do c.state_args[k] = v end
    c.state_vars = {}
    for k, v in pairs(self.state_vars) do c.state_vars[k] = v end
    c.enter_args = {}
    for k, v in pairs(self.enter_args) do c.enter_args[k] = v end
    c.exit_args = {}
    for k, v in pairs(self.exit_args) do c.exit_args[k] = v end
    c.forward_event = self.forward_event
    return c
end


local PushPop = {}
PushPop.__index = PushPop

function PushPop.new()
    local self = setmetatable({}, PushPop)
    self._state_stack = {}
    self._context_stack = {}
    self.pings = 0
    self.leaves = 0
    self.__compartment = self:__prepareEnter("Idle", nil, nil)
    self.__next_compartment = nil
    return self
end

function PushPop:_frame_init()
    local __e = PushPopFrameEvent.new("$>", self.__compartment.enter_args)
    local __ctx = PushPopFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    self:__kernel(__e)
    table.remove(self._context_stack)
end

function PushPop._create()
    local c = PushPop.new()
    c:_frame_init()
    return c
end

function PushPop:hsm_chain()
    local t = {}
    t["Idle"] = {"Idle"}
    t["Nested"] = {"Nested"}
    return t
end
function PushPop:__prepareEnter(leaf, state_args, enter_args)
    state_args = state_args or {}
    enter_args = enter_args or {}
    local comp = nil
    local chain = self:hsm_chain()[leaf]
    for i = 1, #chain do
        local new_comp = PushPopCompartment.new(chain[i])
        new_comp.state_args = {}
        for j = 1, #state_args do new_comp.state_args[j] = state_args[j] end
        new_comp.enter_args = {}
        for j = 1, #enter_args do new_comp.enter_args[j] = enter_args[j] end
        new_comp.parent_compartment = comp
        comp = new_comp
    end
    return comp
end

function PushPop:__prepareExit(exit_args)
    local comp = self.__compartment
    while comp ~= nil do
        comp.exit_args = {}
        for j = 1, #exit_args do comp.exit_args[j] = exit_args[j] end
        comp = comp.parent_compartment
    end
end

function PushPop:__kernel(__e)
    -- Route event to current state.
    self:__router(__e)
    -- Drain any transitions queued by the handler.
    while self.__next_compartment ~= nil do
        local next_compartment = self.__next_compartment
        self.__next_compartment = nil
        local exit_event = PushPopFrameEvent.new("<$", self.__compartment.exit_args)
        self:__router(exit_event)
        self.__compartment = next_compartment
        local forward_event = next_compartment.forward_event
        next_compartment.forward_event = nil
        if forward_event == nil then
            local enter_event = PushPopFrameEvent.new("$>", self.__compartment.enter_args)
            self:__router(enter_event)
        elseif forward_event._message == "$>" then
            self:__router(forward_event)
        else
            local enter_event = PushPopFrameEvent.new("$>", self.__compartment.enter_args)
            self:__router(enter_event)
            self:__router(forward_event)
        end
        for _, ctx in ipairs(self._context_stack) do
            ctx._transitioned = true
        end
    end
end

function PushPop:__router(__e)
    local handler = self["_state_" .. self.__compartment.state]
    if handler then
        handler(self, __e, self.__compartment)
    end
end

function PushPop:__transition(next_compartment)
    self.__next_compartment = next_compartment
end

function PushPop:ping()
    local __e = PushPopFrameEvent.new("ping", {})
    local __ctx = PushPopFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function PushPop:nest()
    local __e = PushPopFrameEvent.new("nest", {})
    local __ctx = PushPopFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function PushPop:unnest()
    local __e = PushPopFrameEvent.new("unnest", {})
    local __ctx = PushPopFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function PushPop:leaf()
    local __e = PushPopFrameEvent.new("leaf", {})
    local __ctx = PushPopFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function PushPop:_state_Idle(__e, compartment)
    if __e._message == "nest" then
        self:_s_Idle_hdl_user_nest(__e, compartment)
        return
    end
    if __e._message == "ping" then
        self:_s_Idle_hdl_user_ping(__e, compartment)
        return
    end
end

function PushPop:_state_Nested(__e, compartment)
    if __e._message == "leaf" then
        self:_s_Nested_hdl_user_leaf(__e, compartment)
        return
    end
    if __e._message == "unnest" then
        self:_s_Nested_hdl_user_unnest(__e, compartment)
        return
    end
end

function PushPop:_s_Idle_hdl_user_nest(__e, compartment)
    self._state_stack[#self._state_stack + 1] = self.__compartment
    local __compartment = self:__prepareEnter("Nested", nil, nil)
    self:__transition(__compartment)
    return
end

function PushPop:_s_Idle_hdl_user_ping(__e, compartment)
    self.pings = self.pings + 1
end

function PushPop:_s_Nested_hdl_user_leaf(__e, compartment)
    self.leaves = self.leaves + 1
end

function PushPop:_s_Nested_hdl_user_unnest(__e, compartment)
    local __saved = table.remove(self._state_stack)
    self:__transition(__saved)
    return
end
