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

ArkScript-lang / Ark / 16852602262

09 Aug 2025 06:49PM UTC coverage: 86.842% (-0.03%) from 86.869%
16852602262

Pull #568

github

web-flow
Merge c5673fd08 into c65ea7e5b
Pull Request #568: feat(closures): captures are no longer fully qualified

92 of 113 new or added lines in 13 files covered. (81.42%)

6 existing lines in 2 files now uncovered.

7537 of 8679 relevant lines covered (86.84%)

125769.07 hits per line

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

83.09
/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 :
537✔
119
        m_state(state), m_exit_code(0), m_running(false)
179✔
120
    {
179✔
121
        m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>());
179✔
122
    }
179✔
123

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

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

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

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

144
        context.locals.clear();
180✔
145
        context.locals.reserve(128);
180✔
146
        context.locals.emplace_back(context.scopes_storage.data(), 0);
180✔
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)
377✔
151
        {
152
            auto it = std::ranges::find(m_state.m_symbols, sym_id);
186✔
153
            if (it != m_state.m_symbols.end())
186✔
154
                context.locals[0].push_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), value);
11✔
155
        }
186✔
156
    }
180✔
157

158
    Value VM::getField(Value* closure, const uint16_t id, ExecutionContext& context)
2,611✔
159
    {
2,611✔
160
        if (closure->valueType() != ValueType::Closure)
2,611✔
161
        {
162
            if (context.last_symbol < m_state.m_symbols.size()) [[likely]]
1✔
163
                throwVMError(
2✔
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()),
179✔
175
                                 m_state.m_symbols[id]));
×
176
        }
177

178
        if (Value* field = closure->refClosure().refScope()[id]; field != nullptr)
5,220✔
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,609✔
182
                return Value(Closure(closure->refClosure().scopePtr(), field->pageAddr()));
1,516✔
183
            else
184
                return *field;
1,093✔
185
        }
186
        else
187
        {
188
            if (!closure->refClosure().hasFieldEndingWith(m_state.m_symbols[id], *this))
1✔
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✔
UNCOV
195
            throwVMError(
×
196
                ErrorKind::Scope,
UNCOV
197
                fmt::format(
×
UNCOV
198
                    "`{0}' isn't in the closure environment: {1}. A variable in the package might have the same name as '{0}', "
×
199
                    "and name resolution tried to fully qualify it. Rename either the variable or the capture to solve this",
UNCOV
200
                    m_state.m_symbols[id],
×
UNCOV
201
                    closure->refClosure().toString(*this)));
×
202
        }
203
    }
2,611✔
204

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

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

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

217
    void VM::listAppendInPlace(Value* list, const std::size_t count, ExecutionContext& context)
1,470✔
218
    {
1,470✔
219
        if (list->valueType() != ValueType::List)
1,470✔
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,938✔
231
            list->push_back(*popAndResolveAsPtr(context));
1,469✔
232
    }
1,470✔
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
        {
NEW
314
            const mapping* map = m_shared_lib_objects.back()->get<mapping* (*)()>("getFunctionsMapping")();
×
315
            // load the mapping data
NEW
316
            std::size_t i = 0;
×
317
            while (map[i].name != nullptr)
×
318
            {
319
                // put it in the global frame, aka the first one
NEW
320
                auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
NEW
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,017✔
388
            {
389
                const auto& [id, val] = scope_view.atPos(i);
1,976✔
390
                new_scope.push_back(id, val);
1,976✔
391
            }
1,976✔
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

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

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

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

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

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

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

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

499
    int VM::safeRun(ExecutionContext& context, std::size_t untilFrameCount, bool fail_with_exception)
196✔
500
    {
196✔
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 = {
196✔
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_RENAME_NEXT_CAPTURE,
553
                &&TARGET_BUILTIN,
554
                &&TARGET_DEL,
555
                &&TARGET_MAKE_CLOSURE,
556
                &&TARGET_GET_FIELD,
557
                &&TARGET_PLUGIN,
558
                &&TARGET_LIST,
559
                &&TARGET_APPEND,
560
                &&TARGET_CONCAT,
561
                &&TARGET_APPEND_IN_PLACE,
562
                &&TARGET_CONCAT_IN_PLACE,
563
                &&TARGET_POP_LIST,
564
                &&TARGET_POP_LIST_IN_PLACE,
565
                &&TARGET_SET_AT_INDEX,
566
                &&TARGET_SET_AT_2_INDEX,
567
                &&TARGET_POP,
568
                &&TARGET_SHORTCIRCUIT_AND,
569
                &&TARGET_SHORTCIRCUIT_OR,
570
                &&TARGET_CREATE_SCOPE,
571
                &&TARGET_RESET_SCOPE_JUMP,
572
                &&TARGET_POP_SCOPE,
573
                &&TARGET_GET_CURRENT_PAGE_ADDR,
574
                &&TARGET_ADD,
575
                &&TARGET_SUB,
576
                &&TARGET_MUL,
577
                &&TARGET_DIV,
578
                &&TARGET_GT,
579
                &&TARGET_LT,
580
                &&TARGET_LE,
581
                &&TARGET_GE,
582
                &&TARGET_NEQ,
583
                &&TARGET_EQ,
584
                &&TARGET_LEN,
585
                &&TARGET_EMPTY,
586
                &&TARGET_TAIL,
587
                &&TARGET_HEAD,
588
                &&TARGET_ISNIL,
589
                &&TARGET_ASSERT,
590
                &&TARGET_TO_NUM,
591
                &&TARGET_TO_STR,
592
                &&TARGET_AT,
593
                &&TARGET_AT_AT,
594
                &&TARGET_MOD,
595
                &&TARGET_TYPE,
596
                &&TARGET_HASFIELD,
597
                &&TARGET_NOT,
598
                &&TARGET_LOAD_CONST_LOAD_CONST,
599
                &&TARGET_LOAD_CONST_STORE,
600
                &&TARGET_LOAD_CONST_SET_VAL,
601
                &&TARGET_STORE_FROM,
602
                &&TARGET_STORE_FROM_INDEX,
603
                &&TARGET_SET_VAL_FROM,
604
                &&TARGET_SET_VAL_FROM_INDEX,
605
                &&TARGET_INCREMENT,
606
                &&TARGET_INCREMENT_BY_INDEX,
607
                &&TARGET_INCREMENT_STORE,
608
                &&TARGET_DECREMENT,
609
                &&TARGET_DECREMENT_BY_INDEX,
610
                &&TARGET_DECREMENT_STORE,
611
                &&TARGET_STORE_TAIL,
612
                &&TARGET_STORE_TAIL_BY_INDEX,
613
                &&TARGET_STORE_HEAD,
614
                &&TARGET_STORE_HEAD_BY_INDEX,
615
                &&TARGET_STORE_LIST,
616
                &&TARGET_SET_VAL_TAIL,
617
                &&TARGET_SET_VAL_TAIL_BY_INDEX,
618
                &&TARGET_SET_VAL_HEAD,
619
                &&TARGET_SET_VAL_HEAD_BY_INDEX,
620
                &&TARGET_CALL_BUILTIN,
621
                &&TARGET_CALL_BUILTIN_WITHOUT_RETURN_ADDRESS,
622
                &&TARGET_LT_CONST_JUMP_IF_FALSE,
623
                &&TARGET_LT_CONST_JUMP_IF_TRUE,
624
                &&TARGET_LT_SYM_JUMP_IF_FALSE,
625
                &&TARGET_GT_CONST_JUMP_IF_TRUE,
626
                &&TARGET_GT_CONST_JUMP_IF_FALSE,
627
                &&TARGET_GT_SYM_JUMP_IF_FALSE,
628
                &&TARGET_EQ_CONST_JUMP_IF_TRUE,
629
                &&TARGET_EQ_SYM_INDEX_JUMP_IF_TRUE,
630
                &&TARGET_NEQ_CONST_JUMP_IF_TRUE,
631
                &&TARGET_NEQ_SYM_JUMP_IF_FALSE,
632
                &&TARGET_CALL_SYMBOL,
633
                &&TARGET_CALL_CURRENT_PAGE,
634
                &&TARGET_GET_FIELD_FROM_SYMBOL,
635
                &&TARGET_GET_FIELD_FROM_SYMBOL_INDEX,
636
                &&TARGET_AT_SYM_SYM,
637
                &&TARGET_AT_SYM_INDEX_SYM_INDEX,
638
                &&TARGET_CHECK_TYPE_OF,
639
                &&TARGET_CHECK_TYPE_OF_BY_INDEX,
640
                &&TARGET_APPEND_IN_PLACE_SYM,
641
                &&TARGET_APPEND_IN_PLACE_SYM_INDEX
642
            };
643

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

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

656
            m_running = true;
196✔
657

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

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

678
                    TARGET(LOAD_SYMBOL_BY_INDEX)
679
                    {
680
                        push(loadSymbolFromIndex(arg, context), context);
333,618✔
681
                        DISPATCH();
333,618✔
682
                    }
115,917✔
683

684
                    TARGET(LOAD_CONST)
685
                    {
686
                        push(loadConstAsPtr(arg), context);
115,917✔
687
                        DISPATCH();
115,917✔
688
                    }
28,550✔
689

690
                    TARGET(POP_JUMP_IF_TRUE)
691
                    {
692
                        if (Value boolean = *popAndResolveAsPtr(context); !!boolean)
35,505✔
693
                            context.ip = arg * 4;  // instructions are 4 bytes
6,955✔
694
                        DISPATCH();
28,550✔
695
                    }
401,126✔
696

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

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

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

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

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

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

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

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

753
                        DISPATCH();
136,477✔
754
                    }
63✔
755

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

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

770
                    TARGET(CALL)
771
                    {
772
                        call(context, arg);
2,595✔
773
                        if (!m_running)
2,588✔
774
                            GOTO_HALT();
×
775
                        DISPATCH();
2,588✔
776
                    }
3,100✔
777

778
                    TARGET(CAPTURE)
779
                    {
780
                        if (!context.saved_scope)
3,100✔
781
                            context.saved_scope = ClosureScope();
619✔
782

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

794
                        DISPATCH();
3,100✔
795
                    }
12✔
796

797
                    TARGET(RENAME_NEXT_CAPTURE)
798
                    {
799
                        context.capture_rename_id = arg;
12✔
800
                        DISPATCH();
12✔
801
                    }
1,530✔
802

803
                    TARGET(BUILTIN)
804
                    {
805
                        push(Builtins::builtins[arg].second, context);
1,530✔
806
                        DISPATCH();
1,530✔
807
                    }
1✔
808

809
                    TARGET(DEL)
810
                    {
811
                        if (Value* var = findNearestVariable(arg, context); var != nullptr)
1✔
812
                        {
813
                            if (var->valueType() == ValueType::User)
×
814
                                var->usertypeRef().del();
×
815
                            *var = Value();
×
816
                            DISPATCH();
×
817
                        }
818

819
                        throwVMError(ErrorKind::Scope, fmt::format("Can not delete unbound variable `{}'", m_state.m_symbols[arg]));
1✔
820
                    }
619✔
821

822
                    TARGET(MAKE_CLOSURE)
823
                    {
824
                        push(Value(Closure(context.saved_scope.value(), m_state.m_constants[arg].pageAddr())), context);
619✔
825
                        context.saved_scope.reset();
619✔
826
                        DISPATCH();
619✔
827
                    }
6✔
828

829
                    TARGET(GET_FIELD)
830
                    {
831
                        Value* var = popAndResolveAsPtr(context);
6✔
832
                        push(getField(var, arg, context), context);
6✔
833
                        DISPATCH();
6✔
834
                    }
×
835

836
                    TARGET(PLUGIN)
837
                    {
838
                        loadPlugin(arg, context);
×
839
                        DISPATCH();
×
840
                    }
632✔
841

842
                    TARGET(LIST)
843
                    {
844
                        {
845
                            Value l = createList(arg, context);
632✔
846
                            push(std::move(l), context);
632✔
847
                        }
632✔
848
                        DISPATCH();
632✔
849
                    }
1,552✔
850

851
                    TARGET(APPEND)
852
                    {
853
                        {
854
                            Value* list = popAndResolveAsPtr(context);
1,552✔
855
                            if (list->valueType() != ValueType::List)
1,552✔
856
                            {
857
                                std::vector<Value> args = { *list };
1✔
858
                                for (uint16_t i = 0; i < arg; ++i)
2✔
859
                                    args.push_back(*popAndResolveAsPtr(context));
1✔
860
                                throw types::TypeCheckingError(
2✔
861
                                    "append",
1✔
862
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* variadic= */ true) } } } },
1✔
863
                                    args);
