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

ArkScript-lang / Ark / 13590401922

28 Feb 2025 02:40PM UTC coverage: 79.05% (-0.1%) from 79.158%
13590401922

Pull #515

github

web-flow
Merge e60b1c3a5 into 12afad189
Pull Request #515: feat(closure): allow capture in nested scopes

21 of 22 new or added lines in 1 file covered. (95.45%)

52 existing lines in 2 files now uncovered.

5860 of 7413 relevant lines covered (79.05%)

70582.63 hits per line

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

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

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

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

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

21
namespace Ark
22
{
23
    using namespace internal;
24

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

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

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

49
            types::generateError(
×
50
                "tail",
×
51
                { { types::Contract { { types::Typedef("value", ValueType::List) } },
×
52
                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
×
53
                { *a });
×
54
        }
336✔
55

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

71
            types::generateError(
×
72
                "head",
×
73
                { { types::Contract { { types::Typedef("value", ValueType::List) } },
×
74
                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
×
75
                { *a });
×
76
        }
1,128✔
77
    }
78

79
    VM::VM(State& state) noexcept :
243✔
80
        m_state(state), m_exit_code(0), m_running(false)
81✔
81
    {
81✔
82
        m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>())->locals.reserve(4);
81✔
83
    }
81✔
84

85
    void VM::init() noexcept
82✔
86
    {
82✔
87
        ExecutionContext& context = *m_execution_contexts.back();
82✔
88
        for (const auto& c : m_execution_contexts)
164✔
89
        {
90
            c->ip = 0;
82✔
91
            c->pp = 0;
82✔
92
            c->sp = 0;
82✔
93
        }
82✔
94

95
        context.sp = 0;
82✔
96
        context.fc = 1;
82✔
97

98
        m_shared_lib_objects.clear();
82✔
99
        context.stacked_closure_scopes.clear();
82✔
100
        context.stacked_closure_scopes.emplace_back(nullptr);
82✔
101

102
        context.saved_scope.reset();
82✔
103
        m_exit_code = 0;
82✔
104

105
        context.locals.clear();
82✔
106
        context.locals.emplace_back();
82✔
107

108
        // loading bound stuff
109
        // put them in the global frame if we can, aka the first one
110
        for (const auto& [sym_id, value] : m_state.m_binded)
109✔
111
        {
112
            auto it = std::ranges::find(m_state.m_symbols, sym_id);
22✔
113
            if (it != m_state.m_symbols.end())
22✔
114
                context.locals[0].push_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), value);
5✔
115
        }
22✔
116
    }
82✔
117

118
    Value& VM::operator[](const std::string& name) noexcept
28✔
119
    {
28✔
120
        // find id of object
121
        const auto it = std::ranges::find(m_state.m_symbols, name);
28✔
122
        if (it == m_state.m_symbols.end())
28✔
123
        {
124
            m_no_value = Builtins::nil;
1✔
125
            return m_no_value;
1✔
126
        }
127

128
        const auto dist = std::distance(m_state.m_symbols.begin(), it);
27✔
129
        if (std::cmp_less(dist, std::numeric_limits<uint16_t>::max()))
27✔
130
        {
131
            ExecutionContext& context = *m_execution_contexts.front();
27✔
132

133
            const auto id = static_cast<uint16_t>(dist);
27✔
134
            Value* var = findNearestVariable(id, context);
27✔
135
            if (var != nullptr)
27✔
136
                return *var;
27✔
137
        }
27✔
138

139
        m_no_value = Builtins::nil;
×
140
        return m_no_value;
×
141
    }
28✔
142

143
    void VM::loadPlugin(const uint16_t id, ExecutionContext& context)
×
144
    {
×
145
        namespace fs = std::filesystem;
146

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

149
        std::string path = file;
×
150
        // bytecode loaded from file
151
        if (m_state.m_filename != ARK_NO_NAME_FILE)
×
152
            path = (fs::path(m_state.m_filename).parent_path() / fs::path(file)).relative_path().string();
×
153

154
        std::shared_ptr<SharedLibrary> lib;
×
155
        // if it exists alongside the .arkc file
156
        if (Utils::fileExists(path))
×
157
            lib = std::make_shared<SharedLibrary>(path);
×
158
        else
159
        {
160
            for (auto const& v : m_state.m_libenv)
×
161
            {
162
                std::string lib_path = (fs::path(v) / fs::path(file)).string();
×
163

164
                // if it's already loaded don't do anything
165
                if (std::ranges::find_if(m_shared_lib_objects, [&](const auto& val) {
×
166
                        return (val->path() == path || val->path() == lib_path);
81✔
167
                    }) != m_shared_lib_objects.end())
×
168
                    return;
×
169

170
                // check in lib_path
171
                if (Utils::fileExists(lib_path))
×
172
                {
173
                    lib = std::make_shared<SharedLibrary>(lib_path);
×
174
                    break;
×
175
                }
176
            }
×
177
        }
178

179
        if (!lib)
×
180
        {
181
            auto lib_path = std::accumulate(
×
182
                std::next(m_state.m_libenv.begin()),
×
183
                m_state.m_libenv.end(),
×
184
                m_state.m_libenv[0].string(),
×
185
                [](const std::string& a, const fs::path& b) -> std::string {
×
186
                    return a + "\n\t- " + b.string();
×
187
                });
×
188
            throwVMError(
×
189
                ErrorKind::Module,
190
                fmt::format("Could not find module '{}'. Searched under\n\t- {}\n\t- {}", file, path, lib_path));
×
191
        }
×
192

193
        m_shared_lib_objects.emplace_back(lib);
×
194

195
        // load the mapping from the dynamic library
196
        try
197
        {
198
            const mapping* map = m_shared_lib_objects.back()->get<mapping* (*)()>("getFunctionsMapping")();
×
199
            // load the mapping data
200
            std::size_t i = 0;
×
201
            while (map[i].name != nullptr)
×
202
            {
203
                // put it in the global frame, aka the first one
204
                auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
205
                if (it != m_state.m_symbols.end())
×
206
                    context.locals[0].push_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), Value(map[i].value));
×
207

208
                ++i;
×
209
            }
×
210
        }
×
211
        catch (const std::system_error& e)
212
        {
213
            throwVMError(
×
214
                ErrorKind::Module,
215
                fmt::format(
×
216
                    "An error occurred while loading module '{}': {}\nIt is most likely because the versions of the module and the language don't match.",
×
217
                    file, e.what()));
×
218
        }
×
219
    }
×
220

221
    void VM::exit(const int code) noexcept
×
222
    {
×
223
        m_exit_code = code;
×
224
        m_running = false;
×
225
    }
×
226

227
    ExecutionContext* VM::createAndGetContext()
6✔
228
    {
6✔
229
        const std::lock_guard lock(m_mutex);
6✔
230

231
        m_execution_contexts.push_back(std::make_unique<ExecutionContext>());
6✔
232
        ExecutionContext* ctx = m_execution_contexts.back().get();
6✔
233
        ctx->stacked_closure_scopes.emplace_back(nullptr);
6✔
234

235
        ctx->locals.reserve(m_execution_contexts.front()->locals.size());
6✔
236
        for (const auto& local : m_execution_contexts.front()->locals)
20✔
237
            ctx->locals.push_back(local);
14✔
238

239
        return ctx;
6✔
240
    }
6✔
241

242
    void VM::deleteContext(ExecutionContext* ec)
5✔
243
    {
5✔
244
        const std::lock_guard lock(m_mutex);
5✔
245

246
        const auto it =
5✔
247
            std::ranges::remove_if(
10✔
248
                m_execution_contexts,
5✔
249
                [ec](const std::unique_ptr<ExecutionContext>& ctx) {
21✔
250
                    return ctx.get() == ec;
16✔
251
                })
252
                .begin();
5✔
253
        m_execution_contexts.erase(it);
5✔
254
    }
