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

ArkScript-lang / Ark / 23209646190

17 Mar 2026 06:16PM UTC coverage: 93.659% (-0.05%) from 93.706%
23209646190

push

github

SuperFola
feat(vm): add a new super instruction CALL_SYMBOL_BY_INDEX

17 of 19 new or added lines in 3 files covered. (89.47%)

132 existing lines in 8 files now uncovered.

9601 of 10251 relevant lines covered (93.66%)

275534.05 hits per line

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

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

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

9
#include <Ark/Utils/Files.hpp>
10
#include <Ark/Utils/Utils.hpp>
11
#include <Ark/Error/Diagnostics.hpp>
12
#include <Ark/TypeChecker.hpp>
13
#include <Ark/VM/ModuleMapping.hpp>
14
#include <Ark/Compiler/Instructions.hpp>
15
#include <Ark/VM/Value/Dict.hpp>
16
#include <Ark/VM/Helpers.hpp>
17

18
namespace Ark
19
{
20
    using namespace internal;
21

22
    VM::VM(State& state) noexcept :
861✔
23
        m_state(state), m_exit_code(0), m_running(false)
287✔
24
    {
287✔
25
        m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>());
287✔
26
    }
287✔
27

28
    void VM::init() noexcept
281✔
29
    {
281✔
30
        ExecutionContext& context = *m_execution_contexts.back();
281✔
31
        for (const auto& c : m_execution_contexts)
562✔
32
        {
33
            c->ip = 0;
281✔
34
            c->pp = 0;
281✔
35
            c->sp = 0;
281✔
36
        }
281✔
37

38
        context.sp = 0;
281✔
39
        context.fc = 1;
281✔
40

41
        m_shared_lib_objects.clear();
281✔
42
        context.stacked_closure_scopes.clear();
281✔
43
        context.stacked_closure_scopes.emplace_back(nullptr);
281✔
44

45
        context.saved_scope.reset();
281✔
46
        m_exit_code = 0;
281✔
47

48
        context.locals.clear();
281✔
49
        context.locals.reserve(128);
281✔
50
        context.locals.emplace_back(context.scopes_storage.data(), 0);
281✔
51

52
        // loading bound stuff
53
        // put them in the global frame if we can, aka the first one
54
        for (const auto& [sym_id, value] : m_state.m_bound)
881✔
55
        {
56
            auto it = std::ranges::find(m_state.m_symbols, sym_id);
576✔
57
            if (it != m_state.m_symbols.end())
576✔
58
                context.locals[0].pushBack(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), value);
24✔
59
        }
576✔
60
    }
281✔
61

62
    Value VM::getField(Value* closure, const uint16_t id, const ExecutionContext& context, const bool push_with_env)
4,422✔
63
    {
4,422✔
64
        if (closure->valueType() != ValueType::Closure)
4,422✔
65
        {
66
            if (context.last_symbol < m_state.m_symbols.size()) [[likely]]
1✔
67
                throwVMError(
2✔
68
                    ErrorKind::Type,
69
                    fmt::format(
3✔
70
                        "`{}' is a {}, not a Closure, can not get the field `{}' from it",
1✔
71
                        m_state.m_symbols[context.last_symbol],
1✔
72
                        std::to_string(closure->valueType()),
1✔
73
                        m_state.m_symbols[id]));
1✔
74
            else
75
                throwVMError(
×
76
                    ErrorKind::Type,
77
                    fmt::format(
×
78
                        "{} is not a Closure, can not get the field `{}' from it",
×
79
                        std::to_string(closure->valueType()),
×
80
                        m_state.m_symbols[id]));
×
81
        }
82

83
        if (Value* field = closure->refClosure().refScope()[id]; field != nullptr)
8,842✔
84
        {
85
            if (push_with_env)
4,420✔
86
                return Value(Closure(closure->refClosure().scopePtr(), field->pageAddr()));
2,472✔
87
            else
88
                return *field;
1,948✔
89
        }
90
        else
91
        {
92
            if (!closure->refClosure().hasFieldEndingWith(m_state.m_symbols[id], *this))
1✔
93
                throwVMError(
1✔
94
                    ErrorKind::Scope,
95
                    fmt::format(
2✔
96
                        "`{0}' isn't in the closure environment: {1}",
1✔
97
                        m_state.m_symbols[id],
1✔
98
                        closure->refClosure().toString(*this)));
1✔
UNCOV
99
            throwVMError(
×
100
                ErrorKind::Scope,
UNCOV
101
                fmt::format(
×
102
                    "`{0}' isn't in the closure environment: {1}. A variable in the package might have the same name as '{0}', "
×
103
                    "and name resolution tried to fully qualify it. Rename either the variable or the capture to solve this",
UNCOV
104
                    m_state.m_symbols[id],
×
105
                    closure->refClosure().toString(*this)));
×
106
        }
107
    }
4,422✔
108

109
    Value VM::createList(const std::size_t count, internal::ExecutionContext& context)
2,441✔
110
    {
2,441✔
111
        Value l(ValueType::List);
2,441✔
112
        if (count != 0)
2,441✔
113
            l.list().reserve(count);
1,301✔
114

115
        for (std::size_t i = 0; i < count; ++i)
5,834✔
116
            l.push_back(*popAndResolveAsPtr(context));
3,393✔
117

118
        return l;
2,441✔
119
    }
2,441✔
120

121
    void VM::listAppendInPlace(Value* list, const std::size_t count, ExecutionContext& context)
3,525✔
122
    {
3,525✔
123
        if (list->valueType() != ValueType::List)
3,525✔
124
        {
125
            std::vector<Value> args = { *list };
1✔
126
            for (std::size_t i = 0; i < count; ++i)
2✔
127
                args.push_back(*popAndResolveAsPtr(context));
1✔
128
            throw types::TypeCheckingError(
2✔
129
                "append!",
1✔
130
                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* is_variadic= */ true) } } } },
1✔
131
                args);
132
        }
1✔
133

134
        for (std::size_t i = 0; i < count; ++i)
7,048✔
135
            list->push_back(*popAndResolveAsPtr(context));
3,524✔
136
    }
3,525✔
137

138
    Value& VM::operator[](const std::string& name) noexcept
36✔
139
    {
36✔
140
        // find id of object
141
        const auto it = std::ranges::find(m_state.m_symbols, name);
36✔
142
        if (it == m_state.m_symbols.end())
36✔
143
        {
144
            m_no_value = Builtins::nil;
1✔
145
            return m_no_value;
1✔
146
        }
147

148
        const auto dist = std::distance(m_state.m_symbols.begin(), it);
35✔
149
        if (std::cmp_less(dist, MaxValue16Bits))
35✔
150
        {
151
            ExecutionContext& context = *m_execution_contexts.front();
35✔
152

153
            const auto id = static_cast<uint16_t>(dist);
35✔
154
            Value* var = findNearestVariable(id, context);
35✔
155
            if (var != nullptr)
35✔
156
                return *var;
35✔
157
        }
35✔
158

UNCOV
159
        m_no_value = Builtins::nil;
×
160
        return m_no_value;
×
161
    }
36✔
162

163
    void VM::loadPlugin(const uint16_t id, ExecutionContext& context)
1✔
164
    {
1✔
165
        namespace fs = std::filesystem;
166

167
        const std::string file = m_state.m_constants[id].stringRef();
1✔
168

169
        std::string path = file;
1✔
170
        // bytecode loaded from file
171
        if (m_state.m_filename != ARK_NO_NAME_FILE)
1✔
172
            path = (fs::path(m_state.m_filename).parent_path() / fs::path(file)).relative_path().string();
1✔
173

174
        std::shared_ptr<SharedLibrary> lib;
1✔
175
        // if it exists alongside the .arkc file
176
        if (Utils::fileExists(path))
1✔
UNCOV
177
            lib = std::make_shared<SharedLibrary>(path);
×
178
        else
179
        {
180
            for (auto const& v : m_state.m_libenv)
3✔
181
            {
182
                std::string lib_path = (fs::path(v) / fs::path(file)).string();
2✔
183

184
                // if it's already loaded don't do anything
185
                if (std::ranges::find_if(m_shared_lib_objects, [&](const auto& val) {
2✔
UNCOV
186
                        return (val->path() == path || val->path() == lib_path);
×
187
                    }) != m_shared_lib_objects.end())
2✔
188
                    return;
287✔
189

190
                // check in lib_path
191
                if (Utils::fileExists(lib_path))
289✔
192
                {
193
                    lib = std::make_shared<SharedLibrary>(lib_path);
1✔
194
                    break;
1✔
195
                }
196
            }
2✔
197
        }
198

199
        if (!lib)
1✔
200
        {
UNCOV
201
            auto lib_path = std::accumulate(
×
202
                std::next(m_state.m_libenv.begin()),
×
203
                m_state.m_libenv.end(),
×
204
                m_state.m_libenv[0].string(),
×
205
                [](const std::string& a, const fs::path& b) -> std::string {
×
206
                    return a + "\n\t- " + b.string();
×
207
                });
×
208
            throwVMError(
×
209
                ErrorKind::Module,
UNCOV
210
                fmt::format("Could not find module '{}'. Searched under\n\t- {}\n\t- {}", file, path, lib_path));
×
211
        }
×
212

213
        m_shared_lib_objects.emplace_back(lib);
1✔
214

215
        // load the mapping from the dynamic library
216
        try
217
        {
218
            std::vector<ScopeView::pair_t> data;
1✔
219
            const mapping* map = m_shared_lib_objects.back()->get<mapping* (*)()>("getFunctionsMapping")();
1✔
220

221
            std::size_t i = 0;
1✔
222
            while (map[i].name != nullptr)
2✔
223
            {
224
                const auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
1✔
225
                if (it != m_state.m_symbols.end())
1✔
226
                    data.emplace_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), Value(map[i].value));
1✔
227

228
                ++i;
1✔
229
            }
1✔
230

231
            context.locals.back().insertFront(data);
1✔
232
        }
1✔
233
        catch (const std::system_error& e)
234
        {
UNCOV
235
            throwVMError(
×
236
                ErrorKind::Module,
UNCOV
237
                fmt::format(
×
238
                    "An error occurred while loading module '{}': {}\nIt is most likely because the versions of the module and the language don't match.",
×
239
                    file, e.what()));
×
240
        }
1✔
241
    }
1✔
242

UNCOV
243
    void VM::exit(const int code) noexcept
×
244
    {
×
245
        m_exit_code = code;
×
246
        m_running = false;
×
247
    }
×
248

249
    ExecutionContext* VM::createAndGetContext()
17✔
250
    {
17✔
251
        const std::lock_guard lock(m_mutex);
17✔
252

253
        ExecutionContext* ctx = nullptr;
17✔
254

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

260
        if (m_execution_contexts.size() > 1)
17✔
261
        {
262
            const auto it = std::ranges::find_if(
28✔
263
                m_execution_contexts,
14✔
264
                [](const std::unique_ptr<ExecutionContext>& context) -> bool {
38✔
265
                    return !context->primary && context->isFree();
38✔
266
                });
267

268
            if (it != m_execution_contexts.end())
14✔
269
            {
270
                ctx = it->get();
10✔
271
                ctx->setActive(true);
10✔
272
                // reset the context before using it
273
                ctx->sp = 0;
10✔
274
                ctx->saved_scope.reset();
10✔
275
                ctx->stacked_closure_scopes.clear();
10✔
276
                ctx->locals.clear();
10✔
277
            }
10✔
278
        }
14✔
279

280
        if (ctx == nullptr)
17✔
281
            ctx = m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>()).get();
7✔
282

283
        assert(!ctx->primary && "The new context shouldn't be marked as primary!");
17✔
284
        assert(ctx != m_execution_contexts.front().get() && "The new context isn't really new!");
17✔
285

286
        const ExecutionContext& primary_ctx = *m_execution_contexts.front();
17✔
287
        ctx->locals.reserve(primary_ctx.locals.size());
17✔
288
        ctx->scopes_storage = primary_ctx.scopes_storage;
17✔
289
        ctx->stacked_closure_scopes.emplace_back(nullptr);
17✔
290
        ctx->fc = 1;
17✔
291

292
        for (const auto& scope_view : primary_ctx.locals)
62✔
293
        {
294
            auto& new_scope = ctx->locals.emplace_back(ctx->scopes_storage.data(), scope_view.m_start);
45✔
295
            for (std::size_t i = 0; i < scope_view.size(); ++i)
3,276✔
296
            {
297
                const auto& [id, val] = scope_view.atPos(i);
3,231✔
298
                new_scope.pushBack(id, val);
3,231✔
299
            }
3,231✔
300
        }
45✔
301

302
        return ctx;
17✔
303
    }
17✔
304

305
    void VM::deleteContext(ExecutionContext* ec)
