9# pragma warning(disable : 4996)
19 State::State(
const std::vector<std::filesystem::path>& libenv) noexcept :
33 bool State::feed(
const std::string& bytecode_filename,
const bool fail_with_exception)
55 catch (
const std::exception& e)
57 if (fail_with_exception)
60 fmt::println(
"{}", e.what());
65 bool State::compile(
const std::string& file,
const std::string& output, std::ostream* stream)
68 if (stream !=
nullptr)
71 for (
const auto& key :
m_bound | std::views::keys)
79 const std::string destination = output.empty() ? (file.substr(0, file.find_last_of(
'.')) +
".arkc") : output;
88 bool State::doFile(
const std::string& file_path,
const uint16_t features, std::ostream* stream)
94 fmt::print(fmt::fg(fmt::color::red),
"Can not find file '{}'\n", file_path);
106 const std::string filename = std::filesystem::path(file_path).filename().replace_extension(
".arkc").string();
107 const std::filesystem::path cache_directory = std::filesystem::path(file_path).parent_path() /
ARK_CACHE_DIRNAME;
108 const std::string bytecode_path = (cache_directory / filename).
string();
114 create_directory(cache_directory);
116 catch (
const std::filesystem::filesystem_error&)
122 if (
compile(file_path, bytecode_path, stream))
125 else if (
feed(bytecode))
130 bool State::doString(
const std::string& code,
const uint16_t features, std::ostream* stream)
135 if (stream !=
nullptr)
150 m_bound[name] =
Value(std::move(function));
156 std::ranges::transform(args, std::back_inserter(val.
list()), [](
const std::string& arg) {
165 m_debug_level = level;
175 using namespace internal;
177 const auto [major, minor, patch] = bcr.
version();
180 const std::string str_version = fmt::format(
"{}.{}.{}", major, minor, patch);
184 const auto bytecode_hash = bcr.
sha256();
186 std::vector<unsigned char> hash(picosha2::k_digest_size);
189 for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
191#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
192 if (hash[j] != bytecode_hash[j])
197 const auto syms = bcr.
symbols();
198 const auto vals = bcr.
values(syms);
201 const auto [pages, _] = bcr.
code(inst_locs);
237 for (std::size_t i = 0, end = pages.size(); i < end; ++i)
239 for (std::size_t j = 0, end_j = pages[i].size(); j < end_j; ++j)
246 return std::ranges::max(pages, {}, &bytecode_t::size).size();
249 void State::extendBytecode(
const std::vector<bytecode_t>& pages,
const std::vector<std::string>& symbols,
const std::vector<Value>& constants)
A bytecode disassembler for ArkScript.
Constants used by ArkScript.
constexpr std::string_view ARK_VERSION
constexpr int ARK_VERSION_MAJOR
#define ARK_CACHE_DIRNAME
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.
Filenames filenames(const Values &values) const
InstLocations instLocations(const Filenames &filenames) 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
std::vector< std::filesystem::path > m_libenv
void setLibDirs(const std::vector< std::filesystem::path > &libenv) noexcept
Set the std search paths.
std::unordered_map< std::string, Value > m_bound
Values bound to the State, to be used by the VM.
void configure(const BytecodeReader &bcr)
Called to configure the state (set the bytecode, debug level, call the compiler......
bool doFile(const std::string &file_path, uint16_t features=DefaultFeatures, std::ostream *stream=nullptr)
Compile a file, and use the resulting bytecode.
std::vector< Value > m_constants
bool compile(const std::string &file, const std::string &output, std::ostream *stream=nullptr)
Reads and compiles code of file.
static void throwStateError(const std::string &message)
std::vector< internal::InstLoc > m_inst_locations
bool doString(const std::string &code, uint16_t features=DefaultFeatures, std::ostream *stream=nullptr)
Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.
std::vector< std::string > m_filenames
static std::size_t maxPageSize(const std::vector< bytecode_t > &pages)
Compute the maximum length of the given code pages.
void reset() noexcept
Reset State (all member variables related to execution)
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.
void loadFunction(const std::string &name, Procedure::CallbackType &&function) noexcept
Register a function in the virtual machine.
std::vector< std::string > m_symbols
void addPagesToContiguousBytecode(const std::vector< bytecode_t > &pages, std::size_t start)
std::size_t m_max_page_size
void setArgs(const std::vector< std::string > &args) noexcept
Set the script arguments in sys:args.
std::vector< bytecode_t > m_pages
State(const std::vector< std::filesystem::path > &libenv={}) noexcept
Construct a new State object.
void setDebug(unsigned level) noexcept
Set the debug level.
void extendBytecode(const std::vector< bytecode_t > &pages, const std::vector< std::string > &symbols, const std::vector< Value > &constants)
Used by the debugger to add code to the VM at runtime.
The welder joins all the compiler passes.
void registerSymbol(const std::string &name)
Register a symbol as a global in the compiler.
bool computeASTFromString(const std::string &code)
const bytecode_t & bytecode() const noexcept
void redirectLogsTo(std::ostream &os)
Redirect the logs to a given stream.
bool saveBytecodeToFile(const std::string &filename)
Save the generated bytecode to a given file.
bool generateBytecode()
Compile the AST processed by computeASTFromFile / computeASTFromString.
bool computeASTFromFile(const std::string &filename)
bool fileExists(const std::string &name) noexcept
Checks if a file exists.
std::vector< uint8_t > readFileAsBytes(const std::string &name)
Helper to read the bytes of a file.
constexpr std::string_view SysArgs
constexpr std::string_view SysProgramName
constexpr std::size_t HeaderSize
constexpr uint16_t DisableCache
std::vector< uint8_t > bytecode_t