• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 22870305923

09 Mar 2026 07:10PM UTC coverage: 93.778% (+0.3%) from 93.519%
22870305923

push

github

SuperFola
feat(logs): allow using a custom output stream for warnings

41 of 42 new or added lines in 12 files covered. (97.62%)

28 existing lines in 5 files now uncovered.

9510 of 10141 relevant lines covered (93.78%)

270952.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.33
/src/arkreactor/State.cpp
1
#include <Ark/State.hpp>
2

3
#include <Ark/Constants.hpp>
4
#include <Ark/Utils/Files.hpp>
5
#include <Ark/Compiler/Welder.hpp>
6

7
#ifdef _MSC_VER
8
#    pragma warning(push)
9
#    pragma warning(disable : 4996)
10
#endif
11

12
#include <Proxy/Picosha2.hpp>
13
#include <Ark/Compiler/BytecodeReader.hpp>
14
#include <fmt/core.h>
15
#include <fmt/color.h>
16

17
namespace Ark
18
{
19
    State::State(const std::vector<std::filesystem::path>& libenv) noexcept :
1,242✔
20
        m_debug_level(0),
414✔
21
        m_features(0),
414✔
22
        m_libenv(libenv),
414✔
23
        m_filename(ARK_NO_NAME_FILE),
414✔
24
        m_max_page_size(0)
414✔
25
    {
414✔
26
        // default value for builtin__sys:args is empty list
27
        const Value val(ValueType::List);
414✔
28
        m_bound[std::string(internal::Language::SysArgs)] = val;
414✔
29

30
        m_bound[std::string(internal::Language::SysProgramName)] = Value("");
414✔
31
    }
414✔
32

33
    bool State::feed(const std::string& bytecode_filename, const bool fail_with_exception)
×
34
    {
×
35
        if (!Utils::fileExists(bytecode_filename))
×
36
            return false;
×
37

38
        return feed(Utils::readFileAsBytes(bytecode_filename), fail_with_exception);
×
39
    }
×
40

41
    bool State::feed(const bytecode_t& bytecode, const bool fail_with_exception)
284✔
42
    {
284✔
43
        BytecodeReader bcr;
284✔
44
        bcr.feed(bytecode);
284✔
45
        if (!bcr.checkMagic())
284✔
46
            return false;
×
47

48
        m_bytecode = bytecode;
284✔
49

50
        try
51
        {
52
            configure(bcr);
284✔
53
            return true;
282✔
54
        }
2✔
55
        catch (const std::exception& e)
56
        {
57
            if (fail_with_exception)
2✔
58
                throw;
2✔
59

60
            fmt::println("{}", e.what());
×
61
            return false;
×
62
        }
2✔
63
    }
288✔
64

65
    bool State::compile(const std::string& file, const std::string& output, std::ostream* stream)
390✔
66
    {
390✔
67
        Welder welder(m_debug_level, m_libenv, m_features);
390✔
68
        if (stream != nullptr)
390✔
69
            welder.redirectLogsTo(*stream);
3✔
70

71
        for (const auto& key : m_bound | std::views::keys)
1,176✔
72
            welder.registerSymbol(key);
786✔
73

74
        if (!welder.computeASTFromFile(file))
390✔
75
            return false;
×
76
        if (!welder.generateBytecode())
311✔
77
            return false;
×
78

79
        const std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
268✔
80
        if ((m_features & DisableCache) == 0 && !welder.saveBytecodeToFile(destination))
268✔
81
            return false;
×
82
        if (!feed(welder.bytecode()))
268✔
83
            return false;
×
84

85
        return true;
268✔
86
    }
390✔
87

88
    bool State::doFile(const std::string& file_path, const uint16_t features, std::ostream* stream)
390✔
89
    {
390✔
90
        m_features = features;
390✔
91

92
        if (!Utils::fileExists(file_path))
390✔
93
        {
94
            fmt::print(fmt::fg(fmt::color::red), "Can not find file '{}'\n", file_path);
×
95
            return false;
×
96
        }
97
        m_filename = file_path;
390✔
98
        m_bound[std::string(internal::Language::SysProgramName)] = Value(std::filesystem::path(m_filename).filename().string());
512✔
99

100
        const bytecode_t bytecode = Utils::readFileAsBytes(file_path);
390✔
101
        BytecodeReader bcr;
390✔
102
        bcr.feed(bytecode);
390✔
103
        if (!bcr.checkMagic())  // couldn't read magic number, it's a source file
390✔
104
        {
105
            // check if it's in the arkscript cache
106
            const std::string filename = std::filesystem::path(file_path).filename().replace_extension(".arkc").string();
390✔
107
            const std::filesystem::path cache_directory = std::filesystem::path(file_path).parent_path() / ARK_CACHE_DIRNAME;
390✔
108
            const std::string bytecode_path = (cache_directory / filename).string();
390✔
109

110
            if (!exists(cache_directory) && (m_features & DisableCache) == 0)
390✔
111
            {
112
                try
113
                {
114
                    create_directory(cache_directory);
19✔
115
                }
19✔
116
                catch (const std::filesystem::filesystem_error&)
117
                {
118
                    m_features |= DisableCache;
×
119
                }
19✔
120
            }
19✔
121

122
            if (compile(file_path, bytecode_path, stream))
390✔
123
                return true;
268✔
124
        }
390✔
125
        else if (feed(bytecode))  // it's a bytecode file
×
126
            return true;
×
127
        return false;
×
128
    }
512✔
129

130
    bool State::doString(const std::string& code, const uint16_t features, std::ostream* stream)
13✔
131
    {
13✔
132
        m_features = features;
13✔
133

134
        Welder welder(m_debug_level, m_libenv, m_features);
13✔
135
        if (stream != nullptr)
13✔
NEW
136
            welder.redirectLogsTo(*stream);
×
137

138
        for (const auto& p : m_bound)
47✔
139
            welder.registerSymbol(p.first);
34✔
140

141
        if (!welder.computeASTFromString(code))
13✔
142
            return false;
×
143
        if (!welder.generateBytecode())
12✔
144
            return false;
×
145
        return feed(welder.bytecode());
12✔
146
    }
13✔
147

148
    void State::loadFunction(const std::string& name, Procedure::CallbackType&& function) noexcept
14✔
149
    {
14✔
150
        m_bound[name] = Value(std::move(function));
14✔
151
    }
14✔
152

153
    void State::setArgs(const std::vector<std::string>& args) noexcept
10✔
154
    {
10✔
155
        Value val(ValueType::List);
10✔
156
        std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
13✔
157
            return Value(arg);
3✔
158
        });
