ArkScript
A small, lisp-inspired, functional scripting language
ExecutionContext.hpp
Go to the documentation of this file.
1/**
2 * @file ExecutionContext.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
4 * @brief Keeping track of the internal data needed by the VM
5 * @date 2021-11-15
6 *
7 * @copyright Copyright (c) 2021-2025
8 *
9 */
10
11#ifndef ARK_VM_EXECUTIONCONTEXT_HPP
12#define ARK_VM_EXECUTIONCONTEXT_HPP
13
14#include <array>
15#include <memory>
16#include <optional>
17#include <atomic>
18
19#include <Ark/Constants.hpp>
20#include <Ark/VM/Value.hpp>
21#include <Ark/VM/ScopeView.hpp>
23
24namespace Ark::internal
25{
27 {
28 static inline unsigned Count = 0;
29
30 std::size_t ip {}; ///< Instruction pointer
31 std::size_t pp {}; ///< Page pointer
32 uint16_t sp {}; ///< Stack pointer
33 uint16_t fc {}; ///< Frame count
34 uint16_t last_symbol;
35 const bool primary; ///< Tells if the current ExecutionContext is the primary one or not
36 uint16_t inst_exec_counter {};
37 std::atomic_bool active;
38
39 std::optional<ClosureScope> saved_scope {}; ///< Scope created by CAPTURE <x> instructions, used by the MAKE_CLOSURE instruction
40 std::optional<uint16_t> capture_rename_id {};
41 std::vector<std::shared_ptr<ClosureScope>> stacked_closure_scopes {}; ///< Stack the closure scopes to keep the closure alive as long as we are calling them
42
43 std::vector<ScopeView> locals {};
44 std::array<ScopeView::pair_t, ScopeStackSize> scopes_storage {}; ///< All the ScopeView use this array to store id->value
45
46 std::array<Value, VMStackSizeWithOverflowBuffer> stack {};
47
48 ExecutionContext() noexcept :
50 primary(Count == 0)
51 {
52 active.store(true);
53 Count++;
54 }
55
56 [[nodiscard]] bool isFree() const
57 {
58 return !active.load();
59 }
60
61 void setActive(const bool toggle)
62 {
63 active.store(toggle);
64 }
65 };
66}
67
68#endif
Subtype of the value type, handling closures.
Constants used by ArkScript.
Default value type handled by the virtual machine.
constexpr uint16_t MaxValue16Bits
Definition Constants.hpp:70
std::array< ScopeView::pair_t, ScopeStackSize > scopes_storage
All the ScopeView use this array to store id->value.
std::vector< std::shared_ptr< ClosureScope > > stacked_closure_scopes
Stack the closure scopes to keep the closure alive as long as we are calling them.
std::optional< uint16_t > capture_rename_id
std::vector< ScopeView > locals
std::array< Value, VMStackSizeWithOverflowBuffer > stack
void setActive(const bool toggle)
const bool primary
Tells if the current ExecutionContext is the primary one or not.
std::size_t ip
Instruction pointer.
std::optional< ClosureScope > saved_scope
Scope created by CAPTURE <x> instructions, used by the MAKE_CLOSURE instruction.