16✔
306
    {
16✔
307
        const std::lock_guard lock(m_mutex);
16✔
308

309
        // 1 + 4 additional contexts, it's a bit much (~600kB per context) to have in memory
310
        if (m_execution_contexts.size() > 5)
16✔
311
        {
312
            const auto it =
1✔
313
                std::ranges::remove_if(
2✔
314
                    m_execution_contexts,
1✔
315
                    [ec](const std::unique_ptr<ExecutionContext>& ctx) {
7✔
316
                        return ctx.get() == ec;
6✔
317
                    })
318
                    .begin();
1✔
319
            m_execution_contexts.erase(it);
1✔
320
        }
1✔
321
        else
322
        {
323
            // mark the used context as ready to be used again
324
            for (std::size_t i = 1; i < m_execution_contexts.size(); ++i)
40✔
325
            {
326
                if (m_execution_contexts[i].get() == ec)
25✔
327
                {
328
                    ec->setActive(false);
15✔
329
                    break;
15✔
330
                }
331
            }
10✔
332
        }
333
    }
16✔
334

335
    Future* VM::createFuture(std::vector<Value>& args)
17✔
336
    {
17✔
337
        const std::lock_guard lock(m_mutex_futures);
17✔
338

339
        ExecutionContext* ctx = createAndGetContext();
17✔
340
        // so that we have access to the presumed symbol id of the function we are calling
341
        // assuming that the callee is always the global context
342
        ctx->last_symbol = m_execution_contexts.front()->last_symbol;
17✔
343

344
        m_futures.push_back(std::make_unique<Future>(ctx, this, args));
17✔
345
        return m_futures.back().get();
17✔
346
    }
17✔
347

348
    void VM::deleteFuture(Future* f)
1✔
349
    {
1✔
350
        const std::lock_guard lock(m_mutex_futures);
1✔
351

352
        std::erase_if(
1✔
353
            m_futures,
1✔
354
            [f](const std::unique_ptr<Future>& future) {
3✔
355
                return future.get() == f;
2✔
356
            });
357
    }
1✔
358

UNCOV
359
    bool VM::forceReloadPlugins() const
×
360
    {
×
361
        // load the mapping from the dynamic library
362
        try
363
        {
UNCOV
364
            for (const auto& shared_lib : m_shared_lib_objects)
×
365
            {
UNCOV
366
                const mapping* map = shared_lib->get<mapping* (*)()>("getFunctionsMapping")();
×
367
                // load the mapping data
UNCOV
368
                std::size_t i = 0;
×
369
                while (map[i].name != nullptr)
×
370
                {
371
                    // put it in the global frame, aka the first one
UNCOV
372
                    auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
373
                    if (it != m_state.m_symbols.end())
×
374
                        m_execution_contexts[0]->locals[0].pushBack(
×
375
                            static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)),
×
376
                            Value(map[i].value));
×
377

UNCOV
378
                    ++i;
×
379
                }
×
380
            }
×
381

UNCOV
382
            return true;
×
383
        }
×
384
        catch (const std::system_error&)
385
        {
UNCOV
386
            return false;
×
387
        }
×
388
    }
×
389

390
    void VM::usePromptFileForDebugger(const std::string& path, std::ostream& os)
6✔
391
    {
6✔
392
        m_debugger = std::make_unique<Debugger>(m_state.m_libenv, path, os, m_state.m_symbols, m_state.m_constants);
6✔
393
    }
6✔
394

395
    void VM::throwVMError(ErrorKind kind, const std::string& message)
37✔
396
    {
37✔
397
        throw std::runtime_error(std::string(errorKinds[static_cast<std::size_t>(kind)]) + ": " + message + "\n");
37✔
398
    }
37✔
399

400
    int VM::run(const bool fail_with_exception)
281✔
401
    {
281✔
402
        init();
281✔
403
        safeRun(*m_execution_contexts[0], 0, fail_with_exception);
281✔
404
        return m_exit_code;
281✔
405
    }
406

407
    int VM::safeRun(ExecutionContext& context, std::size_t untilFrameCount, bool fail_with_exception)
312✔
408
    {
312✔
409
#if ARK_USE_COMPUTED_GOTOS
410
#    define TARGET(op) TARGET_##op:
411
#    define DISPATCH_GOTO()            \
412
        _Pragma("GCC diagnostic push") \
413
            _Pragma("GCC diagnostic ignored \"-Wpedantic\"") goto* opcode_targets[inst];
414
        _Pragma("GCC diagnostic pop")
415
#    define GOTO_HALT() goto dispatch_end
416
#else
417
#    define TARGET(op) case op:
418
#    define DISPATCH_GOTO() goto dispatch_opcode
419
#    define GOTO_HALT() break
420
#endif
421

422
#define NEXTOPARG()                                                                                                               \
423
    do                                                                                                                            \
424
    {                                                                                                                             \
425
        inst = m_state.inst(context.pp, context.ip);                                                                              \
426
        padding = m_state.inst(context.pp, context.ip + 1);                                                                       \
427
        arg = static_cast<uint16_t>((m_state.inst(context.pp, context.ip + 2) << 8) +                                             \
428
                                    m_state.inst(context.pp, context.ip + 3));                                                    \
429
        context.ip += 4;                                                                                                          \
430
        context.inst_exec_counter = (context.inst_exec_counter + 1) % VMOverflowBufferSize;                                       \
431
        if (context.inst_exec_counter < 2 && context.sp >= VMStackSize)                                                           \
432
        {                                                                                                                         \
433
            if (context.pp != 0)                                                                                                  \
434
                throw Error("Stack overflow. You could consider rewriting your function to make use of tail-call optimization."); \
435
            else                                                                                                                  \
436
                throw Error("Stack overflow. Are you trying to call a function with too many arguments?");                        \
437
        }                                                                                                                         \
438
    } while (false)
439
#define DISPATCH() \
440
    NEXTOPARG();   \
441
    DISPATCH_GOTO();
442
#define UNPACK_ARGS()                                                                 \
443
    do                                                                                \
444
    {                                                                                 \
445
        secondary_arg = static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12); \
446
        primary_arg = arg & 0x0fff;                                                   \
447
    } while (false)
448

449
#if ARK_USE_COMPUTED_GOTOS
450
#    pragma GCC diagnostic push
451
#    pragma GCC diagnostic ignored "-Wpedantic"
452
            constexpr std::array opcode_targets = {
312✔
453
                // cppcheck-suppress syntaxError ; cppcheck do not know about labels addresses (GCC extension)
454
                &&TARGET_NOP,
455
                &&TARGET_LOAD_FAST,
456
                &&TARGET_LOAD_FAST_BY_INDEX,
457
                &&TARGET_LOAD_SYMBOL,
458
                &&TARGET_LOAD_CONST,
459
                &&TARGET_POP_JUMP_IF_TRUE,
460
                &&TARGET_STORE,
461
                &&TARGET_STORE_REF,
462
                &&TARGET_SET_VAL,
463
                &&TARGET_POP_JUMP_IF_FALSE,
464
                &&TARGET_JUMP,
465
                &&TARGET_RET,
466
                &&TARGET_HALT,
467
                &&TARGET_PUSH_RETURN_ADDRESS,
468
                &&TARGET_CALL,
469
                &&TARGET_TAIL_CALL_SELF,
470
                &&TARGET_CAPTURE,
471
                &&TARGET_RENAME_NEXT_CAPTURE,
472
                &&TARGET_BUILTIN,
473
                &&TARGET_DEL,
474
                &&TARGET_MAKE_CLOSURE,
475
                &&TARGET_GET_FIELD,
476
                &&TARGET_GET_FIELD_AS_CLOSURE,
477
                &&TARGET_PLUGIN,
478
                &&TARGET_LIST,
479
                &&TARGET_APPEND,
480
                &&TARGET_CONCAT,
481
                &&TARGET_APPEND_IN_PLACE,
482
                &&TARGET_CONCAT_IN_PLACE,
483
                &&TARGET_POP_LIST,
484
                &&TARGET_POP_LIST_IN_PLACE,
485
                &&TARGET_SET_AT_INDEX,
486
                &&TARGET_SET_AT_2_INDEX,
487
                &&TARGET_POP,
488
                &&TARGET_SHORTCIRCUIT_AND,
489
                &&TARGET_SHORTCIRCUIT_OR,
490
                &&TARGET_CREATE_SCOPE,
491
                &&TARGET_RESET_SCOPE_JUMP,
492
                &&TARGET_POP_SCOPE,
493
                &&TARGET_APPLY,
494
                &&TARGET_BREAKPOINT,
495
                &&TARGET_ADD,
496
                &&TARGET_SUB,
497
                &&TARGET_MUL,
498
                &&TARGET_DIV,
499
                &&TARGET_GT,
500
                &&TARGET_LT,
501
                &&TARGET_LE,
502
                &&TARGET_GE,
503
                &&TARGET_NEQ,
504
                &&TARGET_EQ,
505
                &&TARGET_LEN,
506
                &&TARGET_IS_EMPTY,
507
                &&TARGET_TAIL,
508
                &&TARGET_HEAD,
509
                &&TARGET_IS_NIL,
510
                &&TARGET_TO_NUM,
511
                &&TARGET_TO_STR,
512
                &&TARGET_AT,
513
                &&TARGET_AT_AT,
514
                &&TARGET_MOD,
515
                &&TARGET_TYPE,
516
                &&TARGET_HAS_FIELD,
517
                &&TARGET_NOT,
518
                &&TARGET_LOAD_CONST_LOAD_CONST,
519
                &&TARGET_LOAD_CONST_STORE,
520
                &&TARGET_LOAD_CONST_SET_VAL,
521
                &&TARGET_STORE_FROM,
522
                &&TARGET_STORE_FROM_INDEX,
523
                &&TARGET_SET_VAL_FROM,
524
                &&TARGET_SET_VAL_FROM_INDEX,
525
                &&TARGET_INCREMENT,
526
                &&TARGET_INCREMENT_BY_INDEX,
527
                &&TARGET_INCREMENT_STORE,
528
                &&TARGET_DECREMENT,
529
                &&TARGET_DECREMENT_BY_INDEX,
530
                &&TARGET_DECREMENT_STORE,
531
                &&TARGET_STORE_TAIL,
532
                &&TARGET_STORE_TAIL_BY_INDEX,
533
                &&TARGET_STORE_HEAD,
534
                &&TARGET_STORE_HEAD_BY_INDEX,
535
                &&TARGET_STORE_LIST,
536
                &&TARGET_SET_VAL_TAIL,
537
                &&TARGET_SET_VAL_TAIL_BY_INDEX,
538
                &&TARGET_SET_VAL_HEAD,
539
                &&TARGET_SET_VAL_HEAD_BY_INDEX,
540
                &&TARGET_CALL_BUILTIN,
541
                &&TARGET_CALL_BUILTIN_WITHOUT_RETURN_ADDRESS,
542
                &&TARGET_LT_CONST_JUMP_IF_FALSE,
543
                &&TARGET_LT_CONST_JUMP_IF_TRUE,
544
                &&TARGET_LT_SYM_JUMP_IF_FALSE,
545
                &&TARGET_GT_CONST_JUMP_IF_TRUE,
546
                &&TARGET_GT_CONST_JUMP_IF_FALSE,
547
                &&TARGET_GT_SYM_JUMP_IF_FALSE,
548
                &&TARGET_EQ_CONST_JUMP_IF_TRUE,
549
                &&TARGET_EQ_SYM_INDEX_JUMP_IF_TRUE,
550
                &&TARGET_NEQ_CONST_JUMP_IF_TRUE,
551
                &&TARGET_NEQ_SYM_JUMP_IF_FALSE,
552
                &&TARGET_CALL_SYMBOL,
553
                &&TARGET_CALL_SYMBOL_BY_INDEX,
554
                &&TARGET_CALL_CURRENT_PAGE,
555
                &&TARGET_GET_FIELD_FROM_SYMBOL,
556
                &&TARGET_GET_FIELD_FROM_SYMBOL_INDEX,
557
                &&TARGET_AT_SYM_SYM,
558
                &&TARGET_AT_SYM_INDEX_SYM_INDEX,
559
                &&TARGET_AT_SYM_INDEX_CONST,
560
                &&TARGET_CHECK_TYPE_OF,
561
                &&TARGET_CHECK_TYPE_OF_BY_INDEX,
562
                &&TARGET_APPEND_IN_PLACE_SYM,
563
                &&TARGET_APPEND_IN_PLACE_SYM_INDEX,
564
                &&TARGET_STORE_LEN,
565
                &&TARGET_LT_LEN_SYM_JUMP_IF_FALSE,
566
                &&TARGET_MUL_BY,
567
                &&TARGET_MUL_BY_INDEX,
568
                &&TARGET_MUL_SET_VAL,
569
                &&TARGET_FUSED_MATH
570
            };
571

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

576
        try