5✔
255

256
    Future* VM::createFuture(std::vector<Value>& args)
6✔
257
    {
6✔
258
        ExecutionContext* ctx = createAndGetContext();
6✔
259

260
        // doing this after having created the context
261
        // because the context uses the mutex and we don't want a deadlock
262
        const std::lock_guard lock(m_mutex);
6✔
263
        m_futures.push_back(std::make_unique<Future>(ctx, this, args));
6✔
264

265
        return m_futures.back().get();
6✔
266
    }
6✔
267

268
    void VM::deleteFuture(Future* f)
×
269
    {
×
270
        const std::lock_guard lock(m_mutex);
×
271

272
        const auto it =
×
273
            std::ranges::remove_if(
×
274
                m_futures,
×
275
                [f](const std::unique_ptr<Future>& future) {
×
276
                    return future.get() == f;
×
277
                })
278
                .begin();
×
279
        m_futures.erase(it);
×
280
    }
×
281

282
    bool VM::forceReloadPlugins() const
×
283
    {
×
284
        // load the mapping from the dynamic library
285
        try
286
        {
287
            for (const auto& shared_lib : m_shared_lib_objects)
×
288
            {
289
                const mapping* map = shared_lib->template get<mapping* (*)()>("getFunctionsMapping")();
×
290
                // load the mapping data
291
                std::size_t i = 0;
×
292
                while (map[i].name != nullptr)
×
293
                {
294
                    // put it in the global frame, aka the first one
295
                    auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
296
                    if (it != m_state.m_symbols.end())
×
297
                        m_execution_contexts[0]->locals[0].push_back(
×
298
                            static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)),
×
299
                            Value(map[i].value));
×
300

301
                    ++i;
×
302
                }
×
303
            }
×
304

305
            return true;
×
306
        }
×
307
        catch (const std::system_error&)
308
        {
309
            return false;
×
310
        }
×
311
    }
×
312

313
    int VM::run(const bool fail_with_exception)
82✔
314
    {
82✔
315
        init();
82✔
316
        safeRun(*m_execution_contexts[0], 0, fail_with_exception);
82✔
317
        return m_exit_code;
82✔
318
    }
319

320
    int VM::safeRun(ExecutionContext& context, std::size_t untilFrameCount, bool fail_with_exception)
162✔
321
    {
162✔
322
#if ARK_USE_COMPUTED_GOTOS
323
#    define TARGET(op) TARGET_##op:
324
#    define DISPATCH_GOTO()            \
325
        _Pragma("GCC diagnostic push") \
326
            _Pragma("GCC diagnostic ignored \"-Wpedantic\"") goto* opcode_targets[inst];
327
        _Pragma("GCC diagnostic pop")
328
#    define GOTO_HALT() goto dispatch_end
329
#else
330
#    define TARGET(op) case op:
331
#    define DISPATCH_GOTO() goto dispatch_opcode
1✔
332
#    define GOTO_HALT() break
333
#endif
1✔
334

335
#define NEXTOPARG()                                                                      \
1✔
336
    do                                                                                   \
337
    {                                                                                    \
338
        inst = m_state.m_pages[context.pp][context.ip];                                  \
339
        padding = m_state.m_pages[context.pp][context.ip + 1];                           \
340
        arg = static_cast<uint16_t>((m_state.m_pages[context.pp][context.ip + 2] << 8) + \
341
                                    m_state.m_pages[context.pp][context.ip + 3]);        \
342
        context.ip += 4;                                                                 \
343
    } while (false)
344
#define DISPATCH() \
345
    NEXTOPARG();   \
346
    DISPATCH_GOTO();
347
#define UNPACK_ARGS()                                                                 \
348
    do                                                                                \
349
    {                                                                                 \
350
        secondary_arg = static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12); \
351
        primary_arg = arg & 0x0fff;                                                   \
352
    } while (false)
353

354
#if ARK_USE_COMPUTED_GOTOS
355
#    pragma GCC diagnostic push
356
#    pragma GCC diagnostic ignored "-Wpedantic"
357
            constexpr std::array opcode_targets = {
162✔
358
                &&TARGET_NOP,
359
                &&TARGET_LOAD_SYMBOL,
360
                &&TARGET_LOAD_CONST,
361
                &&TARGET_POP_JUMP_IF_TRUE,
362
                &&TARGET_STORE,
363
                &&TARGET_SET_VAL,
364
                &&TARGET_POP_JUMP_IF_FALSE,
365
                &&TARGET_JUMP,
366
                &&TARGET_RET,
367
                &&TARGET_HALT,
368
                &&TARGET_CALL,
369
                &&TARGET_CAPTURE,
370
                &&TARGET_BUILTIN,
371
                &&TARGET_DEL,
372
                &&TARGET_MAKE_CLOSURE,
373
                &&TARGET_GET_FIELD,
374
                &&TARGET_PLUGIN,
375
                &&TARGET_LIST,
376
                &&TARGET_APPEND,
377
                &&TARGET_CONCAT,
378
                &&TARGET_APPEND_IN_PLACE,
379
                &&TARGET_CONCAT_IN_PLACE,
380
                &&TARGET_POP_LIST,
381
                &&TARGET_POP_LIST_IN_PLACE,
382
                &&TARGET_SET_AT_INDEX,
383
                &&TARGET_SET_AT_2_INDEX,
384
                &&TARGET_POP,
385
                &&TARGET_DUP,
386
                &&TARGET_CREATE_SCOPE,
387
                &&TARGET_POP_SCOPE,
388
                &&TARGET_ADD,
389
                &&TARGET_SUB,
390
                &&TARGET_MUL,
391
                &&TARGET_DIV,
392
                &&TARGET_GT,
393
                &&TARGET_LT,
394
                &&TARGET_LE,
395
                &&TARGET_GE,
396
                &&TARGET_NEQ,
397
                &&TARGET_EQ,
398
                &&TARGET_LEN,
399
                &&TARGET_EMPTY,
400
                &&TARGET_TAIL,
401
                &&TARGET_HEAD,
402
                &&TARGET_ISNIL,
403
                &&TARGET_ASSERT,
404
                &&TARGET_TO_NUM,
405
                &&TARGET_TO_STR,
406
                &&TARGET_AT,
407
                &&TARGET_AT_AT,
408
                &&TARGET_MOD,
409
                &&TARGET_TYPE,
410
                &&TARGET_HASFIELD,
411
                &&TARGET_NOT,
412
                &&TARGET_LOAD_CONST_LOAD_CONST,
413
                &&TARGET_LOAD_CONST_STORE,
414
                &&TARGET_LOAD_CONST_SET_VAL,
415
                &&TARGET_STORE_FROM,
416
                &&TARGET_SET_VAL_FROM,
417
                &&TARGET_INCREMENT,
418
                &&TARGET_DECREMENT,
419
                &&TARGET_STORE_TAIL,
420
                &&TARGET_STORE_HEAD,
421
                &&TARGET_SET_VAL_TAIL,
422
                &&TARGET_SET_VAL_HEAD,
423
                &&TARGET_CALL_BUILTIN
424
            };
425
#    pragma GCC diagnostic pop
426
#endif
427

428
        try
