---
source: framec/tests/gdscript_snapshots.rs
expression: "compile_fixture(\"12_no_persist\", \"gdscript\")"
---
class NoPersistFrameEvent:
    var _message
    var _parameters

    func _init(message: String, parameters):
        self._message = message
        self._parameters = parameters


class NoPersistFrameContext:
    var event
    var _return
    var _data
    var _transitioned

    func _init(event: NoPersistFrameEvent, default_return):
        self.event = event
        self._return = default_return
        self._data = {}
        self._transitioned = false


class NoPersistCompartment:
    var state
    var state_args
    var state_vars
    var enter_args
    var exit_args
    var forward_event
    var parent_compartment

    func _init(state: String, parent_compartment = null):
        self.state = state
        self.state_args = []
        self.state_vars = {}
        self.enter_args = []
        self.exit_args = []
        self.forward_event = null
        self.parent_compartment = parent_compartment

    func copy() -> NoPersistCompartment:
        var c = NoPersistCompartment.new(self.state, self.parent_compartment)
        c.state_args = self.state_args.duplicate()
        c.state_vars = self.state_vars.duplicate()
        c.enter_args = self.enter_args.duplicate()
        c.exit_args = self.exit_args.duplicate()
        c.forward_event = self.forward_event
        return c


class NoPersist:
    var _state_stack
    var __compartment
    var __next_compartment
    var _context_stack
    var count = 0
    var cache = -1

    func _init():
        self._state_stack = []
        self._context_stack = []
        self.__compartment = self.__prepareEnter("Active", [], [])
        self.__next_compartment = null

    static func _create():
        var c = NoPersist.new()
        var __e = NoPersistFrameEvent.new("$>", c.__compartment.enter_args)
        var __ctx = NoPersistFrameContext.new(__e, null)
        c._context_stack.append(__ctx)
        c.__kernel(__e)
        c._context_stack.pop_back()
        return c

    func hsm_chain() -> Dictionary:
        return {
            "Active": ["Active"],
        }
    func __prepareEnter(leaf, state_args, enter_args):
        var comp = null
        for name in self.hsm_chain()[leaf]:
            var new_comp = NoPersistCompartment.new(name)
            new_comp.state_args = state_args.duplicate()
            new_comp.enter_args = enter_args.duplicate()
            new_comp.parent_compartment = comp
            comp = new_comp
        return comp

    func __prepareExit(exit_args):
        var comp = self.__compartment
        while comp != null:
            comp.exit_args = exit_args.duplicate()
            comp = comp.parent_compartment

    func __kernel(__e):
        # Route event to current state
        self.__router(__e)
        # Drain any transitions queued by the handler
        while self.__next_compartment != null:
            var next_compartment = self.__next_compartment
            self.__next_compartment = null
            # Exit the current (leaf) state
            self.__router(NoPersistFrameEvent.new("<$", self.__compartment.exit_args))
            # Switch to the new compartment
            self.__compartment = next_compartment
            if next_compartment.forward_event == null:
                # No forwarded event — synthesize a fresh $>
                self.__router(NoPersistFrameEvent.new("$>", self.__compartment.enter_args))
            else:
                if next_compartment.forward_event._message == "$>":
                    # Forwarded event IS $> — dispatch directly so the
                    # destination's $> receives the caller's payload
                    self.__router(next_compartment.forward_event)
                else:
                    # Forwarded event is not $> — initialize the destination
                    # with a fresh $>, then dispatch the forward to it
                    self.__router(NoPersistFrameEvent.new("$>", self.__compartment.enter_args))
                    self.__router(next_compartment.forward_event)
            next_compartment.forward_event = null
            # Mark all stacked contexts as transitioned
            for ctx in self._context_stack:
                ctx._transitioned = true

    func __router(__e):
        if self.__compartment.state == "Active":
            self._state_Active(__e, self.__compartment)

    func __transition(next_compartment):
        self.__next_compartment = next_compartment

    func bump():
        var __e = NoPersistFrameEvent.new("bump", [])
        var __ctx = NoPersistFrameContext.new(__e, null)
        self._context_stack.append(__ctx)
        self.__kernel(__e)
        return self._context_stack.pop_back()._return

    func set_cache(v: int):
        var __e = NoPersistFrameEvent.new("set_cache", [v])
        var __ctx = NoPersistFrameContext.new(__e, null)
        self._context_stack.append(__ctx)
        self.__kernel(__e)
        return self._context_stack.pop_back()._return

    func get_count() -> int:
        var __e = NoPersistFrameEvent.new("get_count", [])
        var __ctx = NoPersistFrameContext.new(__e, null)
        self._context_stack.append(__ctx)
        self.__kernel(__e)
        return self._context_stack.pop_back()._return

    func get_cache() -> int:
        var __e = NoPersistFrameEvent.new("get_cache", [])
        var __ctx = NoPersistFrameContext.new(__e, null)
        self._context_stack.append(__ctx)
        self.__kernel(__e)
        return self._context_stack.pop_back()._return

    func _state_Active(__e, compartment):
        if __e._message == "bump":
            self._s_Active_hdl_user_bump(__e, compartment)
            return
        if __e._message == "get_cache":
            self._s_Active_hdl_user_get_cache(__e, compartment)
            return
        if __e._message == "get_count":
            self._s_Active_hdl_user_get_count(__e, compartment)
            return
        if __e._message == "set_cache":
            self._s_Active_hdl_user_set_cache(__e, compartment)
            return

    func _s_Active_hdl_user_bump(__e, compartment):
        self.count = self.count + 1

    func _s_Active_hdl_user_get_cache(__e, compartment):
        self._context_stack[-1]._return = self.cache

    func _s_Active_hdl_user_get_count(__e, compartment):
        self._context_stack[-1]._return = self.count

    func _s_Active_hdl_user_set_cache(__e, compartment):
        var v = __e._parameters[0]
        self.cache = v

    func snapshot() -> PackedByteArray:
        if not self._context_stack.is_empty():
            push_error("E700: system not quiescent")
            return PackedByteArray()
        # Serialize compartment chain iteratively
        var _ser_chain = func(comp):
            var chain = []
            var cur = comp
            while cur != null:
                chain.append(cur)
                cur = cur.parent_compartment
            chain.reverse()
            var result = null
            for c in chain:
                var d = {}
                d["state"] = c.state
                d["state_args"] = c.state_args.duplicate()
                d["state_vars"] = c.state_vars.duplicate()
                d["enter_args"] = c.enter_args.duplicate()
                d["exit_args"] = c.exit_args.duplicate()
                d["parent_compartment"] = result
                result = d
            return result
        var state_data = {}
        state_data["_compartment"] = _ser_chain.call(self.__compartment)
        var stack_arr = []
        for c in self._state_stack:
            stack_arr.append(_ser_chain.call(c))
        state_data["_state_stack"] = stack_arr
        state_data["count"] = self.count
        return var_to_bytes(state_data)

    func restore(data: PackedByteArray):
        var state_data = bytes_to_var(data)
        var _deser_chain = func(d):
            if d == null:
                return null
            # Collect chain into array (child first)
            var chain = []
            var cur = d
            while cur != null:
                chain.append(cur)
                cur = cur.get("parent_compartment", null)
            chain.reverse()
            var result = null
            for cd in chain:
                var comp = NoPersistCompartment.new(cd["state"])
                comp.state_args = cd.get("state_args", {})
                comp.state_vars = cd.get("state_vars", {})
                comp.enter_args = cd.get("enter_args", {})
                comp.exit_args = cd.get("exit_args", {})
                comp.parent_compartment = result
                result = comp
            return result
        self.__compartment = _deser_chain.call(state_data["_compartment"])
        self.__next_compartment = null
        self._state_stack = []
        for c in state_data.get("_state_stack", []):
            self._state_stack.append(_deser_chain.call(c))
        self._context_stack = []
        self.count = state_data.get("count", null)