577
        {
578
            uint8_t inst = 0;
312✔
579
            uint8_t padding = 0;
312✔
580
            uint16_t arg = 0;
312✔
581
            uint16_t primary_arg = 0;
312✔
582
            uint16_t secondary_arg = 0;
312✔
583

584
            m_running = true;
312✔
585

586
            DISPATCH();
312✔
587
            // cppcheck-suppress unreachableCode ; analysis cannot follow the chain of goto... but it works!
588
            {
589
#if !ARK_USE_COMPUTED_GOTOS
590
            dispatch_opcode:
591
                switch (inst)
592
#endif
UNCOV
593
                {
×
594
#pragma region "Instructions"
595
                    TARGET(NOP)
596
                    {
UNCOV
597
                        DISPATCH();
×
598
                    }
152,647✔
599

600
                    TARGET(LOAD_FAST)
601
                    {
602
                        push(loadSymbol(arg, context), context);
152,647✔
603
                        DISPATCH();
152,647✔
604
                    }
335,315✔
605

606
                    TARGET(LOAD_FAST_BY_INDEX)
607
                    {
608
                        push(loadSymbolFromIndex(arg, context), context);
335,315✔
609
                        DISPATCH();
335,315✔
610
                    }
4,600✔
611

612
                    TARGET(LOAD_SYMBOL)
613
                    {
614
                        // force resolving the reference
615
                        push(*loadSymbol(arg, context), context);
4,600✔
616
                        DISPATCH();
4,600✔
617
                    }
119,320✔
618

619
                    TARGET(LOAD_CONST)
620
                    {
621
                        push(loadConstAsPtr(arg), context);
119,320✔
622
                        DISPATCH();
119,320✔
623
                    }
32,123✔
624

625
                    TARGET(POP_JUMP_IF_TRUE)
626
                    {
627
                        if (Value boolean = *popAndResolveAsPtr(context); !!boolean)
40,963✔
628
                            jump(arg, context);
8,840✔
629
                        DISPATCH();
32,123✔
630
                    }
403,507✔
631

632
                    TARGET(STORE)
633
                    {
634
                        store(arg, popAndResolveAsPtr(context), context);
403,507✔
635
                        DISPATCH();
403,507✔
636
                    }
473✔
637

638
                    TARGET(STORE_REF)
639
                    {
640
                        // Not resolving a potential ref is on purpose!
641
                        // This instruction is only used by functions when storing arguments
642
                        const Value* tmp = pop(context);
473✔
643
                        store(arg, tmp, context);
473✔
644
                        DISPATCH();
473✔
645
                    }
23,991✔
646

647
                    TARGET(SET_VAL)
648
                    {
649
                        setVal(arg, popAndResolveAsPtr(context), context);
23,991✔
650
                        DISPATCH();
23,991✔
651
                    }
19,016✔
652

653
                    TARGET(POP_JUMP_IF_FALSE)
654
                    {
655
                        if (Value boolean = *popAndResolveAsPtr(context); !boolean)
20,092✔
656
                            jump(arg, context);
1,076✔
657
                        DISPATCH();
19,016✔
658
                    }
122,108✔
659

660
                    TARGET(JUMP)
661
                    {
662
                        jump(arg, context);
122,108✔
663
                        DISPATCH();
122,108✔
664
                    }
139,626✔
665

666
                    TARGET(RET)
667
                    {
668
                        {
669
                            Value ts = *popAndResolveAsPtr(context);
139,626✔
670
                            Value ts1 = *popAndResolveAsPtr(context);
139,626✔
671

672
                            if (ts1.valueType() == ValueType::InstPtr)
139,626✔
673
                            {
674
                                context.ip = ts1.pageAddr();
135,216✔
675
                                // we always push PP then IP, thus the next value
676
                                // MUST be the page pointer
677
                                context.pp = pop(context)->pageAddr();
135,216✔
678

679
                                returnFromFuncCall(context);
135,216✔
680
                                if (ts.valueType() == ValueType::Garbage)
135,216✔
681
                                    push(Builtins::nil, context);
1✔
682
                                else
683
                                    push(std::move(ts), context);
135,215✔
684
                            }
135,216✔
685
                            else if (ts1.valueType() == ValueType::Garbage)
4,410✔
686
                            {
687
                                const Value* ip = pop(context);
2,525✔
688
                                assert(ip->valueType() == ValueType::InstPtr && "Expected instruction pointer on the stack (is the stack trashed?)");
2,525✔
689
                                context.ip = ip->pageAddr();
2,525✔
690
                                context.pp = pop(context)->pageAddr();
2,525✔
691

692
                                returnFromFuncCall(context);
2,525✔
693
                                push(std::move(ts), context);
2,525✔
694
                            }
2,525✔
695
                            else if (ts.valueType() == ValueType::InstPtr)
1,885✔
696
                            {
697
                                context.ip = ts.pageAddr();
1,885✔
698
                                context.pp = ts1.pageAddr();
1,885✔
699
                                returnFromFuncCall(context);
1,885✔
700
                                push(Builtins::nil, context);
1,885✔
701
                            }
1,885✔
702
                            else
UNCOV
703
                                throw Error(
×
UNCOV
704
                                    fmt::format(
×
UNCOV
705
                                        "Unhandled case when returning from function call. TS=({}){}, TS1=({}){}",
×
UNCOV
706
                                        std::to_string(ts.valueType()),
×
UNCOV
707
                                        ts.toString(*this),
×
UNCOV
708
                                        std::to_string(ts1.valueType()),
×
UNCOV
709
                                        ts1.toString(*this)));
×
710

711
                            if (context.fc <= untilFrameCount)
139,626✔
712
                                GOTO_HALT();
18✔
713
                        }
139,626✔
714

715
                        DISPATCH();
139,608✔
716
                    }
92✔
717

718
                    TARGET(HALT)
719
                    {
720
                        m_running = false;
92✔
721
                        GOTO_HALT();
92✔
722
                    }
143,770✔
723

724
                    TARGET(PUSH_RETURN_ADDRESS)
725
                    {
726
                        push(Value(static_cast<PageAddr_t>(context.pp)), context);
143,770✔
727
                        // arg * 4 to skip over the call instruction, so that the return address points to AFTER the call
728
                        push(Value(ValueType::InstPtr, static_cast<PageAddr_t>(arg * 4)), context);
143,770✔
729
                        context.inst_exec_counter++;
143,770✔
730
                        DISPATCH();
143,770✔
731
                    }
2,515✔
732

733
                    TARGET(CALL)
734
                    {
735
                        call(context, arg);
2,515✔
736
                        if (!m_running)
2,512✔
UNCOV
737
                            GOTO_HALT();
×
738
                        DISPATCH();
2,512✔
739
                    }
87,616✔
740

741
                    TARGET(TAIL_CALL_SELF)
742
                    {
743
                        jump(0, context);
87,616✔
744
                        context.locals.back().reset();
87,616✔
745
                        DISPATCH();
87,616✔
746
                    }
3,209✔
747

748
                    TARGET(CAPTURE)
749
                    {
750
                        if (!context.saved_scope)
3,209✔
751
                            context.saved_scope = ClosureScope();
632✔
752

753
                        const Value* ptr = findNearestVariable(arg, context);
3,209✔
754
                        if (!ptr)
3,209✔
UNCOV
755
                            throwVMError(ErrorKind::Scope, fmt::format("Couldn't capture `{}' as it is currently unbound", m_state.m_symbols[arg]));
×
756
                        else
757
                        {
758
                            ptr = ptr->valueType() == ValueType::Reference ? ptr->reference() : ptr;
3,209✔
759
                            uint16_t id = context.capture_rename_id.value_or(arg);
3,209✔
760
                            context.saved_scope.value().push_back(id, *ptr);
3,209✔
761
                            context.capture_rename_id.reset();
3,209✔
762
                        }
763

764
                        DISPATCH();
3,209✔
765
                    }
13✔
766

767
                    TARGET(RENAME_NEXT_CAPTURE)
768
                    {
769
                        context.capture_rename_id = arg;
13✔
770
                        DISPATCH();
13✔
771
                    }
2,168✔
772

773
                    TARGET(BUILTIN)
774
                    {
775
                        push(Builtins::builtins[arg].second, context);
2,168✔
776
                        DISPATCH();
2,168✔
777
                    }
2✔
778

779
                    TARGET(DEL)
780
                    {
781
                        if (Value* var = findNearestVariable(arg, context); var != nullptr)
2✔
782
                        {
783
                            if (var->valueType() == ValueType::User)
1✔
784
                                var->usertypeRef().del();
1✔
785
                            *var = Value();
1✔
786
                            DISPATCH();
1✔
787
                        }
788

789
                        throwVMError(ErrorKind::Scope, fmt::format("Can not delete unbound variable `{}'", m_state.m_symbols[arg]));
1✔
790
                    }
632✔
791

792
                    TARGET(MAKE_CLOSURE)
793
                    {
794
                        push(Value(Closure(context.saved_scope.value(), m_state.m_constants[arg].pageAddr())), context);
632✔
795
                        context.saved_scope.reset();
632✔
796
                        DISPATCH();
632✔
UNCOV
797
                    }
×
798

799
                    TARGET(GET_FIELD)
800
                    {
UNCOV
801
                        Value* var = popAndResolveAsPtr(context);
×
UNCOV
802
                        push(getField(var, arg, context), context);
×
UNCOV
803
                        DISPATCH();
×
804
                    }
2,472✔
805

806
                    TARGET(GET_FIELD_AS_CLOSURE)
807
                    {
808
                        Value* var = popAndResolveAsPtr(context);
2,472✔
809
                        push(getField(var, arg, context, /* push_with_env= */ true), context);
2,472✔
810
                        DISPATCH();
2,472✔
811
                    }
1✔
812

813
                    TARGET(PLUGIN)
814
                    {
815
                        loadPlugin(arg, context);
1✔
816
                        DISPATCH();
1✔
817
                    }
1,148✔
818

819
                    TARGET(LIST)
820
                    {
821
                        {
822
                            Value l = createList(arg, context);
1,148✔
823
                            push(std::move(l), context);
1,148✔
824
                        }
1,148✔
825
                        DISPATCH();
1,148✔
826
                    }
30✔
827

828
                    TARGET(APPEND)
829
                    {
830
                        {
831
                            Value* list = popAndResolveAsPtr(context);
30✔
832
                            if (list->valueType() != ValueType::List)
30✔
833
                            {
834
                                std::vector<Value> args = { *list };
1✔
835
                                for (uint16_t i = 0; i < arg; ++i)
2✔
836
                                    args.push_back(*popAndResolveAsPtr(context));
1✔
837
                                throw types::TypeCheckingError(
2✔
838
                                    "append",
1✔
839
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* is_variadic= */ true) } } } },
1✔
840
                                    args);
841
                            }
1✔
842

843
                            const auto size = static_cast<uint16_t>(list->constList().size());
29✔
844

845
                            Value obj { *list };
29✔
846
                            obj.list().reserve(size + arg);
29✔
847

848
                            for (uint16_t i = 0; i < arg; ++i)
58✔
849
                                obj.push_back(*popAndResolveAsPtr(context));
29✔
850
                            push(std::move(obj), context);
29✔
851
                        }
29✔
852
                        DISPATCH();
29✔
853
                    }
15✔
854

855
                    TARGET(CONCAT)