429
        {
430
            uint8_t inst = 0;
162✔
431
            uint8_t padding = 0;
162✔
432
            uint16_t arg = 0;
162✔
433
            uint16_t primary_arg = 0;
162✔
434
            uint16_t secondary_arg = 0;
162✔
435

436
            m_running = true;
162✔
437

438
            DISPATCH();
162✔
439
            {
440
#if !ARK_USE_COMPUTED_GOTOS
441
            dispatch_opcode:
442
                switch (inst)
443
#endif
444
                {
×
445
#pragma region "Instructions"
446
                    TARGET(NOP)
447
                    {
448
                        DISPATCH();
×
449
                    }
634,764✔
450

451
                    TARGET(LOAD_SYMBOL)
452
                    {
453
                        push(loadSymbol(arg, context), context);
634,764✔
454
                        DISPATCH();
634,709✔
455
                    }
292,924✔
456

457
                    TARGET(LOAD_CONST)
458
                    {
459
                        push(loadConstAsPtr(arg), context);
292,924✔
460
                        DISPATCH();
292,924✔
461
                    }
293,168✔
462

463
                    TARGET(POP_JUMP_IF_TRUE)
464
                    {
465
                        if (Value boolean = *popAndResolveAsPtr(context); !!boolean)
396,768✔
466
                            context.ip = arg * 4;  // instructions are 4 bytes
103,600✔
467
                        DISPATCH();
293,168✔
468
                    }
396,617✔
469

470
                    TARGET(STORE)
471
                    {
472
                        store(arg, popAndResolveAsPtr(context), context);
396,617✔
473
                        DISPATCH();
396,617✔
474
                    }
35,147✔
475

476
                    TARGET(SET_VAL)
477
                    {
478
                        setVal(arg, popAndResolveAsPtr(context), context);
35,147✔
479
                        DISPATCH();
35,151✔
480
                    }
24,578✔
481

482
                    TARGET(POP_JUMP_IF_FALSE)
483
                    {
484
                        if (Value boolean = *popAndResolveAsPtr(context); !boolean)
26,774✔
485
                            context.ip = arg * 4;  // instructions are 4 bytes
2,196✔
486
                        DISPATCH();
24,570✔
487
                    }
205,319✔
488

489
                    TARGET(JUMP)
490
                    {
491
                        context.ip = arg * 4;  // instructions are 4 bytes
205,319✔
492
                        DISPATCH();
205,319✔
493
                    }
120,580✔
494

495
                    TARGET(RET)
496
                    {
497
                        {
498
                            Value ip_or_val = *popAndResolveAsPtr(context);
120,580✔
499
                            // no return value on the stack
500
                            if (ip_or_val.valueType() == ValueType::InstPtr) [[unlikely]]
120,580✔
501
                            {
502
                                context.ip = ip_or_val.pageAddr();
1,312✔
503
                                // we always push PP then IP, thus the next value
504
                                // MUST be the page pointer
505
                                context.pp = pop(context)->pageAddr();
1,312✔
506

507
                                returnFromFuncCall(context);
1,312✔
508
                                push(Builtins::nil, context);
1,312✔
509
                            }
1,312✔
510
                            // value on the stack
511
                            else [[likely]]
512
                            {
513
                                Value* ip;
514
                                do
119,268✔
515
                                {
516
                                    ip = popAndResolveAsPtr(context);
119,268✔
517
                                } while (ip->valueType() != ValueType::InstPtr);
119,268✔
518

519
                                context.ip = ip->pageAddr();
119,268✔
520
                                context.pp = pop(context)->pageAddr();
119,268✔
521

522
                                returnFromFuncCall(context);
119,268✔
523
                                push(std::move(ip_or_val), context);
119,268✔
524
                            }
525

526
                            if (context.fc <= untilFrameCount)
120,580✔
527
                                GOTO_HALT();
6✔
528
                        }
120,580✔
529

530
                        DISPATCH();
120,574✔
531
                    }
52✔
532

533
                    TARGET(HALT)
534
                    {
535
                        m_running = false;
52✔
536
                        GOTO_HALT();
52✔
537
                    }
124,677✔
538

539
                    TARGET(CALL)
540
                    {
541
                        // stack pointer + 2 because we push IP and PP
542
                        if (context.sp + 2u >= VMStackSize) [[unlikely]]
124,677✔
543
                            throwVMError(
1✔
544
                                ErrorKind::VM,
545
                                fmt::format(
2✔
546
                                    "Maximum recursion depth exceeded. You could consider rewriting your function `{}' to make use of tail-call optimization.",
1✔
547
                                    m_state.m_symbols[context.last_symbol]));
1✔
548
                        call(context, arg);
124,676✔
549
                        if (!m_running)
124,672✔
550
                            GOTO_HALT();
×
551
                        DISPATCH();
124,672✔
552
                    }
457✔
553

554
                    TARGET(CAPTURE)
555
                    {
556
                        if (!context.saved_scope)
457✔
557
                            context.saved_scope = Scope();
102✔
558

559
                        Value* ptr = findNearestVariable(arg, context);
457✔
560
                        if (!ptr)
457✔
561
                            throwVMError(ErrorKind::Scope, fmt::format("Couldn't capture `{}' as it is currently unbound", m_state.m_symbols[arg]));
×
562
                        else
563
                        {
564
                            ptr = ptr->valueType() == ValueType::Reference ? ptr->reference() : ptr;
457✔
565
                            context.saved_scope.value().push_back(arg, *ptr);
457✔
566
                        }
567

568
                        DISPATCH();
457✔
569
                    }
399✔
570

571
                    TARGET(BUILTIN)
572
                    {
573
                        push(Builtins::builtins[arg].second, context);
399✔
574
                        DISPATCH();
399✔
575
                    }
1✔
576

577
                    TARGET(DEL)
578
                    {
579
                        if (Value* var = findNearestVariable(arg, context); var != nullptr)
1✔
580
                        {
581
                            if (var->valueType() == ValueType::User)
×
582
                                var->usertypeRef().del();
×
583
                            *var = Value();
×
584
                            DISPATCH();
×
585
                        }
586

587
                        throwVMError(ErrorKind::Scope, fmt::format("Can not delete unbound variable `{}'", m_state.m_symbols[arg]));
1✔
588
                    }
102✔
589

590
                    TARGET(MAKE_CLOSURE)
102✔
591
                    {
592
                        push(Value(Closure(context.saved_scope.value(), m_state.m_constants[arg].pageAddr())), context);
102✔
593
                        context.saved_scope.reset();
102✔
594
                        DISPATCH();
102✔
595
                    }
1,528✔
596

597
                    TARGET(GET_FIELD)
598
                    {
599
                        Value* var = popAndResolveAsPtr(context);
1,528✔
600
                        if (var->valueType() != ValueType::Closure)
1,528✔
601
                        {
602
                            if (context.last_symbol < m_state.m_symbols.size()) [[likely]]
1✔
603
                                throwVMError(
1✔
604
                                    ErrorKind::Type,
605
                                    fmt::format(
4✔
606
                                        "`{}' is a {}, not a Closure, can not get the field `{}' from it",
1✔
607
                                        m_state.m_symbols[context.last_symbol],
1✔
608
                                        types_to_str[static_cast<std::size_t>(var->valueType())],
1✔
609
                                        m_state.m_symbols[arg]));
1✔
610
                            else
611
                                throwVMError(ErrorKind::Type,
×
612
                                             fmt::format(
×
613
                                                 "{} is not a Closure, can not get the field `{}' from it",
×
614
                                                 types_to_str[static_cast<std::size_t>(var->valueType())],
×
615
                                                 m_state.m_symbols[arg]));
×
616
                        }
×
617

618
                        if (Value* field = var->refClosure().refScope()[arg]; field != nullptr)
1,527✔
619
                        {
620
                            // check for CALL instruction (the instruction because context.ip is already on the next instruction word)
621
                            if (m_state.m_pages[context.pp][context.ip] == CALL)
1,525✔
622
                                push(Value(Closure(var->refClosure().scopePtr(), field->pageAddr())), context);
688✔
623
                            else
624
                                push(field, context);
837✔
625
                        }
1,525✔
626
                        else
627
                        {
628
                            if (!var->refClosure().hasFieldEndingWith(m_state.m_symbols[arg], *this))
2✔
629
                                throwVMError(
1✔
630
                                    ErrorKind::Scope,
631
                                    fmt::format(
2✔
632
                                        "`{0}' isn't in the closure environment: {1}",
1✔
633
                                        m_state.m_symbols[arg],
1✔
634
                                        var->refClosure().toString(*this)));
1✔
635
                            throwVMError(
1✔
636
                                ErrorKind::Scope,
637
                                fmt::format(
2✔
638
                                    "`{0}' isn't in the closure environment: {1}. A variable in the package might have the same name as '{0}', "
1✔
639
                                    "and name resolution tried to fully qualify it. Rename either the variable or the capture to solve this",
640
                                    m_state.m_symbols[arg],
1✔
641
                                    var->refClosure().toString(*this)));
1✔
642
                        }
643
                        DISPATCH();
1,525✔
644
                    }
×
645

646
                    TARGET(PLUGIN)
647
                    {
648
                        loadPlugin(arg, context);
×
649
                        DISPATCH();
×
650
                    }
830✔
651

652
                    TARGET(LIST)
653
                    {
654
                        {
655
                            Value l(ValueType::List);
830✔
656
                            if (arg != 0)
830✔
657
                                l.list().reserve(arg);
449✔
658

659
                            for (uint16_t i = 0; i < arg; ++i)
2,017✔
660
                                l.push_back(*popAndResolveAsPtr(context));
1,187✔
661
                            push(std::move(l), context);
830✔
662
                        }
830✔
663
                        DISPATCH();
830✔
664
                    }
1,033✔
665

666
                    TARGET(APPEND)
667
                    {
668
                        {
669
                            Value* list = popAndResolveAsPtr(context);
1,033✔
670
                            if (list->valueType() != ValueType::List)
1,033✔
671
                                types::generateError(
×
672
                                    "append",
×
673
                                    { { types::Contract { { types::Typedef("list", ValueType::List) } } } },
×
674
                                    { *list });
×
675

676
                            const auto size = static_cast<uint16_t>(list->constList().size());
1,033✔
677

678
                            Value obj { *list };
1,033✔
679
                            obj.list().reserve(size + arg);
1,033✔
680

681
                            for (uint16_t i = 0; i < arg; ++i)
2,066✔
682
                                obj.push_back(*popAndResolveAsPtr(context));
1,033✔
683
                            push(std::move(obj), context);
1,033✔
684
                        }
1,033✔
685
                        DISPATCH();
1,033✔
686
                    }
2✔
687

688
                    TARGET(CONCAT)
689
                    {
690
                        {
691
                            Value* list = popAndResolveAsPtr(context);
2✔
692
                            if (list->valueType() != ValueType::List)
2✔
693
                                types::generateError(
×
694
                                    "concat",
×
695
                                    { { types::Contract { { types::Typedef("list", ValueType::List) } } } },
×
696
                                    { *list });
×
697

698
                            Value obj { *list };
2✔
699

700
                            for (uint16_t i = 0; i < arg; ++i)
4✔
701
                            {
702
                                Value* next = popAndResolveAsPtr(context);
2✔
703

704
                                if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
2✔
705
                                    types::generateError(
×
706
                                        "concat",
×
707
                                        { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
×
708
                                        { *list, *next });
×
709

710
                                std::ranges::copy(next->list(), std::back_inserter(obj.list()));
2✔
711
                            }
2✔
712
                            push(std::move(obj), context);
2✔
713
                        }
2✔
714
                        DISPATCH();
2✔
715
                    }
1,205✔
716

717
                    TARGET(APPEND_IN_PLACE)
718
                    {
719
                        Value* list = popAndResolveAsPtr(context);
1,205✔
720

721
                        if (list->valueType() != ValueType::List)
1,205✔
722
                            types::generateError(
×
723
                                "append!",
×
724
                                { { types::Contract { { types::Typedef("list", ValueType::List) } } } },
×
725
                                { *list });
×
726

727
                        for (uint16_t i = 0; i < arg; ++i)
2,410✔
728
                            list->push_back(*popAndResolveAsPtr(context));
1,205✔
729
                        DISPATCH();
1,205✔
730
                    }
50✔
731

732
                    TARGET(CONCAT_IN_PLACE)
733
                    {
734
                        Value* list = popAndResolveAsPtr(context);
50✔
735

736
                        if (list->valueType() != ValueType::List)
50✔
737
                            types::generateError(
×
738
                                "concat",
×
739
                                { { types::Contract { { types::Typedef("list", ValueType::List) } } } },
×
740
                                { *list });
×
741

742
                        for (uint16_t i = 0; i < arg; ++i)
130✔
743
                        {
744
                            Value* next = popAndResolveAsPtr(context);
80✔
745

746
                            if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
80✔
747
                                types::generateError(
×
748
                                    "concat!",
×
749
                                    { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
×
750
                                    { *list, *next });
×
751

752
                            std::ranges::copy(next->list(), std::back_inserter(list->list()));
80✔
753
                        }
80✔
754
                        DISPATCH();
50✔
755
                    }
4✔
756

757
                    TARGET(POP_LIST)
758
                    {
759
                        {
760
                            Value list = *popAndResolveAsPtr(context);
4✔
761
                            Value number = *popAndResolveAsPtr(context);
4✔
762

763
                            if (list.valueType() != ValueType::List || number.valueType() != ValueType::Number)
4✔
764
                                types::generateError(
×
765
                                    "pop",
×
766
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
×
767
                                    { list, number });
×
768

769
                            long idx = static_cast<long>(number.number());
4✔
770
                            idx = idx < 0 ? static_cast<long>(list.list().size()) + idx : idx;
4✔
771
                            if (std::cmp_greater_equal(idx, list.list().size()))
4✔
772
                                throwVMError(
1✔
773
                                    ErrorKind::Index,
774
                                    fmt::format("pop index ({}) out of range (list size: {})", idx, list.list().size()));
1✔
775

776
                            list.list().erase(list.list().begin() + idx);
3✔
777
                            push(list, context);
3✔
778
                        }
4✔
779
                        DISPATCH();
3✔
780
                    }
52✔
781

782
                    TARGET(POP_LIST_IN_PLACE)
783
                    {
784
                        {
785
                            Value* list = popAndResolveAsPtr(context);
52✔
786
                            Value number = *popAndResolveAsPtr(context);
52✔
787

788
                            if (list->valueType() != ValueType::List || number.valueType() != ValueType::Number)
52✔
789
                                types::generateError(
×
790
                                    "pop!",
×
791
                                    { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
×
792
                                    { *list, number });
×
793

794
                            long idx = static_cast<long>(number.number());
52✔
795
                            idx = idx < 0 ? static_cast<long>(list->list().size()) + idx : idx;
52✔
796
                            if (std::cmp_greater_equal(idx, list->list().size()))
52✔
797
                                throwVMError(
1✔
798
                                    ErrorKind::Index,
799
                                    fmt::format("pop! index ({}) out of range (list size: {})", idx, list->list().size()));
1✔
800

801
                            list->list().erase(list->list().begin() + idx);
51✔
802
                        }
52✔
803
                        DISPATCH();
51✔
804
                    }
488✔
805

806
                    TARGET(SET_AT_INDEX)
807
                    {
808
                        {
809
                            Value* list = popAndResolveAsPtr(context);
488✔
810
                            Value number = *popAndResolveAsPtr(context);
488✔
811
                            Value new_value = *popAndResolveAsPtr(context);
488✔
812

813
                            if (!list->isIndexable() || number.valueType() != ValueType::Number || (list->valueType() == ValueType::String && new_value.valueType() != ValueType::String))
488✔
814
                                types::generateError(
×
815
                                    "@=",
×
816
                                    { { types::Contract {
×
817
                                          { types::Typedef("list", ValueType::List),
×
818
                                            types::Typedef("index", ValueType::Number),
×
819
                                            types::Typedef("new_value", ValueType::Any) } } },
×
820
                                      { types::Contract {
×
821
                                          { types::Typedef("string", ValueType::String),
×
822
                                            types::Typedef("index", ValueType::Number),
×
823
                                            types::Typedef("char", ValueType::String) } } } },
×
824
                                    { *list, number });
×
825

826
                            const std::size_t size = list->valueType() == ValueType::List ? list->list().size() : list->stringRef().size();
488✔
827
                            long idx = static_cast<long>(number.number());
488✔
828
                            idx = idx < 0 ? static_cast<long>(size) + idx : idx;
488✔
829
                            if (std::cmp_greater_equal(idx, size))
488✔
830
                                throwVMError(
1✔
831
                                    ErrorKind::Index,
832
                                    fmt::format("@= index ({}) out of range (indexable size: {})", idx, size));
1✔
833

834
                            if (list->valueType() == ValueType::List)
487✔
835
                                list->list()[static_cast<std::size_t>(idx)] = new_value;
485✔
836
                            else
837
                                list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
2✔
838
                        }
488✔
839
                        DISPATCH();
487✔
840
                    }
8✔
841

842
                    TARGET(SET_AT_2_INDEX)
843
                    {
844
                        {
845
                            Value* list = popAndResolveAsPtr(context);
8✔
846
                            Value x = *popAndResolveAsPtr(context);
8✔
847
                            Value y = *popAndResolveAsPtr(context);
8✔
848
                            Value new_value = *popAndResolveAsPtr(context);
8✔
849

850
                            if (list->valueType() != ValueType::List || x.valueType() != ValueType::Number || y.valueType() != ValueType::Number)
8✔
851
                                types::generateError(
×
852
                                    "@@=",
×
853
                                    { { types::Contract {
×
854
                                        { types::Typedef("list", ValueType::List),
×
855
                                          types::Typedef("x", ValueType::Number),
×
856
                                          types::Typedef("y", ValueType::Number),
×
857
                                          types::Typedef("new_value", ValueType::Any) } } } },
×
858
                                    { *list, x, y });
×
859

860
                            long idx_y = static_cast<long>(x.number());
8✔
861
                            idx_y = idx_y < 0 ? static_cast<long>(list->list().size()) + idx_y : idx_y;
8✔
862
                            if (std::cmp_greater_equal(idx_y, list->list().size()))
8✔
863
                                throwVMError(
1✔
864
                                    ErrorKind::Index,
865
                                    fmt::format("@@= index (y: {}) out of range (list size: {})", idx_y, list->list().size()));
1✔
866

867
                            if (!list->list()[static_cast<std::size_t>(idx_y)].isIndexable() ||
11✔
868
                                (list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::String && new_value.valueType() != ValueType::String))
7✔
869
                                types::generateError(
×
870
                                    "@@=",
×
871
                                    { { types::Contract {
×
872
                                          { types::Typedef("list", ValueType::List),
×
873
                                            types::Typedef("x", ValueType::Number),
×
874
                                            types::Typedef("y", ValueType::Number),
×
875
                                            types::Typedef("new_value", ValueType::Any) } } },
×
876
                                      { types::Contract {
×
877
                                          { types::Typedef("string", ValueType::String),
×
878
                                            types::Typedef("x", ValueType::Number),
×
879
                                            types::Typedef("y", ValueType::Number),
×
880
                                            types::Typedef("char", ValueType::String) } } } },
×
881
                                    { *list, x, y });
×
882

883
                            const bool is_list = list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
7✔
884
                            const std::size_t size =
7✔
885
                                is_list
14✔
886
                                ? list->list()[static_cast<std::size_t>(idx_y)].list().size()
5✔
887
                                : list->list()[static_cast<std::size_t>(idx_y)].stringRef().size();
2✔
888

889
                            long idx_x = static_cast<long>(y.number());
7✔
890
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
7✔
891
                            if (std::cmp_greater_equal(idx_x, size))
7✔
892
                                throwVMError(
1✔
893
                                    ErrorKind::Index,
894
                                    fmt::format("@@= index (x: {}) out of range (inner indexable size: {})", idx_x, size));
1✔
895

896
                            if (is_list)
6✔
897
                                list->list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)] = new_value;
4✔
898
                            else
899
                                list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
2✔
900
                        }
8✔
901
                        DISPATCH();
6✔
902
                    }
9,116✔
903

904
                    TARGET(POP)
905
                    {
906
                        pop(context);
9,116✔
907
                        DISPATCH();
9,116✔
908
                    }
7,952✔
909

910
                    TARGET(DUP)
911
                    {
912
                        context.stack[context.sp] = context.stack[context.sp - 1];
7,952✔
913
                        ++context.sp;
7,952✔
914
                        DISPATCH();
7,952✔
915
                    }
1,748✔
916

917
                    TARGET(CREATE_SCOPE)
918
                    {
919
                        context.locals.emplace_back();
1,748✔
920
                        DISPATCH();
1,748✔
921
                    }
1,748✔
922

923
                    TARGET(POP_SCOPE)
924
                    {
925
                        context.locals.pop_back();
1,748✔
926
                        DISPATCH();
1,748✔
927
                    }
25,160✔
928

929
#pragma endregion
930

931
#pragma region "Operators"
932

933
                    TARGET(ADD)
934
                    {
935
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
25,160✔
936

937
                        if (a->valueType() == ValueType::Number && b->valueType() == ValueType::Number)
25,155✔
938
                            push(Value(a->number() + b->number()), context);
18,057✔
939
                        else if (a->valueType() == ValueType::String && b->valueType() == ValueType::String)
7,102✔
940
                            push(Value(a->string() + b->string()), context);
7,102✔
941
                        else
942
                            types::generateError(
×
943
                                "+",
×
944
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } },
×
945
                                    types::Contract { { types::Typedef("a", ValueType::String), types::Typedef("b", ValueType::String) } } } },
×
946
                                { *a, *b });
×
947
                        DISPATCH();
25,155✔
948
                    }
