ArkScript
A small, lisp-inspired, functional scripting language
State.cpp
Go to the documentation of this file.
1#include <Ark/VM/State.hpp>
2
3#include <Ark/Constants.hpp>
4#include <Ark/Utils/Files.hpp>
6
7#ifdef _MSC_VER
8# pragma warning(push)
9# pragma warning(disable : 4996)
10#endif
11
12#include <Proxy/Picosha2.hpp>
14#include <fmt/core.h>
15#include <fmt/color.h>
16
17namespace Ark
18{
19 State::State(const std::vector<std::filesystem::path>& libenv) noexcept :
20 m_debug_level(0),
21 m_libenv(libenv),
22 m_filename(ARK_NO_NAME_FILE),
23 m_max_page_size(0)
24 {
25 // default value for builtin__sys:args is empty list
26 const Value val(ValueType::List);
27 m_binded[std::string(internal::Language::SysArgs)] = val;
28
29 m_binded[std::string(internal::Language::SysProgramName)] = Value("");
30 }
31
32 bool State::feed(const std::string& bytecode_filename, const bool fail_with_exception)
33 {
34 if (!Utils::fileExists(bytecode_filename))
35 return false;
36
37 return feed(Utils::readFileAsBytes(bytecode_filename), fail_with_exception);
38 }
39
40 bool State::feed(const bytecode_t& bytecode, const bool fail_with_exception)
41 {
43 bcr.feed(bytecode);
44 if (!bcr.checkMagic())
45 return false;
46
47 m_bytecode = bytecode;
48
49 try
50 {
51 configure(bcr);
52 return true;
53 }
54 catch (const std::exception& e)
55 {
56 if (fail_with_exception)
57 throw;
58
59 fmt::println("{}", e.what());
60 return false;
61 }
62 }
63
64 bool State::compile(const std::string& file, const std::string& output, const uint16_t features) const
65 {
66 Welder welder(m_debug_level, m_libenv, features);
67 for (const auto& p : m_binded)
68 welder.registerSymbol(p.first);
69
70 if (!welder.computeASTFromFile(file))
71 return false;
72 if (!welder.generateBytecode())
73 return false;
74
75 const std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
76 if (!welder.saveBytecodeToFile(destination))
77 return false;
78
79 return true;
80 }
81
82 bool State::doFile(const std::string& file_path, const uint16_t features)
83 {
84 if (!Utils::fileExists(file_path))
85 {
86 fmt::print(fmt::fg(fmt::color::red), "Can not find file '{}'\n", file_path);
87 return false;
88 }
89 m_filename = file_path;
90 m_binded[std::string(internal::Language::SysProgramName)] = Value(std::filesystem::path(m_filename).filename().string());
91
92 const bytecode_t bytecode = Utils::readFileAsBytes(file_path);
94 bcr.feed(bytecode);
95 if (!bcr.checkMagic()) // couldn't read magic number, it's a source file
96 {
97 // check if it's in the arkscript cache
98 const std::string filename = std::filesystem::path(file_path).filename().replace_extension(".arkc").string();
99 const std::filesystem::path cache_directory = std::filesystem::path(file_path).parent_path() / ARK_CACHE_DIRNAME;
100 const std::string bytecode_path = (cache_directory / filename).string();
101
102 if (!exists(cache_directory))
103 create_directory(cache_directory);
104
105 if (compile(file_path, bytecode_path, features) && feed(bytecode_path))
106 return true;
107 }
108 else if (feed(bytecode)) // it's a bytecode file
109 return true;
110 return false;
111 }
112
113 bool State::doString(const std::string& code, const uint16_t features)
114 {
115 Welder welder(m_debug_level, m_libenv, features);
116 for (const auto& p : m_binded)
117 welder.registerSymbol(p.first);
118
119 if (!welder.computeASTFromString(code))
120 return false;
121 if (!welder.generateBytecode())
122 return false;
123 return feed(welder.bytecode());
124 }
125
126 void State::loadFunction(const std::string& name, Procedure::CallbackType&& function) noexcept
127 {
128 m_binded[name] = Value(std::move(function));
129 }
130
131 void State::setArgs(const std::vector<std::string>& args) noexcept
132 {
134 std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
135 return Value(arg);
136 });
137
138 m_binded[std::string(internal::Language::SysArgs)] = val;
139 }
140
141 void State::setDebug(const unsigned level) noexcept
142 {
143 m_debug_level = level;
144 }
145
146 void State::setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept
147 {
148 m_libenv = libenv;
149 }
150
152 {
153 using namespace internal;
154
155 const auto [major, minor, patch] = bcr.version();
156 if (major != ARK_VERSION_MAJOR)
157 {
158 const std::string str_version = fmt::format("{}.{}.{}", major, minor, patch);
159 throwStateError(fmt::format("Compiler and VM versions don't match: got {} while running {}", str_version, ARK_VERSION));
160 }
161
162 const auto bytecode_hash = bcr.sha256();
163
164 std::vector<unsigned char> hash(picosha2::k_digest_size);
165 picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize + picosha2::k_digest_size, m_bytecode.end(), hash);
166 // checking integrity
167 for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
168 {
169#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
170 if (hash[j] != bytecode_hash[j])
171 throwStateError("Integrity check failed");
172#endif
173 }
174
175 const auto syms = bcr.symbols();
176 const auto vals = bcr.values(syms);
177 const auto files = bcr.filenames(vals);
178 const auto inst_locs = bcr.instLocations(files);
179 const auto [pages, _] = bcr.code(inst_locs);
180
181 m_symbols = syms.symbols;
182 m_constants = vals.values;
183 m_filenames = files.filenames;
184 m_inst_locations = inst_locs.locations;
185
186 m_max_page_size = 0;
187 for (const bytecode_t& page : pages)
188 {
189 if (page.size() > m_max_page_size)
190 m_max_page_size = page.size();
191 }
192
193 // Make m_code as a big contiguous chunk of instructions,
194 // aligned on the biggest page size.
195 // This might have a downside when we have a single big page and
196 // a bunch of smaller ones, though I couldn't measure it while testing.
197 m_code.resize(m_max_page_size * pages.size(), Instruction::NOP);
198 for (std::size_t i = 0, end = pages.size(); i < end; ++i)
199 {
200 for (std::size_t j = 0, end_j = pages[i].size(); j < end_j; ++j)
201 m_code[i * m_max_page_size + j] = pages[i][j];
202 }
203 }
204
205 void State::reset() noexcept
206 {
207 m_symbols.clear();
208 m_constants.clear();
209 m_filenames.clear();
210 m_inst_locations.clear();
211 m_max_page_size = 0;
212 m_code.clear();
213 m_binded.clear();
214
215 // default value for builtin__sys:args is empty list
216 const Value val(ValueType::List);
217 m_binded[std::string(internal::Language::SysArgs)] = val;
218
220 }
221}
222
223#ifdef _MSC_VER
224# pragma warning(pop)
225#endif
A bytecode disassembler for ArkScript.
Constants used by ArkScript.
constexpr std::string_view ARK_VERSION
Definition Constants.hpp:22
constexpr int ARK_VERSION_MAJOR
Definition Constants.hpp:18
#define ARK_NO_NAME_FILE
Definition Constants.hpp:27
#define ARK_CACHE_DIRNAME
Definition Constants.hpp:26
Lots of utilities about the filesystem.
State used by the virtual machine: it loads the bytecode, can compile it if needed,...
In charge of welding everything needed to compile code.
This class is just a helper to.
Symbols symbols() const
Filenames filenames(const Values &values) const
InstLocations instLocations(const Filenames &filenames) const
Version version() const
Code code(const InstLocations &instLocations) const
Values values(const Symbols &symbols) const
std::vector< unsigned char > sha256() const
void feed(const std::string &file)
Construct needed data before displaying information about a given file.
std::function< Value(std::vector< Value > &, VM *)> CallbackType
Definition Procedure.hpp:29
std::vector< std::filesystem::path > m_libenv
Definition State.hpp:148
void setLibDirs(const std::vector< std::filesystem::path > &libenv) noexcept
Set the std search paths.
Definition State.cpp:146
void configure(const BytecodeReader &bcr)
Called to configure the state (set the bytecode, debug level, call the compiler......
Definition State.cpp:151
bytecode_t m_bytecode
Definition State.hpp:147
bool doFile(const std::string &file_path, uint16_t features=DefaultFeatures)
Compile a file, and use the resulting bytecode.
Definition State.cpp:82
std::string m_filename
Definition State.hpp:149
std::vector< Value > m_constants
Definition State.hpp:153
static void throwStateError(const std::string &message)
Definition State.hpp:140
std::vector< internal::InstLoc > m_inst_locations
Definition State.hpp:155
std::vector< std::string > m_filenames
Definition State.hpp:154
void reset() noexcept
Reset State (all member variables related to execution)
Definition State.cpp:205
bool feed(const std::string &bytecode_filename, bool fail_with_exception=false)
Feed the state by giving it the path to an existing bytecode file.
Definition State.cpp:32
std::unordered_map< std::string, Value > m_binded
Values binded to the State, to be used by the VM.
Definition State.hpp:160
void loadFunction(const std::string &name, Procedure::CallbackType &&function) noexcept
Register a function in the virtual machine.
Definition State.cpp:126
std::vector< std::string > m_symbols
Definition State.hpp:152
bytecode_t m_code
Definition State.hpp:157
std::size_t m_max_page_size
Definition State.hpp:156
bool compile(const std::string &file, const std::string &output, uint16_t features) const
Reads and compiles code of file.
Definition State.cpp:64
void setArgs(const std::vector< std::string > &args) noexcept
Set the script arguments in sys:args.
Definition State.cpp:131
bool doString(const std::string &code, uint16_t features=DefaultFeatures)
Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.
Definition State.cpp:113
unsigned m_debug_level
Definition State.hpp:145
State(const std::vector< std::filesystem::path > &libenv={}) noexcept
Construct a new State object.
Definition State.cpp:19
void setDebug(unsigned level) noexcept
Set the debug level.
Definition State.cpp:141
List_t & list()
Definition Value.hpp:169
The welder joins all the compiler passes.
Definition Welder.hpp:37
void registerSymbol(const std::string &name)
Register a symbol as a global in the compiler.
Definition Welder.cpp:31
bool computeASTFromString(const std::string &code)
Definition Welder.cpp:44
const bytecode_t & bytecode() const noexcept
Definition Welder.cpp:109
bool saveBytecodeToFile(const std::string &filename)
Save the generated bytecode to a given file.
Definition Welder.cpp:82
bool generateBytecode()
Compile the AST processed by computeASTFromFile / computeASTFromString.
Definition Welder.cpp:51
bool computeASTFromFile(const std::string &filename)
Definition Welder.cpp:36
bool fileExists(const std::string &name) noexcept
Checks if a file exists.
Definition Files.hpp:28
std::vector< uint8_t > readFileAsBytes(const std::string &name)
Helper to read the bytes of a file.
Definition Files.hpp:62
constexpr std::string_view SysArgs
Definition Common.hpp:127
constexpr std::string_view SysProgramName
Definition Common.hpp:128
constexpr std::size_t HeaderSize
Definition Common.hpp:39
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22