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

ArkScript-lang / Ark / 11087505602

28 Sep 2024 10:31PM UTC coverage: 75.218% (+1.6%) from 73.576%
11087505602

push

github

SuperFola
fix(json compiler): disable import solver in json compiler

1 of 1 new or added line in 1 file covered. (100.0%)

364 existing lines in 24 files now uncovered.

4841 of 6436 relevant lines covered (75.22%)

9644.12 hits per line

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

60.16
/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 :
156✔
20
        m_debug_level(0),
78✔
21
        m_libenv(libenv),
78✔
22
        m_filename(ARK_NO_NAME_FILE)
78✔
23
    {}
156✔
24

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

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

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

40
        m_bytecode = bytecode;
26✔
41

42
        try
×
43
        {
44
            configure(bcr);
26✔
45
            return true;
26✔
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
    }
26✔
53

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

60
        if (!welder.computeASTFromFile(file, (features & FeatureTestFailOnException) > 0))
74✔
61
            return false;
×
62
        if (!welder.generateBytecode((features & FeatureTestFailOnException) > 0))
35✔
63
            return false;
×
64

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

69
        return true;
22✔
70
    }
74✔
71

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

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

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

95
            if (compile(file, path, features) && feed(path))
74✔
96
                return true;
22✔
97
        }
74✔
98
        else if (feed(bytecode))  // it's a bytecode file
×
99
            return true;
×
100
        return false;
×
101
    }
126✔
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, (features & FeatureTestFailOnException) > 0))
4✔
110
            return false;
×
111
        if (!welder.generateBytecode((features & FeatureTestFailOnException) > 0))
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
×
122
    {
×
123
        Value val(ValueType::List);
×
124
        std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
×
125
            return Value(arg);
×
126
        });
127

UNCOV
128
        m_binded[std::string(internal::Language::SysArgs)] = val;
×
129
        m_binded[std::string(internal::Language::SysPlatform)] = Value(ARK_PLATFORM_NAME);
×
130
    }
×
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)
26✔
143
    {
26✔
144
        using namespace internal;
145

146
        const auto [major, minor, patch] = bcr.version();
26✔
147
        if (major != ARK_VERSION_MAJOR)
26✔
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();
26✔
156

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

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

172
        m_symbols = syms.symbols;
26✔
173
        m_constants = vals.values;
26✔
174
        m_pages = pages;
26✔
175
    }
26✔
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

© 2026 Coveralls, Inc