117✔
949

950
                    TARGET(SUB)
951
                    {
952
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
117✔
953

954
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
117✔
955
                            types::generateError(
×
956
                                "-",
×
957
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
958
                                { *a, *b });
×
959
                        push(Value(a->number() - b->number()), context);
117✔
960
                        DISPATCH();
117✔
961
                    }
1,319✔
962

963
                    TARGET(MUL)
964
                    {
965
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,319✔
966

967
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
1,319✔
968
                            types::generateError(
×
969
                                "*",
×
970
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
971
                                { *a, *b });
×
972
                        push(Value(a->number() * b->number()), context);
1,319✔
973
                        DISPATCH();
1,319✔
974
                    }
1,054✔
975

976
                    TARGET(DIV)
977
                    {
978
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,054✔
979

980
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
1,054✔
981
                            types::generateError(
×
982
                                "/",
×
983
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
984
                                { *a, *b });
×
985
                        auto d = b->number();
1,054✔
986
                        if (d == 0)
1,054✔
987
                            throwVMError(ErrorKind::DivisionByZero, fmt::format("Can not compute expression (/ {} {})", a->toString(*this), b->toString(*this)));
1✔
988

989
                        push(Value(a->number() / d), context);
1,053✔
990
                        DISPATCH();
1,053✔
991
                    }
