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

ArkScript-lang / Ark / 16781701999

06 Aug 2025 03:42PM UTC coverage: 86.718% (+0.5%) from 86.247%
16781701999

push

github

SuperFola
fix: addressing cppcheck warnings

3 of 3 new or added lines in 3 files covered. (100.0%)

135 existing lines in 1 file now uncovered.

7502 of 8651 relevant lines covered (86.72%)

126395.65 hits per line

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

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

3
#include <utility>
4
#include <numeric>
5
#include <limits>
6
#include <fmt/core.h>
7
#include <fmt/color.h>
8
#include <fmt/ostream.h>
9

10
#include <Ark/Utils/Files.hpp>
11
#include <Ark/Utils/Utils.hpp>
12
#include <Ark/TypeChecker.hpp>
13
#include <Ark/Compiler/Instructions.hpp>
14

15
struct mapping
16
{
17
    char* name;
18
    Ark::Value (*value)(std::vector<Ark::Value>&, Ark::VM*);
19
};
20

21
namespace Ark
22
{
23
    using namespace internal;
24

25
    namespace helper
26
    {
27
        inline Value tail(Value* a)
337✔
28
        {
337✔
29
            if (a->valueType() == ValueType::List)
337✔
30
            {
31
                if (a->constList().size() < 2)
69✔
32
                    return Value(ValueType::List);
18✔
33

34
                std::vector<Value> tmp(a->constList().size() - 1);
51✔
35
                for (std::size_t i = 1, end = a->constList().size(); i < end; ++i)
327✔
36
                    tmp[i - 1] = a->constList()[i];
276✔
37
                return Value(std::move(tmp));
51✔
38
            }
52✔
39
            if (a->valueType() == ValueType::String)
268✔
40
            {
41
                if (a->string().size() < 2)
267✔
42
                    return Value(ValueType::String);
50✔
43

44
                Value b { *a };
217✔
45
                b.stringRef().erase(b.stringRef().begin());
217✔
46
                return b;
217✔
47
            }
217✔
48

49
            throw types::TypeCheckingError(
2✔
50
                "tail",
1✔
51
                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
52
                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
53
                { *a });
1✔
54
        }
337✔
55

56
        inline Value head(Value* a)
1,131✔
57
        {
1,131✔
58
            if (a->valueType() == ValueType::List)
1,131✔
59
            {
60
                if (a->constList().empty())
863✔
61
                    return Builtins::nil;
×
62
                return a->constList()[0];
863✔
63
            }
64
            if (a->valueType() == ValueType::String)
268✔
65
            {
66
                if (a->string().empty())
267✔
67
                    return Value(ValueType::String);
1✔
68
                return Value(std::string(1, a->stringRef()[0]));
267✔
69
            }
70

71
            throw types::TypeCheckingError(
2✔
72
                "head",
1✔
73
                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
74
                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
75
                { *a });
1✔
76
        }
1,131✔
77

78
        inline Value at(Value& container, Value& index, VM& vm)
15,492✔
79
        {
15,492✔
80
            if (index.valueType() != ValueType::Number)
15,492✔
81
                throw types::TypeCheckingError(
5✔
82
                    "@",
1✔
83
                    { { types::Contract { { types::Typedef("src", ValueType::List), types::Typedef("idx", ValueType::Number) } },
2✔
84
                        types::Contract { { types::Typedef("src", ValueType::String), types::Typedef("idx", ValueType::Number) } } } },
1✔
85
                    { container, index });
1✔
86

87
            const auto num = static_cast<long>(index.number());
15,491✔
88

89
            if (container.valueType() == ValueType::List)
15,491✔
90
            {
91
                const auto i = static_cast<std::size_t>(num < 0 ? static_cast<long>(container.list().size()) + num : num);
7,723✔
92
                if (i < container.list().size())
7,723✔
93
                    return container.list()[i];
7,722✔
94
                else
95
                    VM::throwVMError(
1✔
96
                        ErrorKind::Index,
97
                        fmt::format("{} out of range {} (length {})", num, container.toString(vm), container.list().size()));
1✔
98
            }
7,723✔
99
            else if (container.valueType() == ValueType::String)
7,768✔
100
            {
101
                const auto i = static_cast<std::size_t>(num < 0 ? static_cast<long>(container.string().size()) + num : num);
7,767✔
102
                if (i < container.string().size())
7,767✔
103
                    return Value(std::string(1, container.string()[i]));
7,766✔
104
                else
105
                    VM::throwVMError(
1✔
106
                        ErrorKind::Index,
107
                        fmt::format("{} out of range \"{}\" (length {})", num, container.string(), container.string().size()));
1✔
108
            }
7,767✔
109
            else
110
                throw types::TypeCheckingError(
2✔
111
                    "@",
1✔
112
                    { { types::Contract { { types::Typedef("src", ValueType::List), types::Typedef("idx", ValueType::Number) } },
2✔
113
                        types::Contract { { types::Typedef("src", ValueType::String), types::Typedef("idx", ValueType::Number) } } } },
1✔
114
                    { container, index });
1✔
115
        }
15,495✔
116
    }
117

118
    VM::VM(State& state) noexcept :
540✔
119
        m_state(state), m_exit_code(0), m_running(false)
180✔
120
    {
180✔
121
        m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>());
180✔
122
    }
180✔
123

124
    void VM::init() noexcept
181✔
125
    {
181✔
126
        ExecutionContext& context = *m_execution_contexts.back();
181✔
127
        for (const auto& c : m_execution_contexts)
362✔
128
        {
129
            c->ip = 0;
181✔
130
            c->pp = 0;
181✔
131
            c->sp = 0;
181✔
132
        }
181✔
133

134
        context.sp = 0;
181✔
135
        context.fc = 1;
181✔
136

137
        m_shared_lib_objects.clear();
181✔
138
        context.stacked_closure_scopes.clear();
181✔
139
        context.stacked_closure_scopes.emplace_back(nullptr);
181✔
140

141
        context.saved_scope.reset();
181✔
142
        m_exit_code = 0;
181✔
143

144
        context.locals.clear();
181✔
145
        context.locals.reserve(128);
181✔
146
        context.locals.emplace_back(context.scopes_storage.data(), 0);
181✔
147

148
        // loading bound stuff
149
        // put them in the global frame if we can, aka the first one
150
        for (const auto& [sym_id, value] : m_state.m_binded)
379✔
151
        {
152
            auto it = std::ranges::find(m_state.m_symbols, sym_id);
187✔
153
            if (it != m_state.m_symbols.end())
187✔
154
                context.locals[0].push_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), value);
11✔
155
        }
187✔
156
    }
181✔
157

158
    Value VM::getField(Value* closure, const uint16_t id, ExecutionContext& context)
2,638✔
159
    {
2,638✔
160
        if (closure->valueType() != ValueType::Closure)
2,638✔
161
        {
162
            if (context.last_symbol < m_state.m_symbols.size()) [[likely]]
1✔
163
                throwVMError(
3✔
164
                    ErrorKind::Type,
165
                    fmt::format(
3✔
166
                        "`{}' is a {}, not a Closure, can not get the field `{}' from it",
1✔
167
                        m_state.m_symbols[context.last_symbol],
1✔
168
                        std::to_string(closure->valueType()),
1✔
169
                        m_state.m_symbols[id]));
1✔
170
            else
171
                throwVMError(ErrorKind::Type,
×
172
                             fmt::format(
×
173
                                 "{} is not a Closure, can not get the field `{}' from it",
×
174
                                 std::to_string(closure->valueType()),
180✔
175
                                 m_state.m_symbols[id]));
×
176
        }
177

178
        if (Value* field = closure->refClosure().refScope()[id]; field != nullptr)
5,274✔
179
        {
180
            // check for CALL instruction (the instruction because context.ip is already on the next instruction word)
181
            if (m_state.inst(context.pp, context.ip) == CALL)
2,635✔
182
                return Value(Closure(closure->refClosure().scopePtr(), field->pageAddr()));
1,530✔
183
            else
184
                return *field;
1,105✔
185
        }
186
        else
187
        {
188
            if (!closure->refClosure().hasFieldEndingWith(m_state.m_symbols[id], *this))
2✔
189
                throwVMError(
1✔
190
                    ErrorKind::Scope,
191
                    fmt::format(
2✔
192
                        "`{0}' isn't in the closure environment: {1}",
1✔
193
                        m_state.m_symbols[id],
1✔
194
                        closure->refClosure().toString(*this)));
1✔
195
            throwVMError(
1✔
196
                ErrorKind::Scope,
197
                fmt::format(
2✔
198
                    "`{0}' isn't in the closure environment: {1}. A variable in the package might have the same name as '{0}', "
1✔
199
                    "and name resolution tried to fully qualify it. Rename either the variable or the capture to solve this",
200
                    m_state.m_symbols[id],
1✔
201
                    closure->refClosure().toString(*this)));
1✔
202
        }
203
    }
2,638✔
204

205
    Value VM::createList(const std::size_t count, internal::ExecutionContext& context)
1,536✔
206
    {
1,536✔
207
        Value l(ValueType::List);
1,536✔
208
        if (count != 0)
1,536✔
209
            l.list().reserve(count);
569✔
210

211
        for (std::size_t i = 0; i < count; ++i)
3,009✔
212
            l.push_back(*popAndResolveAsPtr(context));
1,473✔
213

214
        return l;
1,536✔
215
    }
1,536✔
216

217
    void VM::listAppendInPlace(Value* list, const std::size_t count, ExecutionContext& context)
1,471✔
218
    {
1,471✔
219
        if (list->valueType() != ValueType::List)
1,471✔
220
        {
221
            std::vector<Value> args = { *list };
1✔
222
            for (std::size_t i = 0; i < count; ++i)
2✔
223
                args.push_back(*popAndResolveAsPtr(context));
1✔
224
            throw types::TypeCheckingError(
2✔
225
                "append!",
1✔
226
                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* variadic= */ true) } } } },
1✔
227
                args);
228
        }
1✔
229

230
        for (std::size_t i = 0; i < count; ++i)
2,940✔
231
            list->push_back(*popAndResolveAsPtr(context));
1,470✔
232
    }
1,471✔
233

234
    Value& VM::operator[](const std::string& name) noexcept
36✔
235
    {
36✔
236
        // find id of object
237
        const auto it = std::ranges::find(m_state.m_symbols, name);
36✔
238
        if (it == m_state.m_symbols.end())
36✔
239
        {
240
            m_no_value = Builtins::nil;
3✔
241
            return m_no_value;
3✔
242
        }
243

244
        const auto dist = std::distance(m_state.m_symbols.begin(), it);
33✔
245
        if (std::cmp_less(dist, std::numeric_limits<uint16_t>::max()))
33✔
246
        {
247
            ExecutionContext& context = *m_execution_contexts.front();
33✔
248

249
            const auto id = static_cast<uint16_t>(dist);
33✔
250
            Value* var = findNearestVariable(id, context);
33✔
251
            if (var != nullptr)
33✔
252
                return *var;
33✔
253
        }
33✔
254

255
        m_no_value = Builtins::nil;
×
256
        return m_no_value;
×
257
    }
36✔
258

259
    void VM::loadPlugin(const uint16_t id, ExecutionContext& context)
×
260
    {
×
261
        namespace fs = std::filesystem;
262

263
        const std::string file = m_state.m_constants[id].stringRef();
×
264

265
        std::string path = file;
×
266
        // bytecode loaded from file
267
        if (m_state.m_filename != ARK_NO_NAME_FILE)
×
268
            path = (fs::path(m_state.m_filename).parent_path() / fs::path(file)).relative_path().string();
×
269

270
        std::shared_ptr<SharedLibrary> lib;
×
271
        // if it exists alongside the .arkc file
272
        if (Utils::fileExists(path))
×
273
            lib = std::make_shared<SharedLibrary>(path);
×
274
        else
275
        {
276
            for (auto const& v : m_state.m_libenv)
×
277
            {
278
                std::string lib_path = (fs::path(v) / fs::path(file)).string();
×
279

280
                // if it's already loaded don't do anything
281
                if (std::ranges::find_if(m_shared_lib_objects, [&](const auto& val) {
×
282
                        return (val->path() == path || val->path() == lib_path);
×
283
                    }) != m_shared_lib_objects.end())
×
284
                    return;
×
285

286
                // check in lib_path
287
                if (Utils::fileExists(lib_path))
×
288
                {
289
                    lib = std::make_shared<SharedLibrary>(lib_path);
×
290
                    break;
×
291
                }
292
            }
×
293
        }
294

295
        if (!lib)
×
296
        {
297
            auto lib_path = std::accumulate(
×
298
                std::next(m_state.m_libenv.begin()),
×
299
                m_state.m_libenv.end(),
×
300
                m_state.m_libenv[0].string(),
×
301
                [](const std::string& a, const fs::path& b) -> std::string {
×
302
                    return a + "\n\t- " + b.string();
×
303
                });
×
304
            throwVMError(
×
305
                ErrorKind::Module,
306
                fmt::format("Could not find module '{}'. Searched under\n\t- {}\n\t- {}", file, path, lib_path));
×
307
        }
×
308

309
        m_shared_lib_objects.emplace_back(lib);
×
310

311
        // load the mapping from the dynamic library
312
        try
313
        {
314
            const mapping* map = m_shared_lib_objects.back()->get<mapping* (*)()>("getFunctionsMapping")();
×
315
            // load the mapping data
316
            std::size_t i = 0;
×
317
            while (map[i].name != nullptr)
×
318
            {
319
                // put it in the global frame, aka the first one
320
                auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
321
                if (it != m_state.m_symbols.end())
×
322
                    context.locals[0].push_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), Value(map[i].value));
×
323

324
                ++i;
×
325
            }
×
326
        }
×
327
        catch (const std::system_error& e)
328
        {
329
            throwVMError(
×
330
                ErrorKind::Module,
331
                fmt::format(
×
332
                    "An error occurred while loading module '{}': {}\nIt is most likely because the versions of the module and the language don't match.",
×
333
                    file, e.what()));
×
334
        }
×
335
    }
×
336

337
    void VM::exit(const int code) noexcept
×
338
    {
×
339
        m_exit_code = code;
×
340
        m_running = false;
×
341
    }
×
342

343
    ExecutionContext* VM::createAndGetContext()
15✔
344
    {
15✔
345
        const std::lock_guard lock(m_mutex);
15✔
346

347
        ExecutionContext* ctx = nullptr;
15✔
348

349
        // Try and find a free execution context.
350
        // If there is only one context, this is the primary one, which can't be reused.
351
        // Otherwise, we can check if a context is marked as free and reserve it!
352
        // It is possible that all contexts are being used, thus we will create one (active by default) in that case.
353

354
        if (m_execution_contexts.size() > 1)
15✔
355
        {
356
            for (std::size_t i = 0; i < m_execution_contexts.size(); ++i)
48✔
357
            {
358
                if (!m_execution_contexts[i]->primary && m_execution_contexts[i]->isFree())
35✔
359
                {
360
                    ctx = m_execution_contexts[i].get();
9✔
361
                    ctx->setActive(true);
9✔
362
                    // reset the context before using it
363
                    ctx->sp = 0;
9✔
364
                    ctx->saved_scope.reset();
9✔
365
                    ctx->stacked_closure_scopes.clear();
9✔
366
                    ctx->locals.clear();
9✔
367
                    break;
9✔
368
                }
369
            }
26✔
370
        }
13✔
371

372
        if (ctx == nullptr)
15✔
373
            ctx = m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>()).get();
6✔
374

375
        assert(!ctx->primary && "The new context shouldn't be marked as primary!");
15✔
376
        assert(ctx != m_execution_contexts.front().get() && "The new context isn't really new!");
15✔
377

378
        const ExecutionContext& primary_ctx = *m_execution_contexts.front();
15✔
379
        ctx->locals.reserve(primary_ctx.locals.size());
15✔
380
        ctx->scopes_storage = primary_ctx.scopes_storage;
15✔
381
        ctx->stacked_closure_scopes.emplace_back(nullptr);
15✔
382
        ctx->fc = 1;
15✔
383

384
        for (const auto& scope_view : primary_ctx.locals)
56✔
385
        {
386
            auto& new_scope = ctx->locals.emplace_back(ctx->scopes_storage.data(), scope_view.m_start);
41✔
387
            for (std::size_t i = 0; i < scope_view.size(); ++i)
2,045✔
388
            {
389
                const auto& [id, val] = scope_view.atPos(i);
2,004✔
390
                new_scope.push_back(id, val);
2,004✔
391
            }
2,004✔
392
        }
41✔
393

394
        return ctx;
15✔
395
    }
16✔
396

397
    void VM::deleteContext(ExecutionContext* ec)
15✔
398
    {
14✔
399
        const std::lock_guard lock(m_mutex);
15✔
400

401
        // 1 + 4 additional contexts, it's a bit much (~600kB per context) to have in memory
402
        if (m_execution_contexts.size() > 5)
14✔
403
        {
404
            const auto it =
1✔
405
                std::ranges::remove_if(
2✔
406
                    m_execution_contexts,
1✔
407
                    [ec](const std::unique_ptr<ExecutionContext>& ctx) {
7✔
408
                        return ctx.get() == ec;
6✔
409
                    })
410
                    .begin();
1✔
411
            m_execution_contexts.erase(it);
1✔
412
        }
1✔
413
        else
414
        {
415
            // mark the used context as ready to be used again
416
            for (std::size_t i = 1; i < m_execution_contexts.size(); ++i)
35✔
417
            {
418
                if (m_execution_contexts[i].get() == ec)
22✔
419
                {
420
                    ec->setActive(false);
13✔
421
                    break;
13✔
422
                }
423
            }
9✔
424
        }
425
    }
14✔
426

427
    Future* VM::createFuture(std::vector<Value>& args)
15✔
428
    {
15✔
429
        ExecutionContext* ctx = createAndGetContext();
15✔
430
        // so that we have access to the presumed symbol id of the function we are calling
431
        // assuming that the callee is always the global context
432
        ctx->last_symbol = m_execution_contexts.front()->last_symbol;
15✔
433

434
        // doing this after having created the context
435
        // because the context uses the mutex and we don't want a deadlock
436
        const std::lock_guard lock(m_mutex);
15✔
437
        m_futures.push_back(std::make_unique<Future>(ctx, this, args));
15✔
438

439
        return m_futures.back().get();
15✔
440
    }
15✔
441

UNCOV
442
    void VM::deleteFuture(Future* f)
×
UNCOV
443
    {
×
UNCOV
444
        const std::lock_guard lock(m_mutex);
×
445

UNCOV
446
        const auto it =
×
UNCOV
447
            std::ranges::remove_if(
×
UNCOV
448
                m_futures,
×
UNCOV
449
                [f](const std::unique_ptr<Future>& future) {
×
UNCOV
450
                    return future.get() == f;
×
451
                })
UNCOV
452
                .begin();
×
UNCOV
453
        m_futures.erase(it);
×
UNCOV
454
    }
×
455

UNCOV
456
    bool VM::forceReloadPlugins() const
×
UNCOV
457
    {
×
458
        // load the mapping from the dynamic library
459
        try
460
        {
UNCOV
461
            for (const auto& shared_lib : m_shared_lib_objects)
×
462
            {
UNCOV
463
                const mapping* map = shared_lib->template get<mapping* (*)()>("getFunctionsMapping")();
×
464
                // load the mapping data
UNCOV
465
                std::size_t i = 0;
×
UNCOV
466
                while (map[i].name != nullptr)
×
467
                {
468
                    // put it in the global frame, aka the first one
UNCOV
469
                    auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
UNCOV
470
                    if (it != m_state.m_symbols.end())
×
UNCOV
471
                        m_execution_contexts[0]->locals[0].push_back(
×
UNCOV
472
                            static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)),
×
UNCOV
473
                            Value(map[i].value));
×
474

UNCOV
475
                    ++i;
×
UNCOV
476
                }
×
UNCOV
477
            }
×
478

UNCOV
479
            return true;
×
UNCOV
480
        }
×
481
        catch (const std::system_error&)
482
        {
UNCOV
483
            return false;
×
UNCOV
484
        }
×
UNCOV
485
    }
×
486

487
    void VM::throwVMError(ErrorKind kind, const std::string& message)
32✔
488
    {
32✔
489
        throw std::runtime_error(std::string(errorKinds[static_cast<std::size_t>(kind)]) + ": " + message + "\n");
32✔
490
    }
32✔
491

492
    int VM::run(const bool fail_with_exception)
181✔
493
    {
181✔
494
        init();
181✔
495
        safeRun(*m_execution_contexts[0], 0, fail_with_exception);
181✔
496
        return m_exit_code;
181✔
497
    }
498

499
    int VM::safeRun(ExecutionContext& context, std::size_t untilFrameCount, bool fail_with_exception)
197✔
500
    {
197✔
501
#if ARK_USE_COMPUTED_GOTOS
502
#    define TARGET(op) TARGET_##op:
503
#    define DISPATCH_GOTO()            \
504
        _Pragma("GCC diagnostic push") \
505
            _Pragma("GCC diagnostic ignored \"-Wpedantic\"") goto* opcode_targets[inst];
506
        _Pragma("GCC diagnostic pop")
507
#    define GOTO_HALT() goto dispatch_end
508
#else
509
#    define TARGET(op) case op:
510
#    define DISPATCH_GOTO() goto dispatch_opcode
511
#    define GOTO_HALT() break
512
#endif
513

514
#define NEXTOPARG()                                                                   \
515
    do                                                                                \
516
    {                                                                                 \
517
        inst = m_state.inst(context.pp, context.ip);                                  \
518
        padding = m_state.inst(context.pp, context.ip + 1);                           \
519
        arg = static_cast<uint16_t>((m_state.inst(context.pp, context.ip + 2) << 8) + \
520
                                    m_state.inst(context.pp, context.ip + 3));        \
521
        context.ip += 4;                                                              \
522
    } while (false)
523
#define DISPATCH() \
524
    NEXTOPARG();   \
525
    DISPATCH_GOTO();
526
#define UNPACK_ARGS()                                                                 \
527
    do                                                                                \
528
    {                                                                                 \
529
        secondary_arg = static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12); \
530
        primary_arg = arg & 0x0fff;                                                   \
531
    } while (false)
532

533
#if ARK_USE_COMPUTED_GOTOS
534
#    pragma GCC diagnostic push
535
#    pragma GCC diagnostic ignored "-Wpedantic"
536
            constexpr std::array opcode_targets = {
197✔
537
                // cppcheck-suppress syntaxError ; cppcheck do not know about labels addresses (GCC extension)
538
                &&TARGET_NOP,
539
                &&TARGET_LOAD_SYMBOL,
540
                &&TARGET_LOAD_SYMBOL_BY_INDEX,
541
                &&TARGET_LOAD_CONST,
542
                &&TARGET_POP_JUMP_IF_TRUE,
543
                &&TARGET_STORE,
544
                &&TARGET_SET_VAL,
545
                &&TARGET_POP_JUMP_IF_FALSE,
546
                &&TARGET_JUMP,
547
                &&TARGET_RET,
548
                &&TARGET_HALT,
549
                &&TARGET_PUSH_RETURN_ADDRESS,
550
                &&TARGET_CALL,
551
                &&TARGET_CAPTURE,
552
                &&TARGET_BUILTIN,
553
                &&TARGET_DEL,
554
                &&TARGET_MAKE_CLOSURE,
555
                &&TARGET_GET_FIELD,
556
                &&TARGET_PLUGIN,
557
                &&TARGET_LIST,
558
                &&TARGET_APPEND,
559
                &&TARGET_CONCAT,
560
                &&TARGET_APPEND_IN_PLACE,
561
                &&TARGET_CONCAT_IN_PLACE,
562
                &&TARGET_POP_LIST,
563
                &&TARGET_POP_LIST_IN_PLACE,
564
                &&TARGET_SET_AT_INDEX,
565
                &&TARGET_SET_AT_2_INDEX,
566
                &&TARGET_POP,
567
                &&TARGET_SHORTCIRCUIT_AND,
568
                &&TARGET_SHORTCIRCUIT_OR,
569
                &&TARGET_CREATE_SCOPE,
570
                &&TARGET_RESET_SCOPE_JUMP,
571
                &&TARGET_POP_SCOPE,
572
                &&TARGET_GET_CURRENT_PAGE_ADDR,
573
                &&TARGET_ADD,
574
                &&TARGET_SUB,
575
                &&TARGET_MUL,
576
                &&TARGET_DIV,
577
                &&TARGET_GT,
578
                &&TARGET_LT,
579
                &&TARGET_LE,
580
                &&TARGET_GE,
581
                &&TARGET_NEQ,
582
                &&TARGET_EQ,
583
                &&TARGET_LEN,
584
                &&TARGET_EMPTY,
585
                &&TARGET_TAIL,
586
                &&TARGET_HEAD,
587
                &&TARGET_ISNIL,
588
                &&TARGET_ASSERT,
589
                &&TARGET_TO_NUM,
590
                &&TARGET_TO_STR,
591
                &&TARGET_AT,
592
                &&TARGET_AT_AT,
593
                &&TARGET_MOD,
594
                &&TARGET_TYPE,
595
                &&TARGET_HASFIELD,
596
                &&TARGET_NOT,
597
                &&TARGET_LOAD_CONST_LOAD_CONST,
598
                &&TARGET_LOAD_CONST_STORE,
599
                &&TARGET_LOAD_CONST_SET_VAL,
600
                &&TARGET_STORE_FROM,
601
                &&TARGET_STORE_FROM_INDEX,
602
                &&TARGET_SET_VAL_FROM,
603
                &&TARGET_SET_VAL_FROM_INDEX,
604
                &&TARGET_INCREMENT,
605
                &&TARGET_INCREMENT_BY_INDEX,
606
                &&TARGET_INCREMENT_STORE,
607
                &&TARGET_DECREMENT,
608
                &&TARGET_DECREMENT_BY_INDEX,
609
                &&TARGET_DECREMENT_STORE,
610
                &&TARGET_STORE_TAIL,
611
                &&TARGET_STORE_TAIL_BY_INDEX,
612
                &&TARGET_STORE_HEAD,
613
                &&TARGET_STORE_HEAD_BY_INDEX,
614
                &&TARGET_STORE_LIST,
615
                &&TARGET_SET_VAL_TAIL,
616
                &&TARGET_SET_VAL_TAIL_BY_INDEX,
617
                &&TARGET_SET_VAL_HEAD,
618
                &&TARGET_SET_VAL_HEAD_BY_INDEX,
619
                &&TARGET_CALL_BUILTIN,
620
                &&TARGET_CALL_BUILTIN_WITHOUT_RETURN_ADDRESS,
621
                &&TARGET_LT_CONST_JUMP_IF_FALSE,
622
                &&TARGET_LT_CONST_JUMP_IF_TRUE,
623
                &&TARGET_LT_SYM_JUMP_IF_FALSE,
624
                &&TARGET_GT_CONST_JUMP_IF_TRUE,
625
                &&TARGET_GT_CONST_JUMP_IF_FALSE,
626
                &&TARGET_GT_SYM_JUMP_IF_FALSE,
627
                &&TARGET_EQ_CONST_JUMP_IF_TRUE,
628
                &&TARGET_EQ_SYM_INDEX_JUMP_IF_TRUE,
629
                &&TARGET_NEQ_CONST_JUMP_IF_TRUE,
630
                &&TARGET_NEQ_SYM_JUMP_IF_FALSE,
631
                &&TARGET_CALL_SYMBOL,
632
                &&TARGET_CALL_CURRENT_PAGE,
633
                &&TARGET_GET_FIELD_FROM_SYMBOL,
634
                &&TARGET_GET_FIELD_FROM_SYMBOL_INDEX,
635
                &&TARGET_AT_SYM_SYM,
636
                &&TARGET_AT_SYM_INDEX_SYM_INDEX,
637
                &&TARGET_CHECK_TYPE_OF,
638
                &&TARGET_CHECK_TYPE_OF_BY_INDEX,
639
                &&TARGET_APPEND_IN_PLACE_SYM,
640
                &&TARGET_APPEND_IN_PLACE_SYM_INDEX
641
            };
642

643
        static_assert(opcode_targets.size() == static_cast<std::size_t>(Instruction::InstructionsCount) && "Some instructions are not implemented in the VM");
644
#    pragma GCC diagnostic pop
645
#endif
646

647
        try
648
        {
649
            uint8_t inst = 0;
197✔
650
            uint8_t padding = 0;
197✔
651
            uint16_t arg = 0;
197✔
652
            uint16_t primary_arg = 0;
197✔
653
            uint16_t secondary_arg = 0;
197✔
654

655
            m_running = true;
197✔
656

657
            DISPATCH();
197✔
658
            // cppcheck-suppress unreachableCode ; analysis cannot follow the chain of goto... but it works!
659
            {
660
#if !ARK_USE_COMPUTED_GOTOS
661
            dispatch_opcode:
662
                switch (inst)
663
#endif
UNCOV
664
                {
×
665
#pragma region "Instructions"
666
                    TARGET(NOP)
667
                    {
UNCOV
668
                        DISPATCH();
×
669
                    }
146,451✔
670

671
                    TARGET(LOAD_SYMBOL)
672
                    {
673
                        push(loadSymbol(arg, context), context);
146,451✔
674
                        DISPATCH();
146,451✔
675
                    }
333,621✔
676

677
                    TARGET(LOAD_SYMBOL_BY_INDEX)
678
                    {
679
                        push(loadSymbolFromIndex(arg, context), context);
333,621✔
680
                        DISPATCH();
333,621✔
681
                    }
115,923✔
682

683
                    TARGET(LOAD_CONST)
684
                    {
685
                        push(loadConstAsPtr(arg), context);
115,923✔
686
                        DISPATCH();
115,923✔
687
                    }
28,567✔
688

689
                    TARGET(POP_JUMP_IF_TRUE)
690
                    {
691
                        if (Value boolean = *popAndResolveAsPtr(context); !!boolean)
35,524✔
692
                            context.ip = arg * 4;  // instructions are 4 bytes
6,957✔
693
                        DISPATCH();
28,567✔
694
                    }
401,134✔
695

696
                    TARGET(STORE)
697
                    {
698
                        store(arg, popAndResolveAsPtr(context), context);
401,134✔
699
                        DISPATCH();
401,134✔
700
                    }
22,498✔
701

702
                    TARGET(SET_VAL)
703
                    {
704
                        setVal(arg, popAndResolveAsPtr(context), context);
22,498✔
705
                        DISPATCH();
22,498✔
706
                    }
27,447✔
707

708
                    TARGET(POP_JUMP_IF_FALSE)
709
                    {
710
                        if (Value boolean = *popAndResolveAsPtr(context); !boolean)
29,533✔
711
                            context.ip = arg * 4;  // instructions are 4 bytes
2,086✔
712
                        DISPATCH();
27,447✔
713
                    }
206,777✔
714

715
                    TARGET(JUMP)
716
                    {
717
                        context.ip = arg * 4;  // instructions are 4 bytes
206,777✔
718
                        DISPATCH();
206,777✔
719
                    }
136,519✔
720

721
                    TARGET(RET)
722
                    {
723
                        {
724
                            Value ip_or_val = *popAndResolveAsPtr(context);
136,519✔
725
                            // no return value on the stack
726
                            if (ip_or_val.valueType() == ValueType::InstPtr) [[unlikely]]
136,519✔
727
                            {
728
                                context.ip = ip_or_val.pageAddr();
2,883✔
729
                                // we always push PP then IP, thus the next value
730
                                // MUST be the page pointer
731
                                context.pp = pop(context)->pageAddr();
2,883✔
732

733
                                returnFromFuncCall(context);
2,883✔
734
                                push(Builtins::nil, context);
2,883✔
735
                            }
2,883✔
736
                            // value on the stack
737
                            else [[likely]]
738
                            {
739
                                const Value* ip = popAndResolveAsPtr(context);
133,636✔
740
                                assert(ip->valueType() == ValueType::InstPtr && "Expected instruction pointer on the stack (is the stack trashed?)");
133,636✔
741
                                context.ip = ip->pageAddr();
133,636✔
742
                                context.pp = pop(context)->pageAddr();
133,636✔
743

744
                                returnFromFuncCall(context);
133,636✔
745
                                push(std::move(ip_or_val), context);
133,636✔
746
                            }
747

748
                            if (context.fc <= untilFrameCount)
136,519✔
749
                                GOTO_HALT();
16✔
750
                        }
136,519✔
751

752
                        DISPATCH();
136,503✔
753
                    }
63✔
754

755
                    TARGET(HALT)
756
                    {
757
                        m_running = false;
63✔
758
                        GOTO_HALT();
63✔
759
                    }
139,377✔
760

761
                    TARGET(PUSH_RETURN_ADDRESS)
762
                    {
763
                        push(Value(static_cast<PageAddr_t>(context.pp)), context);
139,377✔
764
                        // arg * 4 to skip over the call instruction, so that the return address points to AFTER the call
765
                        push(Value(ValueType::InstPtr, static_cast<PageAddr_t>(arg * 4)), context);
139,377✔
766
                        DISPATCH();
139,377✔
767
                    }
2,610✔
768

769
                    TARGET(CALL)
770
                    {
771
                        call(context, arg);
2,610✔
772
                        if (!m_running)
2,603✔
773
                            GOTO_HALT();
×
774
                        DISPATCH();
2,603✔
775
                    }
3,104✔
776

777
                    TARGET(CAPTURE)
778
                    {
779
                        if (!context.saved_scope)
3,104✔
780
                            context.saved_scope = ClosureScope();
622✔
781

782
                        const Value* ptr = findNearestVariable(arg, context);
3,104✔
783
                        if (!ptr)
3,104✔
UNCOV
784
                            throwVMError(ErrorKind::Scope, fmt::format("Couldn't capture `{}' as it is currently unbound", m_state.m_symbols[arg]));
×
785
                        else
786
                        {
787
                            ptr = ptr->valueType() == ValueType::Reference ? ptr->reference() : ptr;
3,104✔
788
                            context.saved_scope.value().push_back(arg, *ptr);
3,104✔
789
                        }
790

791
                        DISPATCH();
3,104✔
792
                    }
1,540✔
793

794
                    TARGET(BUILTIN)
795
                    {
796
                        push(Builtins::builtins[arg].second, context);
1,540✔
797
                        DISPATCH();
1,540✔
798
                    }
1✔
799

800
                    TARGET(DEL)
801
                    {
802
                        if (Value* var = findNearestVariable(arg, context); var != nullptr)
1✔
803
                        {
UNCOV
804
                            if (var->valueType() == ValueType::User)
×
UNCOV
805
                                var->usertypeRef().del();
×
UNCOV
806
                            *var = Value();
×
UNCOV
807
                            DISPATCH();
×
808
                        }
809

810
                        throwVMError(ErrorKind::Scope, fmt::format("Can not delete unbound variable `{}'", m_state.m_symbols[arg]));
1✔
811
                    }
622✔
812

813
                    TARGET(MAKE_CLOSURE)
814
                    {
815
                        push(Value(Closure(context.saved_scope.value(), m_state.m_constants[arg].pageAddr())), context);
622✔
816
                        context.saved_scope.reset();
622✔
817
                        DISPATCH();
622✔
818
                    }
6✔
819

820
                    TARGET(GET_FIELD)
821
                    {
822
                        Value* var = popAndResolveAsPtr(context);
6✔
823
                        push(getField(var, arg, context), context);
6✔
824
                        DISPATCH();
6✔
UNCOV
825
                    }
×
826

827
                    TARGET(PLUGIN)
828
                    {
UNCOV
829
                        loadPlugin(arg, context);
×
UNCOV
830
                        DISPATCH();
×
831
                    }
633✔
832

833
                    TARGET(LIST)
834
                    {
835
                        {
836
                            Value l = createList(arg, context);
633✔
837
                            push(std::move(l), context);
633✔
838
                        }
633✔
839
                        DISPATCH();
633✔
840
                    }
1,552✔
841

842
                    TARGET(APPEND)
843
                    {
844
                        {
845
                            Value* list = popAndResolveAsPtr(context);
1,552✔
846
                            if (list->valueType() != ValueType::List)
1,552✔
847
                            {
848
                                std::vector<Value> args = { *list };
1✔
849
                                for (uint16_t i = 0; i < arg; ++i)
2✔
850
                                    args.push_back(*popAndResolveAsPtr(context));
1✔
851
                                throw types::TypeCheckingError(
2✔
852
                                    "append",
1✔
853
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* variadic= */ true) } } } },
1✔
854
                                    args);
855
                            }
1✔
856

857
                            const auto size = static_cast<uint16_t>(list->constList().size());
1,551✔
858

859
                            Value obj { *list };
1,551✔
860
                            obj.list().reserve(size + arg);
1,551✔
861

862
                            for (uint16_t i = 0; i < arg; ++i)
3,102✔
863
                                obj.push_back(*popAndResolveAsPtr(context));
1,551✔
864
                            push(std::move(obj), context);
1,551✔
865
                        }
1,551✔
866
                        DISPATCH();
1,551✔
867
                    }
12✔
868

869
                    TARGET(CONCAT)
870
                    {
871
                        {
872
                            Value* list = popAndResolveAsPtr(context);
12✔
873
                            Value obj { *list };
12✔
874

875
                            for (uint16_t i = 0; i < arg; ++i)
24✔
876
                            {
877
                                Value* next = popAndResolveAsPtr(context);
14✔
878

879
                                if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
14✔
880
                                    throw types::TypeCheckingError(
4✔
881
                                        "concat",
2✔
882
                                        { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
883
                                        { *list, *next });
2✔
884

885
                                std::ranges::copy(next->list(), std::back_inserter(obj.list()));
12✔
886
                            }
12✔
887
                            push(std::move(obj), context);
10✔
888
                        }
12✔
889
                        DISPATCH();
10✔
UNCOV
890
                    }
×
891

892
                    TARGET(APPEND_IN_PLACE)
893
                    {
UNCOV
894
                        Value* list = popAndResolveAsPtr(context);
×
UNCOV
895
                        listAppendInPlace(list, arg, context);
×
UNCOV
896
                        DISPATCH();
×
897
                    }
562✔
898

899
                    TARGET(CONCAT_IN_PLACE)
900
                    {
901
                        Value* list = popAndResolveAsPtr(context);
562✔
902

903
                        for (uint16_t i = 0; i < arg; ++i)
1,152✔
904
                        {
905
                            Value* next = popAndResolveAsPtr(context);
592✔
906

907
                            if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
592✔
908
                                throw types::TypeCheckingError(
4✔
909
                                    "concat!",
2✔
910
                                    { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
911
                                    { *list, *next });
2✔
912

913
                            std::ranges::copy(next->list(), std::back_inserter(list->list()));
590✔
914
                        }
590✔
915
                        DISPATCH();
560✔
916
                    }
6✔
917

918
                    TARGET(POP_LIST)
919
                    {
920
                        {
921
                            Value list = *popAndResolveAsPtr(context);
6✔
922
                            Value number = *popAndResolveAsPtr(context);
6✔
923

924
                            if (list.valueType() != ValueType::List || number.valueType() != ValueType::Number)
6✔
925
                                throw types::TypeCheckingError(
2✔
926
                                    "pop",
1✔
927
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
928
                                    { list, number });
1✔
929

930
                            long idx = static_cast<long>(number.number());
5✔
931
                            idx = idx < 0 ? static_cast<long>(list.list().size()) + idx : idx;
5✔
932
                            if (std::cmp_greater_equal(idx, list.list().size()) || idx < 0)
5✔
933
                                throwVMError(
2✔
934
                                    ErrorKind::Index,
935
                                    fmt::format("pop index ({}) out of range (list size: {})", idx, list.list().size()));
2✔
936

937
                            list.list().erase(list.list().begin() + idx);
3✔
938
                            push(list, context);
3✔
939
                        }
6✔
940
                        DISPATCH();
3✔
941
                    }
84✔
942

943
                    TARGET(POP_LIST_IN_PLACE)
944
                    {
945
                        {
946
                            Value* list = popAndResolveAsPtr(context);
84✔
947
                            Value number = *popAndResolveAsPtr(context);
84✔
948

949
                            if (list->valueType() != ValueType::List || number.valueType() != ValueType::Number)
84✔
950
                                throw types::TypeCheckingError(
2✔
951
                                    "pop!",
1✔
952
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
953
                                    { *list, number });
1✔
954

955
                            long idx = static_cast<long>(number.number());
83✔
956
                            idx = idx < 0 ? static_cast<long>(list->list().size()) + idx : idx;
83✔
957
                            if (std::cmp_greater_equal(idx, list->list().size()) || idx < 0)
83✔
958
                                throwVMError(
2✔
959
                                    ErrorKind::Index,
960
                                    fmt::format("pop! index ({}) out of range (list size: {})", idx, list->list().size()));
2✔
961

962
                            list->list().erase(list->list().begin() + idx);
81✔
963
                        }
84✔
964
                        DISPATCH();
81✔
965
                    }
490✔
966

967
                    TARGET(SET_AT_INDEX)
968
                    {
969
                        {
970
                            Value* list = popAndResolveAsPtr(context);
490✔
971
                            Value number = *popAndResolveAsPtr(context);
490✔
972
                            Value new_value = *popAndResolveAsPtr(context);
490✔
973

974
                            if (!list->isIndexable() || number.valueType() != ValueType::Number || (list->valueType() == ValueType::String && new_value.valueType() != ValueType::String))
490✔
975
                                throw types::TypeCheckingError(
2✔
976
                                    "@=",
1✔
977
                                    { { types::Contract {
3✔
978
                                          { types::Typedef("list", ValueType::List),
3✔
979
                                            types::Typedef("index", ValueType::Number),
1✔
980
                                            types::Typedef("new_value", ValueType::Any) } } },
1✔
981
                                      { types::Contract {
1✔
982
                                          { types::Typedef("string", ValueType::String),
3✔
983
                                            types::Typedef("index", ValueType::Number),
1✔
984
                                            types::Typedef("char", ValueType::String) } } } },
1✔
985
                                    { *list, number, new_value });
1✔
986

987
                            const std::size_t size = list->valueType() == ValueType::List ? list->list().size() : list->stringRef().size();
489✔
988
                            long idx = static_cast<long>(number.number());
489✔
989
                            idx = idx < 0 ? static_cast<long>(size) + idx : idx;
489✔
990
                            if (std::cmp_greater_equal(idx, size) || idx < 0)
489✔
991
                                throwVMError(
2✔
992
                                    ErrorKind::Index,
993
                                    fmt::format("@= index ({}) out of range (indexable size: {})", idx, size));
2✔
994

995
                            if (list->valueType() == ValueType::List)
487✔
996
                                list->list()[static_cast<std::size_t>(idx)] = new_value;
485✔
997
                            else
998
                                list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
2✔
999
                        }
490✔
1000
                        DISPATCH();
487✔
1001
                    }
12✔
1002

1003
                    TARGET(SET_AT_2_INDEX)
1004
                    {
1005
                        {
1006
                            Value* list = popAndResolveAsPtr(context);
12✔
1007
                            Value x = *popAndResolveAsPtr(context);
12✔
1008
                            Value y = *popAndResolveAsPtr(context);
12✔
1009
                            Value new_value = *popAndResolveAsPtr(context);
12✔
1010

1011
                            if (list->valueType() != ValueType::List || x.valueType() != ValueType::Number || y.valueType() != ValueType::Number)
12✔
1012
                                throw types::TypeCheckingError(
2✔
1013
                                    "@@=",
1✔
1014
                                    { { types::Contract {
2✔
1015
                                        { types::Typedef("list", ValueType::List),
4✔
1016
                                          types::Typedef("x", ValueType::Number),
1✔
1017
                                          types::Typedef("y", ValueType::Number),
1✔
1018
                                          types::Typedef("new_value", ValueType::Any) } } } },
1✔
1019
                                    { *list, x, y, new_value });
1✔
1020

1021
                            long idx_y = static_cast<long>(x.number());
11✔
1022
                            idx_y = idx_y < 0 ? static_cast<long>(list->list().size()) + idx_y : idx_y;
11✔
1023
                            if (std::cmp_greater_equal(idx_y, list->list().size()) || idx_y < 0)
11✔
1024
                                throwVMError(
2✔
1025
                                    ErrorKind::Index,
1026
                                    fmt::format("@@= index (y: {}) out of range (list size: {})", idx_y, list->list().size()));
2✔
1027

1028
                            if (!list->list()[static_cast<std::size_t>(idx_y)].isIndexable() ||
13✔
1029
                                (list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::String && new_value.valueType() != ValueType::String))
8✔
1030
                                throw types::TypeCheckingError(
2✔
1031
                                    "@@=",
1✔
1032
                                    { { types::Contract {
3✔
1033
                                          { types::Typedef("list", ValueType::List),
4✔
1034
                                            types::Typedef("x", ValueType::Number),
1✔
1035
                                            types::Typedef("y", ValueType::Number),
1✔
1036
                                            types::Typedef("new_value", ValueType::Any) } } },
1✔
1037
                                      { types::Contract {
1✔
1038
                                          { types::Typedef("string", ValueType::String),
4✔
1039
                                            types::Typedef("x", ValueType::Number),
1✔
1040
                                            types::Typedef("y", ValueType::Number),
1✔
1041
                                            types::Typedef("char", ValueType::String) } } } },
1✔
1042
                                    { *list, x, y, new_value });
1✔
1043

1044
                            const bool is_list = list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
8✔
1045
                            const std::size_t size =
8✔
1046
                                is_list
16✔
1047
                                ? list->list()[static_cast<std::size_t>(idx_y)].list().size()
6✔
1048
                                : list->list()[static_cast<std::size_t>(idx_y)].stringRef().size();
2✔
1049

1050
                            long idx_x = static_cast<long>(y.number());
8✔
1051
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
8✔
1052
                            if (std::cmp_greater_equal(idx_x, size) || idx_x < 0)
8✔
1053
                                throwVMError(
2✔
1054
                                    ErrorKind::Index,
1055
                                    fmt::format("@@= index (x: {}) out of range (inner indexable size: {})", idx_x, size));
2✔
1056

1057
                            if (is_list)
6✔
1058
                                list->list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)] = new_value;
4✔
1059
                            else
1060
                                list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
2✔
1061
                        }
12✔
1062
                        DISPATCH();
6✔
1063
                    }
2,727✔
1064

1065
                    TARGET(POP)
1066
                    {
1067
                        pop(context);
2,727✔
1068
                        DISPATCH();
2,727✔
1069
                    }
23,415✔
1070

1071
                    TARGET(SHORTCIRCUIT_AND)
1072
                    {
1073
                        if (!*peekAndResolveAsPtr(context))
23,415✔
1074
                            context.ip = arg * 4;
733✔
1075
                        else
1076
                            pop(context);
22,682✔
1077
                        DISPATCH();
23,415✔
1078
                    }
811✔
1079

1080
                    TARGET(SHORTCIRCUIT_OR)
1081
                    {
1082
                        if (!!*peekAndResolveAsPtr(context))
811✔
1083
                            context.ip = arg * 4;
216✔
1084
                        else
1085
                            pop(context);
595✔
1086
                        DISPATCH();
811✔
1087
                    }
2,752✔
1088

1089
                    TARGET(CREATE_SCOPE)
1090
                    {
1091
                        context.locals.emplace_back(context.scopes_storage.data(), context.locals.back().storageEnd());
2,752✔
1092
                        DISPATCH();
2,752✔
1093
                    }
31,125✔
1094

1095
                    TARGET(RESET_SCOPE_JUMP)
1096
                    {
1097
                        context.locals.back().reset();
31,125✔
1098
                        context.ip = arg * 4;  // instructions are 4 bytes
31,125✔
1099
                        DISPATCH();
31,125✔
1100
                    }
2,752✔
1101

1102
                    TARGET(POP_SCOPE)
1103
                    {
1104
                        context.locals.pop_back();
2,752✔
1105
                        DISPATCH();
2,752✔
UNCOV
1106
                    }
×
1107

1108
                    TARGET(GET_CURRENT_PAGE_ADDR)
1109
                    {
UNCOV
1110
                        context.last_symbol = arg;
×
UNCOV
1111
                        push(Value(static_cast<PageAddr_t>(context.pp)), context);
×
UNCOV
1112
                        DISPATCH();
×
1113
                    }
26,904✔
1114

1115
#pragma endregion
1116

1117
#pragma region "Operators"
1118

1119
                    TARGET(ADD)
1120
                    {
1121
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
26,904✔
1122

1123
                        if (a->valueType() == ValueType::Number && b->valueType() == ValueType::Number)
26,904✔
1124
                            push(Value(a->number() + b->number()), context);
19,459✔
1125
                        else if (a->valueType() == ValueType::String && b->valueType() == ValueType::String)
7,445✔
1126
                            push(Value(a->string() + b->string()), context);
7,444✔
1127
                        else
1128
                            throw types::TypeCheckingError(
2✔
1129
                                "+",
1✔
1130
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } },
2✔
1131
                                    types::Contract { { types::Typedef("a", ValueType::String), types::Typedef("b", ValueType::String) } } } },
1✔
1132
                                { *a, *b });
1✔
1133
                        DISPATCH();
26,903✔
1134
                    }
328✔
1135

1136
                    TARGET(SUB)
1137
                    {
1138
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
328✔
1139

1140
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
328✔
1141
                            throw types::TypeCheckingError(
2✔
1142
                                "-",
1✔
1143
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1144
                                { *a, *b });
1✔
1145
                        push(Value(a->number() - b->number()), context);
327✔
1146
                        DISPATCH();
327✔
1147
                    }
2,213✔
1148

1149
                    TARGET(MUL)
1150
                    {
1151
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
2,213✔
1152

1153
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
2,213✔
1154
                            throw types::TypeCheckingError(
2✔
1155
                                "*",
1✔
1156
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1157
                                { *a, *b });
1✔
1158
                        push(Value(a->number() * b->number()), context);
2,212✔
1159
                        DISPATCH();
2,212✔
1160
                    }
1,062✔
1161

1162
                    TARGET(DIV)
1163
                    {
1164
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,062✔
1165

1166
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
1,062✔
1167
                            throw types::TypeCheckingError(
2✔
1168
                                "/",
1✔
1169
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1170
                                { *a, *b });
1✔
1171
                        auto d = b->number();
1,061✔
1172
                        if (d == 0)
1,061✔
1173
                            throwVMError(ErrorKind::DivisionByZero, fmt::format("Can not compute expression (/ {} {})", a->toString(*this), b->toString(*this)));
1✔
1174

1175
                        push(Value(a->number() / d), context);
1,060✔
1176
                        DISPATCH();
1,060✔
1177
                    }
160✔
1178

1179
                    TARGET(GT)
1180
                    {
1181
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
160✔
1182
                        push(*b < *a ? Builtins::trueSym : Builtins::falseSym, context);
160✔
1183
                        DISPATCH();
160✔
1184
                    }
28,820✔
1185

1186
                    TARGET(LT)
1187
                    {
1188
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
28,820✔
1189
                        push(*a < *b ? Builtins::trueSym : Builtins::falseSym, context);
28,820✔
1190
                        DISPATCH();
28,820✔
1191
                    }
7,192✔
1192

1193
                    TARGET(LE)
1194
                    {
1195
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
7,192✔
1196
                        push((((*a < *b) || (*a == *b)) ? Builtins::trueSym : Builtins::falseSym), context);
7,192✔
1197
                        DISPATCH();
7,192✔
1198
                    }
5,730✔
1199

1200
                    TARGET(GE)
1201
                    {
1202
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
5,730✔
1203
                        push(!(*a < *b) ? Builtins::trueSym : Builtins::falseSym, context);
5,730✔
1204
                        DISPATCH();
5,730✔
1205
                    }
752✔
1206

1207
                    TARGET(NEQ)
1208
                    {
1209
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
752✔
1210
                        push(*a != *b ? Builtins::trueSym : Builtins::falseSym, context);
752✔
1211
                        DISPATCH();
752✔
1212
                    }
17,449✔
1213

1214
                    TARGET(EQ)
1215
                    {
1216
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
17,449✔
1217
                        push(*a == *b ? Builtins::trueSym : Builtins::falseSym, context);
17,449✔
1218
                        DISPATCH();
17,449✔
1219
                    }
12,199✔
1220

1221
                    TARGET(LEN)
1222
                    {
1223
                        const Value* a = popAndResolveAsPtr(context);
12,199✔
1224

1225
                        if (a->valueType() == ValueType::List)
12,199✔
1226
                            push(Value(static_cast<int>(a->constList().size())), context);
4,573✔
1227
                        else if (a->valueType() == ValueType::String)
7,626✔
1228
                            push(Value(static_cast<int>(a->string().size())), context);
7,625✔
1229
                        else
1230
                            throw types::TypeCheckingError(
2✔
1231
                                "len",
1✔
1232
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1233
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1234
                                { *a });
1✔
1235
                        DISPATCH();
12,198✔
1236
                    }
540✔
1237

1238
                    TARGET(EMPTY)
1239
                    {
1240
                        const Value* a = popAndResolveAsPtr(context);
540✔
1241

1242
                        if (a->valueType() == ValueType::List)
540✔
1243
                            push(a->constList().empty() ? Builtins::trueSym : Builtins::falseSym, context);
86✔
1244
                        else if (a->valueType() == ValueType::String)
454✔
1245
                            push(a->string().empty() ? Builtins::trueSym : Builtins::falseSym, context);
453✔
1246
                        else
1247
                            throw types::TypeCheckingError(
2✔
1248
                                "empty?",
1✔
1249
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1250
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1251
                                { *a });
1✔
1252
                        DISPATCH();
539✔
1253
                    }
335✔
1254

1255
                    TARGET(TAIL)
1256
                    {
1257
                        Value* const a = popAndResolveAsPtr(context);
335✔
1258
                        push(helper::tail(a), context);
336✔
1259
                        DISPATCH();
334✔
1260
                    }
1,099✔
1261

1262
                    TARGET(HEAD)
1263
                    {
1264
                        Value* const a = popAndResolveAsPtr(context);
1,099✔
1265
                        push(helper::head(a), context);
1,100✔
1266
                        DISPATCH();
1,098✔
1267
                    }
2,322✔
1268

1269
                    TARGET(ISNIL)
1270
                    {
1271
                        const Value* a = popAndResolveAsPtr(context);
2,322✔
1272
                        push((*a == Builtins::nil) ? Builtins::trueSym : Builtins::falseSym, context);
2,322✔
1273
                        DISPATCH();
2,322✔
1274
                    }
596✔
1275

1276
                    TARGET(ASSERT)
1277
                    {
1278
                        Value* const b = popAndResolveAsPtr(context);
596✔
1279
                        Value* const a = popAndResolveAsPtr(context);
596✔
1280

1281
                        if (b->valueType() != ValueType::String)
596✔
1282
                            throw types::TypeCheckingError(
2✔
1283
                                "assert",
1✔
1284
                                { { types::Contract { { types::Typedef("expr", ValueType::Any), types::Typedef("message", ValueType::String) } } } },
1✔
1285
                                { *a, *b });
1✔
1286

1287
                        if (*a == Builtins::falseSym)
595✔
UNCOV
1288
                            throw AssertionFailed(b->stringRef());
×
1289
                        DISPATCH();
595✔
1290
                    }
15✔
1291

1292
                    TARGET(TO_NUM)
1293
                    {
1294
                        const Value* a = popAndResolveAsPtr(context);
15✔
1295

1296
                        if (a->valueType() != ValueType::String)
15✔
1297
                            throw types::TypeCheckingError(
2✔
1298
                                "toNumber",
1✔
1299
                                { { types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1300
                                { *a });
1✔
1301

1302
                        double val;
1303
                        if (Utils::isDouble(a->string(), &val))
14✔
1304
                            push(Value(val), context);
11✔
1305
                        else
1306
                            push(Builtins::nil, context);
3✔
1307
                        DISPATCH();
14✔
1308
                    }
119✔
1309

1310
                    TARGET(TO_STR)
1311
                    {
1312
                        const Value* a = popAndResolveAsPtr(context);
119✔
1313
                        push(Value(a->toString(*this)), context);
119✔
1314
                        DISPATCH();
119✔
1315
                    }
1,044✔
1316

1317
                    TARGET(AT)
1318
                    {
1319
                        Value& b = *popAndResolveAsPtr(context);
1,044✔
1320
                        Value& a = *popAndResolveAsPtr(context);
1,044✔
1321
                        push(helper::at(a, b, *this), context);
1,048✔
1322
                        DISPATCH();
1,040✔
1323
                    }
26✔
1324

1325
                    TARGET(AT_AT)
1326
                    {
1327
                        {
1328
                            const Value* x = popAndResolveAsPtr(context);
26✔
1329
                            const Value* y = popAndResolveAsPtr(context);
26✔
1330
                            Value& list = *popAndResolveAsPtr(context);
26✔
1331

1332
                            if (y->valueType() != ValueType::Number || x->valueType() != ValueType::Number ||
26✔
1333
                                list.valueType() != ValueType::List)
25✔
1334
                                throw types::TypeCheckingError(
2✔
1335
                                    "@@",
1✔
1336
                                    { { types::Contract {
2✔
1337
                                        { types::Typedef("src", ValueType::List),
3✔
1338
                                          types::Typedef("y", ValueType::Number),
1✔
1339
                                          types::Typedef("x", ValueType::Number) } } } },
1✔
1340
                                    { list, *y, *x });
1✔
1341

1342
                            long idx_y = static_cast<long>(y->number());
25✔
1343
                            idx_y = idx_y < 0 ? static_cast<long>(list.list().size()) + idx_y : idx_y;
25✔
1344
                            if (std::cmp_greater_equal(idx_y, list.list().size()) || idx_y < 0)
25✔
1345
                                throwVMError(
2✔
1346
                                    ErrorKind::Index,
1347
                                    fmt::format("@@ index ({}) out of range (list size: {})", idx_y, list.list().size()));
2✔
1348

1349
                            const bool is_list = list.list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
23✔
1350
                            const std::size_t size =
23✔
1351
                                is_list
46✔
1352
                                ? list.list()[static_cast<std::size_t>(idx_y)].list().size()
16✔
1353
                                : list.list()[static_cast<std::size_t>(idx_y)].stringRef().size();
7✔
1354

1355
                            long idx_x = static_cast<long>(x->number());
23✔
1356
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
23✔
1357
                            if (std::cmp_greater_equal(idx_x, size) || idx_x < 0)
23✔
1358
                                throwVMError(
2✔
1359
                                    ErrorKind::Index,
1360
                                    fmt::format("@@ index (x: {}) out of range (inner indexable size: {})", idx_x, size));
2✔
1361

1362
                            if (is_list)
21✔
1363
                                push(list.list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)], context);
14✔
1364
                            else
1365
                                push(Value(std::string(1, list.list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)])), context);
7✔
1366
                        }
1367
                        DISPATCH();
21✔
1368
                    }
16,354✔
1369

1370
                    TARGET(MOD)
1371
                    {
1372
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
16,354✔
1373
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
16,354✔
1374
                            throw types::TypeCheckingError(
2✔
1375
                                "mod",
1✔
1376
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1377
                                { *a, *b });
1✔
1378
                        push(Value(std::fmod(a->number(), b->number())), context);
16,353✔
1379
                        DISPATCH();
16,353✔
1380
                    }
22✔
1381

1382
                    TARGET(TYPE)
1383
                    {
1384
                        const Value* a = popAndResolveAsPtr(context);
22✔
1385
                        push(Value(std::to_string(a->valueType())), context);
22✔
1386
                        DISPATCH();
22✔
1387
                    }
3✔
1388

1389
                    TARGET(HASFIELD)
1390
                    {
1391
                        {
1392
                            Value* const field = popAndResolveAsPtr(context);
3✔
1393
                            Value* const closure = popAndResolveAsPtr(context);
3✔
1394
                            if (closure->valueType() != ValueType::Closure || field->valueType() != ValueType::String)
3✔
1395
                                throw types::TypeCheckingError(
2✔
1396
                                    "hasField",
1✔
1397
                                    { { types::Contract { { types::Typedef("closure", ValueType::Closure), types::Typedef("field", ValueType::String) } } } },
1✔
1398
                                    { *closure, *field });
1✔
1399

1400
                            auto it = std::ranges::find(m_state.m_symbols, field->stringRef());
2✔
1401
                            if (it == m_state.m_symbols.end())
2✔
1402
                            {
1403
                                push(Builtins::falseSym, context);
1✔
1404
                                DISPATCH();
1✔
1405
                            }
1406

1407
                            auto id = static_cast<std::uint16_t>(std::distance(m_state.m_symbols.begin(), it));
1✔
1408
                            push(closure->refClosure().refScope()[id] != nullptr ? Builtins::trueSym : Builtins::falseSym, context);
1✔
1409
                        }
1410
                        DISPATCH();
1✔
1411
                    }
3,570✔
1412

1413
                    TARGET(NOT)
1414
                    {
1415
                        const Value* a = popAndResolveAsPtr(context);
3,570✔
1416
                        push(!(*a) ? Builtins::trueSym : Builtins::falseSym, context);
3,570✔
1417
                        DISPATCH();
3,570✔
1418
                    }
5,995✔
1419

1420
#pragma endregion
1421

1422
#pragma region "Super Instructions"
1423
                    TARGET(LOAD_CONST_LOAD_CONST)
1424
                    {
1425
                        UNPACK_ARGS();
5,995✔
1426
                        push(loadConstAsPtr(primary_arg), context);
5,995✔
1427
                        push(loadConstAsPtr(secondary_arg), context);
5,995✔
1428
                        DISPATCH();
5,995✔
1429
                    }
8,012✔
1430

1431
                    TARGET(LOAD_CONST_STORE)
1432
                    {
1433
                        UNPACK_ARGS();
8,012✔
1434
                        store(secondary_arg, loadConstAsPtr(primary_arg), context);
8,012✔
1435
                        DISPATCH();
8,012✔
1436
                    }
241✔
1437

1438
                    TARGET(LOAD_CONST_SET_VAL)
1439
                    {
1440
                        UNPACK_ARGS();
241✔
1441
                        setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
241✔
1442
                        DISPATCH();
240✔
1443
                    }
20✔
1444

1445
                    TARGET(STORE_FROM)
1446
                    {
1447
                        UNPACK_ARGS();
20✔
1448
                        store(secondary_arg, loadSymbol(primary_arg, context), context);
20✔
1449
                        DISPATCH();
19✔
1450
                    }
1,096✔
1451

1452
                    TARGET(STORE_FROM_INDEX)
1453
                    {
1454
                        UNPACK_ARGS();
1,096✔
1455
                        store(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
1,096✔
1456
                        DISPATCH();
1,096✔
1457
                    }
547✔
1458

1459
                    TARGET(SET_VAL_FROM)
1460
                    {
1461
                        UNPACK_ARGS();
547✔
1462
                        setVal(secondary_arg, loadSymbol(primary_arg, context), context);
547✔
1463
                        DISPATCH();
547✔
1464
                    }
223✔
1465

1466
                    TARGET(SET_VAL_FROM_INDEX)
1467
                    {
1468
                        UNPACK_ARGS();
223✔
1469
                        setVal(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
223✔
1470
                        DISPATCH();
223✔
1471
                    }
16✔
1472

1473
                    TARGET(INCREMENT)
1474
                    {
1475
                        UNPACK_ARGS();
16✔
1476
                        {
1477
                            Value* var = loadSymbol(primary_arg, context);
16✔
1478

1479
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1480
                            if (var->valueType() == ValueType::Reference)
16✔
1481
                                var = var->reference();
×
1482

1483
                            if (var->valueType() == ValueType::Number)
16✔
1484
                                push(Value(var->number() + secondary_arg), context);
16✔
1485
                            else
UNCOV
1486
                                throw types::TypeCheckingError(
×
UNCOV
1487
                                    "+",
×
UNCOV
1488
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
UNCOV
1489
                                    { *var, Value(secondary_arg) });
×
1490
                        }
1491
                        DISPATCH();
16✔
1492
                    }
87,973✔
1493

1494
                    TARGET(INCREMENT_BY_INDEX)
1495
                    {
1496
                        UNPACK_ARGS();
87,973✔
1497
                        {
1498
                            Value* var = loadSymbolFromIndex(primary_arg, context);
87,973✔
1499

1500
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1501
                            if (var->valueType() == ValueType::Reference)
87,973✔
1502
                                var = var->reference();
×
1503

1504
                            if (var->valueType() == ValueType::Number)
87,973✔
1505
                                push(Value(var->number() + secondary_arg), context);
87,972✔
1506
                            else
1507
                                throw types::TypeCheckingError(
2✔
1508
                                    "+",
1✔
1509
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1510
                                    { *var, Value(secondary_arg) });
1✔
1511
                        }
1512
                        DISPATCH();
87,972✔
1513
                    }
31,123✔
1514

1515
                    TARGET(INCREMENT_STORE)
1516
                    {
1517
                        UNPACK_ARGS();
31,123✔
1518
                        {
1519
                            Value* var = loadSymbol(primary_arg, context);
31,123✔
1520

1521
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1522
                            if (var->valueType() == ValueType::Reference)
31,123✔
UNCOV
1523
                                var = var->reference();
×
1524

1525
                            if (var->valueType() == ValueType::Number)
31,123✔
1526
                            {
1527
                                Value val = Value(var->number() + secondary_arg);
31,123✔
1528
                                setVal(primary_arg, &val, context);
31,123✔
1529
                            }
31,123✔
1530
                            else
1531
                                throw types::TypeCheckingError(
×
UNCOV
1532
                                    "+",
×
1533
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
UNCOV
1534
                                    { *var, Value(secondary_arg) });
×
1535
                        }
1536
                        DISPATCH();
31,123✔
1537
                    }
1,776✔
1538

1539
                    TARGET(DECREMENT)
1540
                    {
1541
                        UNPACK_ARGS();
1,776✔
1542
                        {
1543
                            Value* var = loadSymbol(primary_arg, context);
1,776✔
1544

1545
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1546
                            if (var->valueType() == ValueType::Reference)
1,776✔
1547
                                var = var->reference();
×
1548

1549
                            if (var->valueType() == ValueType::Number)
1,776✔
1550
                                push(Value(var->number() - secondary_arg), context);
1,776✔
1551
                            else
UNCOV
1552
                                throw types::TypeCheckingError(
×
UNCOV
1553
                                    "-",
×
UNCOV
1554
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1555
                                    { *var, Value(secondary_arg) });
×
1556
                        }
1557
                        DISPATCH();
1,776✔
1558
                    }
194,408✔
1559

1560
                    TARGET(DECREMENT_BY_INDEX)
1561
                    {
1562
                        UNPACK_ARGS();
194,408✔
1563
                        {
1564
                            Value* var = loadSymbolFromIndex(primary_arg, context);
194,408✔
1565

1566
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1567
                            if (var->valueType() == ValueType::Reference)
194,408✔
UNCOV
1568
                                var = var->reference();
×
1569

1570
                            if (var->valueType() == ValueType::Number)
194,408✔
1571
                                push(Value(var->number() - secondary_arg), context);
194,407✔
1572
                            else
1573
                                throw types::TypeCheckingError(
2✔
1574
                                    "-",
1✔
1575
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1576
                                    { *var, Value(secondary_arg) });
1✔
1577
                        }
1578
                        DISPATCH();
194,407✔
1579
                    }
×
1580

1581
                    TARGET(DECREMENT_STORE)
1582
                    {
1583
                        UNPACK_ARGS();
×
1584
                        {
UNCOV
1585
                            Value* var = loadSymbol(primary_arg, context);
×
1586

1587
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
UNCOV
1588
                            if (var->valueType() == ValueType::Reference)
×
UNCOV
1589
                                var = var->reference();
×
1590

UNCOV
1591
                            if (var->valueType() == ValueType::Number)
×
1592
                            {
UNCOV
1593
                                Value val = Value(var->number() - secondary_arg);
×
UNCOV
1594
                                setVal(primary_arg, &val, context);
×
UNCOV
1595
                            }
×
1596
                            else
UNCOV
1597
                                throw types::TypeCheckingError(
×
UNCOV
1598
                                    "-",
×
UNCOV
1599
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
UNCOV
1600
                                    { *var, Value(secondary_arg) });
×
1601
                        }
UNCOV
1602
                        DISPATCH();
×
UNCOV
1603
                    }
×
1604

1605
                    TARGET(STORE_TAIL)
1606
                    {
UNCOV
1607
                        UNPACK_ARGS();
×
1608
                        {
1609
                            Value* list = loadSymbol(primary_arg, context);
×
UNCOV
1610
                            Value tail = helper::tail(list);
×
1611
                            store(secondary_arg, &tail, context);
×
1612
                        }
×
1613
                        DISPATCH();
×
1614
                    }
1✔
1615

1616
                    TARGET(STORE_TAIL_BY_INDEX)
1617
                    {
1618
                        UNPACK_ARGS();
1✔
1619
                        {
1620
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1621
                            Value tail = helper::tail(list);
1✔
1622
                            store(secondary_arg, &tail, context);
1✔
1623
                        }
1✔
1624
                        DISPATCH();
1✔
UNCOV
1625
                    }
×
1626

1627
                    TARGET(STORE_HEAD)
1628
                    {
UNCOV
1629
                        UNPACK_ARGS();
×
1630
                        {
1631
                            Value* list = loadSymbol(primary_arg, context);
×
UNCOV
1632
                            Value head = helper::head(list);
×
1633
                            store(secondary_arg, &head, context);
×
1634
                        }
×
1635
                        DISPATCH();
×
1636
                    }
31✔
1637

1638
                    TARGET(STORE_HEAD_BY_INDEX)
1639
                    {
1640
                        UNPACK_ARGS();
31✔
1641
                        {
1642
                            Value* list = loadSymbolFromIndex(primary_arg, context);
31✔
1643
                            Value head = helper::head(list);
31✔
1644
                            store(secondary_arg, &head, context);
31✔
1645
                        }
31✔
1646
                        DISPATCH();
31✔
1647
                    }
903✔
1648

1649
                    TARGET(STORE_LIST)
1650
                    {
1651
                        UNPACK_ARGS();
903✔
1652
                        {
1653
                            Value l = createList(primary_arg, context);
903✔
1654
                            store(secondary_arg, &l, context);
903✔
1655
                        }
903✔
1656
                        DISPATCH();
903✔
1657
                    }
×
1658

1659
                    TARGET(SET_VAL_TAIL)
1660
                    {
UNCOV
1661
                        UNPACK_ARGS();
×
1662
                        {
UNCOV
1663
                            Value* list = loadSymbol(primary_arg, context);
×
UNCOV
1664
                            Value tail = helper::tail(list);
×
UNCOV
1665
                            setVal(secondary_arg, &tail, context);
×
UNCOV
1666
                        }
×
1667
                        DISPATCH();
×
1668
                    }
1✔
1669

1670
                    TARGET(SET_VAL_TAIL_BY_INDEX)
1671
                    {
1672
                        UNPACK_ARGS();
1✔
1673
                        {
1674
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1675
                            Value tail = helper::tail(list);
1✔
1676
                            setVal(secondary_arg, &tail, context);
1✔
1677
                        }
1✔
1678
                        DISPATCH();
1✔
UNCOV
1679
                    }
×
1680

1681
                    TARGET(SET_VAL_HEAD)
1682
                    {
UNCOV
1683
                        UNPACK_ARGS();
×
1684
                        {
UNCOV
1685
                            Value* list = loadSymbol(primary_arg, context);
×
UNCOV
1686
                            Value head = helper::head(list);
×
UNCOV
1687
                            setVal(secondary_arg, &head, context);
×
UNCOV
1688
                        }
×
UNCOV
1689
                        DISPATCH();
×
1690
                    }
1✔
1691

1692
                    TARGET(SET_VAL_HEAD_BY_INDEX)
1693
                    {
1694
                        UNPACK_ARGS();
1✔
1695
                        {
1696
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1697
                            Value head = helper::head(list);
1✔
1698
                            setVal(secondary_arg, &head, context);
1✔
1699
                        }
1✔
1700
                        DISPATCH();
1✔
1701
                    }
809✔
1702

1703
                    TARGET(CALL_BUILTIN)
1704
                    {
1705
                        UNPACK_ARGS();
809✔
1706
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1707
                        callBuiltin(context, Builtins::builtins[primary_arg].second, secondary_arg);
809✔
1708
                        if (!m_running)
751✔
UNCOV
1709
                            GOTO_HALT();
×
1710
                        DISPATCH();
751✔
1711
                    }
11,203✔
1712

1713
                    TARGET(CALL_BUILTIN_WITHOUT_RETURN_ADDRESS)
1714
                    {
1715
                        UNPACK_ARGS();
11,203✔
1716
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1717
                        callBuiltin(context, Builtins::builtins[primary_arg].second, secondary_arg, /* remove_return_address= */ false);
11,203✔
1718
                        if (!m_running)
11,202✔
UNCOV
1719
                            GOTO_HALT();
×
1720
                        DISPATCH();
11,202✔
1721
                    }
857✔
1722

1723
                    TARGET(LT_CONST_JUMP_IF_FALSE)
1724
                    {
1725
                        UNPACK_ARGS();
857✔
1726
                        const Value* sym = popAndResolveAsPtr(context);
857✔
1727
                        if (!(*sym < *loadConstAsPtr(primary_arg)))
857✔
1728
                            context.ip = secondary_arg * 4;
122✔
1729
                        DISPATCH();
857✔
1730
                    }
21,902✔
1731

1732
                    TARGET(LT_CONST_JUMP_IF_TRUE)
1733
                    {
1734
                        UNPACK_ARGS();
21,902✔
1735
                        const Value* sym = popAndResolveAsPtr(context);
21,902✔
1736
                        if (*sym < *loadConstAsPtr(primary_arg))
21,902✔
1737
                            context.ip = secondary_arg * 4;
10,950✔
1738
                        DISPATCH();
21,902✔
1739
                    }
5,543✔
1740

1741
                    TARGET(LT_SYM_JUMP_IF_FALSE)
1742
                    {
1743
                        UNPACK_ARGS();
5,543✔
1744
                        const Value* sym = popAndResolveAsPtr(context);
5,543✔
1745
                        if (!(*sym < *loadSymbol(primary_arg, context)))
5,543✔
1746
                            context.ip = secondary_arg * 4;
536✔
1747
                        DISPATCH();
5,543✔
1748
                    }
172,500✔
1749

1750
                    TARGET(GT_CONST_JUMP_IF_TRUE)
1751
                    {
1752
                        UNPACK_ARGS();
172,500✔
1753
                        const Value* sym = popAndResolveAsPtr(context);
172,500✔
1754
                        const Value* cst = loadConstAsPtr(primary_arg);
172,500✔
1755
                        if (*cst < *sym)
172,500✔
1756
                            context.ip = secondary_arg * 4;
86,586✔
1757
                        DISPATCH();
172,500✔
1758
                    }
15✔
1759

1760
                    TARGET(GT_CONST_JUMP_IF_FALSE)
1761
                    {
1762
                        UNPACK_ARGS();
15✔
1763
                        const Value* sym = popAndResolveAsPtr(context);
15✔
1764
                        const Value* cst = loadConstAsPtr(primary_arg);
15✔
1765
                        if (!(*cst < *sym))
15✔
1766
                            context.ip = secondary_arg * 4;
3✔
1767
                        DISPATCH();
15✔
UNCOV
1768
                    }
×
1769

1770
                    TARGET(GT_SYM_JUMP_IF_FALSE)
1771
                    {
UNCOV
1772
                        UNPACK_ARGS();
×
UNCOV
1773
                        const Value* sym = popAndResolveAsPtr(context);
×
UNCOV
1774
                        const Value* rhs = loadSymbol(primary_arg, context);
×
UNCOV
1775
                        if (!(*rhs < *sym))
×
UNCOV
1776
                            context.ip = secondary_arg * 4;
×
UNCOV
1777
                        DISPATCH();
×
1778
                    }
1,238✔
1779

1780
                    TARGET(EQ_CONST_JUMP_IF_TRUE)
1781
                    {
1782
                        UNPACK_ARGS();
1,238✔
1783
                        const Value* sym = popAndResolveAsPtr(context);
1,238✔
1784
                        if (*sym == *loadConstAsPtr(primary_arg))
1,238✔
1785
                            context.ip = secondary_arg * 4;
251✔
1786
                        DISPATCH();
1,238✔
1787
                    }
86,933✔
1788

1789
                    TARGET(EQ_SYM_INDEX_JUMP_IF_TRUE)
1790
                    {
1791
                        UNPACK_ARGS();
86,933✔
1792
                        const Value* sym = popAndResolveAsPtr(context);
86,933✔
1793
                        if (*sym == *loadSymbolFromIndex(primary_arg, context))
86,933✔
1794
                            context.ip = secondary_arg * 4;
529✔
1795
                        DISPATCH();
86,933✔
1796
                    }
1✔
1797

1798
                    TARGET(NEQ_CONST_JUMP_IF_TRUE)
1799
                    {
1800
                        UNPACK_ARGS();
1✔
1801
                        const Value* sym = popAndResolveAsPtr(context);
1✔
1802
                        if (*sym != *loadConstAsPtr(primary_arg))
1✔
1803
                            context.ip = secondary_arg * 4;
1✔
1804
                        DISPATCH();
1✔
1805
                    }
15✔
1806

1807
                    TARGET(NEQ_SYM_JUMP_IF_FALSE)
1808
                    {
1809
                        UNPACK_ARGS();
15✔
1810
                        const Value* sym = popAndResolveAsPtr(context);
15✔
1811
                        if (*sym == *loadSymbol(primary_arg, context))
15✔
1812
                            context.ip = secondary_arg * 4;
5✔
1813
                        DISPATCH();
15✔
1814
                    }
26,093✔
1815

1816
                    TARGET(CALL_SYMBOL)
1817
                    {
1818
                        UNPACK_ARGS();
26,093✔
1819
                        call(context, secondary_arg, loadSymbol(primary_arg, context));
26,093✔
1820
                        if (!m_running)
26,093✔
UNCOV
1821
                            GOTO_HALT();
×
1822
                        DISPATCH();
26,093✔
1823
                    }
109,861✔
1824

1825
                    TARGET(CALL_CURRENT_PAGE)
1826
                    {
1827
                        UNPACK_ARGS();
109,861✔
1828
                        context.last_symbol = primary_arg;
109,861✔
1829
                        call(context, secondary_arg, /* function_ptr= */ nullptr, /* or_address= */ static_cast<PageAddr_t>(context.pp));
109,861✔
1830
                        if (!m_running)
109,860✔
UNCOV
1831
                            GOTO_HALT();
×
1832
                        DISPATCH();
109,860✔
1833
                    }
1,917✔
1834

1835
                    TARGET(GET_FIELD_FROM_SYMBOL)
1836
                    {
1837
                        UNPACK_ARGS();
1,917✔
1838
                        push(getField(loadSymbol(primary_arg, context), secondary_arg, context), context);
1,917✔
1839
                        DISPATCH();
1,917✔
1840
                    }
715✔
1841

1842
                    TARGET(GET_FIELD_FROM_SYMBOL_INDEX)
1843
                    {
1844
                        UNPACK_ARGS();
715✔
1845
                        push(getField(loadSymbolFromIndex(primary_arg, context), secondary_arg, context), context);
718✔
1846
                        DISPATCH();
712✔
1847
                    }
14,448✔
1848

1849
                    TARGET(AT_SYM_SYM)
1850
                    {
1851
                        UNPACK_ARGS();
14,448✔
1852
                        push(helper::at(*loadSymbol(primary_arg, context), *loadSymbol(secondary_arg, context), *this), context);
14,448✔
1853
                        DISPATCH();
14,448✔
UNCOV
1854
                    }
×
1855

1856
                    TARGET(AT_SYM_INDEX_SYM_INDEX)
1857
                    {
UNCOV
1858
                        UNPACK_ARGS();
×
UNCOV
1859
                        push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadSymbolFromIndex(secondary_arg, context), *this), context);
×
UNCOV
1860
                        DISPATCH();
×
1861
                    }
6✔
1862

1863
                    TARGET(CHECK_TYPE_OF)
1864
                    {
1865
                        UNPACK_ARGS();
6✔
1866
                        const Value* sym = loadSymbol(primary_arg, context);
6✔
1867
                        const Value* cst = loadConstAsPtr(secondary_arg);
6✔
1868
                        push(
6✔
1869
                            cst->valueType() == ValueType::String &&
12✔
1870
                                    std::to_string(sym->valueType()) == cst->string()
6✔
1871
                                ? Builtins::trueSym
1872
                                : Builtins::falseSym,
1873
                            context);
6✔
1874
                        DISPATCH();
6✔
1875
                    }
74✔
1876

1877
                    TARGET(CHECK_TYPE_OF_BY_INDEX)
1878
                    {
1879
                        UNPACK_ARGS();
74✔
1880
                        const Value* sym = loadSymbolFromIndex(primary_arg, context);
74✔
1881
                        const Value* cst = loadConstAsPtr(secondary_arg);
74✔
1882
                        push(
74✔
1883
                            cst->valueType() == ValueType::String &&
148✔
1884
                                    std::to_string(sym->valueType()) == cst->string()
74✔
1885
                                ? Builtins::trueSym
1886
                                : Builtins::falseSym,
1887
                            context);
74✔
1888
                        DISPATCH();
74✔
1889
                    }
1,459✔
1890

1891
                    TARGET(APPEND_IN_PLACE_SYM)
1892
                    {
1893
                        UNPACK_ARGS();
1,459✔
1894
                        listAppendInPlace(loadSymbol(primary_arg, context), secondary_arg, context);
1,459✔
1895
                        DISPATCH();
1,459✔
1896
                    }
12✔
1897

1898
                    TARGET(APPEND_IN_PLACE_SYM_INDEX)
1899
                    {
1900
                        UNPACK_ARGS();
12✔
1901
                        listAppendInPlace(loadSymbolFromIndex(primary_arg, context), secondary_arg, context);
12✔
1902
                        DISPATCH();
11✔
1903
                    }
1904
#pragma endregion
1905
                }
79✔
1906
#if ARK_USE_COMPUTED_GOTOS
1907
            dispatch_end:
1908
                do
79✔
1909
                {
1910
                } while (false);
79✔
1911
#endif
1912
            }
1913
        }
197✔
1914
        catch (const Error& e)
1915
        {
1916
            if (fail_with_exception)
75✔
1917
            {
1918
                std::stringstream stream;
75✔
1919
                backtrace(context, stream, /* colorize= */ false);
75✔
1920
                // It's important we have an Ark::Error here, as the constructor for NestedError
1921
                // does more than just aggregate error messages, hence the code duplication.
1922
                throw NestedError(e, stream.str());
75✔
1923
            }
75✔
1924
            else
UNCOV
1925
                showBacktraceWithException(Error(e.details(/* colorize= */ true)), context);
×
1926
        }
154✔
1927
        catch (const std::exception& e)
1928
        {
1929
            if (fail_with_exception)
43✔
1930
            {
1931
                std::stringstream stream;
43✔
1932
                backtrace(context, stream, /* colorize= */ false);
43✔
1933
                throw NestedError(e, stream.str());
43✔
1934
            }
43✔
1935
            else
UNCOV
1936
                showBacktraceWithException(e, context);
×
1937
        }
118✔
1938
        catch (...)
1939
        {
UNCOV
1940
            if (fail_with_exception)
×
UNCOV
1941
                throw;
×
1942

1943
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1944
            throw;
1945
#endif
UNCOV
1946
            fmt::println("Unknown error");
×
UNCOV
1947
            backtrace(context);
×
UNCOV
1948
            m_exit_code = 1;
×
1949
        }
161✔
1950

1951
        return m_exit_code;
79✔
1952
    }
236✔
1953

1954
    uint16_t VM::findNearestVariableIdWithValue(const Value& value, ExecutionContext& context) const noexcept
2,048✔
1955
    {
2,048✔
1956
        for (auto& local : std::ranges::reverse_view(context.locals))
2,098,178✔
1957
        {
1958
            if (const auto id = local.idFromValue(value); id < m_state.m_symbols.size())
2,096,130✔
1959
                return id;
2,048✔
1960
        }
2,096,130✔
UNCOV
1961
        return std::numeric_limits<uint16_t>::max();
×
1962
    }
2,048✔
1963

1964
    void VM::throwArityError(std::size_t passed_arg_count, std::size_t expected_arg_count, internal::ExecutionContext& context)
5✔
1965
    {
5✔
1966
        std::vector<std::string> arg_names;
5✔
1967
        arg_names.reserve(expected_arg_count + 1);
5✔
1968
        if (expected_arg_count > 0)
5✔
1969
            arg_names.emplace_back("");  // for formatting, so that we have a space between the function and the args
5✔
1970

1971
        std::size_t index = 0;
5✔
1972
        while (m_state.inst(context.pp, index) == STORE)
12✔
1973
        {
1974
            const auto id = static_cast<uint16_t>((m_state.inst(context.pp, index + 2) << 8) + m_state.inst(context.pp, index + 3));
7✔
1975
            arg_names.push_back(m_state.m_symbols[id]);
7✔
1976
            index += 4;
7✔
1977
        }
7✔
1978
        // we only the blank space for formatting and no arg names, probably because of a CALL_BUILTIN_WITHOUT_RETURN_ADDRESS
1979
        if (arg_names.size() == 1 && index == 0)
5✔
1980
        {
1981
            assert(m_state.inst(context.pp, 0) == CALL_BUILTIN_WITHOUT_RETURN_ADDRESS && "expected a CALL_BUILTIN_WITHOUT_RETURN_ADDRESS instruction or STORE instructions");
2✔
1982
            for (std::size_t i = 0; i < expected_arg_count; ++i)
4✔
1983
                arg_names.push_back(std::string(1, static_cast<char>('a' + i)));
2✔
1984
        }
2✔
1985

1986
        std::vector<std::string> arg_vals;
5✔
1987
        arg_vals.reserve(passed_arg_count + 1);
5✔
1988
        if (passed_arg_count > 0)
5✔
1989
            arg_vals.emplace_back("");  // for formatting, so that we have a space between the function and the args
4✔
1990

1991
        for (std::size_t i = 0; i < passed_arg_count && i + 1 <= context.sp; ++i)
15✔
1992
            // -1 on the stack because we always point to the next available slot
1993
            arg_vals.push_back(context.stack[context.sp - i - 1].toString(*this));
10✔
1994

1995
        // set ip/pp to the callee location so that the error can pinpoint the line
1996
        // where the bad call happened
1997
        if (context.sp >= 2 + passed_arg_count)
5✔
1998
        {
1999
            context.ip = context.stack[context.sp - 1 - passed_arg_count].pageAddr();
5✔
2000
            context.pp = context.stack[context.sp - 2 - passed_arg_count].pageAddr();
5✔
2001
            returnFromFuncCall(context);
5✔
2002
        }
5✔
2003

2004
        std::string function_name = (context.last_symbol < m_state.m_symbols.size())
10✔
2005
            ? m_state.m_symbols[context.last_symbol]
5✔
UNCOV
2006
            : Value(static_cast<PageAddr_t>(context.pp)).toString(*this);
×
2007

2008
        throwVMError(
5✔
2009
            ErrorKind::Arity,
2010
            fmt::format(
10✔
2011
                "When calling `({}{})', received {} argument{}, but expected {}: `({}{})'",
5✔
2012
                function_name,
2013
                fmt::join(arg_vals, " "),
5✔
2014
                passed_arg_count,
2015
                passed_arg_count > 1 ? "s" : "",
5✔
2016
                expected_arg_count,
2017
                function_name,
2018
                fmt::join(arg_names, " ")));
5✔
2019
    }
10✔
2020

UNCOV
2021
    void VM::showBacktraceWithException(const std::exception& e, internal::ExecutionContext& context)
×
UNCOV
2022
    {
×
UNCOV
2023
        std::string text = e.what();
×
UNCOV
2024
        if (!text.empty() && text.back() != '\n')
×
UNCOV
2025
            text += '\n';
×
UNCOV
2026
        fmt::println("{}", text);
×
UNCOV
2027
        backtrace(context);
×
2028
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2029
        // don't report a "failed" exit code so that the fuzzers can more accurately triage crashes
2030
        m_exit_code = 0;
2031
#else
UNCOV
2032
        m_exit_code = 1;
×
2033
#endif
UNCOV
2034
    }
×
2035

2036
    std::optional<InstLoc> VM::findSourceLocation(const std::size_t ip, const std::size_t pp)
2,168✔
2037
    {
2,168✔
2038
        std::optional<InstLoc> match = std::nullopt;
2,168✔
2039

2040
        for (const auto location : m_state.m_inst_locations)
8,732✔
2041
        {
2042
            if (location.page_pointer == pp && !match)
6,564✔
2043
                match = location;
2,168✔
2044

2045
            // select the best match: we want to find the location that's nearest our instruction pointer,
2046
            // but not equal to it as the IP will always be pointing to the next instruction,
2047
            // not yet executed. Thus, the erroneous instruction is the previous one.
2048
            if (location.page_pointer == pp && match && location.inst_pointer < ip / 4)
6,564✔
2049
                match = location;
2,287✔
2050

2051
            // early exit because we won't find anything better, as inst locations are ordered by ascending (pp, ip)
2052
            if (location.page_pointer > pp || (location.page_pointer == pp && location.inst_pointer >= ip / 4))
6,564✔
2053
                break;
13✔
2054
        }
6,564✔
2055

2056
        return match;
2,168✔
2057
    }
2058

UNCOV
2059
    std::string VM::debugShowSource()
×
UNCOV
2060
    {
×
UNCOV
2061
        const auto& context = m_execution_contexts.front();
×
UNCOV
2062
        auto maybe_source_loc = findSourceLocation(context->ip, context->pp);
×
UNCOV
2063
        if (maybe_source_loc)
×
2064
        {
UNCOV
2065
            const auto filename = m_state.m_filenames[maybe_source_loc->filename_id];
×
UNCOV
2066
            return fmt::format("{}:{} -- IP: {}, PP: {}", filename, maybe_source_loc->line + 1, maybe_source_loc->inst_pointer, maybe_source_loc->page_pointer);
×
UNCOV
2067
        }
×
UNCOV
2068
        return "No source location found";
×
UNCOV
2069
    }
×
2070

2071
    void VM::backtrace(ExecutionContext& context, std::ostream& os, const bool colorize)
118✔
2072
    {
118✔
2073
        const std::size_t saved_ip = context.ip;
118✔
2074
        const std::size_t saved_pp = context.pp;
118✔
2075
        const uint16_t saved_sp = context.sp;
118✔
2076
        const std::size_t max_consecutive_traces = 7;
118✔
2077

2078
        const auto maybe_location = findSourceLocation(context.ip, context.pp);
118✔
2079
        if (maybe_location)
118✔
2080
        {
2081
            const auto filename = m_state.m_filenames[maybe_location->filename_id];
118✔
2082

2083
            if (Utils::fileExists(filename))
118✔
2084
                Diagnostics::makeContext(
236✔
2085
                    os,
118✔
2086
                    filename,
2087
                    /* expr= */ std::nullopt,
118✔
2088
                    /* sym_size= */ 0,
2089
                    maybe_location->line,
118✔
2090
                    /* col_start= */ 0,
2091
                    /* maybe_context= */ std::nullopt,
118✔
2092
                    /* whole_line= */ true,
2093
                    /* colorize= */ colorize);
118✔
2094
            fmt::println(os, "");
118✔
2095
        }
118✔
2096

2097
        if (context.fc > 1)
118✔
2098
        {
2099
            // display call stack trace
2100
            const ScopeView old_scope = context.locals.back();
2✔
2101

2102
            std::string previous_trace;
2✔
2103
            std::size_t displayed_traces = 0;
2✔
2104
            std::size_t consecutive_similar_traces = 0;
2✔
2105

2106
            while (context.fc != 0 && context.pp != 0)
2,050✔
2107
            {
2108
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2,048✔
2109
                const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
2,048✔
2110

2111
                const uint16_t id = findNearestVariableIdWithValue(
2,048✔
2112
                    Value(static_cast<PageAddr_t>(context.pp)),
2,048✔
2113
                    context);
2,048✔
2114
                const auto func_name = (id < m_state.m_symbols.size()) ? m_state.m_symbols[id] : "???";
2,048✔
2115

2116
                if (func_name + loc_as_text != previous_trace)
2,048✔
2117
                {
2118
                    fmt::println(
4✔
2119
                        os,
2✔
2120
                        "[{:4}] In function `{}'{}",
2✔
2121
                        fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
2✔
2122
                        fmt::styled(func_name, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
2✔
2123
                        loc_as_text);
2124
                    previous_trace = func_name + loc_as_text;
2✔
2125
                    ++displayed_traces;
2✔
2126
                    consecutive_similar_traces = 0;
2✔
2127
                }
2✔
2128
                else if (consecutive_similar_traces == 0)
2,046✔
2129
                {
2130
                    fmt::println(os, "       ...");
1✔
2131
                    ++consecutive_similar_traces;
1✔
2132
                }
1✔
2133

2134
                const Value* ip;
2,048✔
2135
                do
2,049✔
2136
                {
2137
                    ip = popAndResolveAsPtr(context);
2,049✔
2138
                } while (ip->valueType() != ValueType::InstPtr);
2,049✔
2139

2140
                context.ip = ip->pageAddr();
2,048✔
2141
                context.pp = pop(context)->pageAddr();
2,048✔
2142
                returnFromFuncCall(context);
2,048✔
2143

2144
                if (displayed_traces > max_consecutive_traces)
2,048✔
2145
                {
UNCOV
2146
                    fmt::println(os, "       ...");
×
UNCOV
2147
                    break;
×
2148
                }
2149
            }
2,048✔
2150

2151
            if (context.pp == 0)
2✔
2152
            {
2153
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2✔
2154
                const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
2✔
2155
                fmt::println(os, "[{:4}] In global scope{}", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()), loc_as_text);
2✔
2156
            }
2✔
2157

2158
            // display variables values in the current scope
2159
            fmt::println(os, "\nCurrent scope variables values:");
2✔
2160
            for (std::size_t i = 0, size = old_scope.size(); i < size; ++i)
3✔
2161
            {
2162
                fmt::println(
2✔
2163
                    os,
1✔
2164
                    "{} = {}",
1✔
2165
                    fmt::styled(m_state.m_symbols[old_scope.atPos(i).first], colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
1✔
2166
                    old_scope.atPos(i).second.toString(*this));
1✔
2167
            }
1✔
2168
        }
2✔
2169

2170
        fmt::println(
236✔
2171
            os,
118✔
2172
            "At IP: {}, PP: {}, SP: {}",
118✔
2173
            // dividing by 4 because the instructions are actually on 4 bytes
2174
            fmt::styled(saved_ip / 4, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
118✔
2175
            fmt::styled(saved_pp, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
118✔
2176
            fmt::styled(saved_sp, colorize ? fmt::fg(fmt::color::yellow) : fmt::text_style()));
118✔
2177
    }
118✔
2178
}
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