864
                            }
1✔
865

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

868
                            Value obj { *list };
1,551✔
869
                            obj.list().reserve(size + arg);
1,551✔
870

871
                            for (uint16_t i = 0; i < arg; ++i)
3,102✔
872
                                obj.push_back(*popAndResolveAsPtr(context));
1,551✔
873
                            push(std::move(obj), context);
1,551✔
874
                        }
1,551✔
875
                        DISPATCH();
1,551✔
876
                    }
12✔
877

878
                    TARGET(CONCAT)
879
                    {
880
                        {
881
                            Value* list = popAndResolveAsPtr(context);
12✔
882
                            Value obj { *list };
12✔
883

884
                            for (uint16_t i = 0; i < arg; ++i)
24✔
885
                            {
886
                                Value* next = popAndResolveAsPtr(context);
14✔
887

888
                                if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
14✔
889
                                    throw types::TypeCheckingError(
4✔
890
                                        "concat",
2✔
891
                                        { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
892
                                        { *list, *next });
2✔
893

894
                                std::ranges::copy(next->list(), std::back_inserter(obj.list()));
12✔
895
                            }
12✔
896
                            push(std::move(obj), context);
10✔
897
                        }
12✔
898
                        DISPATCH();
10✔
899
                    }
×
900

901
                    TARGET(APPEND_IN_PLACE)
902
                    {
903
                        Value* list = popAndResolveAsPtr(context);
×
904
                        listAppendInPlace(list, arg, context);
×
905
                        DISPATCH();
×
906
                    }
562✔
907

908
                    TARGET(CONCAT_IN_PLACE)
909
                    {
910
                        Value* list = popAndResolveAsPtr(context);
562✔
911

912
                        for (uint16_t i = 0; i < arg; ++i)
1,152✔
913
                        {
914
                            Value* next = popAndResolveAsPtr(context);
592✔
915

916
                            if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
592✔
917
                                throw types::TypeCheckingError(
4✔
918
                                    "concat!",
2✔
919
                                    { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
920
                                    { *list, *next });
2✔
921

922
                            std::ranges::copy(next->list(), std::back_inserter(list->list()));
590✔
923
                        }
590✔
924
                        DISPATCH();
560✔
925
                    }
6✔
926

927
                    TARGET(POP_LIST)
928
                    {
929
                        {
930
                            Value list = *popAndResolveAsPtr(context);
6✔
931
                            Value number = *popAndResolveAsPtr(context);
6✔
932

933
                            if (list.valueType() != ValueType::List || number.valueType() != ValueType::Number)
6✔
934
                                throw types::TypeCheckingError(
2✔
935
                                    "pop",
1✔
936
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
937
                                    { list, number });
1✔
938

939
                            long idx = static_cast<long>(number.number());
5✔
940
                            idx = idx < 0 ? static_cast<long>(list.list().size()) + idx : idx;
5✔
941
                            if (std::cmp_greater_equal(idx, list.list().size()) || idx < 0)
5✔
942
                                throwVMError(
2✔
943
                                    ErrorKind::Index,
944
                                    fmt::format("pop index ({}) out of range (list size: {})", idx, list.list().size()));
2✔
945

946
                            list.list().erase(list.list().begin() + idx);
3✔
947
                            push(list, context);
3✔
948
                        }
6✔
949
                        DISPATCH();
3✔
950
                    }
83✔
951

952
                    TARGET(POP_LIST_IN_PLACE)
953
                    {
954
                        {
955
                            Value* list = popAndResolveAsPtr(context);
83✔
956
                            Value number = *popAndResolveAsPtr(context);
83✔
957

958
                            if (list->valueType() != ValueType::List || number.valueType() != ValueType::Number)
83✔
959
                                throw types::TypeCheckingError(
2✔
960
                                    "pop!",
1✔
961
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
962
                                    { *list, number });
1✔
963

964
                            long idx = static_cast<long>(number.number());
82✔
965
                            idx = idx < 0 ? static_cast<long>(list->list().size()) + idx : idx;
82✔
966
                            if (std::cmp_greater_equal(idx, list->list().size()) || idx < 0)
82✔
967
                                throwVMError(
2✔
968
                                    ErrorKind::Index,
969
                                    fmt::format("pop! index ({}) out of range (list size: {})", idx, list->list().size()));
2✔
970

971
                            list->list().erase(list->list().begin() + idx);
80✔
972
                        }
83✔
973
                        DISPATCH();
80✔
974
                    }
490✔
975

976
                    TARGET(SET_AT_INDEX)
977
                    {
978
                        {
979
                            Value* list = popAndResolveAsPtr(context);
490✔
980
                            Value number = *popAndResolveAsPtr(context);
490✔
981
                            Value new_value = *popAndResolveAsPtr(context);
490✔
982

983
                            if (!list->isIndexable() || number.valueType() != ValueType::Number || (list->valueType() == ValueType::String && new_value.valueType() != ValueType::String))
490✔
984
                                throw types::TypeCheckingError(
2✔
985
                                    "@=",
1✔
986
                                    { { types::Contract {
3✔
987
                                          { types::Typedef("list", ValueType::List),
3✔
988
                                            types::Typedef("index", ValueType::Number),
1✔
989
                                            types::Typedef("new_value", ValueType::Any) } } },
1✔
990
                                      { types::Contract {
1✔
991
                                          { types::Typedef("string", ValueType::String),
3✔
992
                                            types::Typedef("index", ValueType::Number),
1✔
993
                                            types::Typedef("char", ValueType::String) } } } },
1✔
994
                                    { *list, number, new_value });
1✔
995

996
                            const std::size_t size = list->valueType() == ValueType::List ? list->list().size() : list->stringRef().size();
489✔
997
                            long idx = static_cast<long>(number.number());
489✔
998
                            idx = idx < 0 ? static_cast<long>(size) + idx : idx;
489✔
999
                            if (std::cmp_greater_equal(idx, size) || idx < 0)
489✔
1000
                                throwVMError(
2✔
1001
                                    ErrorKind::Index,
1002
                                    fmt::format("@= index ({}) out of range (indexable size: {})", idx, size));
2✔
1003

1004
                            if (list->valueType() == ValueType::List)
487✔
1005
                                list->list()[static_cast<std::size_t>(idx)] = new_value;
485✔
1006
                            else
1007
                                list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
2✔
1008
                        }
490✔
1009
                        DISPATCH();
487✔
1010
                    }
12✔
1011

1012
                    TARGET(SET_AT_2_INDEX)
1013
                    {
1014
                        {
1015
                            Value* list = popAndResolveAsPtr(context);
12✔
1016
                            Value x = *popAndResolveAsPtr(context);
12✔
1017
                            Value y = *popAndResolveAsPtr(context);
12✔
1018
                            Value new_value = *popAndResolveAsPtr(context);
12✔
1019

1020
                            if (list->valueType() != ValueType::List || x.valueType() != ValueType::Number || y.valueType() != ValueType::Number)
12✔
1021
                                throw types::TypeCheckingError(
2✔
1022
                                    "@@=",
1✔
1023
                                    { { types::Contract {
2✔
1024
                                        { types::Typedef("list", ValueType::List),
4✔
1025
                                          types::Typedef("x", ValueType::Number),
1✔
1026
                                          types::Typedef("y", ValueType::Number),
1✔
1027
                                          types::Typedef("new_value", ValueType::Any) } } } },
1✔
1028
                                    { *list, x, y, new_value });
1✔
1029

1030
                            long idx_y = static_cast<long>(x.number());
11✔
1031
                            idx_y = idx_y < 0 ? static_cast<long>(list->list().size()) + idx_y : idx_y;
11✔
1032
                            if (std::cmp_greater_equal(idx_y, list->list().size()) || idx_y < 0)
11✔
1033
                                throwVMError(
2✔
1034
                                    ErrorKind::Index,
1035
                                    fmt::format("@@= index (y: {}) out of range (list size: {})", idx_y, list->list().size()));
2✔
1036

1037
                            if (!list->list()[static_cast<std::size_t>(idx_y)].isIndexable() ||
13✔
1038
                                (list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::String && new_value.valueType() != ValueType::String))
8✔
1039
                                throw types::TypeCheckingError(
2✔
1040
                                    "@@=",
1✔
1041
                                    { { types::Contract {
3✔
1042
                                          { types::Typedef("list", ValueType::List),
4✔
1043
                                            types::Typedef("x", ValueType::Number),
1✔
1044
                                            types::Typedef("y", ValueType::Number),
1✔
1045
                                            types::Typedef("new_value", ValueType::Any) } } },
1✔
1046
                                      { types::Contract {
1✔
1047
                                          { types::Typedef("string", ValueType::String),
4✔
1048
                                            types::Typedef("x", ValueType::Number),
1✔
1049
                                            types::Typedef("y", ValueType::Number),
1✔
1050
                                            types::Typedef("char", ValueType::String) } } } },
1✔
1051
                                    { *list, x, y, new_value });
1✔
1052

1053
                            const bool is_list = list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
8✔
1054
                            const std::size_t size =
8✔
1055
                                is_list
16✔
1056
                                ? list->list()[static_cast<std::size_t>(idx_y)].list().size()
6✔
1057
                                : list->list()[static_cast<std::size_t>(idx_y)].stringRef().size();
2✔
1058

1059
                            long idx_x = static_cast<long>(y.number());
8✔
1060
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
8✔
1061
                            if (std::cmp_greater_equal(idx_x, size) || idx_x < 0)
8✔
1062
                                throwVMError(
2✔
1063
                                    ErrorKind::Index,
1064
                                    fmt::format("@@= index (x: {}) out of range (inner indexable size: {})", idx_x, size));
2✔
1065

1066
                            if (is_list)
6✔
1067
                                list->list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)] = new_value;
4✔
1068
                            else
1069
                                list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
2✔
1070
                        }
12✔
1071
                        DISPATCH();
6✔
1072
                    }