173,026✔
992

993
                    TARGET(GT)
994
                    {
995
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
173,026✔
996
                        push((*a != *b && !(*a < *b)) ? Builtins::trueSym : Builtins::falseSym, context);
173,026✔
997
                        DISPATCH();
173,026✔
998
                    }
39,607✔
999

1000
                    TARGET(LT)
1001
                    {
1002
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
39,607✔
1003
                        push((*a < *b) ? Builtins::trueSym : Builtins::falseSym, context);
39,600✔
1004
                        DISPATCH();
39,598✔
1005
                    }
7,178✔
1006

1007
                    TARGET(LE)
1008
                    {
1009
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
7,178✔
1010
                        push((((*a < *b) || (*a == *b)) ? Builtins::trueSym : Builtins::falseSym), context);
7,178✔
1011
                        DISPATCH();
7,178✔
1012
                    }
5,251✔
1013

1014
                    TARGET(GE)
1015
                    {
1016
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
5,251✔
1017
                        push(!(*a < *b) ? Builtins::trueSym : Builtins::falseSym, context);
5,251✔
1018
                        DISPATCH();
5,251✔
1019
                    }
617✔
1020

1021
                    TARGET(NEQ)
1022
                    {
1023
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
617✔
1024
                        push((*a != *b) ? Builtins::trueSym : Builtins::falseSym, context);
617✔
1025
                        DISPATCH();
617✔
1026
                    }
89,308✔
1027

1028
                    TARGET(EQ)
1029
                    {
1030
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
89,308✔
1031
                        push((*a == *b) ? Builtins::trueSym : Builtins::falseSym, context);
89,308✔
1032
                        DISPATCH();
89,308✔
1033
                    }
10,603✔
1034

1035
                    TARGET(LEN)
1036
                    {
1037
                        Value* a = popAndResolveAsPtr(context);
10,603✔
1038

1039
                        if (a->valueType() == ValueType::List)
10,603✔
1040
                            push(Value(static_cast<int>(a->constList().size())), context);
3,145✔
1041
                        else if (a->valueType() == ValueType::String)
7,458✔
1042
                            push(Value(static_cast<int>(a->string().size())), context);
7,458✔
1043
                        else
1044
                            types::generateError(
×
1045
                                "len",
×
1046
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
×
1047
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
×
1048
                                { *a });
×
1049
                        DISPATCH();
10,603✔
1050
                    }
532✔
1051

1052
                    TARGET(EMPTY)
1053
                    {
1054
                        Value* a = popAndResolveAsPtr(context);
532✔
1055

1056
                        if (a->valueType() == ValueType::List)
532✔
1057
                            push(a->constList().empty() ? Builtins::trueSym : Builtins::falseSym, context);
81✔
1058
                        else if (a->valueType() == ValueType::String)
451✔
1059
                            push(a->string().empty() ? Builtins::trueSym : Builtins::falseSym, context);
451✔
1060
                        else
1061
                            types::generateError(
×
1062
                                "empty?",
×
1063
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
×
1064
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
×
1065
                                { *a });
×
1066
                        DISPATCH();
532✔
1067
                    }
334✔
1068

1069
                    TARGET(TAIL)
1070
                    {
1071
                        Value* a = popAndResolveAsPtr(context);
334✔
1072
                        push(helper::tail(a), context);
334✔
1073
                        DISPATCH();
334✔
1074
                    }
1,096✔
1075

1076
                    TARGET(HEAD)
1077
                    {
1078
                        Value* a = popAndResolveAsPtr(context);
1,096✔
1079
                        push(helper::head(a), context);
1,096✔
1080
                        DISPATCH();
1,096✔
1081
                    }
1,268✔
1082

1083
                    TARGET(ISNIL)
1084
                    {
1085
                        Value* a = popAndResolveAsPtr(context);
1,268✔
1086
                        push((*a == Builtins::nil) ? Builtins::trueSym : Builtins::falseSym, context);
1,268✔
1087
                        DISPATCH();
1,268✔
1088
                    }
539✔
1089

1090
                    TARGET(ASSERT)
1091
                    {
1092
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
539✔
1093

1094
                        if (b->valueType() != ValueType::String)
539✔
UNCOV
1095
                            types::generateError(
×
1096
                                "assert",
×
1097
                                { { types::Contract { { types::Typedef("expr", ValueType::Any), types::Typedef("message", ValueType::String) } } } },
×
1098
                                { *a, *b });
×
1099

1100
                        if (*a == Builtins::falseSym)
539✔
UNCOV
1101
                            throw AssertionFailed(b->stringRef());
×
1102
                        DISPATCH();
539✔
1103
                    }
13✔
1104

1105
                    TARGET(TO_NUM)
1106
                    {
1107
                        Value* a = popAndResolveAsPtr(context);
13✔
1108

1109
                        if (a->valueType() != ValueType::String)
13✔
UNCOV
1110
                            types::generateError(
×
1111
                                "toNumber",
×
1112
                                { { types::Contract { { types::Typedef("value", ValueType::String) } } } },
×
1113
                                { *a });
×
1114

1115
                        double val;
1116
                        if (Utils::isDouble(a->string(), &val))
13✔
1117
                            push(Value(val), context);
10✔
1118
                        else
1119
                            push(Builtins::nil, context);
3✔
1120
                        DISPATCH();
13✔
1121
                    }
16✔
1122

1123
                    TARGET(TO_STR)
1124
                    {
1125
                        Value* a = popAndResolveAsPtr(context);
16✔
1126
                        push(Value(a->toString(*this)), context);
16✔
1127
                        DISPATCH();
16✔
1128
                    }
14,354✔
1129

1130
                    TARGET(AT)
1131
                    {
1132
                        {
1133
                            Value* b = popAndResolveAsPtr(context);
14,354✔
1134
                            Value a = *popAndResolveAsPtr(context);  // be careful, it's not a pointer
14,351✔
1135

1136
                            if (b->valueType() != ValueType::Number)
14,366✔
UNCOV
1137
                                types::generateError(
×
1138
                                    "@",
×
1139
                                    { { types::Contract { { types::Typedef("src", ValueType::List), types::Typedef("idx", ValueType::Number) } },
×
1140
                                        types::Contract { { types::Typedef("src", ValueType::String), types::Typedef("idx", ValueType::Number) } } } },
×
1141
                                    { a, *b });
×
1142

1143
                            long idx = static_cast<long>(b->number());
14,366✔
1144

1145
                            if (a.valueType() == ValueType::List)
14,355✔
1146
                            {
1147
                                if (std::cmp_less(std::abs(idx), a.list().size()))
6,742✔
1148
                                    push(a.list()[static_cast<std::size_t>(idx < 0 ? static_cast<long>(a.list().size()) + idx : idx)], context);
6,743✔
1149
                                else
1150
                                    throwVMError(
1✔
1151
                                        ErrorKind::Index,
1152
                                        fmt::format("{} out of range {} (length {})", idx, a.toString(*this), a.list().size()));
1✔
1153
                            }
6,740✔
1154
                            else if (a.valueType() == ValueType::String)
7,613✔
1155
                            {
1156
                                if (std::cmp_less(std::abs(idx), a.string().size()))
7,613✔
1157
                                    push(Value(std::string(1, a.string()[static_cast<std::size_t>(idx < 0 ? static_cast<long>(a.string().size()) + idx : idx)])), context);
7,612✔
1158
                                else
1159
                                    throwVMError(
1✔
1160
                                        ErrorKind::Index,
1161
                                        fmt::format("{} out of range \"{}\" (length {})", idx, a.string(), a.string().size()));
1✔
1162
                            }
7,612✔
1163
                            else
UNCOV
1164
                                types::generateError(
×
1165
                                    "@",
×
1166
                                    { { types::Contract { { types::Typedef("src", ValueType::List), types::Typedef("idx", ValueType::Number) } },
×
1167
                                        types::Contract { { types::Typedef("src", ValueType::String), types::Typedef("idx", ValueType::Number) } } } },
×
1168
                                    { a, *b });
×
1169
                        }
14,354✔
1170
                        DISPATCH();
14,352✔
1171
                    }
15✔
1172

1173
                    TARGET(AT_AT)
