ArkScript
A small, fast, functional and scripting language for video games
Closure.hpp
Go to the documentation of this file.
1/**
2 * @file Closure.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Subtype of the value type, handling closures
5 * @date 2024-04-21
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef VM_VALUE_CLOSURE_HPP
12#define VM_VALUE_CLOSURE_HPP
13
14#include <memory>
15#include <iostream>
16#include <string>
17
18#include <Ark/Platform.hpp>
19
20namespace Ark
21{
22 class VM;
23}
24
25namespace Ark::internal
26{
27 using PageAddr_t = uint16_t;
28
29 class ClosureScope;
30
31 /**
32 * @brief Closure management
33 *
34 */
35 class Closure
36 {
37 public:
38 /**
39 * @brief Construct a new Closure object
40 *
41 * @param scope the scope of the function turned into a closure
42 * @param pa the current page address of the function turned into a closure
43 */
44 Closure(const ClosureScope& scope, PageAddr_t pa) noexcept;
45
46 /**
47 * @brief Construct a new Closure object
48 * @param scope_ptr a shared pointer to the scope of the function turned into a closure
49 * @param pa the current page address of the function turned into a closure
50 */
51 Closure(const std::shared_ptr<ClosureScope>& scope_ptr, PageAddr_t pa) noexcept;
52
53 [[nodiscard]] const ClosureScope& scope() const noexcept { return *m_scope; }
54 [[nodiscard]] ClosureScope& refScope() const noexcept { return *m_scope; }
55 [[nodiscard]] const std::shared_ptr<ClosureScope>& scopePtr() const { return m_scope; }
56
57 /**
58 *
59 * @return PageAddr_t the bytecode page address this closure refers to
60 */
61 [[nodiscard]] PageAddr_t pageAddr() const { return m_page_addr; }
62
63 /**
64 * @brief Used when generating error messages in the VM, to see if a symbol might have been wrongly fully qualified
65 *
66 * @param end
67 * @param vm
68 * @return true if the closure has a field which is the end of 'end'
69 */
70 [[nodiscard]] bool hasFieldEndingWith(const std::string& end, const VM& vm) const;
71
72 /**
73 * @brief Print the closure to a string
74 *
75 * @param vm
76 */
77 std::string toString(VM& vm) const noexcept;
78
79 friend ARK_API bool operator==(const Closure& A, const Closure& B) noexcept;
80 friend ARK_API_INLINE bool operator<(const Closure& A, const Closure& B) noexcept;
81
82 private:
83 std::shared_ptr<ClosureScope> m_scope;
84 // keep track of the code page number, in case we need it later
86 };
87
88 inline bool operator<(const Closure& A, const Closure& B) noexcept
89 {
90 return A.m_page_addr < B.m_page_addr;
91 }
92}
93
94#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
#define ARK_API_INLINE
Definition Platform.hpp:50
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:45
A class to store fields captured by a closure.
Closure management.
Definition Closure.hpp:36
std::string toString(VM &vm) const noexcept
Print the closure to a string.
Definition Closure.cpp:27
PageAddr_t pageAddr() const
Definition Closure.hpp:61
PageAddr_t m_page_addr
Definition Closure.hpp:85
friend ARK_API_INLINE bool operator<(const Closure &A, const Closure &B) noexcept
Definition Closure.hpp:88
const ClosureScope & scope() const noexcept
Definition Closure.hpp:53
ClosureScope & refScope() const noexcept
Definition Closure.hpp:54
bool hasFieldEndingWith(const std::string &end, const VM &vm) const
Used when generating error messages in the VM, to see if a symbol might have been wrongly fully quali...
Definition Closure.cpp:20
friend ARK_API bool operator==(const Closure &A, const Closure &B) noexcept
Definition Closure.cpp:41
const std::shared_ptr< ClosureScope > & scopePtr() const
Definition Closure.hpp:55
std::shared_ptr< ClosureScope > m_scope
Definition Closure.hpp:83
bool operator<(const Namespace &, const Namespace &)
Definition Namespace.hpp:27
uint16_t PageAddr_t
Definition Closure.hpp:27