2,702✔
1073

1074
                    TARGET(POP)
1075
                    {
1076
                        pop(context);
2,702✔
1077
                        DISPATCH();
2,702✔
1078
                    }
23,415✔
1079

1080
                    TARGET(SHORTCIRCUIT_AND)
1081
                    {
1082
                        if (!*peekAndResolveAsPtr(context))
23,415✔
1083
                            context.ip = arg * 4;
733✔
1084
                        else
1085
                            pop(context);
22,682✔
1086
                        DISPATCH();
23,415✔
1087
                    }
811✔
1088

1089
                    TARGET(SHORTCIRCUIT_OR)
1090
                    {
1091
                        if (!!*peekAndResolveAsPtr(context))
811✔
1092
                            context.ip = arg * 4;
216✔
1093
                        else
1094
                            pop(context);
595✔
1095
                        DISPATCH();
811✔
1096
                    }
2,752✔
1097

1098
                    TARGET(CREATE_SCOPE)
1099
                    {
1100
                        context.locals.emplace_back(context.scopes_storage.data(), context.locals.back().storageEnd());
2,752✔
1101
                        DISPATCH();
2,752✔
1102
                    }
31,125✔
1103

1104
                    TARGET(RESET_SCOPE_JUMP)
1105
                    {
1106
                        context.locals.back().reset();
31,125✔
1107
                        context.ip = arg * 4;  // instructions are 4 bytes
31,125✔
1108
                        DISPATCH();
31,125✔
1109
                    }
2,752✔
1110

1111
                    TARGET(POP_SCOPE)
1112
                    {
1113
                        context.locals.pop_back();
2,752✔
1114
                        DISPATCH();
2,752✔
1115
                    }
×
1116

1117
                    TARGET(GET_CURRENT_PAGE_ADDR)
1118
                    {
1119
                        context.last_symbol = arg;
×
1120
                        push(Value(static_cast<PageAddr_t>(context.pp)), context);
×
1121
                        DISPATCH();
×
1122
                    }
26,904✔
1123

1124
#pragma endregion
1125

1126
#pragma region "Operators"
1127

1128
                    TARGET(ADD)
