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

ArkScript-lang / Ark / 13499352488

24 Feb 2025 01:37PM UTC coverage: 79.102% (+0.01%) from 79.091%
13499352488

push

github

SuperFola
fix(cmake): set fmtlib visibility to default, upgrade fmtlib to 11.1.3

5848 of 7393 relevant lines covered (79.1%)

59713.36 hits per line

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

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

3
#include <Ark/Constants.hpp>
4
#include <Ark/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 <picosha2.h>
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 :
320✔
20
        m_debug_level(0),
160✔
21
        m_libenv(libenv),
160✔
22
        m_filename(ARK_NO_NAME_FILE)
160✔
23
    {}
320✔
24

25
    bool State::feed(const std::string& bytecode_filename)
63✔
26
    {
63✔
27
        if (!Utils::fileExists(bytecode_filename))
63✔
28
            return false;
×
29

30
        return feed(Utils::readFileAsBytes(bytecode_filename));
63✔
31
    }
63✔
32

×
33
    bool State::feed(const bytecode_t& bytecode)
67✔
34
    {
67✔
35
        BytecodeReader bcr;
67✔
36
        bcr.feed(bytecode);
67✔
37
        if (!bcr.checkMagic())
67✔
38
            return false;
×
39

40
        m_bytecode = bytecode;
67✔
41

42
        try
×
43
        {
44
            configure(bcr);
67✔
45
            return true;
67✔
46
        }
×
47
        catch (const std::exception& e)  // FIXME I don't like this shit
48
        {
49
            fmt::println("{}", e.what());
×
50
            return false;
×
51
        }
×
52
    }
67✔
53

54
    bool State::compile(const std::string& file, const std::string& output, const uint16_t features) const
156✔
55
    {
156✔
56
        Welder welder(m_debug_level, m_libenv, features);
156✔
57
        for (auto& p : m_binded)
170✔
58
            welder.registerSymbol(p.first);
14✔
59

60
        if (!welder.computeASTFromFile(file))
156✔
61
            return false;
×
62
        if (!welder.generateBytecode())
97✔
63
            return false;
×
64

65
        const std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
63✔
66
        if (!welder.saveBytecodeToFile(destination))
63✔
67
            return false;
×
68

69
        return true;
63✔
70
    }
156✔
71

72
    bool State::doFile(const std::string& file, const uint16_t features)
156✔
73
    {
156✔
74
        if (!Utils::fileExists(file))
156✔
75
        {
76
            fmt::print(fmt::fg(fmt::color::red), "Can not find file '{}'\n", file);
×
77
            return false;
×
78
        }
×
79
        m_filename = file;
156✔
80

×
81
        const bytecode_t bytecode = Utils::readFileAsBytes(file);
156✔
82
        BytecodeReader bcr;
156✔
83
        bcr.feed(bytecode);
156✔
84
        if (!bcr.checkMagic())  // couldn't read magic number, it's a source file
156✔
85
        {
86
            // check if it's in the arkscript cache
87
            const std::string short_filename = (std::filesystem::path(file)).filename().string();
156✔
88
            const std::string filename = short_filename.substr(0, short_filename.find_last_of('.')) + ".arkc";
156✔
89
            const std::filesystem::path directory = (std::filesystem::path(file)).parent_path() / ARK_CACHE_DIRNAME;
156✔
90
            const std::string path = (directory / filename).string();
156✔
91

92
            if (!exists(directory))  // create ark cache directory
156✔
93
                create_directory(directory);
13✔
94

95
            if (compile(file, path, features) && feed(path))
156✔
96
                return true;
63✔
97
        }
156✔
98
        else if (feed(bytecode))  // it's a bytecode file
×
99
            return true;
×
100
        return false;
×
101
    }
249✔
102

103
    bool State::doString(const std::string& code, const uint16_t features)
4✔
104
    {
4✔
105
        Welder welder(m_debug_level, m_libenv, features);
4✔
106
        for (auto& p : m_binded)
8✔
107
            welder.registerSymbol(p.first);
4✔
108

109
        if (!welder.computeASTFromString(code))
4✔
110
            return false;
×
111
        if (!welder.generateBytecode())
4✔
112
            return false;
×
113
        return feed(welder.bytecode());
4✔
114
    }
4✔
115

116
    void State::loadFunction(const std::string& name, const Value::ProcType function) noexcept
4✔
117
    {
4✔
118
        m_binded[name] = Value(function);
4✔
119
    }
4✔
120

121
    void State::setArgs(const std::vector<std::string>& args) noexcept
7✔
122
    {
7✔
123
        Value val(ValueType::List);
7✔
124
        std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
7✔
125
            return Value(arg);
×
126
        });
127

128
        m_binded[std::string(internal::Language::SysArgs)] = val;
7✔
129
        m_binded[std::string(internal::Language::SysPlatform)] = Value(ARK_PLATFORM_NAME);
7✔
130
    }
7✔
131

132
    void State::setDebug(const unsigned level) noexcept
×
133
    {
×
134
        m_debug_level = level;
×
135
    }
×
136

137
    void State::setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept
×
138
    {
×
139
        m_libenv = libenv;
×
140
    }
×
141

142
    void State::configure(const BytecodeReader& bcr)
67✔
143
    {
67✔
144
        using namespace internal;
145

146
        const auto [major, minor, patch] = bcr.version();
67✔
147
        if (major != ARK_VERSION_MAJOR)
67✔
148
        {
149
            std::string str_version = std::to_string(major) + "." +
×
150
                std::to_string(minor) + "." +
×
151
                std::to_string(patch);
×
152
            throwStateError(fmt::format("Compiler and VM versions don't match: got {} while running {}", str_version, ARK_VERSION));
×
153
        }
×
154

155
        const auto bytecode_hash = bcr.sha256();
67✔
156

157
        std::vector<unsigned char> hash(picosha2::k_digest_size);
67✔
158
        picosha2::hash256(m_bytecode.begin() + 18 + picosha2::k_digest_size, m_bytecode.end(), hash);
67✔
159
        // checking integrity
1✔
160
        for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
2,211✔
161
        {
162
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
163
            if (hash[j] != bytecode_hash[j])
2,144✔
164
                throwStateError("Integrity check failed");
×
165
#endif
166
        }
2,144✔
167

168
        const auto syms = bcr.symbols();
67✔
169
        const auto vals = bcr.values(syms);
67✔
170
        const auto [pages, _] = bcr.code(vals);
134✔
171

172
        m_symbols = syms.symbols;
67✔
173
        m_constants = vals.values;
67✔
174
        m_pages = pages;
67✔
175
    }
67✔
176

177
    void State::reset() noexcept
×
178
    {
×
179
        m_symbols.clear();
×
180
        m_constants.clear();
×
181
        m_pages.clear();
×
182
        m_binded.clear();
×
183
    }
×
184
}
185

186
#ifdef _MSC_VER
187
#    pragma warning(pop)
188
#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

© 2025 Coveralls, Inc