856
                    {
857
                        {
858
                            Value* list = popAndResolveAsPtr(context);
15✔
859
                            Value obj { *list };
15✔
860

861
                            for (uint16_t i = 0; i < arg; ++i)
30✔
862
                            {
863
                                Value* next = popAndResolveAsPtr(context);
17✔
864

865
                                if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
17✔
866
                                    throw types::TypeCheckingError(
4✔
867
                                        "concat",
2✔
868
                                        { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
869
                                        { *list, *next });
2✔
870

871
                                std::ranges::copy(next->list(), std::back_inserter(obj.list()));
15✔
872
                            }
15✔
873
                            push(std::move(obj), context);
13✔
874
                        }
15✔
875
                        DISPATCH();
13✔
876
                    }
1✔
877

878
                    TARGET(APPEND_IN_PLACE)
879
                    {
880
                        Value* list = popAndResolveAsPtr(context);
1✔
881
                        listAppendInPlace(list, arg, context);
1✔
882
                        DISPATCH();
1✔
883
                    }
570✔
884

885
                    TARGET(CONCAT_IN_PLACE)
886
                    {
887
                        Value* list = popAndResolveAsPtr(context);
570✔
888

889
                        for (uint16_t i = 0; i < arg; ++i)
1,175✔
890
                        {
891
                            Value* next = popAndResolveAsPtr(context);
607✔
892

893
                            if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
607✔
894
                                throw types::TypeCheckingError(
4✔
895
                                    "concat!",
2✔
896
                                    { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
897
                                    { *list, *next });
2✔
898

899
                            std::ranges::copy(next->list(), std::back_inserter(list->list()));
605✔
900
                        }
605✔
901
                        DISPATCH();
568✔
902
                    }
6✔
903

904
                    TARGET(POP_LIST)
905
                    {
906
                        {
907
                            Value list = *popAndResolveAsPtr(context);
6✔
908
                            Value number = *popAndResolveAsPtr(context);
6✔
909

910
                            if (list.valueType() != ValueType::List || number.valueType() != ValueType::Number)
6✔
911
                                throw types::TypeCheckingError(
2✔
912
                                    "pop",
1✔
913
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
914
                                    { list, number });
1✔
915

916
                            long idx = static_cast<long>(number.number());
5✔
917
                            idx = idx < 0 ? static_cast<long>(list.list().size()) + idx : idx;
5✔
918
                            if (std::cmp_greater_equal(idx, list.list().size()) || idx < 0)
5✔
919
                                throwVMError(
2✔
920
                                    ErrorKind::Index,
921
                                    fmt::format("pop index ({}) out of range (list size: {})", idx, list.list().size()));
2✔
922

923
                            list.list().erase(list.list().begin() + idx);
3✔
924
                            push(list, context);
3✔
925
                        }
6✔
926
                        DISPATCH();
3✔
927
                    }
237✔
928

929
                    TARGET(POP_LIST_IN_PLACE)
930
                    {
931
                        {
932
                            Value* list = popAndResolveAsPtr(context);
237✔
933
                            Value number = *popAndResolveAsPtr(context);
237✔
934

935
                            if (list->valueType() != ValueType::List || number.valueType() != ValueType::Number)
237✔
936
                                throw types::TypeCheckingError(
2✔
937
                                    "pop!",
1✔
938
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
939
                                    { *list, number });
1✔
940

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

948
                            // Save the value we're removing to push it later.
949
                            // We need to save the value and push later because we're using a pointer to 'list', and pushing before erasing
950
                            // would overwrite values from the stack.
951
                            if (arg)
234✔
952
                                number = list->list()[static_cast<std::size_t>(idx)];
219✔
953
                            list->list().erase(list->list().begin() + idx);
234✔
954
                            if (arg)
234✔
955
                                push(number, context);
219✔
956
                        }
237✔
957
                        DISPATCH();
234✔
958
                    }
512✔
959

960
                    TARGET(SET_AT_INDEX)
961
                    {
962
                        {
963
                            Value* list = popAndResolveAsPtr(context);
512✔
964
                            Value number = *popAndResolveAsPtr(context);
512✔
965
                            Value new_value = *popAndResolveAsPtr(context);
512✔
966

967
                            if (!list->isIndexable() || number.valueType() != ValueType::Number || (list->valueType() == ValueType::String && new_value.valueType() != ValueType::String))
512✔
968
                                throw types::TypeCheckingError(
2✔
969
                                    "@=",
1✔
970
                                    { { types::Contract {
3✔
971
                                          { types::Typedef("list", ValueType::List),
3✔
972
                                            types::Typedef("index", ValueType::Number),
1✔
973
                                            types::Typedef("new_value", ValueType::Any) } } },
1✔
974
                                      { types::Contract {
1✔
975
                                          { types::Typedef("string", ValueType::String),
3✔
976
                                            types::Typedef("index", ValueType::Number),
1✔
977
                                            types::Typedef("char", ValueType::String) } } } },
1✔
978
                                    { *list, number, new_value });
1✔
979

980
                            const std::size_t size = list->valueType() == ValueType::List ? list->list().size() : list->stringRef().size();
511✔
981
                            long idx = static_cast<long>(number.number());
511✔
982
                            idx = idx < 0 ? static_cast<long>(size) + idx : idx;
511✔
983
                            if (std::cmp_greater_equal(idx, size) || idx < 0)
511✔
984
                                throwVMError(
2✔
985
                                    ErrorKind::Index,
986
                                    fmt::format("@= index ({}) out of range (indexable size: {})", idx, size));
2✔
987

988
                            if (list->valueType() == ValueType::List)
509✔
989
                            {
990
                                list->list()[static_cast<std::size_t>(idx)] = new_value;
505✔
991
                                if (arg)
505✔
992
                                    push(new_value, context);
2✔
993
                            }
505✔
994
                            else
995
                            {
996
                                list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
4✔
997
                                if (arg)
4✔
998
                                    push(Value(std::string(1, new_value.string()[0])), context);
2✔
999
                            }
1000
                        }
512✔
1001
                        DISPATCH();
509✔
1002
                    }
359✔
1003

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

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

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

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

1045
                            const bool is_list = list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
355✔
1046
                            const std::size_t size =
355✔
1047
                                is_list
710✔
1048
                                ? list->list()[static_cast<std::size_t>(idx_y)].list().size()
350✔
1049
                                : list->list()[static_cast<std::size_t>(idx_y)].stringRef().size();
5✔
1050

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

1058
                            if (is_list)
353✔
1059
                            {
1060
                                list->list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)] = new_value;
348✔
1061
                                if (arg)
348✔
1062
                                    push(new_value, context);
2✔
1063
                            }
348✔
1064
                            else
1065
                            {
1066
                                list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
5✔
1067
                                if (arg)
5✔
1068
                                    push(Value(std::string(1, new_value.string()[0])), context);
3✔
1069
                            }
1070
                        }
359✔
1071
                        DISPATCH();
353✔
1072
                    }
4,940✔
1073

1074
                    TARGET(POP)
1075
                    {
1076
                        pop(context);
4,940✔
1077
                        DISPATCH();
4,940✔
1078
                    }
24,356✔
1079

1080
                    TARGET(SHORTCIRCUIT_AND)
1081
                    {
1082
                        if (!*peekAndResolveAsPtr(context))
24,356✔
1083
                            jump(arg, context);
1,033✔
1084
                        else
1085
                            pop(context);
23,323✔
1086
                        DISPATCH();
24,356✔
1087
                    }
1,192✔
1088

1089
                    TARGET(SHORTCIRCUIT_OR)
1090
                    {
1091
                        if (!!*peekAndResolveAsPtr(context))
1,192✔
1092
                            jump(arg, context);
220✔
1093
                        else
1094
                            pop(context);
972✔
1095
                        DISPATCH();
1,192✔
1096
                    }
3,185✔
1097

1098
                    TARGET(CREATE_SCOPE)
1099
                    {
1100
                        context.locals.emplace_back(context.scopes_storage.data(), context.locals.back().storageEnd());
3,185✔
1101
                        DISPATCH();
3,185✔
1102
                    }
33,525✔
1103

1104
                    TARGET(RESET_SCOPE_JUMP)
1105
                    {
1106
                        context.locals.back().reset();
33,525✔
1107
                        jump(arg, context);
33,525✔
1108
                        DISPATCH();
33,525✔
1109
                    }
3,184✔
1110

1111
                    TARGET(POP_SCOPE)
1112
                    {
1113
                        context.locals.pop_back();
3,184✔
1114
                        DISPATCH();
3,184✔
1115
                    }
187✔
1116

1117
                    TARGET(APPLY)
1118
                    {
1119
                        {
1120
                            const Value args_list = *popAndResolveAsPtr(context),
187✔
1121
                                        func = *popAndResolveAsPtr(context);
187✔
1122
                            if (args_list.valueType() != ValueType::List || !func.isFunction())
187✔
1123
                            {
1124
                                throw types::TypeCheckingError(
4✔
1125
                                    "apply",
2✔
1126
                                    { {
8✔
1127
                                        types::Contract {
2✔
1128
                                            { types::Typedef("func", ValueType::PageAddr),
4✔
1129
                                              types::Typedef("args", ValueType::List) } },
2✔
1130
                                        types::Contract {
2✔
1131
                                            { types::Typedef("func", ValueType::Closure),
4✔
1132
                                              types::Typedef("args", ValueType::List) } },
2✔
1133
                                        types::Contract {
2✔
1134
                                            { types::Typedef("func", ValueType::CProc),
4✔
1135
                                              types::Typedef("args", ValueType::List) } },
2✔
1136
                                    } },
1137
                                    { func, args_list });
2✔
UNCOV
1138
                            }
×
1139

1140
                            push(func, context);
185✔
1141
                            for (const Value& a : args_list.constList())
490✔
1142
                                push(a, context);
305✔
1143

1144
                            call(context, static_cast<uint16_t>(args_list.constList().size()));
185✔
1145
                        }
187✔
1146
                        DISPATCH();
147✔
1147
                    }
24✔
1148

1149
#pragma endregion
1150

1151
#pragma region "Operators"
1152

1153
                    TARGET(BREAKPOINT)
1154
                    {
1155
                        {
1156
                            bool breakpoint_active = true;
24✔
1157
                            if (arg == 1)
24✔
1158
                                breakpoint_active = *popAndResolveAsPtr(context) == Builtins::trueSym;
19✔
1159

1160
                            if (m_state.m_features & FeatureVMDebugger && breakpoint_active)
24✔
1161
                            {
1162
                                initDebugger(context);
12✔
1163
                                m_debugger->run(*this, context, /* from_breakpoint= */ true);
12✔
1164
                                m_debugger->resetContextToSavedState(context);
12✔
1165

1166
                                if (m_debugger->shouldQuitVM())
12✔
1167
                                    GOTO_HALT();
1✔
1168
                            }
11✔
1169
                        }
1170
                        DISPATCH();
23✔
1171
                    }
29,092✔
1172

1173
                    TARGET(ADD)
1174
                    {
1175
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
29,092✔
1176

1177
                        if (a->valueType() == ValueType::Number && b->valueType() == ValueType::Number)
29,092✔
1178
                            push(Value(a->number() + b->number()), context);
20,430✔
1179
                        else if (a->valueType() == ValueType::String && b->valueType() == ValueType::String)
8,662✔
1180
                            push(Value(a->string() + b->string()), context);
8,661✔
1181
                        else
1182
                            throw types::TypeCheckingError(
2✔
1183
                                "+",
1✔
1184
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } },
2✔
1185
                                    types::Contract { { types::Typedef("a", ValueType::String), types::Typedef("b", ValueType::String) } } } },
1✔
1186
                                { *a, *b });
1✔
1187
                        DISPATCH();
29,091✔
1188
                    }
382✔
1189

1190
                    TARGET(SUB)
1191
                    {
1192
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
382✔
1193

1194
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
382✔
1195
                            throw types::TypeCheckingError(
2✔
1196
                                "-",
1✔
1197
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1198
                                { *a, *b });
1✔
1199
                        push(Value(a->number() - b->number()), context);
381✔
1200
                        DISPATCH();
381✔
1201
                    }
830✔
1202

1203
                    TARGET(MUL)
1204
                    {
1205
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
830✔
1206

1207
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
830✔
1208
                            throw types::TypeCheckingError(
2✔
1209
                                "*",
1✔
1210
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1211
                                { *a, *b });
1✔
1212
                        push(Value(a->number() * b->number()), context);
829✔
1213
                        DISPATCH();
829✔
1214
                    }
144✔
1215

1216
                    TARGET(DIV)
1217
                    {
1218
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
144✔
1219

1220
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
144✔
1221
                            throw types::TypeCheckingError(
2✔
1222
                                "/",
1✔
1223
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1224
                                { *a, *b });
1✔
1225
                        auto d = b->number();
143✔
1226
                        if (d == 0)
143✔
1227
                            throwVMError(ErrorKind::DivisionByZero, fmt::format("Can not compute expression (/ {} {})", a->toString(*this), b->toString(*this)));
1✔
1228

1229
                        push(Value(a->number() / d), context);
142✔
1230
                        DISPATCH();
142✔
1231
                    }
222✔
1232

1233
                    TARGET(GT)
1234
                    {
1235
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
222✔
1236
                        push(*b < *a ? Builtins::trueSym : Builtins::falseSym, context);
222✔
1237
                        DISPATCH();
222✔
1238
                    }
21,716✔
1239

1240
                    TARGET(LT)
1241
                    {
1242
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
21,716✔
1243
                        push(*a < *b ? Builtins::trueSym : Builtins::falseSym, context);
21,716✔
1244
                        DISPATCH();
21,716✔
1245
                    }
7,297✔
1246

1247
                    TARGET(LE)
1248
                    {
1249
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
7,297✔
1250
                        push((*a < *b || *a == *b) ? Builtins::trueSym : Builtins::falseSym, context);
7,297✔
1251
                        DISPATCH();
7,297✔
1252
                    }
5,935✔
1253

1254
                    TARGET(GE)
1255
                    {
1256
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
5,935✔
1257
                        push((*b < *a || *a == *b) ? Builtins::trueSym : Builtins::falseSym, context);
5,935✔
1258
                        DISPATCH();
5,935✔
1259
                    }
1,597✔
1260

1261
                    TARGET(NEQ)
1262
                    {
1263
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,597✔
1264
                        push(*a != *b ? Builtins::trueSym : Builtins::falseSym, context);
1,597✔
1265
                        DISPATCH();
1,597✔
1266
                    }
18,748✔
1267

1268
                    TARGET(EQ)
1269
                    {
1270
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
18,748✔
1271
                        push(*a == *b ? Builtins::trueSym : Builtins::falseSym, context);
18,748✔
1272
                        DISPATCH();
18,748✔
1273
                    }
4,008✔
1274

1275
                    TARGET(LEN)
1276
                    {
1277
                        const Value* a = popAndResolveAsPtr(context);
4,008✔
1278

1279
                        if (a->valueType() == ValueType::List)
4,008✔
1280
                            push(Value(static_cast<int>(a->constList().size())), context);
1,585✔
1281
                        else if (a->valueType() == ValueType::String)
2,423✔
1282
                            push(Value(static_cast<int>(a->string().size())), context);
2,417✔
1283
                        else if (a->valueType() == ValueType::Dict)
6✔
1284
                            push(Value(static_cast<int>(a->dict().size())), context);
5✔
1285
                        else
1286
                            throw types::TypeCheckingError(
2✔
1287
                                "len",
1✔
1288
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
3✔
1289
                                    types::Contract { { types::Typedef("value", ValueType::String) } },
1✔
1290
                                    types::Contract { { types::Typedef("value", ValueType::Dict) } } } },
1✔
1291
                                { *a });
1✔
1292
                        DISPATCH();
4,007✔
1293
                    }
637✔
1294

1295
                    TARGET(IS_EMPTY)
1296
                    {
1297
                        const Value* a = popAndResolveAsPtr(context);
637✔
1298

1299
                        if (a->valueType() == ValueType::List)
637✔
1300
                            push(a->constList().empty() ? Builtins::trueSym : Builtins::falseSym, context);
129✔
1301
                        else if (a->valueType() == ValueType::String)
508✔
1302
                            push(a->string().empty() ? Builtins::trueSym : Builtins::falseSym, context);
500✔
1303
                        else if (a->valueType() == ValueType::Dict)
8✔
1304
                            push(std::cmp_equal(a->dict().size(), 0) ? Builtins::trueSym : Builtins::falseSym, context);
4✔
1305
                        else if (a->valueType() == ValueType::Nil)
4✔
1306
                            push(Builtins::trueSym, context);
3✔
1307
                        else
1308
                            throw types::TypeCheckingError(
2✔
1309
                                "empty?",
1✔
1310
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
4✔
1311
                                    types::Contract { { types::Typedef("value", ValueType::Nil) } },
1✔
1312
                                    types::Contract { { types::Typedef("value", ValueType::String) } },
1✔
1313
                                    types::Contract { { types::Typedef("value", ValueType::Dict) } } } },
1✔
1314
                                { *a });
1✔
1315
                        DISPATCH();
636✔
1316
                    }
342✔
1317

1318
                    TARGET(TAIL)
1319
                    {
1320
                        Value* const a = popAndResolveAsPtr(context);
342✔
1321
                        push(helper::tail(a), context);
342✔
1322
                        DISPATCH();
341✔
1323
                    }
1,134✔
1324

1325
                    TARGET(HEAD)
1326
                    {
1327
                        Value* const a = popAndResolveAsPtr(context);
1,134✔
1328
                        push(helper::head(a), context);
1,134✔
1329
                        DISPATCH();
1,133✔
1330
                    }
2,394✔
1331

1332
                    TARGET(IS_NIL)
1333
                    {
1334
                        const Value* a = popAndResolveAsPtr(context);
2,394✔
1335
                        push((*a == Builtins::nil) ? Builtins::trueSym : Builtins::falseSym, context);
2,394✔
1336
                        DISPATCH();
2,394✔
1337
                    }
17✔
1338

1339
                    TARGET(TO_NUM)
1340
                    {
1341
                        const Value* a = popAndResolveAsPtr(context);
17✔
1342

1343
                        if (a->valueType() != ValueType::String)
17✔
1344
                            throw types::TypeCheckingError(
2✔
1345
                                "toNumber",
1✔
1346
                                { { types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1347
                                { *a });
1✔
1348

1349
                        double val;
1350
                        if (Utils::isDouble(a->string(), &val))
16✔
1351
                            push(Value(val), context);
12✔
1352
                        else
1353
                            push(Builtins::nil, context);
4✔
1354
                        DISPATCH();
16✔
1355
                    }
161✔
1356

1357
                    TARGET(TO_STR)
1358
                    {
1359
                        const Value* a = popAndResolveAsPtr(context);
161✔
1360
                        push(Value(a->toString(*this)), context);
161✔
1361
                        DISPATCH();
161✔
1362
                    }
745✔
1363

1364
                    TARGET(AT)
1365
                    {
1366
                        Value& b = *popAndResolveAsPtr(context);
745✔
1367
                        Value& a = *popAndResolveAsPtr(context);
745✔
1368
                        push(helper::at(a, b, *this), context);
745✔
1369
                        DISPATCH();
743✔
1370
                    }
892✔
1371

1372
                    TARGET(AT_AT)
1373
                    {
1374
                        {
1375
                            const Value* x = popAndResolveAsPtr(context);
892✔
1376
                            const Value* y = popAndResolveAsPtr(context);
892✔
1377
                            Value& list = *popAndResolveAsPtr(context);
892✔
1378

1379
                            push(helper::atAt(x, y, list), context);
892✔
1380
                        }
1381
                        DISPATCH();
887✔
1382
                    }
16,750✔
1383

1384
                    TARGET(MOD)
1385
                    {
1386
                        const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
16,750✔
1387
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
16,750✔
1388
                            throw types::TypeCheckingError(
2✔
1389
                                "mod",
1✔
1390
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1391
                                { *a, *b });
1✔
1392
                        push(Value(std::fmod(a->number(), b->number())), context);
16,749✔
1393
                        DISPATCH();
16,749✔
1394
                    }
29✔
1395

1396
                    TARGET(TYPE)
1397
                    {
1398
                        const Value* a = popAndResolveAsPtr(context);
29✔
1399
                        push(Value(std::to_string(a->valueType())), context);
29✔
1400
                        DISPATCH();
29✔
1401
                    }
3✔
1402

1403
                    TARGET(HAS_FIELD)
1404
                    {
1405
                        {
1406
                            Value* const field = popAndResolveAsPtr(context);
3✔
1407
                            Value* const closure = popAndResolveAsPtr(context);
3✔
1408
                            if (closure->valueType() != ValueType::Closure || field->valueType() != ValueType::String)
3✔
1409
                                throw types::TypeCheckingError(
2✔
1410
                                    "hasField",
1✔
1411
                                    { { types::Contract { { types::Typedef("closure", ValueType::Closure), types::Typedef("field", ValueType::String) } } } },
1✔
1412
                                    { *closure, *field });
1✔
1413

1414
                            auto it = std::ranges::find(m_state.m_symbols, field->stringRef());
2✔
1415
                            if (it == m_state.m_symbols.end())
2✔
1416
                            {
1417
                                push(Builtins::falseSym, context);
1✔
1418
                                DISPATCH();
1✔
1419
                            }
1420

1421
                            auto id = static_cast<std::uint16_t>(std::distance(m_state.m_symbols.begin(), it));
1✔
1422
                            push(closure->refClosure().refScope()[id] != nullptr ? Builtins::trueSym : Builtins::falseSym, context);
1✔
1423
                        }
1424
                        DISPATCH();
1✔
1425
                    }
3,711✔
1426

1427
                    TARGET(NOT)
1428
                    {
1429
                        const Value* a = popAndResolveAsPtr(context);
3,711✔
1430
                        push(!(*a) ? Builtins::trueSym : Builtins::falseSym, context);
3,711✔
1431
                        DISPATCH();
3,711✔
1432
                    }
8,137✔
1433

1434
#pragma endregion
1435

1436
#pragma region "Super Instructions"
1437
                    TARGET(LOAD_CONST_LOAD_CONST)
1438
                    {
1439
                        UNPACK_ARGS();
8,137✔
1440
                        push(loadConstAsPtr(primary_arg), context);
8,137✔
1441
                        push(loadConstAsPtr(secondary_arg), context);
8,137✔
1442
                        context.inst_exec_counter++;
8,137✔
1443
                        DISPATCH();
8,137✔
1444
                    }
10,123✔
1445

1446
                    TARGET(LOAD_CONST_STORE)
1447
                    {
1448
                        UNPACK_ARGS();
10,123✔
1449
                        store(secondary_arg, loadConstAsPtr(primary_arg), context);
10,123✔
1450
                        DISPATCH();
10,123✔
1451
                    }
1,029✔
1452

1453
                    TARGET(LOAD_CONST_SET_VAL)
1454
                    {
1455
                        UNPACK_ARGS();
1,029✔
1456
                        setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
1,029✔
1457
                        DISPATCH();
1,028✔
1458
                    }
25✔
1459

1460
                    TARGET(STORE_FROM)
1461
                    {
1462
                        UNPACK_ARGS();
25✔
1463
                        store(secondary_arg, loadSymbol(primary_arg, context), context);
25✔
1464
                        DISPATCH();
24✔
1465
                    }
1,281✔
1466

1467
                    TARGET(STORE_FROM_INDEX)
1468
                    {
1469
                        UNPACK_ARGS();
1,281✔
1470
                        store(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
1,281✔
1471
                        DISPATCH();
1,281✔
1472
                    }
628✔
1473

1474
                    TARGET(SET_VAL_FROM)
1475
                    {
1476
                        UNPACK_ARGS();
628✔
1477
                        setVal(secondary_arg, loadSymbol(primary_arg, context), context);
628✔
1478
                        DISPATCH();
628✔
1479
                    }
622✔
1480

1481
                    TARGET(SET_VAL_FROM_INDEX)
1482
                    {
1483
                        UNPACK_ARGS();
622✔
1484
                        setVal(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
622✔
1485
                        DISPATCH();
622✔
1486
                    }
68✔
1487

1488
                    TARGET(INCREMENT)
1489
                    {
1490
                        UNPACK_ARGS();
68✔
1491
                        {
1492
                            Value* var = loadSymbol(primary_arg, context);
68✔
1493

1494
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1495
                            if (var->valueType() == ValueType::Reference)
68✔
UNCOV
1496
                                var = var->reference();
×
1497

1498
                            if (var->valueType() == ValueType::Number)
68✔
1499
                                push(Value(var->number() + secondary_arg), context);
67✔
1500
                            else
1501
                                throw types::TypeCheckingError(
2✔
1502
                                    "+",
1✔
1503
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1504
                                    { *var, Value(secondary_arg) });
1✔
1505
                        }
1506
                        DISPATCH();
67✔
1507
                    }
88,021✔
1508

1509
                    TARGET(INCREMENT_BY_INDEX)
1510
                    {
1511
                        UNPACK_ARGS();
88,021✔
1512
                        {
1513
                            Value* var = loadSymbolFromIndex(primary_arg, context);
88,021✔
1514

1515
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1516
                            if (var->valueType() == ValueType::Reference)
88,021✔
UNCOV
1517
                                var = var->reference();
×
1518

1519
                            if (var->valueType() == ValueType::Number)
88,021✔
1520
                                push(Value(var->number() + secondary_arg), context);
88,020✔
1521
                            else
1522
                                throw types::TypeCheckingError(
2✔
1523
                                    "+",
1✔
1524
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1525
                                    { *var, Value(secondary_arg) });
1✔
1526
                        }
1527
                        DISPATCH();
88,020✔
1528
                    }
33,816✔
1529

1530
                    TARGET(INCREMENT_STORE)
1531
                    {
1532
                        UNPACK_ARGS();
33,816✔
1533
                        {
1534
                            Value* var = loadSymbol(primary_arg, context);
33,816✔
1535

1536
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1537
                            if (var->valueType() == ValueType::Reference)
33,816✔
UNCOV
1538
                                var = var->reference();
×
1539

1540
                            if (var->valueType() == ValueType::Number)
33,816✔
1541
                            {
1542
                                auto val = Value(var->number() + secondary_arg);
33,815✔
1543
                                setVal(primary_arg, &val, context);
33,815✔
1544
                            }
33,815✔
1545
                            else
1546
                                throw types::TypeCheckingError(
2✔
1547
                                    "+",
1✔
1548
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1549
                                    { *var, Value(secondary_arg) });
1✔
1550
                        }
1551
                        DISPATCH();
33,815✔
1552
                    }
3,474✔
1553

1554
                    TARGET(DECREMENT)
1555
                    {
1556
                        UNPACK_ARGS();
3,474✔
1557
                        {
1558
                            Value* var = loadSymbol(primary_arg, context);
3,474✔
1559

1560
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1561
                            if (var->valueType() == ValueType::Reference)
3,474✔
UNCOV
1562
                                var = var->reference();
×
1563

1564
                            if (var->valueType() == ValueType::Number)
3,474✔
1565
                                push(Value(var->number() - secondary_arg), context);
3,473✔
1566
                            else
1567
                                throw types::TypeCheckingError(
2✔
1568
                                    "-",
1✔
1569
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1570
                                    { *var, Value(secondary_arg) });
1✔
1571
                        }
1572
                        DISPATCH();
3,473✔
1573
                    }
194,422✔
1574

1575
                    TARGET(DECREMENT_BY_INDEX)
1576
                    {
1577
                        UNPACK_ARGS();
194,422✔
1578
                        {
1579
                            Value* var = loadSymbolFromIndex(primary_arg, context);
194,422✔
1580

1581
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1582
                            if (var->valueType() == ValueType::Reference)
194,422✔
UNCOV
1583
                                var = var->reference();
×
1584

1585
                            if (var->valueType() == ValueType::Number)
194,422✔
1586
                                push(Value(var->number() - secondary_arg), context);
194,421✔
1587
                            else
1588
                                throw types::TypeCheckingError(
2✔
1589
                                    "-",
1✔
1590
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1591
                                    { *var, Value(secondary_arg) });
1✔
1592
                        }
1593
                        DISPATCH();
194,421✔
1594
                    }
866✔
1595

1596
                    TARGET(DECREMENT_STORE)
1597
                    {
1598
                        UNPACK_ARGS();
866✔
1599
                        {
1600
                            Value* var = loadSymbol(primary_arg, context);
866✔
1601

1602
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1603
                            if (var->valueType() == ValueType::Reference)
866✔
UNCOV
1604
                                var = var->reference();
×
1605

1606
                            if (var->valueType() == ValueType::Number)
866✔
1607
                            {
1608
                                auto val = Value(var->number() - secondary_arg);
865✔
1609
                                setVal(primary_arg, &val, context);
865✔
1610
                            }
865✔
1611
                            else
1612
                                throw types::TypeCheckingError(
2✔
1613
                                    "-",
1✔
1614
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1615
                                    { *var, Value(secondary_arg) });
1✔
1616
                        }
1617
                        DISPATCH();
865✔
1618
                    }
1✔
1619

1620
                    TARGET(STORE_TAIL)
1621
                    {
1622
                        UNPACK_ARGS();
1✔
1623
                        {
1624
                            Value* list = loadSymbol(primary_arg, context);
1✔
1625
                            Value tail = helper::tail(list);
1✔
1626
                            store(secondary_arg, &tail, context);
1✔
1627
                        }
1✔
1628
                        DISPATCH();
1✔
1629
                    }
8✔
1630

1631
                    TARGET(STORE_TAIL_BY_INDEX)
1632
                    {
1633
                        UNPACK_ARGS();
8✔
1634
                        {
1635
                            Value* list = loadSymbolFromIndex(primary_arg, context);
8✔
1636
                            Value tail = helper::tail(list);
8✔
1637
                            store(secondary_arg, &tail, context);
8✔
1638
                        }
8✔
1639
                        DISPATCH();
8✔
1640
                    }
4✔
1641

1642
                    TARGET(STORE_HEAD)
1643
                    {
1644
                        UNPACK_ARGS();
4✔
1645
                        {
1646
                            Value* list = loadSymbol(primary_arg, context);
4✔
1647
                            Value head = helper::head(list);
4✔
1648
                            store(secondary_arg, &head, context);
4✔
1649
                        }
4✔
1650
                        DISPATCH();
4✔
1651
                    }
38✔
1652

1653
                    TARGET(STORE_HEAD_BY_INDEX)
1654
                    {
1655
                        UNPACK_ARGS();
38✔
1656
                        {
1657
                            Value* list = loadSymbolFromIndex(primary_arg, context);
38✔
1658
                            Value head = helper::head(list);
38✔
1659
                            store(secondary_arg, &head, context);
38✔
1660
                        }
38✔
1661
                        DISPATCH();
38✔
1662
                    }
1,293✔
1663

1664
                    TARGET(STORE_LIST)
1665
                    {
1666
                        UNPACK_ARGS();
1,293✔
1667
                        {
1668
                            Value l = createList(primary_arg, context);
1,293✔
1669
                            store(secondary_arg, &l, context);
1,293✔
1670
                        }
1,293✔
1671
                        DISPATCH();
1,293✔
1672
                    }
3✔
1673

1674
                    TARGET(SET_VAL_TAIL)
1675
                    {
1676
                        UNPACK_ARGS();
3✔
1677
                        {
1678
                            Value* list = loadSymbol(primary_arg, context);
3✔
1679
                            Value tail = helper::tail(list);
3✔
1680
                            setVal(secondary_arg, &tail, context);
3✔
1681
                        }
3✔
1682
                        DISPATCH();
3✔
1683
                    }
1✔
1684

1685
                    TARGET(SET_VAL_TAIL_BY_INDEX)
1686
                    {
1687
                        UNPACK_ARGS();
1✔
1688
                        {
1689
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1690
                            Value tail = helper::tail(list);
1✔
1691
                            setVal(secondary_arg, &tail, context);
1✔
1692
                        }
1✔
1693
                        DISPATCH();
1✔
1694
                    }
1✔
1695

1696
                    TARGET(SET_VAL_HEAD)
1697
                    {
1698
                        UNPACK_ARGS();
1✔
1699
                        {
1700
                            Value* list = loadSymbol(primary_arg, context);
1✔
1701
                            Value head = helper::head(list);
1✔
1702
                            setVal(secondary_arg, &head, context);
1✔
1703
                        }
1✔
1704
                        DISPATCH();
1✔
1705
                    }
1✔
1706

1707
                    TARGET(SET_VAL_HEAD_BY_INDEX)
1708
                    {
1709
                        UNPACK_ARGS();
1✔
1710
                        {
1711
                            Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1712
                            Value head = helper::head(list);
1✔
1713
                            setVal(secondary_arg, &head, context);
1✔
1714
                        }
1✔
1715
                        DISPATCH();
1✔
1716
                    }
1,802✔
1717

1718
                    TARGET(CALL_BUILTIN)
1719
                    {
1720
                        UNPACK_ARGS();
1,802✔
1721
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1722
                        callBuiltin(
3,604✔
1723
                            context,
1,802✔
1724
                            Builtins::builtins[primary_arg].second,
1,802✔
1725
                            secondary_arg,
1,802✔
1726
                            /* remove_return_address= */ true,
1727
                            /* remove_builtin= */ false);
1728
                        if (!m_running)
1,719✔
UNCOV
1729
                            GOTO_HALT();
×
1730
                        DISPATCH();
1,719✔
1731
                    }
11,709✔
1732

1733
                    TARGET(CALL_BUILTIN_WITHOUT_RETURN_ADDRESS)
1734
                    {
1735
                        UNPACK_ARGS();
11,709✔
1736
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1737
                        callBuiltin(
23,418✔
1738
                            context,
11,709✔
1739
                            Builtins::builtins[primary_arg].second,
11,709✔
1740
                            secondary_arg,
11,709✔
1741
                            // we didn't have a PUSH_RETURN_ADDRESS instruction before,
1742
                            // so do not attempt to remove (pp,ip) from the stack: they're not there!
1743
                            /* remove_return_address= */ false,
1744
                            /* remove_builtin= */ false);
1745
                        if (!m_running)
11,708✔
UNCOV
1746
                            GOTO_HALT();
×
1747
                        DISPATCH();
11,708✔
1748
                    }
877✔
1749

1750
                    TARGET(LT_CONST_JUMP_IF_FALSE)
1751
                    {
1752
                        UNPACK_ARGS();
877✔
1753
                        const Value* sym = popAndResolveAsPtr(context);
877✔
1754
                        if (!(*sym < *loadConstAsPtr(primary_arg)))
877✔
1755
                            jump(secondary_arg, context);
124✔
1756
                        DISPATCH();
877✔
1757
                    }
21,988✔
1758

1759
                    TARGET(LT_CONST_JUMP_IF_TRUE)
1760
                    {
1761
                        UNPACK_ARGS();
21,988✔
1762
                        const Value* sym = popAndResolveAsPtr(context);
21,988✔
1763
                        if (*sym < *loadConstAsPtr(primary_arg))
21,988✔
1764
                            jump(secondary_arg, context);
10,960✔
1765
                        DISPATCH();
21,988✔
1766
                    }
7,347✔
1767

1768
                    TARGET(LT_SYM_JUMP_IF_FALSE)
1769
                    {
1770
                        UNPACK_ARGS();
7,347✔
1771
                        const Value* sym = popAndResolveAsPtr(context);
7,347✔
1772
                        if (!(*sym < *loadSymbol(primary_arg, context)))
7,347✔
1773
                            jump(secondary_arg, context);
718✔
1774
                        DISPATCH();
7,347✔
1775
                    }
172,535✔
1776

1777
                    TARGET(GT_CONST_JUMP_IF_TRUE)
1778
                    {
1779
                        UNPACK_ARGS();
172,535✔
1780
                        const Value* sym = popAndResolveAsPtr(context);
172,535✔
1781
                        const Value* cst = loadConstAsPtr(primary_arg);
172,535✔
1782
                        if (*cst < *sym)
172,535✔
1783
                            jump(secondary_arg, context);
86,604✔
1784
                        DISPATCH();
172,535✔
1785
                    }
187✔
1786

1787
                    TARGET(GT_CONST_JUMP_IF_FALSE)
1788
                    {
1789
                        UNPACK_ARGS();
187✔
1790
                        const Value* sym = popAndResolveAsPtr(context);
187✔
1791
                        const Value* cst = loadConstAsPtr(primary_arg);
187✔
1792
                        if (!(*cst < *sym))
187✔
1793
                            jump(secondary_arg, context);
42✔
1794
                        DISPATCH();
187✔
1795
                    }
6✔
1796

1797
                    TARGET(GT_SYM_JUMP_IF_FALSE)
1798
                    {
1799
                        UNPACK_ARGS();
6✔
1800
                        const Value* sym = popAndResolveAsPtr(context);
6✔
1801
                        const Value* rhs = loadSymbol(primary_arg, context);
6✔
1802
                        if (!(*rhs < *sym))
6✔
1803
                            jump(secondary_arg, context);
1✔
1804
                        DISPATCH();
6✔
1805
                    }
1,099✔
1806

1807
                    TARGET(EQ_CONST_JUMP_IF_TRUE)
1808
                    {
1809
                        UNPACK_ARGS();
1,099✔
1810
                        const Value* sym = popAndResolveAsPtr(context);
1,099✔
1811
                        if (*sym == *loadConstAsPtr(primary_arg))
1,099✔
1812
                            jump(secondary_arg, context);
41✔
1813
                        DISPATCH();
1,099✔
1814
                    }
87,353✔
1815

1816
                    TARGET(EQ_SYM_INDEX_JUMP_IF_TRUE)
1817
                    {
1818
                        UNPACK_ARGS();
87,353✔
1819
                        const Value* sym = popAndResolveAsPtr(context);
87,353✔
1820
                        if (*sym == *loadSymbolFromIndex(primary_arg, context))
87,353✔
1821
                            jump(secondary_arg, context);
549✔
1822
                        DISPATCH();
87,353✔
1823
                    }
11✔
1824

1825
                    TARGET(NEQ_CONST_JUMP_IF_TRUE)
1826
                    {
1827
                        UNPACK_ARGS();
11✔
1828
                        const Value* sym = popAndResolveAsPtr(context);
11✔
1829
                        if (*sym != *loadConstAsPtr(primary_arg))
11✔
1830
                            jump(secondary_arg, context);
2✔
1831
                        DISPATCH();
11✔
1832
                    }
30✔
1833

1834
                    TARGET(NEQ_SYM_JUMP_IF_FALSE)
1835
                    {
1836
                        UNPACK_ARGS();
30✔
1837
                        const Value* sym = popAndResolveAsPtr(context);
30✔
1838
                        if (*sym == *loadSymbol(primary_arg, context))
30✔
1839
                            jump(secondary_arg, context);
10✔
1840
                        DISPATCH();
30✔
1841
                    }
28,225✔
1842

1843
                    TARGET(CALL_SYMBOL)
1844
                    {
1845
                        UNPACK_ARGS();
28,225✔
1846
                        call(context, secondary_arg, /* function_ptr= */ loadSymbol(primary_arg, context));
28,225✔
1847
                        if (!m_running)
28,223✔
UNCOV
1848
                            GOTO_HALT();
×
1849
                        DISPATCH();
28,223✔
1850
                    }
1,100✔
1851

1852
                    TARGET(CALL_SYMBOL_BY_INDEX)
1853
                    {
1854
                        UNPACK_ARGS();
1,100✔
1855
                        call(context, secondary_arg, /* function_ptr= */ loadSymbolFromIndex(primary_arg, context));
1,100✔
1856
                        if (!m_running)
1,095✔
NEW
1857
                            GOTO_HALT();
×
1858
                        DISPATCH();
1,095✔
1859
                    }
109,875✔
1860

1861
                    TARGET(CALL_CURRENT_PAGE)
1862
                    {
1863
                        UNPACK_ARGS();
109,875✔
1864
                        context.last_symbol = primary_arg;
109,875✔
1865
                        call(context, secondary_arg, /* function_ptr= */ nullptr, /* or_address= */ static_cast<PageAddr_t>(context.pp));
109,875✔
1866
                        if (!m_running)
109,874✔
UNCOV
1867
                            GOTO_HALT();
×
1868
                        DISPATCH();
109,874✔
1869
                    }
1,638✔
1870

1871
                    TARGET(GET_FIELD_FROM_SYMBOL)
1872
                    {
1873
                        UNPACK_ARGS();
1,638✔
1874
                        push(getField(loadSymbol(primary_arg, context), secondary_arg, context), context);
1,638✔
1875
                        DISPATCH();
1,638✔
1876
                    }
312✔
1877

1878
                    TARGET(GET_FIELD_FROM_SYMBOL_INDEX)
1879
                    {
1880
                        UNPACK_ARGS();
312✔
1881
                        push(getField(loadSymbolFromIndex(primary_arg, context), secondary_arg, context), context);
312✔
1882
                        DISPATCH();
310✔
1883
                    }
16,138✔
1884

1885
                    TARGET(AT_SYM_SYM)
1886
                    {
1887
                        UNPACK_ARGS();
16,138✔
1888
                        push(helper::at(*loadSymbol(primary_arg, context), *loadSymbol(secondary_arg, context), *this), context);
16,138✔
1889
                        DISPATCH();
16,138✔
1890
                    }
49✔
1891

1892
                    TARGET(AT_SYM_INDEX_SYM_INDEX)
1893
                    {
1894
                        UNPACK_ARGS();
49✔
1895
                        push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadSymbolFromIndex(secondary_arg, context), *this), context);
49✔
1896
                        DISPATCH();
49✔
1897
                    }
2,700✔
1898

1899
                    TARGET(AT_SYM_INDEX_CONST)
1900
                    {
1901
                        UNPACK_ARGS();
2,700✔
1902
                        push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadConstAsPtr(secondary_arg), *this), context);
2,700✔
1903
                        DISPATCH();
2,697✔
1904
                    }
2✔
1905

1906
                    TARGET(CHECK_TYPE_OF)
1907
                    {
1908
                        UNPACK_ARGS();
2✔
1909
                        const Value* sym = loadSymbol(primary_arg, context);
2✔
1910
                        const Value* cst = loadConstAsPtr(secondary_arg);
2✔
1911
                        push(
2✔
1912
                            cst->valueType() == ValueType::String &&
4✔
1913
                                    std::to_string(sym->valueType()) == cst->string()
2✔
1914
                                ? Builtins::trueSym
1915
                                : Builtins::falseSym,
1916
                            context);
2✔
1917
                        DISPATCH();
2✔
1918
                    }
131✔
1919

1920
                    TARGET(CHECK_TYPE_OF_BY_INDEX)
1921
                    {
1922
                        UNPACK_ARGS();
131✔
1923
                        const Value* sym = loadSymbolFromIndex(primary_arg, context);
131✔
1924
                        const Value* cst = loadConstAsPtr(secondary_arg);
131✔
1925
                        push(
131✔
1926
                            cst->valueType() == ValueType::String &&
262✔
1927
                                    std::to_string(sym->valueType()) == cst->string()
131✔
1928
                                ? Builtins::trueSym
1929
                                : Builtins::falseSym,
1930
                            context);
131✔
1931
                        DISPATCH();
131✔
1932
                    }
3,510✔
1933

1934
                    TARGET(APPEND_IN_PLACE_SYM)
1935
                    {
1936
                        UNPACK_ARGS();
3,510✔
1937
                        listAppendInPlace(loadSymbol(primary_arg, context), secondary_arg, context);
3,510✔
1938
                        DISPATCH();
3,510✔
1939
                    }
14✔
1940

1941
                    TARGET(APPEND_IN_PLACE_SYM_INDEX)
1942
                    {
1943
                        UNPACK_ARGS();
14✔
1944
                        listAppendInPlace(loadSymbolFromIndex(primary_arg, context), secondary_arg, context);
14✔
1945
                        DISPATCH();
13✔
1946
                    }
123✔
1947

1948
                    TARGET(STORE_LEN)
1949
                    {
1950
                        UNPACK_ARGS();
123✔
1951
                        {
1952
                            Value* a = loadSymbolFromIndex(primary_arg, context);
123✔
1953
                            Value len;
123✔
1954
                            if (a->valueType() == ValueType::List)
123✔
1955
                                len = Value(static_cast<int>(a->constList().size()));
43✔
1956
                            else if (a->valueType() == ValueType::String)
80✔
1957
                                len = Value(static_cast<int>(a->string().size()));
79✔
1958
                            else
1959
                                throw types::TypeCheckingError(
2✔
1960
                                    "len",
1✔
1961
                                    { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1962
                                        types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1963
                                    { *a });
1✔
1964
                            store(secondary_arg, &len, context);
122✔
1965
                        }
123✔
1966
                        DISPATCH();
122✔
1967
                    }
9,247✔
1968

1969
                    TARGET(LT_LEN_SYM_JUMP_IF_FALSE)
1970
                    {
1971
                        UNPACK_ARGS();
9,247✔
1972
                        {
1973
                            const Value* sym = loadSymbol(primary_arg, context);
9,247✔
1974
                            Value size;
9,247✔
1975

1976
                            if (sym->valueType() == ValueType::List)
9,247✔
1977
                                size = Value(static_cast<int>(sym->constList().size()));
3,576✔
1978
                            else if (sym->valueType() == ValueType::String)
5,671✔
1979
                                size = Value(static_cast<int>(sym->string().size()));
5,670✔
1980
                            else
1981
                                throw types::TypeCheckingError(
2✔
1982
                                    "len",
1✔
1983
                                    { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
1984
                                        types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1985
                                    { *sym });
1✔
1986

1987
                            if (!(*popAndResolveAsPtr(context) < size))
9,246✔
1988
                                jump(secondary_arg, context);
1,213✔
1989
                        }
9,247✔
1990
                        DISPATCH();
9,246✔
1991
                    }
521✔
1992

1993
                    TARGET(MUL_BY)
1994
                    {
1995
                        UNPACK_ARGS();
521✔
1996
                        {
1997
                            Value* var = loadSymbol(primary_arg, context);
521✔
1998
                            const int other = static_cast<int>(secondary_arg) - 2048;
521✔
1999

2000
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
2001
                            if (var->valueType() == ValueType::Reference)
521✔
UNCOV
2002
                                var = var->reference();
×
2003

2004
                            if (var->valueType() == ValueType::Number)
521✔
2005
                                push(Value(var->number() * other), context);
520✔
2006
                            else
2007
                                throw types::TypeCheckingError(
2✔
2008
                                    "*",
1✔
2009
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2010
                                    { *var, Value(other) });
1✔
2011
                        }
2012
                        DISPATCH();
520✔
2013
                    }
36✔
2014

2015
                    TARGET(MUL_BY_INDEX)
2016
                    {
2017
                        UNPACK_ARGS();
36✔
2018
                        {
2019
                            Value* var = loadSymbolFromIndex(primary_arg, context);
36✔
2020
                            const int other = static_cast<int>(secondary_arg) - 2048;
36✔
2021

2022
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
2023
                            if (var->valueType() == ValueType::Reference)
36✔
UNCOV
2024
                                var = var->reference();
×
2025

2026
                            if (var->valueType() == ValueType::Number)
36✔
2027
                                push(Value(var->number() * other), context);
35✔
2028
                            else
2029
                                throw types::TypeCheckingError(
2✔
2030
                                    "*",
1✔
2031
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2032
                                    { *var, Value(other) });
1✔
2033
                        }
2034
                        DISPATCH();
35✔
2035
                    }
2✔
2036

2037
                    TARGET(MUL_SET_VAL)
2038
                    {
2039
                        UNPACK_ARGS();
2✔
2040
                        {
2041
                            Value* var = loadSymbol(primary_arg, context);
2✔
2042
                            const int other = static_cast<int>(secondary_arg) - 2048;
2✔
2043

2044
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
2045
                            if (var->valueType() == ValueType::Reference)
2✔
UNCOV
2046
                                var = var->reference();
×
2047

2048
                            if (var->valueType() == ValueType::Number)
2✔
2049
                            {
2050
                                auto val = Value(var->number() * other);
1✔
2051
                                setVal(primary_arg, &val, context);
1✔
2052
                            }
1✔
2053
                            else
2054
                                throw types::TypeCheckingError(
2✔
2055
                                    "*",
1✔
2056
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2057
                                    { *var, Value(other) });
1✔
2058
                        }
2059
                        DISPATCH();
1✔
2060
                    }
1,110✔
2061

2062
                    TARGET(FUSED_MATH)
2063
                    {
2064
                        const auto op1 = static_cast<Instruction>(padding),
1,110✔
2065
                                   op2 = static_cast<Instruction>((arg & 0xff00) >> 8),
1,110✔
2066
                                   op3 = static_cast<Instruction>(arg & 0x00ff);
1,110✔
2067
                        const std::size_t arg_count = (op1 != NOP) + (op2 != NOP) + (op3 != NOP);
1,110✔
2068

2069
                        const Value* d = popAndResolveAsPtr(context);
1,110✔
2070
                        const Value* c = popAndResolveAsPtr(context);
1,110✔
2071
                        const Value* b = popAndResolveAsPtr(context);
1,110✔
2072

2073
                        if (d->valueType() != ValueType::Number || c->valueType() != ValueType::Number)
1,110✔
2074
                            throw types::TypeCheckingError(
2✔
2075
                                helper::mathInstToStr(op1),
1✔
2076
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2077
                                { *c, *d });
1✔
2078

2079
                        double temp = helper::doMath(c->number(), d->number(), op1);
1,109✔
2080
                        if (b->valueType() != ValueType::Number)
1,109✔
2081
                            throw types::TypeCheckingError(
4✔
2082
                                helper::mathInstToStr(op2),
2✔
2083
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
2✔
2084
                                { *b, Value(temp) });
2✔
2085
                        temp = helper::doMath(b->number(), temp, op2);
1,107✔
2086

2087
                        if (arg_count == 2)
1,106✔
2088
                            push(Value(temp), context);
1,069✔
2089
                        else if (arg_count == 3)
37✔
2090
                        {
2091
                            const Value* a = popAndResolveAsPtr(context);
37✔
2092
                            if (a->valueType() != ValueType::Number)
37✔
2093
                                throw types::TypeCheckingError(
2✔
2094
                                    helper::mathInstToStr(op3),
1✔
2095
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2096
                                    { *a, Value(temp) });
1✔
2097

2098
                            temp = helper::doMath(a->number(), temp, op3);
36✔
2099
                            push(Value(temp), context);
36✔
2100
                        }
36✔
2101
                        else
UNCOV
2102
                            throw Error(
×
UNCOV
2103
                                fmt::format(
×
UNCOV
2104
                                    "FUSED_MATH got {} arguments, expected 2 or 3. Arguments: {:x}{:x}{:x}. There is a bug in the codegen!",
×
UNCOV
2105
                                    arg_count, static_cast<uint8_t>(op1), static_cast<uint8_t>(op2), static_cast<uint8_t>(op3)));
×
2106
                        DISPATCH();
1,105✔
2107
                    }
2108
#pragma endregion
2109
                }
111✔
2110
#if ARK_USE_COMPUTED_GOTOS
2111
            dispatch_end:
2112
                do
111✔
2113
                {
2114
                } while (false);
111✔
2115
#endif
2116
            }
2117
        }
312✔
2118
        catch (const Error& e)
2119
        {
2120
            if (fail_with_exception)
140✔
2121
            {
2122
                std::stringstream stream;
139✔
2123
                backtrace(context, stream, /* colorize= */ false);
139✔
2124
                // It's important we have an Ark::Error here, as the constructor for NestedError
2125
                // does more than just aggregate error messages, hence the code duplication.
2126
                throw NestedError(e, stream.str(), *this);
139✔
2127
            }
139✔
2128
            else
2129
                showBacktraceWithException(Error(e.details(/* colorize= */ true, *this)), context);
1✔
2130
        }
251✔
2131
        catch (const std::exception& e)
2132
        {
2133
            if (fail_with_exception)
61✔
2134
            {
2135
                std::stringstream stream;
61✔
2136
                backtrace(context, stream, /* colorize= */ false);
61✔
2137
                throw NestedError(e, stream.str());
61✔
2138
            }
61✔
2139
            else
UNCOV
2140
                showBacktraceWithException(e, context);
×
2141
        }
201✔
2142
        catch (...)
2143
        {
UNCOV
2144
            if (fail_with_exception)
×
UNCOV
2145
                throw;
×
2146

2147
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2148
            throw;
2149
#endif
2150
            fmt::println("Unknown error");
×
2151
            backtrace(context);
×
2152
            m_exit_code = 1;
×
2153
        }
261✔
2154

2155
        return m_exit_code;
112✔
2156
    }
403✔
2157

2158
    uint16_t VM::findNearestVariableIdWithValue(const Value& value, ExecutionContext& context) const noexcept
2,056✔
2159
    {
2,056✔
2160
        for (auto& local : std::ranges::reverse_view(context.locals))
2,098,202✔
2161
        {
2162
            if (const auto id = local.idFromValue(value); id < m_state.m_symbols.size())
2,096,146✔
2163
                return id;
2,050✔
2164
        }
2,096,146✔
2165
        return MaxValue16Bits;
6✔
2166
    }
2,056✔
2167

2168
    void VM::throwArityError(std::size_t passed_arg_count, std::size_t expected_arg_count, ExecutionContext& context, const bool skip_function)
7✔
2169
    {
7✔
2170
        std::vector<std::string> arg_names;
7✔
2171
        arg_names.reserve(expected_arg_count + 1);
7✔
2172

2173
        std::size_t index = 0;
7✔
2174
        while (m_state.inst(context.pp, index) == STORE ||
14✔
2175
               m_state.inst(context.pp, index) == STORE_REF)
7✔
2176
        {
UNCOV
2177
            const auto id = static_cast<uint16_t>((m_state.inst(context.pp, index + 2) << 8) + m_state.inst(context.pp, index + 3));
×
UNCOV
2178
            arg_names.insert(arg_names.begin(), m_state.m_symbols[id]);
×
UNCOV
2179
            index += 4;
×
UNCOV
2180
        }
×
2181
        // we have no arg names, probably because of a CALL_BUILTIN_WITHOUT_RETURN_ADDRESS
2182
        if (arg_names.empty() && index == 0)
7✔
2183
        {
2184
            for (std::size_t i = 0; i < expected_arg_count; ++i)
5✔
2185
                arg_names.emplace_back(1, static_cast<char>('a' + i));
2✔
2186
        }
3✔
2187
        if (expected_arg_count > 0)
7✔
2188
            arg_names.insert(arg_names.begin(), "");  // for formatting, so that we have a space between the function and the args
6✔
2189

2190
        std::vector<std::string> arg_vals;
7✔
2191
        arg_vals.reserve(passed_arg_count + 1);
7✔
2192

2193
        for (std::size_t i = 0; i < passed_arg_count && i + 1 <= context.sp; ++i)
20✔
2194
            // -1 on the stack because we always point to the next available slot
2195
            arg_vals.push_back(context.stack[context.sp - passed_arg_count + i].toString(*this));
13✔
2196
        if (passed_arg_count > 0)
7✔
2197
            arg_vals.insert(arg_vals.begin(), "");  // for formatting, so that we have a space between the function and the args
6✔
2198

2199
        // set ip/pp to the callee location so that the error can pinpoint the line
2200
        // where the bad call happened
2201
        if (context.sp >= 2 + passed_arg_count)
7✔
2202
        {
2203
            // -2/-3 instead of -1/-2 to skip over the function pushed on the stack
2204
            context.ip = context.stack[context.sp - 1 - (skip_function ? 1 : 0) - passed_arg_count].pageAddr();
7✔
2205
            context.pp = context.stack[context.sp - 2 - (skip_function ? 1 : 0) - passed_arg_count].pageAddr();
7✔
2206
            context.sp -= 2;
7✔
2207
            returnFromFuncCall(context);
7✔
2208
        }
7✔
2209

2210
        std::string function_name = (context.last_symbol < m_state.m_symbols.size())
14✔
2211
            ? m_state.m_symbols[context.last_symbol]
7✔
UNCOV
2212
            : Value(static_cast<PageAddr_t>(context.pp)).toString(*this);
×
2213

2214
        throwVMError(
7✔
2215
            ErrorKind::Arity,
2216
            fmt::format(
14✔
2217
                "When calling `({}{})', received {} argument{}, but expected {}: `({}{})'",
7✔
2218
                function_name,
2219
                fmt::join(arg_vals, " "),
7✔
2220
                passed_arg_count,
2221
                passed_arg_count > 1 ? "s" : "",
7✔
2222
                expected_arg_count,
2223
                function_name,
2224
                fmt::join(arg_names, " ")));
7✔
2225
    }
14✔
2226

2227
    void VM::initDebugger(ExecutionContext& context)
13✔
2228
    {
13✔
2229
        if (!m_debugger)
13✔
UNCOV
2230
            m_debugger = std::make_unique<Debugger>(context, m_state.m_libenv, m_state.m_symbols, m_state.m_constants);
×
2231
        else
2232
            m_debugger->saveState(context);
13✔
2233
    }
13✔
2234

2235
    void VM::showBacktraceWithException(const std::exception& e, ExecutionContext& context)
1✔
2236
    {
1✔
2237
        std::string text = e.what();
1✔
2238
        if (!text.empty() && text.back() != '\n')
1✔
UNCOV
2239
            text += '\n';
×
2240
        fmt::println(std::cerr, "{}", text);
1✔
2241

2242
        // If code being run from the debugger crashed, ignore it and don't trigger a debugger inside the VM inside the debugger inside the VM
2243
        const bool error_from_debugger = m_debugger && m_debugger->isRunning();
1✔
2244
        if (m_state.m_features & FeatureVMDebugger && !error_from_debugger)
1✔
2245
            initDebugger(context);
1✔
2246

2247
        const std::size_t saved_ip = context.ip;
1✔
2248
        const std::size_t saved_pp = context.pp;
1✔
2249
        const uint16_t saved_sp = context.sp;
1✔
2250

2251
        backtrace(context);
1✔
2252

2253
        fmt::println(
1✔
2254
            std::cerr,
2255
            "At IP: {}, PP: {}, SP: {}",
1✔
2256
            // dividing by 4 because the instructions are actually on 4 bytes
2257
            fmt::styled(saved_ip / 4, fmt::fg(fmt::color::cyan)),
1✔
2258
            fmt::styled(saved_pp, fmt::fg(fmt::color::green)),
1✔
2259
            fmt::styled(saved_sp, fmt::fg(fmt::color::yellow)));
1✔
2260

2261
        if (m_debugger && !error_from_debugger)
1✔
2262
        {
2263
            m_debugger->resetContextToSavedState(context);
1✔
2264
            m_debugger->run(*this, context, /* from_breakpoint= */ false);
1✔
2265
        }
1✔
2266

2267
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2268
        // don't report a "failed" exit code so that the fuzzers can more accurately triage crashes
2269
        m_exit_code = 0;
2270
#else
2271
        m_exit_code = 1;
1✔
2272
#endif
2273
    }
1✔
2274

2275
    std::optional<InstLoc> VM::findSourceLocation(const std::size_t ip, const std::size_t pp) const
2,278✔
2276
    {
2,278✔
2277
        std::optional<InstLoc> match = std::nullopt;
2,278✔
2278

2279
        for (const auto location : m_state.m_inst_locations)
11,233✔
2280
        {
2281
            if (location.page_pointer == pp && !match)
8,955✔
2282
                match = location;
2,278✔
2283

2284
            // select the best match: we want to find the location that's nearest our instruction pointer,
2285
            // but not equal to it as the IP will always be pointing to the next instruction,
2286
            // not yet executed. Thus, the erroneous instruction is the previous one.
2287
            if (location.page_pointer == pp && match && location.inst_pointer < ip / 4)
8,955✔
2288
                match = location;
2,481✔
2289

2290
            // early exit because we won't find anything better, as inst locations are ordered by ascending (pp, ip)
2291
            if (location.page_pointer > pp || (location.page_pointer == pp && location.inst_pointer >= ip / 4))
8,955✔
2292
                break;
2,087✔
2293
        }
8,955✔
2294

2295
        return match;
2,278✔
2296
    }
2297

UNCOV
2298
    std::string VM::debugShowSource() const
×
UNCOV
2299
    {
×
UNCOV
2300
        const auto& context = m_execution_contexts.front();
×
UNCOV
2301
        auto maybe_source_loc = findSourceLocation(context->ip, context->pp);
×
UNCOV
2302
        if (maybe_source_loc)
×
2303
        {
UNCOV
2304
            const auto filename = m_state.m_filenames[maybe_source_loc->filename_id];
×
UNCOV
2305
            return fmt::format("{}:{} -- IP: {}, PP: {}", filename, maybe_source_loc->line + 1, maybe_source_loc->inst_pointer, maybe_source_loc->page_pointer);
×
UNCOV
2306
        }
×
UNCOV
2307
        return "No source location found";
×
UNCOV
2308
    }
×
2309

2310
    void VM::backtrace(ExecutionContext& context, std::ostream& os, const bool colorize)
201✔
2311
    {
201✔
2312
        constexpr std::size_t max_consecutive_traces = 7;
201✔
2313

2314
        const auto maybe_location = findSourceLocation(context.ip, context.pp);
201✔
2315
        if (maybe_location)
201✔
2316
        {
2317
            const auto filename = m_state.m_filenames[maybe_location->filename_id];
201✔
2318

2319
            if (Utils::fileExists(filename))
201✔
2320
                Diagnostics::makeContext(
398✔
2321
                    Diagnostics::ErrorLocation {
398✔
2322
                        .filename = filename,
199✔
2323
                        .start = FilePos { .line = maybe_location->line, .column = 0 },
199✔
2324
                        .end = std::nullopt,
199✔
2325
                        .maybe_content = std::nullopt },
199✔
2326
                    os,
199✔
2327
                    /* maybe_context= */ std::nullopt,
199✔
2328
                    /* colorize= */ colorize);
199✔
2329
            fmt::println(os, "");
201✔
2330
        }
201✔
2331

2332
        if (context.fc > 1)
201✔
2333
        {
2334
            // display call stack trace
2335
            const ScopeView old_scope = context.locals.back();
9✔
2336

2337
            std::string previous_trace;
9✔
2338
            std::size_t displayed_traces = 0;
9✔
2339
            std::size_t consecutive_similar_traces = 0;
9✔
2340

2341
            while (context.fc != 0 && context.pp != 0 && context.sp > 0)
2,065✔
2342
            {
2343
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2,056✔
2344
                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,056✔
2345

2346
                const uint16_t id = findNearestVariableIdWithValue(
2,056✔
2347
                    Value(static_cast<PageAddr_t>(context.pp)),
2,056✔
2348
                    context);
2,056✔
2349
                const std::string& func_name = (id < m_state.m_symbols.size()) ? m_state.m_symbols[id] : "???";
2,056✔
2350

2351
                if (func_name + loc_as_text != previous_trace)
2,056✔
2352
                {
2353
                    fmt::println(
20✔
2354
                        os,
10✔
2355
                        "[{:4}] In function `{}'{}",
10✔
2356
                        fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
10✔
2357
                        fmt::styled(func_name, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
10✔
2358
                        loc_as_text);
2359
                    previous_trace = func_name + loc_as_text;
10✔
2360
                    ++displayed_traces;
10✔
2361
                    consecutive_similar_traces = 0;
10✔
2362
                }
10✔
2363
                else if (consecutive_similar_traces == 0)
2,046✔
2364
                {
2365
                    fmt::println(os, "       ...");
1✔
2366
                    ++consecutive_similar_traces;
1✔
2367
                }
1✔
2368

2369
                const Value* ip;
2,056✔
2370
                do
6,269✔
2371
                {
2372
                    ip = popAndResolveAsPtr(context);
6,269✔
2373
                } while (ip->valueType() != ValueType::InstPtr);
6,269✔
2374

2375
                context.ip = ip->pageAddr();
2,056✔
2376
                context.pp = pop(context)->pageAddr();
2,056✔
2377
                returnFromFuncCall(context);
2,056✔
2378

2379
                if (displayed_traces > max_consecutive_traces)
2,056✔
2380
                {
UNCOV
2381
                    fmt::println(os, "       ...");
×
UNCOV
2382
                    break;
×
2383
                }
2384
            }
2,056✔
2385

2386
            if (context.pp == 0)
9✔
2387
            {
2388
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
9✔
2389
                const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
9✔
2390
                fmt::println(os, "[{:4}] In global scope{}", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()), loc_as_text);
9✔
2391
            }
9✔
2392

2393
            // display variables values in the current scope
2394
            fmt::println(os, "\nCurrent scope variables values:");
9✔
2395
            for (std::size_t i = 0, size = old_scope.size(); i < size; ++i)
10✔
2396
            {
2397
                fmt::println(
2✔
2398
                    os,
1✔
2399
                    "{} = {}",
1✔
2400
                    fmt::styled(m_state.m_symbols[old_scope.atPos(i).first], colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
1✔
2401
                    old_scope.atPos(i).second.toString(*this));
1✔
2402
            }
1✔
2403
        }
9✔
2404
    }
201✔
2405
}
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