1174
                    {
1175
                        {
1176
                            Value* x = popAndResolveAsPtr(context);
15✔
1177
                            Value* y = popAndResolveAsPtr(context);
15✔
1178
                            Value list = *popAndResolveAsPtr(context);  // be careful, it's not a pointer
15✔
1179

1180
                            if (y->valueType() != ValueType::Number || x->valueType() != ValueType::Number ||
15✔
1181
                                list.valueType() != ValueType::List)
15✔
UNCOV
1182
                                types::generateError(
×
1183
                                    "@@",
×
1184
                                    { { types::Contract {
×
1185
                                        { types::Typedef("src", ValueType::List),
×
1186
                                          types::Typedef("y", ValueType::Number),
×
1187
                                          types::Typedef("x", ValueType::Number) } } } },
×
1188
                                    { list, *y, *x });
×
1189

1190
                            long idx_y = static_cast<long>(y->number());
15✔
1191
                            idx_y = idx_y < 0 ? static_cast<long>(list.list().size()) + idx_y : idx_y;
15✔
1192
                            if (std::cmp_greater_equal(idx_y, list.list().size()))
15✔
1193
                                throwVMError(
1✔
1194
                                    ErrorKind::Index,
1195
                                    fmt::format("@@ index ({}) out of range (list size: {})", idx_y, list.list().size()));
1✔
1196

1197
                            const bool is_list = list.list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
14✔
1198
                            const std::size_t size =
14✔
1199
                                is_list
28✔
1200
                                ? list.list()[static_cast<std::size_t>(idx_y)].list().size()
7✔
1201
                                : list.list()[static_cast<std::size_t>(idx_y)].stringRef().size();
7✔
1202

1203
                            long idx_x = static_cast<long>(x->number());
14✔
1204
                            idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
14✔
1205
                            if (std::cmp_greater_equal(idx_x, size))
14✔
1206
                                throwVMError(
1✔
1207
                                    ErrorKind::Index,
1208
                                    fmt::format("@@ index (x: {}) out of range (inner indexable size: {})", idx_x, size));
1✔
1209

1210
                            if (is_list)
13✔
1211
                                push(list.list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)], context);
6✔
1212
                            else
1213
                                push(Value(std::string(1, list.list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)])), context);
7✔
1214
                        }
15✔
1215
                        DISPATCH();
13✔
1216
                    }
783✔
1217

1218
                    TARGET(MOD)
1219
                    {
1220
                        Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
783✔
1221
                        if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
783✔
UNCOV
1222
                            types::generateError(
×
1223
                                "mod",
×
1224
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1225
                                { *a, *b });
×
1226
                        push(Value(std::fmod(a->number(), b->number())), context);
783✔
1227
                        DISPATCH();
783✔
1228
                    }
87✔
1229

1230
                    TARGET(TYPE)
1231
                    {
1232
                        Value* a = popAndResolveAsPtr(context);
87✔
1233
                        if (a == &m_undefined_value) [[unlikely]]
87✔
UNCOV
1234
                            types::generateError(
×
1235
                                "type",
×
1236
                                { { types::Contract { { types::Typedef("value", ValueType::Any) } } } },
×
1237
                                {});
×
1238

1239
                        push(Value(types_to_str[static_cast<unsigned>(a->valueType())]), context);
87✔
1240
                        DISPATCH();
87✔
1241
                    }
2✔
1242

1243
                    TARGET(HASFIELD)
1244
                    {
1245
                        {
1246
                            Value *field = popAndResolveAsPtr(context), *closure = popAndResolveAsPtr(context);
2✔
1247
                            if (closure->valueType() != ValueType::Closure || field->valueType() != ValueType::String)
2✔
NEW
1248
                                types::generateError(
×
1249
                                    "hasField",
×
1250
                                    { { types::Contract { { types::Typedef("closure", ValueType::Closure), types::Typedef("field", ValueType::String) } } } },
×
1251
                                    { *closure, *field });
×
1252

1253
                            auto it = std::find(m_state.m_symbols.begin(), m_state.m_symbols.end(), field->stringRef());
2✔
1254
                            if (it == m_state.m_symbols.end())
2✔
1255
                            {
1256
                                push(Builtins::falseSym, context);
1✔
1257
                                DISPATCH();
1✔
1258
                            }
1259

1260
                            auto id = static_cast<std::uint16_t>(std::distance(m_state.m_symbols.begin(), it));
1✔
1261
                            push(closure->refClosure().refScope()[id] != nullptr ? Builtins::trueSym : Builtins::falseSym, context);
1✔
1262
                        }
1263
                        DISPATCH();
1✔
1264
                    }
2,338✔
1265

1266
                    TARGET(NOT)
1267
                    {
1268
                        Value* a = popAndResolveAsPtr(context);
2,338✔
1269
                        push(!(*a) ? Builtins::trueSym : Builtins::falseSym, context);
2,338✔
1270
                        DISPATCH();
2,338✔
1271
                    }
5,349✔
1272

1273
#pragma endregion
1274

1275
#pragma region "Super Instructions"
1276
                    TARGET(LOAD_CONST_LOAD_CONST)
1277
                    {
1278
                        UNPACK_ARGS();
5,349✔
1279
                        push(loadConstAsPtr(primary_arg), context);
5,349✔
1280
                        push(loadConstAsPtr(secondary_arg), context);
5,349✔
1281
                        DISPATCH();
5,349✔
1282
                    }
5,272✔
1283

1284
                    TARGET(LOAD_CONST_STORE)
1285
                    {
1286
                        UNPACK_ARGS();
5,272✔
1287
                        store(secondary_arg, loadConstAsPtr(primary_arg), context);
5,272✔
1288
                        DISPATCH();
5,272✔
1289
                    }
208✔
1290

1291
                    TARGET(LOAD_CONST_SET_VAL)
1292
                    {
1293
                        UNPACK_ARGS();
208✔
1294
                        setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
208✔
1295
                        DISPATCH();
207✔
1296
                    }
582✔
1297

1298
                    TARGET(STORE_FROM)
1299
                    {
1300
                        UNPACK_ARGS();
582✔
1301
                        store(secondary_arg, loadSymbol(primary_arg, context), context);
582✔
1302
                        DISPATCH();
581✔
1303
                    }
158✔
1304

1305
                    TARGET(SET_VAL_FROM)
1306
                    {
1307
                        UNPACK_ARGS();
158✔
1308
                        setVal(secondary_arg, loadSymbol(primary_arg, context), context);
158✔
1309
                        DISPATCH();
158✔
1310
                    }
104,788✔
1311

1312
                    TARGET(INCREMENT)
1313
                    {
1314
                        UNPACK_ARGS();
104,788✔
1315
                        {
1316
                            Value* var = loadSymbol(primary_arg, context);
104,788✔
1317

1318
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1319
                            if (var->valueType() == ValueType::Reference)
104,787✔
UNCOV
1320
                                var = var->reference();
×
1321

1322
                            if (var->valueType() == ValueType::Number)
104,787✔
1323
                                push(Value(var->number() + secondary_arg), context);
104,787✔
1324
                            else
UNCOV
1325
                                types::generateError(
×
UNCOV
1326
                                    "+",
×
1327
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1328
                                    { *var, Value(secondary_arg) });
×
1329
                        }
1330
                        DISPATCH();
104,787✔
1331
                    }
195,622✔
1332

1333
                    TARGET(DECREMENT)