1129
                    {
1130
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
26,904✔
1131

1132
                        if (a->valueType() == ValueType::Number && b->valueType() == ValueType::Number)
26,904✔
1133
                            push(Value(a->number() + b->number()), context);
19,459✔
1134
                        else if (a->valueType() == ValueType::String && b->valueType() == ValueType::String)
7,445✔
1135
                            push(Value(a->string() + b->string()), context);
7,444✔
1136
                        else
1137
                            throw types::TypeCheckingError(
2✔
1138
                                "+",
1✔
1139
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } },
2✔
1140
                                    types::Contract { { types::Typedef("a", ValueType::String), types::Typedef("b", ValueType::String) } } } },
1✔
1141
                                { *a, *b });
1✔
1142
                        DISPATCH();
26,903✔
1143
                    }
328✔
1144

1145
                    TARGET(SUB)
1146
                    {
1147
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
328✔
1148

1149
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
328✔
1150
                            throw types::TypeCheckingError(
2✔
1151
                                "-",
1✔
1152
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1153
                                { *a, *b });
1✔
1154
                        push(Value(a->number() - b->number()), context);
327✔
1155
                        DISPATCH();
327✔
1156
                    }
2,213✔
1157

1158
                    TARGET(MUL)
1159
                    {
1160
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
2,213✔
1161

1162
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
2,213✔
1163
                            throw types::TypeCheckingError(
2✔
1164
                                "*",
1✔
1165
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1166
                                { *a, *b });
1✔
1167
                        push(Value(a->number() * b->number()), context);
2,212✔
1168
                        DISPATCH();
2,212✔
1169
                    }
1,062✔
1170

1171
                    TARGET(DIV)
1172
                    {
1173
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,062✔
1174

1175
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
1,062✔
1176
                            throw types::TypeCheckingError(
2✔
1177
                                "/",
1✔
1178
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1179
                                { *a, *b });
1✔
1180
                        auto d = b->number();
1,061✔
1181
                        if (d == 0)
1,061✔
1182
                            throwVMError(ErrorKind::DivisionByZero, fmt::format("Can not compute expression (/ {} {})", a->toString(*this), b->toString(*this)));
1✔
1183

1184
                        push(Value(a->number() / d), context);
1,060✔
1185
                        DISPATCH();
1,060✔
1186
                    }
160✔
1187

1188
                    TARGET(GT)
1189
                    {
1190
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
160✔
1191
                        push(*b < *a ? Builtins::trueSym : Builtins::falseSym, context);
160✔
1192
                        DISPATCH();
160✔
1193
                    }
28,820✔
1194

1195
                    TARGET(LT)
1196
                    {
1197
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
28,820✔
1198
                        push(*a < *b ? Builtins::trueSym : Builtins::falseSym, context);
28,820✔
1199
                        DISPATCH();
28,820✔
1200
                    }
7,192✔
1201

1202
                    TARGET(LE)
1203
                    {
1204
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
7,192✔
1205
                        push((((*a < *b) || (*a == *b)) ? Builtins::trueSym : Builtins::falseSym), context);
7,192✔
1206
                        DISPATCH();
7,192✔
1207
                    }
5,730✔
1208

1209
                    TARGET(GE)
1210
                    {
1211
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
5,730✔
1212
                        push(!(*a < *b) ? Builtins::trueSym : Builtins::falseSym, context);
5,730✔
1213
                        DISPATCH();
5,730✔
1214
                    }
748✔
1215

1216
                    TARGET(NEQ)
1217
                    {
1218
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
748✔
1219
                        push(*a != *b ? Builtins::trueSym : Builtins::falseSym, context);
748✔
1220
                        DISPATCH();
748✔
1221
                    }
17,449✔
1222

1223
                    TARGET(EQ)
1224
                    {
1225
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
17,449✔
1226
                        push(*a == *b ? Builtins::trueSym : Builtins::falseSym, context);
17,449✔
1227
                        DISPATCH();
17,449✔
1228
                    }
12,199✔
1229

1230
                    TARGET(LEN)
1231
                    {
1232
                        const Value* a = popAndResolveAsPtr(context);
12,199✔
1233

1234
                        if (a->valueType() == ValueType::List)
12,199✔
1235
                            push(Value(static_cast<int>(a->constList().size())), context);
4,573✔
1236
                        else if (a->valueType() == ValueType::String)
7,626✔
1237
                            push(Value(static_cast<int>(a->string().size())), context);
7,625✔
1238
                        else
1239
                            throw types::TypeCheckingError(
2✔
1240
                                "len",
1✔
1241
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1242
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1243
                                { *a });
1✔
1244
                        DISPATCH();
12,198✔
1245
                    }
540✔
1246

1247
                    TARGET(EMPTY)
1248
                    {
1249
                        const Value* a = popAndResolveAsPtr(context);
540✔
1250

1251
                        if (a->valueType() == ValueType::List)
540✔
1252
                            push(a->constList().empty() ? Builtins::trueSym : Builtins::falseSym, context);
86✔
1253
                        else if (a->valueType() == ValueType::String)
454✔
1254
                            push(a->string().empty() ? Builtins::trueSym : Builtins::falseSym, context);
453✔
1255
                        else
1256
                            throw types::TypeCheckingError(
2✔
1257
                                "empty?",
1✔
1258
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1259
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1260
                                { *a });
1✔
1261
                        DISPATCH();
539✔
1262
                    }
335✔
1263

1264
                    TARGET(TAIL)
1265
                    {
1266
                        Value* const a = popAndResolveAsPtr(context);
335✔
1267
                        push(helper::tail(a), context);
336✔
1268
                        DISPATCH();
334✔
1269
                    }
1,099✔
1270

1271
                    TARGET(HEAD)
1272
                    {
1273
                        Value* const a = popAndResolveAsPtr(context);
1,099✔
1274
                        push(helper::head(a), context);
1,100✔
1275
                        DISPATCH();
1,098✔
1276
                    }
2,322✔
1277

1278
                    TARGET(ISNIL)
1279
                    {
1280
                        const Value* a = popAndResolveAsPtr(context);
2,322✔
1281
                        push((*a == Builtins::nil) ? Builtins::trueSym : Builtins::falseSym, context);
2,322✔
1282
                        DISPATCH();
2,322✔
1283
                    }
596✔
1284

1285
                    TARGET(ASSERT)
1286
                    {
1287
                        Value* const b = popAndResolveAsPtr(context);
596✔
1288
                        Value* const a = popAndResolveAsPtr(context);
596✔
1289

1290
                        if (b->valueType() != ValueType::String)
596✔
1291
                            throw types::TypeCheckingError(
2✔
1292
                                "assert",
1✔
1293
                                { { types::Contract { { types::Typedef("expr", ValueType::Any), types::Typedef("message", ValueType::String) } } } },
1✔
1294
                                { *a, *b });
1✔
1295

1296
                        if (*a == Builtins::falseSym)
595✔
1297
                            throw AssertionFailed(b->stringRef());
×
1298
                        DISPATCH();
595✔
1299
                    }
15✔
1300

1301
                    TARGET(TO_NUM)
1302
                    {
1303
                        const Value* a = popAndResolveAsPtr(context);
15✔
1304

1305
                        if (a->valueType() != ValueType::String)
15✔
1306
                            throw types::TypeCheckingError(
2✔
1307
                                "toNumber",
1✔
1308
                                { { types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1309
                                { *a });
1✔
1310

1311
                        double val;
1312
                        if (Utils::isDouble(a->string(), &val))
14✔
1313
                            push(Value(val), context);
11✔
1314
                        else
1315
                            push(Builtins::nil, context);
3✔
1316
                        DISPATCH();
14✔
1317
                    }
120✔
1318

1319
                    TARGET(TO_STR)
1320
                    {
1321
                        const Value* a = popAndResolveAsPtr(context);
120✔
1322
                        push(Value(a->toString(*this)), context);
120✔
1323
                        DISPATCH();
120✔
1324
                    }
1,044✔
1325

1326
                    TARGET(AT)
1327
                    {
1328
                        Value& b = *popAndResolveAsPtr(context);
1,044✔
1329
                        Value& a = *popAndResolveAsPtr(context);
1,044✔
1330
                        push(helper::at(a, b, *this), context);
1,048✔
1331
                        DISPATCH();
1,040✔
1332
                    }
26✔
1333

1334
                    TARGET(AT_AT)
1335
                    {
1336
                        {
1337
                            const Value* x = popAndResolveAsPtr(context);
26✔
1338
                            const Value* y = popAndResolveAsPtr(context);
26✔
1339
                            Value& list = *popAndResolveAsPtr(context);
26✔
1340

1341
                            if (y->valueType() != ValueType::Number || x->valueType() != ValueType::Number ||
26✔
1342
                                list.valueType() != ValueType::List)
25✔
1343
                                throw types::TypeCheckingError(
2✔
1344
                                    "@@",
1✔
1345
                                    { { types::Contract {
2✔
1346
                                        { types::Typedef("src", ValueType::List),
3✔
1347
                                          types::Typedef("y", ValueType::Number),
1✔
1348
                                          types::Typedef("x", ValueType::Number) } } } },
1✔
1349
                                    { list, *y, *x });
1✔
1350

1351
                            long idx_y = static_cast<long>(y->number());
25✔
1352
                            idx_y = idx_y < 0 ? static_cast<long>(list.list().size()) + idx_y : idx_y;
25✔
1353
                            if (std::cmp_greater_equal(idx_y, list.list().size()) || idx_y < 0)
25✔
1354
                                throwVMError(
2✔
1355
                                    ErrorKind::Index,
1356
                                    fmt::format("@@ index ({}) out of range (list size: {})", idx_y, list.list().size()));
2✔
1357

1358
                            const bool is_list = list.list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
23✔
1359
                            const std::size_t size =
23✔
1360
                                is_list
46✔
1361
                                ? list.list()[static_cast<std::size_t>(idx_y)].list().size()
16✔
1362
                                : list.list()[static_cast<std::size_t>(idx_y)].stringRef().size();
7✔
1363

1364
                            long idx_x = static_cast<long>(x->number());
23✔
1365
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
23✔
1366
                            if (std::cmp_greater_equal(idx_x, size) || idx_x < 0)
23✔
1367
                                throwVMError(
2✔
1368
                                    ErrorKind::Index,
1369
                                    fmt::format("@@ index (x: {}) out of range (inner indexable size: {})", idx_x, size));
2✔
1370

1371
                            if (is_list)
21✔
1372
                                push(list.list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)], context);
14✔
1373
                            else
1374
                                push(Value(std::string(1, list.list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)])), context);
7✔
1375
                        }
1376
                        DISPATCH();
21✔
1377
                    }
16,354✔
1378

1379
                    TARGET(MOD)
1380
                    {
1381
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
16,354✔
1382
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
16,354✔
1383
                            throw types::TypeCheckingError(
2✔
1384
                                "mod",
1✔
1385
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1386
                                { *a, *b });
1✔
1387
                        push(Value(std::fmod(a->number(), b->number())), context);
16,353✔
1388
                        DISPATCH();
16,353✔
1389
                    }
16✔
1390

1391
                    TARGET(TYPE)
1392
                    {
1393
                        const Value* a = popAndResolveAsPtr(context);
16✔
1394
                        push(Value(std::to_string(a->valueType())), context);
16✔
1395
                        DISPATCH();
16✔
1396
                    }
3✔
1397

1398
                    TARGET(HASFIELD)
1399
                    {
1400
                        {
1401
                            Value* const field = popAndResolveAsPtr(context);
3✔
1402
                            Value* const closure = popAndResolveAsPtr(context);
3✔
1403
                            if (closure->valueType() != ValueType::Closure || field->valueType() != ValueType::String)
3✔
1404
                                throw types::TypeCheckingError(
2✔
1405
                                    "hasField",
1✔
1406
                                    { { types::Contract { { types::Typedef("closure", ValueType::Closure), types::Typedef("field", ValueType::String) } } } },
1✔
1407
                                    { *closure, *field });
1✔
1408

1409
                            auto it = std::ranges::find(m_state.m_symbols, field->stringRef());
2✔
1410
                            if (it == m_state.m_symbols.end())
2✔
1411
                            {
1412
                                push(Builtins::falseSym, context);
1✔
1413
                                DISPATCH();
1✔
1414
                            }
1415

1416
                            auto id = static_cast<std::uint16_t>(std::distance(m_state.m_symbols.begin(), it));
1✔
1417
                            push(closure->refClosure().refScope()[id] != nullptr ? Builtins::trueSym : Builtins::falseSym, context);
1✔
1418
                        }
1419
                        DISPATCH();
1✔
1420
                    }
3,568✔
1421

1422
                    TARGET(NOT)
1423
                    {
1424
                        const Value* a = popAndResolveAsPtr(context);
3,568✔
1425
                        push(!(*a) ? Builtins::trueSym : Builtins::falseSym, context);
3,568✔
1426
                        DISPATCH();
3,568✔
1427
                    }
5,995✔
1428

1429
#pragma endregion
1430

1431
#pragma region "Super Instructions"
1432
                    TARGET(LOAD_CONST_LOAD_CONST)
1433
                    {
1434
                        UNPACK_ARGS();
5,995✔
1435
                        push(loadConstAsPtr(primary_arg), context);
5,995✔
1436
                        push(loadConstAsPtr(secondary_arg), context);
5,995✔
1437
                        DISPATCH();
5,995✔
1438
                    }
8,007✔
1439

1440
                    TARGET(LOAD_CONST_STORE)
1441
                    {
1442
                        UNPACK_ARGS();
8,007✔
1443
                        store(secondary_arg, loadConstAsPtr(primary_arg), context);
8,007✔
1444
                        DISPATCH();
8,007✔
1445
                    }
240✔
1446

1447
                    TARGET(LOAD_CONST_SET_VAL)
1448
                    {
1449
                        UNPACK_ARGS();
240✔
1450
                        setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
240✔
1451
                        DISPATCH();
239✔
1452
                    }
20✔
1453

1454
                    TARGET(STORE_FROM)
1455
                    {
1456
                        UNPACK_ARGS();
20✔
1457
                        store(secondary_arg, loadSymbol(primary_arg, context), context);
20✔
1458
                        DISPATCH();
19✔
1459
                    }
1,096✔
1460

1461
                    TARGET(STORE_FROM_INDEX)
