aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/WebAssembly/Execution
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-11 03:50:50 +0900
committernsfisis <nsfisis@gmail.com>2024-07-11 04:14:03 +0900
commit8a083ed74e9f4472441175e187208012927ed357 (patch)
tree3686a0f61f3fccc5ded3eda28b92b8bf7765fd2f /src/WebAssembly/Execution
parent26f49b7e27076e689541b9e13a1b54f60a4ee5c2 (diff)
downloadphp-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.tar.gz
php-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.tar.zst
php-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.zip
feat: simplify ValType structure
Diffstat (limited to 'src/WebAssembly/Execution')
-rw-r--r--src/WebAssembly/Execution/Allocator.php5
-rw-r--r--src/WebAssembly/Execution/ElemInst.php5
-rw-r--r--src/WebAssembly/Execution/Ref.php7
-rw-r--r--src/WebAssembly/Execution/Refs/RefNull.php7
-rw-r--r--src/WebAssembly/Execution/Runtime.php16
-rw-r--r--src/WebAssembly/Execution/Stack.php7
6 files changed, 27 insertions, 20 deletions
diff --git a/src/WebAssembly/Execution/Allocator.php b/src/WebAssembly/Execution/Allocator.php
index 67eb467..4e2adbf 100644
--- a/src/WebAssembly/Execution/Allocator.php
+++ b/src/WebAssembly/Execution/Allocator.php
@@ -9,8 +9,8 @@ use Nsfisis\Waddiwasi\WebAssembly\Structure\Modules\Func;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Modules\Module;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\GlobalType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\MemType;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\TableType;
+use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
use RuntimeException;
use function count;
@@ -144,9 +144,10 @@ final readonly class Allocator
}
/**
+ * @param ValType::FuncRef|ValType::ExternRef $refType
* @param list<Ref> $elem
*/
- private function allocElem(RefType $refType, array $elem): int
+ private function allocElem(ValType $refType, array $elem): int
{
$elemInst = new ElemInst($refType, $elem);
$this->store->elems[] = $elemInst;
diff --git a/src/WebAssembly/Execution/ElemInst.php b/src/WebAssembly/Execution/ElemInst.php
index 422cd62..8840292 100644
--- a/src/WebAssembly/Execution/ElemInst.php
+++ b/src/WebAssembly/Execution/ElemInst.php
@@ -4,15 +4,16 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\Execution;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType;
+use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
final readonly class ElemInst
{
/**
+ * @param ValType::FuncRef|ValType::ExternRef $type
* @param list<Ref> $elem
*/
public function __construct(
- public RefType $type,
+ public ValType $type,
public array $elem,
) {
}
diff --git a/src/WebAssembly/Execution/Ref.php b/src/WebAssembly/Execution/Ref.php
index f7b6760..f9791d2 100644
--- a/src/WebAssembly/Execution/Ref.php
+++ b/src/WebAssembly/Execution/Ref.php
@@ -4,11 +4,14 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\Execution;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType;
+use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
abstract readonly class Ref
{
- final public static function RefNull(RefType $type): Refs\RefNull
+ /**
+ * @param ValType::FuncRef|ValType::ExternRef $type
+ */
+ final public static function RefNull(ValType $type): Refs\RefNull
{
return new Refs\RefNull($type);
}
diff --git a/src/WebAssembly/Execution/Refs/RefNull.php b/src/WebAssembly/Execution/Refs/RefNull.php
index 438ed9e..f73fa9d 100644
--- a/src/WebAssembly/Execution/Refs/RefNull.php
+++ b/src/WebAssembly/Execution/Refs/RefNull.php
@@ -5,12 +5,15 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\Execution\Refs;
use Nsfisis\Waddiwasi\WebAssembly\Execution\Ref;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType;
+use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
final readonly class RefNull extends Ref
{
+ /**
+ * @param ValType::FuncRef|ValType::ExternRef $type
+ */
public function __construct(
- public RefType $type,
+ public ValType $type,
) {
}
}
diff --git a/src/WebAssembly/Execution/Runtime.php b/src/WebAssembly/Execution/Runtime.php
index dbefea2..4e8f3e8 100644
--- a/src/WebAssembly/Execution/Runtime.php
+++ b/src/WebAssembly/Execution/Runtime.php
@@ -13,11 +13,9 @@ use Nsfisis\Waddiwasi\WebAssembly\Structure\Modules\ElemModes;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Modules\Module;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\FuncType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\Limits;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\NumType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ResultType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\TableType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValTypes;
use RuntimeException;
use function abs;
use function array_map;
@@ -2639,14 +2637,12 @@ final class Runtime
private static function defaultValueFromValType(ValType $type): int|float|Ref
{
- return match ($type::class) {
- ValTypes\NumType::class => match ($type->inner) {
- NumType::I32 => 0,
- NumType::I64 => 0,
- NumType::F32 => 0.0,
- NumType::F64 => 0.0,
- },
- ValTypes\RefType::class => Ref::RefNull($type->inner),
+ return match ($type) {
+ ValType::I32 => 0,
+ ValType::I64 => 0,
+ ValType::F32 => 0.0,
+ ValType::F64 => 0.0,
+ ValType::FuncRef, ValType::ExternRef => Ref::RefNull($type),
default => throw new RuntimeException("unreachable"),
};
}
diff --git a/src/WebAssembly/Execution/Stack.php b/src/WebAssembly/Execution/Stack.php
index 4181dcd..befa3dc 100644
--- a/src/WebAssembly/Execution/Stack.php
+++ b/src/WebAssembly/Execution/Stack.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\Execution;
-use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType;
+use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
use function assert;
use function count;
use function is_float;
@@ -54,7 +54,10 @@ final class Stack
$this->pushValue((int)$value);
}
- public function pushRefNull(RefType $type): void
+ /**
+ * @param ValType::FuncRef|ValType::ExternRef $type
+ */
+ public function pushRefNull(ValType $type): void
{
$this->pushValue(Ref::RefNull($type));
}