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

ArkScript-lang / Ark / 17215213570

25 Aug 2025 04:52PM UTC coverage: 87.662% (+0.3%) from 87.348%
17215213570

push

github

SuperFola
feat(tests): adding double plugin loading test

7581 of 8648 relevant lines covered (87.66%)

133240.4 hits per line

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

76.03
/src/arkreactor/VM/State.cpp
1
#include <Ark/VM/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 :
849✔
20
        m_debug_level(0),
283✔
21
        m_libenv(libenv),
283✔
22
        m_filename(ARK_NO_NAME_FILE),
283✔
23
        m_max_page_size(0)
283✔
24
    {
283✔
25
        // default value for builtin__sys:args is empty list
26
        const Value val(ValueType::List);
283✔
27
        m_binded[std::string(internal::Language::SysArgs)] = val;
283✔
28

29
        m_binded[std::string(internal::Language::SysProgramName)] = Value("");
283✔
30
    }
283✔
31

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

37
        return feed(Utils::readFileAsBytes(bytecode_filename), fail_with_exception);
173✔
38
    }
173✔
39

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

47
        m_bytecode = bytecode;
185✔
48

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

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

64
    bool State::compile(const std::string& file, const std::string& output, const uint16_t features) const
271✔
65
    {
271✔
66
        Welder welder(m_debug_level, m_libenv, features);
271✔
67
        for (const auto& p : m_binded)
813✔
68
            welder.registerSymbol(p.first);
542✔
69

70
        if (!welder.computeASTFromFile(file))
271✔
71
            return false;
×
72
        if (!welder.generateBytecode())
207✔
73
            return false;
×
74

75
        const std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
173✔
76
        if (!welder.saveBytecodeToFile(destination))
173✔
77
            return false;
×
78

79
        return true;
173✔
80
    }
271✔
81

82
    bool State::doFile(const std::string& file_path, const uint16_t features)
271✔
83
    {
271✔
84
        if (!Utils::fileExists(file_path))
271✔
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;
271✔
90
        m_binded[std::string(internal::Language::SysProgramName)] = Value(std::filesystem::path(m_filename).filename().string());
369✔
91

92
        const bytecode_t bytecode = Utils::readFileAsBytes(file_path);
271✔
93
        BytecodeReader bcr;
271✔
94
        bcr.feed(bytecode);
271✔
95
        if (!bcr.checkMagic())  // couldn't read magic number, it's a source file
271✔
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();
271✔
99
            const std::filesystem::path cache_directory = std::filesystem::path(file_path).parent_path() / ARK_CACHE_DIRNAME;
271✔
100
            const std::string bytecode_path = (cache_directory / filename).string();
271✔
101

102
            if (!exists(cache_directory))
271✔
103
                create_directory(cache_directory);
16✔
104

105
            if (compile(file_path, bytecode_path, features) && feed(bytecode_path))
271✔
106
                return true;
173✔
107
        }
271✔
108
        else if (feed(bytecode))  // it's a bytecode file
×
109
            return true;
×
110
        return false;
×
111
    }
369✔
112

113
    bool State::doString(const std::string& code, const uint16_t features)
10✔
114
    {
10✔
115
        Welder welder(m_debug_level, m_libenv, features);
10✔
116
        for (const auto& p : m_binded)
37✔
117
            welder.registerSymbol(p.first);
27✔
118

119
        if (!welder.computeASTFromString(code))
10✔
120
            return false;
×
121
        if (!welder.generateBytecode())
10✔
122
            return false;
×
123
        return feed(welder.bytecode());
10✔
124
    }
10✔
125

126
    void State::loadFunction(const std::string& name, Procedure::CallbackType&& function) noexcept
7✔
127
    {
7✔
128
        m_binded[name] = Value(std::move(function));
7✔
129
    }
7✔
130

131
    void State::setArgs(const std::vector<std::string>& args) noexcept
10✔
132
    {
10✔
133
        Value val(ValueType::List);
10✔
134
        std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
13✔
135
            return Value(arg);
3✔
136
        });
137

138
        m_binded[std::string(internal::Language::SysArgs)] = val;
10✔
139
    }
10✔
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

151
    void State::configure(const BytecodeReader& bcr)
185✔
152
    {
185✔
153
        using namespace internal;
154

155
        const auto [major, minor, patch] = bcr.version();
185✔
156
        if (major != ARK_VERSION_MAJOR)
185✔
157
        {
158
            const std::string str_version = fmt::format("{}.{}.{}", major, minor, patch);
2✔
159
            throwStateError(fmt::format("Compiler and VM versions don't match: got {} while running {}", str_version, ARK_VERSION));
1✔
160
        }
1✔
161

162
        const auto bytecode_hash = bcr.sha256();
184✔
163

164
        std::vector<unsigned char> hash(picosha2::k_digest_size);
184✔
165
        picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize + picosha2::k_digest_size, m_bytecode.end(), hash);
184✔
166
        // checking integrity
167
        for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
6,043✔
168
        {
169
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
170
            if (hash[j] != bytecode_hash[j])
5,859✔
171
                throwStateError("Integrity check failed");
1✔
172
#endif
173
        }
5,858✔
174

175
        const auto syms = bcr.symbols();
183✔
176
        const auto vals = bcr.values(syms);
183✔
177
        const auto files = bcr.filenames(vals);
183✔
178
        const auto inst_locs = bcr.instLocations(files);
183✔
179
        const auto [pages, _] = bcr.code(inst_locs);
223,482✔
180

181
        m_symbols = syms.symbols;
183✔
182
        m_constants = vals.values;
183✔
183
        m_filenames = files.filenames;
183✔
184
        m_inst_locations = inst_locs.locations;
183✔
185

186
        m_max_page_size = 0;
183✔
187
        for (const bytecode_t& page : pages)
2,939✔
188
        {
189
            if (page.size() > m_max_page_size)
2,573✔
190
                m_max_page_size = page.size();
204✔
191
        }
2,573✔
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);
183✔
198
        for (std::size_t i = 0, end = pages.size(); i < end; ++i)
2,939✔
199
        {
200
            for (std::size_t j = 0, end_j = pages[i].size(); j < end_j; ++j)
225,506✔
201
                m_code[i * m_max_page_size + j] = pages[i][j];
220,360✔
202
        }
2,573✔
203
    }
187✔
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

219
        m_binded[std::string(internal::Language::SysProgramName)] = Value("");
×
220
    }
×
221
}
222

223
#ifdef _MSC_VER
224
#    pragma warning(pop)
225
#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