aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-10 19:30:09 +0900
committernsfisis <nsfisis@gmail.com>2024-07-10 19:30:20 +0900
commit2984d445831ebf0cd8c909f589dc203018ae2b0b (patch)
tree465c6e304b85fcd322389dd8b530cea5acabd929 /src
parent1011da8798ba7370d914dbce9feac1b06ab81ed5 (diff)
downloadphp-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.tar.gz
php-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.tar.zst
php-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.zip
test: implement "register" action of WAST
Diffstat (limited to 'src')
-rw-r--r--src/Execution/Extern.php28
-rw-r--r--src/Execution/Externs/Func.php16
-rw-r--r--src/Execution/Externs/Global_.php16
-rw-r--r--src/Execution/Externs/Mem.php16
-rw-r--r--src/Execution/Externs/Table.php16
-rw-r--r--src/Execution/Runtime.php21
-rw-r--r--src/Execution/Store.php19
7 files changed, 128 insertions, 4 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution;
+
+abstract readonly class Extern
+{
+ final public static function Func(FuncInst $func): Externs\Func
+ {
+ return new Externs\Func($func);
+ }
+
+ final public static function Table(TableInst $table): Externs\Table
+ {
+ return new Externs\Table($table);
+ }
+
+ final public static function Mem(MemInst $mem): Externs\Mem
+ {
+ return new Externs\Mem($mem);
+ }
+
+ final public static function Global_(GlobalInst $global): Externs\Global_
+ {
+ return new Externs\Global_($global);
+ }
+}
diff --git a/src/Execution/Externs/Func.php b/src/Execution/Externs/Func.php
new file mode 100644
index 0000000..124309c
--- /dev/null
+++ b/src/Execution/Externs/Func.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\FuncInst;
+
+final readonly class Func extends Extern
+{
+ protected function __construct(
+ public FuncInst $func,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Global_.php b/src/Execution/Externs/Global_.php
new file mode 100644
index 0000000..ccf9ad0
--- /dev/null
+++ b/src/Execution/Externs/Global_.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\GlobalInst;
+
+final readonly class Global_ extends Extern
+{
+ protected function __construct(
+ public GlobalInst $global,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Mem.php b/src/Execution/Externs/Mem.php
new file mode 100644
index 0000000..bd1f119
--- /dev/null
+++ b/src/Execution/Externs/Mem.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\MemInst;
+
+final readonly class Mem extends Extern
+{
+ protected function __construct(
+ public MemInst $mem,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Table.php b/src/Execution/Externs/Table.php
new file mode 100644
index 0000000..fde1e32
--- /dev/null
+++ b/src/Execution/Externs/Table.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\TableInst;
+
+final readonly class Table extends Extern
+{
+ protected function __construct(
+ public TableInst $table,
+ ) {
+ }
+}
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php
index 02c526c..2cc37b2 100644
--- a/src/Execution/Runtime.php
+++ b/src/Execution/Runtime.php
@@ -56,11 +56,30 @@ final class Runtime
}
/**
- * @param list<ExternVal> $externVals
+ * @param array<string, array<string, Extern>> $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<ExternVal> $externVals
+ */
+ private static function doInstantiate(
+ Store $store,
+ Module $module,
array $externVals,
): self {
$allocator = new Allocator($store);
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"),
+ };
}
}