1462
                    {
1463
                        UNPACK_ARGS();
1,096✔
1464
                        store(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
1,096✔
1465
                        DISPATCH();
1,096✔
1466
                    }
547✔
1467

1468
                    TARGET(SET_VAL_FROM)
1469
                    {
1470
                        UNPACK_ARGS();
547✔
1471
                        setVal(secondary_arg, loadSymbol(primary_arg, context), context);
547✔
1472
                        DISPATCH();
547✔
1473
                    }
221✔
1474

1475
                    TARGET(SET_VAL_FROM_INDEX)
1476
                    {
1477
                        UNPACK_ARGS();
221✔
1478
                        setVal(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
221✔
1479
                        DISPATCH();
221✔
1480
                    }
16✔
1481

1482
                    TARGET(INCREMENT)
1483
                    {
1484
                        UNPACK_ARGS();
16✔
1485
                        {
1486
                            Value* var = loadSymbol(primary_arg, context);
16✔
1487

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

1492
                            if (var->valueType() == ValueType::Number)
16✔
1493
                                push(Value(var->number() + secondary_arg), context);
16✔
1494
                            else
1495
                                throw types::TypeCheckingError(
×
1496
                                    "+",
×
1497
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1498
                                    { *var, Value(secondary_arg) });
×
1499
                        }
1500
                        DISPATCH();
16✔
1501
                    }
87,973✔
1502

1503
                    TARGET(INCREMENT_BY_INDEX)
1504
                    {
1505
                        UNPACK_ARGS();
87,973✔
1506
                        {
1507
                            Value* var = loadSymbolFromIndex(primary_arg, context);
87,973✔
1508

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

1513
                            if (var->valueType() == ValueType::Number)
87,973✔
1514
                                push(Value(var->number() + secondary_arg), context);
87,972✔
1515
                            else
1516
                                throw types::TypeCheckingError(
2✔
1517
                                    "+",
1✔
1518
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1519
                                    { *var, Value(secondary_arg) });
1✔
1520
                        }
1521
                        DISPATCH();
87,972✔
1522
                    }
31,112✔
1523

1524
                    TARGET(INCREMENT_STORE)
1525
                    {
1526
                        UNPACK_ARGS();
31,112✔
1527
                        {
1528
                            Value* var = loadSymbol(primary_arg, context);
31,112✔
1529

1530
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1531
                            if (var->valueType() == ValueType::Reference)
31,112✔
1532
                                var = var->reference();
×
1533

1534
                            if (var->valueType() == ValueType::Number)
31,112✔
1535
                            {
1536
                                Value val = Value(var->number() + secondary_arg);
31,112✔
1537
                                setVal(primary_arg, &val, context);
31,112✔
1538
                            }
31,112✔
1539
                            else
1540
                                throw types::TypeCheckingError(
×
1541
                                    "+",
×
1542
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1543
                                    { *var, Value(secondary_arg) });
×
1544
                        }
1545
                        DISPATCH();
31,112✔
1546
                    }
1,776✔
1547

1548
                    TARGET(DECREMENT)
1549
                    {
1550
                        UNPACK_ARGS();
1,776✔
1551
                        {
1552
                            Value* var = loadSymbol(primary_arg, context);
1,776✔
1553

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

1558
                            if (var->valueType() == ValueType::Number)
1,776✔
1559
                                push(Value(var->number() - secondary_arg), context);
1,776✔
1560
                            else
1561
                                throw types::TypeCheckingError(
×
1562
                                    "-",
×
1563
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1564
                                    { *var, Value(secondary_arg) });
×
1565
                        }
1566
                        DISPATCH();
1,776✔
1567
                    }
194,408✔
1568

1569
                    TARGET(DECREMENT_BY_INDEX)
1570
                    {
1571
                        UNPACK_ARGS();
194,408✔
1572
                        {
1573
                            Value* var = loadSymbolFromIndex(primary_arg, context);
194,408✔
1574

1575
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1576
                            if (var->valueType() == ValueType::Reference)
194,408✔
1577
                                var = var->reference();
×
1578

1579
                            if (var->valueType() == ValueType::Number)
194,408✔
1580
                                push(Value(var->number() - secondary_arg), context);
194,407✔
1581
                            else
1582
                                throw types::TypeCheckingError(
2✔
1583
                                    "-",
1✔
1584
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1585
                                    { *var, Value(secondary_arg) });
1✔
1586
                        }
1587
                        DISPATCH();
194,407✔
1588
                    }
×
1589

1590
                    TARGET(DECREMENT_STORE)
1591
                    {
1592
                        UNPACK_ARGS();
×
1593
                        {
1594
                            Value* var = loadSymbol(primary_arg, context);
×
1595

1596
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1597
                            if (var->valueType() == ValueType::Reference)
×
1598
                                var = var->reference();
×
1599

1600
                            if (var->valueType() == ValueType::Number)
×
1601
                            {
1602
                                Value val = Value(var->number() - secondary_arg);
×
1603
                                setVal(primary_arg, &val, context);
×
1604
                            }
×
1605
                            else
1606
                                throw types::TypeCheckingError(
×
1607
                                    "-",
×
1608
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1609
                                    { *var, Value(secondary_arg) });
×
1610
                        }
1611
                        DISPATCH();
×
1612
                    }
×
1613

1614
                    TARGET(STORE_TAIL)
1615
                    {
1616
                        UNPACK_ARGS();
×
1617
                        {
1618
                            Value* list = loadSymbol(primary_arg, context);
×
1619
                            Value tail = helper::tail(list);
×
1620
                            store(secondary_arg, &tail, context);
×
1621
                        }
×
1622
                        DISPATCH();
×
1623
                    }
1✔
1624

1625
                    TARGET(STORE_TAIL_BY_INDEX)
1626
                    {
1627
                        UNPACK_ARGS();
1✔
1628
                        {
1629
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1630
                            Value tail = helper::tail(list);
1✔
1631
                            store(secondary_arg, &tail, context);
1✔
1632
                        }
1✔
1633
                        DISPATCH();
1✔
1634
                    }
×
1635

1636
                    TARGET(STORE_HEAD)
1637
                    {
1638
                        UNPACK_ARGS();
×
1639
                        {
1640
                            Value* list = loadSymbol(primary_arg, context);
×
1641
                            Value head = helper::head(list);
×
1642
                            store(secondary_arg, &head, context);
×
1643
                        }
×
1644
                        DISPATCH();
×
1645
                    }
31✔
1646

1647
                    TARGET(STORE_HEAD_BY_INDEX)
1648
                    {
1649
                        UNPACK_ARGS();
31✔
1650
                        {
1651
                            Value* list = loadSymbolFromIndex(primary_arg, context);
31✔
1652
                            Value head = helper::head(list);
31✔
1653
                            store(secondary_arg, &head, context);
31✔
1654
                        }
31✔
1655
                        DISPATCH();
31✔
1656
                    }
903✔
1657

1658
                    TARGET(STORE_LIST)
1659
                    {
1660
                        UNPACK_ARGS();
903✔
1661
                        {
1662
                            Value l = createList(primary_arg, context);
903✔
1663
                            store(secondary_arg, &l, context);
903✔
1664
                        }
903✔
1665
                        DISPATCH();
903✔
1666
                    }
×
1667

1668
                    TARGET(SET_VAL_TAIL)
1669
                    {
1670
                        UNPACK_ARGS();
×
1671
                        {
1672
                            Value* list = loadSymbol(primary_arg, context);
×
1673
                            Value tail = helper::tail(list);
×
1674
                            setVal(secondary_arg, &tail, context);
×
1675
                        }
×
1676
                        DISPATCH();
×
1677
                    }
1✔
1678

1679
                    TARGET(SET_VAL_TAIL_BY_INDEX)
1680
                    {
1681
                        UNPACK_ARGS();
1✔
1682
                        {
1683
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1684
                            Value tail = helper::tail(list);
1✔
1685
                            setVal(secondary_arg, &tail, context);
1✔
1686
                        }
1✔
1687
                        DISPATCH();
1✔
1688
                    }
×
1689

1690
                    TARGET(SET_VAL_HEAD)
1691
                    {
1692
                        UNPACK_ARGS();
×
1693
                        {
1694
                            Value* list = loadSymbol(primary_arg, context);
×
1695
                            Value head = helper::head(list);
×
1696
                            setVal(secondary_arg, &head, context);
×
1697
                        }
×
1698
                        DISPATCH();
×
1699
                    }
1✔
1700

1701
                    TARGET(SET_VAL_HEAD_BY_INDEX)
1702
                    {
1703
                        UNPACK_ARGS();
1✔
1704
                        {
1705
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1706
                            Value head = helper::head(list);
1✔
1707
                            setVal(secondary_arg, &head, context);
1✔
1708
                        }
1✔
1709
                        DISPATCH();
1✔
1710
                    }
807✔
1711

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

1722
                    TARGET(CALL_BUILTIN_WITHOUT_RETURN_ADDRESS)
1723
                    {
1724
                        UNPACK_ARGS();
11,203✔
1725
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1726
                        callBuiltin(context, Builtins::builtins[primary_arg].second, secondary_arg, /* remove_return_address= */ false);
11,203✔
1727
                        if (!m_running)
11,202✔
1728
                            GOTO_HALT();
×
1729
                        DISPATCH();
11,202✔
1730
                    }
857✔
1731

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

1741
                    TARGET(LT_CONST_JUMP_IF_TRUE)
1742
                    {
1743
                        UNPACK_ARGS();
21,902✔
1744
                        const Value* sym = popAndResolveAsPtr(context);
21,902✔
1745
                        if (*sym < *loadConstAsPtr(primary_arg))
21,902✔
1746
                            context.ip = secondary_arg * 4;
10,950✔
1747
                        DISPATCH();
21,902✔
1748
                    }
5,543✔
1749

1750
                    TARGET(LT_SYM_JUMP_IF_FALSE)
1751
                    {
1752
                        UNPACK_ARGS();
5,543✔
1753
                        const Value* sym = popAndResolveAsPtr(context);
5,543✔
1754
                        if (!(*sym < *loadSymbol(primary_arg, context)))
5,543✔
1755
                            context.ip = secondary_arg * 4;
536✔
1756
                        DISPATCH();
5,543✔
1757
                    }
172,500✔
1758

1759
                    TARGET(GT_CONST_JUMP_IF_TRUE)
1760
                    {
1761
                        UNPACK_ARGS();
172,500✔
1762
                        const Value* sym = popAndResolveAsPtr(context);
172,500✔
1763
                        const Value* cst = loadConstAsPtr(primary_arg);
172,500✔
1764
                        if (*cst < *sym)
172,500✔
1765
                            context.ip = secondary_arg * 4;
86,586✔
1766
                        DISPATCH();
172,500✔
1767
                    }
15✔
1768

1769
                    TARGET(GT_CONST_JUMP_IF_FALSE)
1770
                    {
1771
                        UNPACK_ARGS();
15✔
1772
                        const Value* sym = popAndResolveAsPtr(context);
15✔
1773
                        const Value* cst = loadConstAsPtr(primary_arg);
15✔
1774
                        if (!(*cst < *sym))
15✔
1775
                            context.ip = secondary_arg * 4;
3✔
1776
                        DISPATCH();
15✔
1777
                    }
×
1778

1779
                    TARGET(GT_SYM_JUMP_IF_FALSE)
1780
                    {
1781
                        UNPACK_ARGS();
×
1782
                        const Value* sym = popAndResolveAsPtr(context);
×
1783
                        const Value* rhs = loadSymbol(primary_arg, context);
×
1784
                        if (!(*rhs < *sym))
×
1785
                            context.ip = secondary_arg * 4;
×
1786
                        DISPATCH();
×
1787
                    }
1,233✔
1788

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

1798
                    TARGET(EQ_SYM_INDEX_JUMP_IF_TRUE)
1799
                    {
1800
                        UNPACK_ARGS();
86,933✔
1801
                        const Value* sym = popAndResolveAsPtr(context);
86,933✔
1802
                        if (*sym == *loadSymbolFromIndex(primary_arg, context))
86,933✔
1803
                            context.ip = secondary_arg * 4;
529✔
1804
                        DISPATCH();
86,933✔
1805
                    }
1✔
1806

1807
                    TARGET(NEQ_CONST_JUMP_IF_TRUE)
1808
                    {
1809
                        UNPACK_ARGS();
1✔
1810
                        const Value* sym = popAndResolveAsPtr(context);
1✔
1811
                        if (*sym != *loadConstAsPtr(primary_arg))
1✔
1812
                            context.ip = secondary_arg * 4;
1✔
1813
                        DISPATCH();
1✔
1814
                    }
15✔
1815

1816
                    TARGET(NEQ_SYM_JUMP_IF_FALSE)
1817
                    {
1818
                        UNPACK_ARGS();
15✔
1819
                        const Value* sym = popAndResolveAsPtr(context);
15✔
1820
                        if (*sym == *loadSymbol(primary_arg, context))
15✔
1821
                            context.ip = secondary_arg * 4;
5✔
1822
                        DISPATCH();
15✔
1823
                    }
26,082✔
1824

1825
                    TARGET(CALL_SYMBOL)
1826
                    {
1827
                        UNPACK_ARGS();
26,082✔
1828
                        call(context, secondary_arg, loadSymbol(primary_arg, context));
26,082✔
1829
                        if (!m_running)
26,082✔
1830
                            GOTO_HALT();
×
1831
                        DISPATCH();
26,082✔
1832
                    }
109,861✔
1833

1834
                    TARGET(CALL_CURRENT_PAGE)
1835
                    {
1836
                        UNPACK_ARGS();
109,861✔
1837
                        context.last_symbol = primary_arg;
109,861✔
1838
                        call(context, secondary_arg, /* function_ptr= */ nullptr, /* or_address= */ static_cast<PageAddr_t>(context.pp));
109,861✔
1839
                        if (!m_running)
109,860✔
1840
                            GOTO_HALT();
×
1841
                        DISPATCH();
109,860✔
1842
                    }
1,891✔
1843

1844
                    TARGET(GET_FIELD_FROM_SYMBOL)
1845
                    {
1846
                        UNPACK_ARGS();
1,891✔
1847
                        push(getField(loadSymbol(primary_arg, context), secondary_arg, context), context);
1,891✔
1848
                        DISPATCH();
1,891✔
1849
                    }
714✔
1850

1851
                    TARGET(GET_FIELD_FROM_SYMBOL_INDEX)
1852
                    {
1853
                        UNPACK_ARGS();
714✔
1854
                        push(getField(loadSymbolFromIndex(primary_arg, context), secondary_arg, context), context);
716✔
1855
                        DISPATCH();
712✔
1856
                    }
14,448✔
1857

1858
                    TARGET(AT_SYM_SYM)
1859
                    {
1860
                        UNPACK_ARGS();
14,448✔
1861
                        push(helper::at(*loadSymbol(primary_arg, context), *loadSymbol(secondary_arg, context), *this), context);
14,448✔
1862
                        DISPATCH();
14,448✔
1863
                    }
×
1864

1865
                    TARGET(AT_SYM_INDEX_SYM_INDEX)
1866
                    {
1867
                        UNPACK_ARGS();
×
1868
                        push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadSymbolFromIndex(secondary_arg, context), *this), context);
×
1869
                        DISPATCH();
×
1870
                    }