1334
                    {
1335
                        UNPACK_ARGS();
195,622✔
1336
                        {
1337
                            Value* var = loadSymbol(primary_arg, context);
195,622✔
1338

1339
                            // use internal reference, shouldn't break anything so far, unless it's already a ref
1340
                            if (var->valueType() == ValueType::Reference)
195,622✔
UNCOV
1341
                                var = var->reference();
×
1342

1343
                            if (var->valueType() == ValueType::Number)
195,622✔
1344
                                push(Value(var->number() - secondary_arg), context);
195,622✔
1345
                            else
UNCOV
1346
                                types::generateError(
×
UNCOV
1347
                                    "-",
×
1348
                                    { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
×
1349
                                    { *var, Value(secondary_arg) });
×
1350
                        }
1351
                        DISPATCH();
195,622✔
1352
                    }
1✔
1353

1354
                    TARGET(STORE_TAIL)
1355
                    {
1356
                        UNPACK_ARGS();
1✔
1357
                        {
1358
                            Value* list = loadSymbol(primary_arg, context);
1✔
1359
                            Value tail = helper::tail(list);
1✔
1360
                            store(secondary_arg, &tail, context);
1✔
1361
                        }
1✔
1362
                        DISPATCH();
1✔
1363
                    }
31✔
1364

1365
                    TARGET(STORE_HEAD)
1366
                    {
1367
                        UNPACK_ARGS();
31✔
1368
                        {
1369
                            Value* list = loadSymbol(primary_arg, context);
31✔
1370
                            Value head = helper::head(list);
31✔
1371
                            store(secondary_arg, &head, context);
31✔
1372
                        }
31✔
1373
                        DISPATCH();
31✔
1374
                    }
1✔
1375

1376
                    TARGET(SET_VAL_TAIL)
1377
                    {
1378
                        UNPACK_ARGS();
1✔
1379
                        {
1380
                            Value* list = loadSymbol(primary_arg, context);
1✔
1381
                            Value tail = helper::tail(list);
1✔
1382
                            setVal(secondary_arg, &tail, context);
1✔
1383
                        }
1✔
1384
                        DISPATCH();
1✔
1385
                    }
1✔
1386

1387
                    TARGET(SET_VAL_HEAD)
1388
                    {
1389
                        UNPACK_ARGS();
1✔
1390
                        {
1391
                            Value* list = loadSymbol(primary_arg, context);
1✔
1392
                            Value head = helper::head(list);
1✔
1393
                            setVal(secondary_arg, &head, context);
1✔
1394
                        }
1✔
1395
                        DISPATCH();
1✔
1396
                    }
10,633✔
1397

1398
                    TARGET(CALL_BUILTIN)
1399
                    {
1400
                        UNPACK_ARGS();
10,633✔
1401
                        // no stack size check because we do not push IP/PP since we are just calling a builtin
1402
                        callBuiltin(context, Builtins::builtins[primary_arg].second, secondary_arg);
10,633✔
1403
                        if (!m_running)
10,624✔
UNCOV
1404
                            GOTO_HALT();
×
1405
                        DISPATCH();
10,624✔
1406
                    }
1407
#pragma endregion
1408
                }
58✔
1409
#if ARK_USE_COMPUTED_GOTOS
1410
            dispatch_end:
1411
                do
58✔
1412
                {
1413
                } while (false);
58✔
1414
#endif
1415
            }
1416
        }
88✔
1417
        catch (const std::exception& e)
1418
        {
1419
            if (fail_with_exception)
30✔
1420
                throw;
30✔
1421

UNCOV
1422
            fmt::println("{}", e.what());
×
UNCOV
1423
            backtrace(context);
×
1424
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1425
            // don't report a "failed" exit code so that the fuzzers can more accurately triage crashes
1426
            m_exit_code = 0;
1427
#else
UNCOV
1428
            m_exit_code = 1;
×
1429
#endif
1430
        }
88✔
1431
        catch (...)
1432
        {
UNCOV
1433
            if (fail_with_exception)
×
UNCOV
1434
                throw;
×
1435

1436
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1437
            throw;
1438
#endif
UNCOV
1439
            fmt::println("Unknown error");
×
UNCOV
1440
            backtrace(context);
×
1441
            m_exit_code = 1;
×
1442
        }
60✔
1443

1444
        return m_exit_code;
58✔
1445
    }
204✔
1446

UNCOV
1447
    uint16_t VM::findNearestVariableIdWithValue(const Value& value, ExecutionContext& context) const noexcept
×
UNCOV
1448
    {
×
1449
        for (auto& local : std::ranges::reverse_view(context.locals))
×
1450
        {
1451
            if (const auto id = local.idFromValue(value); id < m_state.m_symbols.size())
×
UNCOV
1452
                return id;
×
1453
        }
×
1454
        return std::numeric_limits<uint16_t>::max();
×
1455
    }
×
1456

1457
    void VM::throwVMError(ErrorKind kind, const std::string& message)
22✔
1458
    {
22✔
1459
        throw std::runtime_error(std::string(errorKinds[static_cast<std::size_t>(kind)]) + ": " + message + "\n");
22✔
1460
    }
22✔
1461

UNCOV
1462
    void VM::backtrace(ExecutionContext& context) noexcept
×
UNCOV
1463
    {
×
1464
        const std::size_t saved_ip = context.ip;
×
1465
        const std::size_t saved_pp = context.pp;
×
1466
        const uint16_t saved_sp = context.sp;
×
1467

1468
        if (const uint16_t original_frame_count = context.fc; original_frame_count > 1)
×
1469
        {
1470
            // display call stack trace
UNCOV
1471
            const Scope old_scope = context.locals.back();
×
1472

1473
            while (context.fc != 0)
×
1474
            {
1475
                fmt::print("[{}] ", fmt::styled(context.fc, fmt::fg(fmt::color::cyan)));
×
UNCOV
1476
                if (context.pp != 0)
×
1477
                {
1478
                    const uint16_t id = findNearestVariableIdWithValue(
×
UNCOV
1479
                        Value(static_cast<PageAddr_t>(context.pp)),
×
1480
                        context);
×
1481

1482
                    if (id < m_state.m_symbols.size())
×
UNCOV
1483
                        fmt::println("In function `{}'", fmt::styled(m_state.m_symbols[id], fmt::fg(fmt::color::green)));
×
1484
                    else  // should never happen
1485
                        fmt::println("In function `{}'", fmt::styled("???", fmt::fg(fmt::color::gold)));
×
1486

1487
                    Value* ip;
×
UNCOV
1488
                    do
×
1489
                    {
1490
                        ip = popAndResolveAsPtr(context);
×
UNCOV
1491
                    } while (ip->valueType() != ValueType::InstPtr);
×
1492

1493
                    context.ip = ip->pageAddr();
×
UNCOV
1494
                    context.pp = pop(context)->pageAddr();
×
1495
                    returnFromFuncCall(context);
×
1496
                }
×
1497
                else
1498
                {
UNCOV
1499
                    fmt::println("In global scope");
×
UNCOV
1500
                    break;
×
1501
                }
1502

UNCOV
1503
                if (original_frame_count - context.fc > 7)
×
1504
                {
1505
                    fmt::println("...");
×
UNCOV
1506
                    break;
×
1507
                }
1508
            }
1509

1510
            // display variables values in the current scope
UNCOV
1511
            fmt::println("\nCurrent scope variables values:");
×
UNCOV
1512
            for (std::size_t i = 0, size = old_scope.size(); i < size; ++i)
×
1513
            {
1514
                fmt::println(
×
UNCOV
1515
                    "{} = {}",
×
1516
                    fmt::styled(m_state.m_symbols[old_scope.m_data[i].first], fmt::fg(fmt::color::cyan)),
×
1517
                    old_scope.m_data[i].second.toString(*this));
×
1518
            }
×
1519

1520
            while (context.fc != 1)
×
1521
            {
1522
                Value* tmp = pop(context);
×
UNCOV
1523
                if (tmp->valueType() == ValueType::InstPtr)
×
1524
                    --context.fc;
×
1525
                *tmp = m_no_value;
×
1526
            }
×
1527
            // pop the PP as well
1528
            pop(context);
×
UNCOV
1529
        }
×
1530

1531
        std::cerr << "At IP: " << (saved_ip / 4)  // dividing by 4 because the instructions are actually on 4 bytes
×
UNCOV
1532
                  << ", PP: " << saved_pp
×
1533
                  << ", SP: " << saved_sp
×
1534
                  << "\n";
×
1535
    }
×
1536
}
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