aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Execution/Store.php
blob: 52a8ce12747dcb1585ab24f3d91b13102fab8da2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\Execution;

use RuntimeException;
use function count;

final class Store
{
    /**
     * @param list<FuncInst> $funcs
     * @param list<TableInst> $tables
     * @param list<MemInst> $mems
     * @param list<GlobalInst> $globals
     * @param list<ElemInst> $elems
     * @param list<DataInst> $datas
     */
    public function __construct(
        public array $funcs,
        public array $tables,
        public array $mems,
        public array $globals,
        public array $elems,
        public array $datas,
    ) {
    }

    public static function empty(): self
    {
        return new self([], [], [], [], [], []);
    }

    public function register(Extern $extern): ExternVal
    {
        match ($extern::class) {
            Externs\Func::class => $this->funcs[] = $extern->func,
            Externs\Table::class => $this->tables[] = $extern->table,
            Externs\Mem::class => $this->mems[] = $extern->mem,
            Externs\Global_::class => $this->globals[] = $extern->global,
            default => throw new RuntimeException("unreachable"),
        };
        return match ($extern::class) {
            Externs\Func::class => ExternVal::Func(count($this->funcs) - 1),
            Externs\Table::class => ExternVal::Table(count($this->tables) - 1),
            Externs\Mem::class => ExternVal::Mem(count($this->mems) - 1),
            Externs\Global_::class => ExternVal::Global_(count($this->globals) - 1),
            default => throw new RuntimeException("unreachable"),
        };
    }
}