4✔
1871

1872
                    TARGET(CHECK_TYPE_OF)
1873
                    {
1874
                        UNPACK_ARGS();
4✔
1875
                        const Value* sym = loadSymbol(primary_arg, context);
4✔
1876
                        const Value* cst = loadConstAsPtr(secondary_arg);
4✔
1877
                        push(
4✔
1878
                            cst->valueType() == ValueType::String &&
8✔
1879
                                    std::to_string(sym->valueType()) == cst->string()
4✔
1880
                                ? Builtins::trueSym
1881
                                : Builtins::falseSym,
1882
                            context);
4✔
1883
                        DISPATCH();
4✔
1884
                    }
74✔
1885

1886
                    TARGET(CHECK_TYPE_OF_BY_INDEX)
1887
                    {
1888
                        UNPACK_ARGS();
74✔
1889
                        const Value* sym = loadSymbolFromIndex(primary_arg, context);
74✔
1890
                        const Value* cst = loadConstAsPtr(secondary_arg);
74✔
1891
                        push(
74✔
1892
                            cst->valueType() == ValueType::String &&
148✔
1893
                                    std::to_string(sym->valueType()) == cst->string()
74✔
1894
                                ? Builtins::trueSym
1895
                                : Builtins::falseSym,
1896
                            context);
74✔
1897
                        DISPATCH();
74✔
1898
                    }
1,458✔
1899

1900
                    TARGET(APPEND_IN_PLACE_SYM)
1901
                    {
1902
                        UNPACK_ARGS();
1,458✔
1903
                        listAppendInPlace(loadSymbol(primary_arg, context), secondary_arg, context);
1,458✔
1904
                        DISPATCH();
1,458✔
1905
                    }
12✔
1906

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

1952
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1953
            throw;
1954
#endif
1955
            fmt::println("Unknown error");
×
1956
            backtrace(context);
×
1957
            m_exit_code = 1;
×
1958
        }
159✔
1959

1960
        return m_exit_code;
79✔
1961
    }
234✔
1962

1963
    uint16_t VM::findNearestVariableIdWithValue(const Value& value, ExecutionContext& context) const noexcept
2,048✔
1964
    {
2,048✔
1965
        for (auto& local : std::ranges::reverse_view(context.locals))
2,098,178✔
1966
        {
1967
            if (const auto id = local.idFromValue(value); id < m_state.m_symbols.size())
2,096,130✔
1968
                return id;
2,048✔
1969
        }
2,096,130✔
1970
        return std::numeric_limits<uint16_t>::max();
×
1971
    }
2,048✔
1972

1973
    void VM::throwArityError(std::size_t passed_arg_count, std::size_t expected_arg_count, internal::ExecutionContext& context)