159

160
        m_bound[std::string(internal::Language::SysArgs)] = val;
10✔
161
    }
10✔
162

163
    void State::setDebug(const unsigned level) noexcept
1✔
164
    {
1✔
165
        m_debug_level = level;
1✔
166
    }
1✔
167

168
    void State::setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept
×
169
    {
×
170
        m_libenv = libenv;
×
171
    }
×
172

173
    void State::configure(const BytecodeReader& bcr)
284✔
174
    {
284✔
175
        using namespace internal;
176

177
        const auto [major, minor, patch] = bcr.version();
284✔
178
        if (major != ARK_VERSION_MAJOR)
284✔
179
        {
180
            const std::string str_version = fmt::format("{}.{}.{}", major, minor, patch);
2✔
181
            throwStateError(fmt::format("Compiler and VM versions don't match: got {} while running {}", str_version, ARK_VERSION));
1✔
182
        }
1✔
183

184
        const auto bytecode_hash = bcr.sha256();
283✔
185

186
        std::vector<unsigned char> hash(picosha2::k_digest_size);
283✔
187
        picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize + picosha2::k_digest_size, m_bytecode.end(), hash);
283✔
188
        // checking integrity
189
        for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
9,310✔
190
        {
191
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
192
            if (hash[j] != bytecode_hash[j])
9,027✔
193
                throwStateError("Integrity check failed");
1✔
194
#endif
195
        }
9,026✔
196

197
        const auto syms = bcr.symbols();
282✔
198
        const auto vals = bcr.values(syms);
282✔
199
        const auto files = bcr.filenames(vals);
282✔
200
        const auto inst_locs = bcr.instLocations(files);
282✔
201
        const auto [pages, _] = bcr.code(inst_locs);
846✔
202

203
        m_symbols = syms.symbols;
282✔
204
        m_constants = vals.values;
282✔
205
        m_filenames = files.filenames;
282✔
206
        m_inst_locations = inst_locs.locations;
282✔
207
        m_pages = pages;
282✔
208
        m_max_page_size = maxPageSize(m_pages);
282✔
209

210
        // Make m_code as a big contiguous chunk of instructions,
211
        // aligned on the biggest page size.
212
        // This might have a downside when we have a single big page and
213
        // a bunch of smaller ones, though I couldn't measure it while testing.
214
        m_code.resize(m_max_page_size * pages.size(), Instruction::NOP);
282✔
215
        addPagesToContiguousBytecode(pages, /* start= */ 0);
282✔
216
    }
286✔
217

218
    void State::reset() noexcept
×
219
    {
×
220
        m_symbols.clear();
×
221
        m_constants.clear();
×
222
        m_filenames.clear();
×
223
        m_inst_locations.clear();
×
224
        m_max_page_size = 0;
×
225
        m_code.clear();
×
226
        m_bound.clear();
×
227

228
        // default value for builtin__sys:args is empty list
229
        const Value val(ValueType::List);
×
230
        m_bound[std::string(internal::Language::SysArgs)] = val;
×
231

232
        m_bound[std::string(internal::Language::SysProgramName)] = Value("");
×
233
    }
×
234

235
    void State::addPagesToContiguousBytecode(const std::vector<bytecode_t>& pages, const std::size_t start)
310✔
236
    {
310✔
237
        for (std::size_t i = 0, end = pages.size(); i < end; ++i)
4,394✔
238
        {
239
            for (std::size_t j = 0, end_j = pages[i].size(); j < end_j; ++j)
410,712✔
240
                m_code[(start + i) * m_max_page_size + j] = pages[i][j];
406,628✔
241
        }
4,084✔
242
    }
310✔
243

244
    std::size_t State::maxPageSize(const std::vector<bytecode_t>& pages)
296✔
245
    {
296✔
246
        return std::ranges::max(pages, {}, &bytecode_t::size).size();
296✔
247
    }
248

249
    void State::extendBytecode(const std::vector<bytecode_t>& pages, const std::vector<std::string>& symbols, const std::vector<Value>& constants)
14✔
250
    {
14✔
251
        m_symbols = symbols;
14✔
252
        m_constants = constants;
14✔
253

254
        // do not modify m_pages so that we can start over
255
        m_max_page_size = std::max(m_max_page_size, maxPageSize(pages));
14✔
256

257
        m_code.resize(m_max_page_size * (m_pages.size() + pages.size()), internal::Instruction::NOP);
14✔
258
        addPagesToContiguousBytecode(m_pages, /* start= */ 0);
14✔
259
        addPagesToContiguousBytecode(pages, /* start= */ m_pages.size());
14✔
260
    }
14✔
261
}
262

263
#ifdef _MSC_VER
264
#    pragma warning(pop)
265
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc