ArkScript
A small, lisp-inspired, functional scripting language
ScopeView.cpp
Go to the documentation of this file.
2
3#include <cassert>
4
5namespace Ark::internal
6{
7 void ScopeView::insertFront(const std::vector<pair_t>& values) noexcept
8 {
9 const std::size_t offset_by = values.size();
10 // If there is one day a bug with bad references, this can be caused by this code,
11 // called when inserting plugins variables in a scope (because we invalidate said
12 // references by moving them to another slot inside m_storage).
13 for (std::size_t i = 0; i < m_size; ++i)
14 {
15 // This is a weak attempt to prevent / notice the bug before it goes in production,
16 // if you hit this assertion read the comments carefully!
17 assert(m_storage[m_start + m_size - i - 1].second.valueType() != ValueType::Reference && "References can not be moved around!");
18 m_storage[m_start + m_size - i + offset_by - 1] = m_storage[m_start + m_size - i - 1];
19 }
20
21 std::size_t i = 0;
22 for (const pair_t& pair : values)
23 {
24 const uint16_t id = pair.first;
25 if (id < m_min_id)
26 m_min_id = id;
27 if (id > m_max_id)
28 m_max_id = id;
29
30 m_storage[m_start + i] = pair;
31 ++i;
32 }
33
34 m_size += offset_by;
35 }
36
37 uint16_t ScopeView::idFromValue(const Value& val) const noexcept
38 {
39 for (std::size_t i = m_start; i < m_start + m_size; ++i)
40 {
41 const auto& [id, value] = m_storage[i];
42 if (value == val)
43 return id;
44 }
45 return MaxValue16Bits;
46 }
47
48 bool operator==(const ScopeView& A, const ScopeView& B) noexcept
49 {
50 // if we have two scopes with the same number of elements and starting at the same position,
51 // they must be identical, as we have a single storage for all scopes
52 return A.m_size == B.m_size && A.m_start == B.m_start;
53 }
54}
The virtual machine scope system.
A class to handle the VM scope more efficiently.
Definition ScopeView.hpp:27
void insertFront(const std::vector< pair_t > &values) noexcept
Insert one or more pairs at the beginning of the scope.
Definition ScopeView.cpp:7
std::pair< uint16_t, Value > pair_t
Definition ScopeView.hpp:29
uint16_t idFromValue(const Value &val) const noexcept
Get the id of a variable based on its value ; used for debug only.
Definition ScopeView.cpp:37
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21
constexpr uint16_t MaxValue16Bits
Definition Constants.hpp:81