5✔
1974
    {
5✔
1975
        std::vector<std::string> arg_names;
5✔
1976
        arg_names.reserve(expected_arg_count + 1);
5✔
1977
        if (expected_arg_count > 0)
5✔
1978
            arg_names.emplace_back("");  // for formatting, so that we have a space between the function and the args
5✔
1979

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

1995
        std::vector<std::string> arg_vals;
5✔
1996
        arg_vals.reserve(passed_arg_count + 1);
5✔
1997
        if (passed_arg_count > 0)
5✔
1998
            arg_vals.emplace_back("");  // for formatting, so that we have a space between the function and the args
4✔
1999

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

2004
        // set ip/pp to the callee location so that the error can pinpoint the line
2005
        // where the bad call happened
2006
        if (context.sp >= 2 + passed_arg_count)
5✔
2007
        {
2008
            context.ip = context.stack[context.sp - 1 - passed_arg_count].pageAddr();
5✔
2009
            context.pp = context.stack[context.sp - 2 - passed_arg_count].pageAddr();
5✔
2010
            returnFromFuncCall(context);
5✔
2011
        }
5✔
2012

2013
        std::string function_name = (context.last_symbol < m_state.m_symbols.size())
10✔
2014
            ? m_state.m_symbols[context.last_symbol]
5✔
2015
            : Value(static_cast<PageAddr_t>(context.pp)).toString(*this);
×
2016

2017
        throwVMError(
5✔
2018
            ErrorKind::Arity,
2019
            fmt::format(
10✔
2020
                "When calling `({}{})', received {} argument{}, but expected {}: `({}{})'",
5✔
2021
                function_name,
2022
                fmt::join(arg_vals, " "),
5✔
2023
                passed_arg_count,
2024
                passed_arg_count > 1 ? "s" : "",
5✔
2025
                expected_arg_count,
2026
                function_name,
2027
                fmt::join(arg_names, " ")));
5✔
2028
    }
10✔
2029

2030
    void VM::showBacktraceWithException(const std::exception& e, internal::ExecutionContext& context)
×
2031
    {
×
2032
        std::string text = e.what();
×
2033
        if (!text.empty() && text.back() != '\n')
×
2034
            text += '\n';
×
2035
        fmt::println("{}", text);
×
2036
        backtrace(context);
×
2037
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2038
        // don't report a "failed" exit code so that the fuzzers can more accurately triage crashes
2039
        m_exit_code = 0;
2040
#else
2041
        m_exit_code = 1;
×
2042
#endif
2043
    }
×
2044

2045
    std::optional<InstLoc> VM::findSourceLocation(const std::size_t ip, const std::size_t pp)
2,167✔
2046
    {
2,167✔
2047
        std::optional<InstLoc> match = std::nullopt;
2,167✔
2048

2049
        for (const auto location : m_state.m_inst_locations)
8,724✔
2050
        {
2051
            if (location.page_pointer == pp && !match)
6,557✔
2052
                match = location;
2,167✔
2053

2054
            // select the best match: we want to find the location that's nearest our instruction pointer,
2055
            // but not equal to it as the IP will always be pointing to the next instruction,
2056
            // not yet executed. Thus, the erroneous instruction is the previous one.
2057
            if (location.page_pointer == pp && match && location.inst_pointer < ip / 4)
6,557✔
2058
                match = location;
2,280✔
2059

2060
            // early exit because we won't find anything better, as inst locations are ordered by ascending (pp, ip)
2061
            if (location.page_pointer > pp || (location.page_pointer == pp && location.inst_pointer >= ip / 4))
6,557✔
2062
                break;
13✔
2063
        }
6,557✔
2064

2065
        return match;
2,167✔
2066
    }
2067

2068
    std::string VM::debugShowSource()
×
2069
    {
×
2070
        const auto& context = m_execution_contexts.front();
×
2071
        auto maybe_source_loc = findSourceLocation(context->ip, context->pp);
×
2072
        if (maybe_source_loc)
×
2073
        {
2074
            const auto filename = m_state.m_filenames[maybe_source_loc->filename_id];
×
2075
            return fmt::format("{}:{} -- IP: {}, PP: {}", filename, maybe_source_loc->line + 1, maybe_source_loc->inst_pointer, maybe_source_loc->page_pointer);
×
2076
        }
×
2077
        return "No source location found";
×
2078
    }
×
2079

2080
    void VM::backtrace(ExecutionContext& context, std::ostream& os, const bool colorize)
117✔
2081
    {
117✔
2082
        const std::size_t saved_ip = context.ip;
117✔
2083
        const std::size_t saved_pp = context.pp;
117✔
2084
        const uint16_t saved_sp = context.sp;
117✔
2085
        const std::size_t max_consecutive_traces = 7;
117✔
2086

2087
        const auto maybe_location = findSourceLocation(context.ip, context.pp);
117✔
2088
        if (maybe_location)
117✔
2089
        {
2090
            const auto filename = m_state.m_filenames[maybe_location->filename_id];
117✔
2091

2092
            if (Utils::fileExists(filename))
117✔
2093
                Diagnostics::makeContext(
234✔
2094
                    os,
117✔
2095
                    filename,
2096
                    /* expr= */ std::nullopt,
117✔
2097
                    /* sym_size= */ 0,
2098
                    maybe_location->line,
117✔
2099
                    /* col_start= */ 0,
2100
                    /* maybe_context= */ std::nullopt,
117✔
2101
                    /* whole_line= */ true,
2102
                    /* colorize= */ colorize);
117✔
2103
            fmt::println(os, "");
117✔
2104
        }
117✔
2105

2106
        if (context.fc > 1)
117✔
2107
        {
2108
            // display call stack trace
2109
            const ScopeView old_scope = context.locals.back();
2✔
2110

2111
            std::string previous_trace;
2✔
2112
            std::size_t displayed_traces = 0;
2✔
2113
            std::size_t consecutive_similar_traces = 0;
2✔
2114

2115
            while (context.fc != 0 && context.pp != 0)
2,050✔
2116
            {
2117
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2,048✔
2118
                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✔
2119

2120
                const uint16_t id = findNearestVariableIdWithValue(
2,048✔
2121
                    Value(static_cast<PageAddr_t>(context.pp)),
2,048✔
2122
                    context);
2,048✔
2123
                const auto func_name = (id < m_state.m_symbols.size()) ? m_state.m_symbols[id] : "???";
2,048✔
2124

2125
                if (func_name + loc_as_text != previous_trace)
2,048✔
2126
                {
2127
                    fmt::println(
4✔
2128
                        os,
2✔
2129
                        "[{:4}] In function `{}'{}",
2✔
2130
                        fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
2✔
2131
                        fmt::styled(func_name, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
2✔
2132
                        loc_as_text);
2133
                    previous_trace = func_name + loc_as_text;
2✔
2134
                    ++displayed_traces;
2✔
2135
                    consecutive_similar_traces = 0;
2✔
2136
                }
2✔
2137
                else if (consecutive_similar_traces == 0)
2,046✔
2138
                {
2139
                    fmt::println(os, "       ...");
1✔
2140
                    ++consecutive_similar_traces;
1✔
2141
                }
1✔
2142

2143
                const Value* ip;
2,048✔
2144
                do
2,049✔
2145
                {
2146
                    ip = popAndResolveAsPtr(context);
2,049✔
2147
                } while (ip->valueType() != ValueType::InstPtr);
2,049✔
2148

2149
                context.ip = ip->pageAddr();
2,048✔
2150
                context.pp = pop(context)->pageAddr();
2,048✔
2151
                returnFromFuncCall(context);
2,048✔
2152

2153
                if (displayed_traces > max_consecutive_traces)
2,048✔
2154
                {
2155
                    fmt::println(os, "       ...");
×
2156
                    break;
×
2157
                }
2158
            }
2,048✔
2159

2160
            if (context.pp == 0)
2✔
2161
            {
2162
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2✔
2163
                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✔
2164
                fmt::println(os, "[{:4}] In global scope{}", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()), loc_as_text);
2✔
2165
            }
2✔
2166

2167
            // display variables values in the current scope
2168
            fmt::println(os, "\nCurrent scope variables values:");
2✔
2169
            for (std::size_t i = 0, size = old_scope.size(); i < size; ++i)
3✔
2170
            {
2171
                fmt::println(
2✔
2172
                    os,
1✔
2173
                    "{} = {}",
1✔
2174
                    fmt::styled(m_state.m_symbols[old_scope.atPos(i).first], colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
1✔
2175
                    old_scope.atPos(i).second.toString(*this));
1✔
2176
            }
1✔
2177
        }
2✔
2178

2179
        fmt::println(
234✔
2180
            os,
117✔
2181
            "At IP: {}, PP: {}, SP: {}",
117✔
2182
            // dividing by 4 because the instructions are actually on 4 bytes
2183
            fmt::styled(saved_ip / 4, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
117✔
2184
            fmt::styled(saved_pp, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
117✔
2185
            fmt::styled(saved_sp, colorize ? fmt::fg(fmt::color::yellow) : fmt::text_style()));
117✔
2186
    }
117✔
2187
}
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