runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
raw_stack.hpp
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_RUNTIME_RAW_STACK_HPP
3#define CEL_RUNTIME_RAW_STACK_HPP
4
5#include <array>
6#include <cel/runtime/value.hpp>
7#include <cstddef>
8#include <memory>
9#include <span>
10#include <tuple>
11#include <utility>
12
13namespace cel {
16
17namespace detail {
18class frame;
19class raw_segment;
20struct stack_state;
21struct frame_access;
22} // namespace detail
23
45// [knowledge] Transfer only internal canonical carriers bytewise, never public facades.
46// [knowledge] Keep the ownership-phase gate nonallocating and release it before ordinary callbacks.
47class raw_stack {
48 // [knowledge] A moved-from stack has no state; state() restores it on demand.
50 std::unique_ptr<detail::stack_state> _state;
51 friend class detail::frame;
52 friend struct detail::frame_access;
53
57 class phase_guard {
59 detail::stack_state* _state;
60
61 public:
63 explicit phase_guard(raw_stack& stack) noexcept;
65 ~phase_guard() noexcept;
67 phase_guard(const phase_guard&) = delete;
69 phase_guard& operator=(const phase_guard&) = delete;
70 };
74 detail::stack_state& state();
78 void require_idle() const;
83 void require_access(const detail::frame* owner) const;
96 template <class T>
97 T read_value(std::size_t floor) {
98 if (depth() <= floor) throw std::out_of_range("operand underflow");
99 phase_guard phase(*this);
100 const auto type = detail::boundary_adapter<T>::type();
101 return detail::boundary_adapter<T>::read(type, borrow(floor, 0, type));
102 }
113 std::size_t prepare(const value_type& type);
118 void publish(const value_type& type, std::size_t offset) noexcept;
129 void push_written(const value_type& type,
130 const void* source,
131 void (*write)(const void*, const value_type&, std::byte*));
140 void clone(const value& source);
148 void take(value& source);
158 const std::byte* borrow(std::size_t floor,
159 std::size_t from_top,
160 const value_type& expected) const;
168 const std::byte* borrow_trusted(std::size_t floor,
169 std::size_t from_top,
170 const value_type& expected) const;
178 void truncate(std::size_t floor) noexcept;
186 value extract(std::size_t floor);
198 void release(std::size_t floor, const value_type& expected, std::byte* destination);
199
200public:
214 raw_stack(const raw_stack&) = delete;
216 raw_stack& operator=(const raw_stack&) = delete;
249 void reserve(std::size_t bytes, std::size_t alignment);
260 void reserve_depth(std::size_t count);
268 void clear();
272 std::size_t depth() const noexcept;
276 std::size_t cursor() const noexcept;
288 template <class T>
289 void push(const T& source) {
290 require_access(nullptr);
291 push_written(detail::boundary_adapter<T>::type(), &source,
292 [](const void* input, const value_type& type, std::byte* destination) {
293 detail::boundary_adapter<T>::write(*static_cast<const T*>(input), type,
294 destination);
295 });
296 }
297
311 template <class T>
312 T pop() {
313 require_access(nullptr);
314 auto result = read_value<T>(0);
315 truncate(depth() - 1);
316 return result;
317 }
318
328 void push_clone(const value& source);
337 void push_take(value& source);
348};
349
351} // namespace cel
352#endif
raw_stack()
Creates an empty reusable stack.
~raw_stack()
Retires all live payloads and releases reserved storage.
void reserve_depth(std::size_t count)
Reserves bookkeeping for count simultaneously live operands, including zero-byte units.
std::size_t depth() const noexcept
Returns the number of live operands, excluding spare initialized bytes.
raw_stack & operator=(const raw_stack &)=delete
Disallows copied assignment.
void push(const T &source)
Appends a canonical copy of a concrete leaf or packed composite.
Definition raw_stack.hpp:289
raw_stack(raw_stack &&other)
Transfers other's storage and live operands, leaving other empty and reusable.
T pop()
Removes and returns an independent concrete copy of the top operand.
Definition raw_stack.hpp:312
void push_take(value &source)
Transfers canonical ownership, leaving source unit after success.
void push_clone(const value &source)
Clones a public owner into canonical stack storage.
value pop_value()
Removes the top operand and transfers its canonical ownership to the returned value.
void clear()
Retires every live operand while retaining reusable storage.
raw_stack & operator=(raw_stack &&other)
Retires prior operands and transfers other, leaving other empty and reusable.
void reserve(std::size_t bytes, std::size_t alignment)
Reserves bytes, alignment, and at least bytes live slots without changing the cursor.
raw_stack(const raw_stack &)=delete
Disallows duplicated mutable ownership.
std::size_t cursor() const noexcept
Returns the active byte cursor, including padding preceding live operands.
An immutable, shared semantic type with a canonical byte layout.
Definition value_type.hpp:65
Holds any type representable by CEL, including client-defined types.
Definition value.hpp:83
Byte extents for stack reservation and type descriptors.
Definition callable.hpp:6