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