From 2984d445831ebf0cd8c909f589dc203018ae2b0b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 10 Jul 2024 19:30:09 +0900 Subject: test: implement "register" action of WAST --- src/Execution/Extern.php | 28 ++++++++++++++++++++++++++++ src/Execution/Externs/Func.php | 16 ++++++++++++++++ src/Execution/Externs/Global_.php | 16 ++++++++++++++++ src/Execution/Externs/Mem.php | 16 ++++++++++++++++ src/Execution/Externs/Table.php | 16 ++++++++++++++++ src/Execution/Runtime.php | 21 ++++++++++++++++++++- src/Execution/Store.php | 19 ++++++++++++++++--- 7 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 src/Execution/Extern.php create mode 100644 src/Execution/Externs/Func.php create mode 100644 src/Execution/Externs/Global_.php create mode 100644 src/Execution/Externs/Mem.php create mode 100644 src/Execution/Externs/Table.php (limited to 'src') diff --git a/src/Execution/Extern.php b/src/Execution/Extern.php new file mode 100644 index 0000000..e0b695e --- /dev/null +++ b/src/Execution/Extern.php @@ -0,0 +1,28 @@ + $externVals + * @param array> $imports */ public static function instantiate( + Store $store, + Module $module, + array $imports, + ): self { + $externVals = []; + foreach ($module->imports as $import) { + $extern = $imports[$import->module][$import->name] ?? null; + if ($extern === null) { + throw new RuntimeException("instantiate: import not found: {$import->module}::{$import->name}"); + } + $externVals[] = $store->register($extern); + } + return self::doInstantiate($store, $module, $externVals); + } + + /** + * @param list $externVals + */ + private static function doInstantiate( Store $store, Module $module, array $externVals, diff --git a/src/Execution/Store.php b/src/Execution/Store.php index 2b1a26d..52a8ce1 100644 --- a/src/Execution/Store.php +++ b/src/Execution/Store.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\Execution; +use RuntimeException; use function count; final class Store @@ -31,9 +32,21 @@ final class Store return new self([], [], [], [], [], []); } - public function registerFunc(FuncInst $func): ExternVals\Func + public function register(Extern $extern): ExternVal { - $this->funcs[] = $func; - return ExternVal::Func(count($this->funcs) - 1); + 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"), + }; } } -- cgit v1.2.3-70-g09d2