diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-11 03:50:50 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-11 04:14:03 +0900 |
| commit | 8a083ed74e9f4472441175e187208012927ed357 (patch) | |
| tree | 3686a0f61f3fccc5ded3eda28b92b8bf7765fd2f /src | |
| parent | 26f49b7e27076e689541b9e13a1b54f60a4ee5c2 (diff) | |
| download | php-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.tar.gz php-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.tar.zst php-waddiwasi-8a083ed74e9f4472441175e187208012927ed357.zip | |
feat: simplify ValType structure
Diffstat (limited to 'src')
20 files changed, 86 insertions, 189 deletions
diff --git a/src/WebAssembly/BinaryFormat/Decoder.php b/src/WebAssembly/BinaryFormat/Decoder.php index cc408a9..e6e8bfb 100644 --- a/src/WebAssembly/BinaryFormat/Decoder.php +++ b/src/WebAssembly/BinaryFormat/Decoder.php @@ -31,12 +31,9 @@ use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\GlobalType; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\Limits; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\MemType; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\Mut; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\NumType; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType; 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\VecType; use function array_reduce; use function assert; use function count; @@ -308,32 +305,27 @@ final class Decoder private function decodeValType(): ValType { - $b = $this->stream->peekByte(); - if ($b === 0x7F) { - $this->stream->seek(1); - return ValType::NumType(NumType::I32); - } elseif ($b === 0x7E) { - $this->stream->seek(1); - return ValType::NumType(NumType::I64); - } elseif ($b === 0x7D) { - $this->stream->seek(1); - return ValType::NumType(NumType::F32); - } elseif ($b === 0x7C) { - $this->stream->seek(1); - return ValType::NumType(NumType::F64); - } elseif ($b === 0x7B) { - $this->stream->seek(1); - return ValType::VecType(VecType::V128); - } else { - return ValType::RefType($this->decodeRefType()); - } + $b = $this->decodeByte(); + return match ($b) { + 0x7F => ValType::I32, + 0x7E => ValType::I64, + 0x7D => ValType::F32, + 0x7C => ValType::F64, + 0x7B => ValType::V128, + 0x70 => ValType::FuncRef, + 0x6F => ValType::ExternRef, + default => throw new InvalidBinaryFormatException("valtype $b"), + }; } - private function decodeRefType(): RefType + /** + * @return ValType::FuncRef|ValType::ExternRef + */ + private function decodeRefType(): ValType { - return match ($this->decodeByte()) { - 0x70 => RefType::FuncRef, - 0x6F => RefType::ExternRef, + $type = $this->decodeValType(); + return match ($type) { + ValType::FuncRef, ValType::ExternRef => $type, default => throw new InvalidBinaryFormatException("reftype"), }; } @@ -436,7 +428,7 @@ final class Decoder $offset = $this->decodeExpr(); $initFuncRefs = $this->decodeVec($this->decodeFuncIdx(...)); return new Elem( - RefType::FuncRef, + ValType::FuncRef, array_map( fn ($funcRef) => [Instr::RefFunc($funcRef)], $initFuncRefs, @@ -482,7 +474,7 @@ final class Decoder $offset = $this->decodeExpr(); $init = $this->decodeVec($this->decodeExpr(...)); return new Elem( - RefType::FuncRef, + ValType::FuncRef, $init, ElemMode::Active(0, $offset), ); @@ -589,11 +581,14 @@ final class Decoder }; } - private function decodeElemKind(): RefType + /** + * @return ValType::FuncRef + */ + private function decodeElemKind(): ValType { $b = $this->decodeByte(); if ($b === 0x00) { - return RefType::FuncRef; + return ValType::FuncRef; } else { throw new InvalidBinaryFormatException("elemkind"); } diff --git a/src/WebAssembly/Debug/Debug.php b/src/WebAssembly/Debug/Debug.php index 738dff2..60ac99b 100644 --- a/src/WebAssembly/Debug/Debug.php +++ b/src/WebAssembly/Debug/Debug.php @@ -11,12 +11,9 @@ use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\GlobalType; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\Limits; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\MemType; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\Mut; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\NumType; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType; 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; final readonly class Debug { @@ -52,7 +49,7 @@ final readonly class Debug private static function tableTypeToString(TableType $type): string { - return self::valTypeToString(ValType::RefType($type->refType)) . ' ' . self::limitsToString($type->limits); + return self::valTypeToString($type->refType) . ' ' . self::limitsToString($type->limits); } private static function memTypeToString(MemType $type): string @@ -74,19 +71,14 @@ final readonly class Debug private static function valTypeToString(ValType $type): string { - return match ($type::class) { - ValTypes\NumType::class => match ($type->inner) { - NumType::I32 => 'i32', - NumType::I64 => 'i64', - NumType::F32 => 'f32', - NumType::F64 => 'f64', - }, - ValTypes\VecType::class => 'v128', - ValTypes\RefType::class => match ($type->inner) { - RefType::FuncRef => 'funcref', - RefType::ExternRef => 'externref', - }, - default => 'unknown', + return match ($type) { + ValType::I32 => 'i32', + ValType::I64 => 'i64', + ValType::F32 => 'f32', + ValType::F64 => 'f64', + ValType::V128 => 'v128', + ValType::ExternRef => 'externref', + ValType::FuncRef => 'funcref', }; } 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)); } diff --git a/src/WebAssembly/Structure/Instructions/Instr.php b/src/WebAssembly/Structure/Instructions/Instr.php index 18531c6..5f9cf29 100644 --- a/src/WebAssembly/Structure/Instructions/Instr.php +++ b/src/WebAssembly/Structure/Instructions/Instr.php @@ -12,7 +12,6 @@ use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Parametric; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Reference; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Table; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Variable; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType; use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; abstract readonly class Instr @@ -616,7 +615,10 @@ abstract readonly class Instr { return new Reference\RefIsNull(); } - final public static function RefNull(RefType $type): Reference\RefNull + /** + * @param ValType::FuncRef|ValType::ExternRef $type + */ + final public static function RefNull(ValType $type): Reference\RefNull { return new Reference\RefNull($type); } diff --git a/src/WebAssembly/Structure/Instructions/Instrs/Reference/RefNull.php b/src/WebAssembly/Structure/Instructions/Instrs/Reference/RefNull.php index 39d0f8f..ed5afff 100644 --- a/src/WebAssembly/Structure/Instructions/Instrs/Reference/RefNull.php +++ b/src/WebAssembly/Structure/Instructions/Instrs/Reference/RefNull.php @@ -5,12 +5,15 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Reference; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instr; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType; +use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; final readonly class RefNull extends Instr { + /** + * @param ValType::FuncRef|ValType::ExternRef $type + */ protected function __construct( - public RefType $type, + public ValType $type, ) { } diff --git a/src/WebAssembly/Structure/Modules/Elem.php b/src/WebAssembly/Structure/Modules/Elem.php index 1b9831f..646315d 100644 --- a/src/WebAssembly/Structure/Modules/Elem.php +++ b/src/WebAssembly/Structure/Modules/Elem.php @@ -5,15 +5,16 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Modules; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instr; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType; +use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; final readonly class Elem { /** + * @param ValType::FuncRef|ValType::ExternRef $type * @param list<list<Instr>> $init */ public function __construct( - public RefType $type, + public ValType $type, public array $init, public ElemMode $mode, ) { diff --git a/src/WebAssembly/Structure/Types/NumType.php b/src/WebAssembly/Structure/Types/NumType.php deleted file mode 100644 index 2d5a3b1..0000000 --- a/src/WebAssembly/Structure/Types/NumType.php +++ /dev/null @@ -1,13 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types; - -enum NumType -{ - case I32; - case I64; - case F32; - case F64; -} diff --git a/src/WebAssembly/Structure/Types/RefType.php b/src/WebAssembly/Structure/Types/RefType.php deleted file mode 100644 index 2225f56..0000000 --- a/src/WebAssembly/Structure/Types/RefType.php +++ /dev/null @@ -1,11 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types; - -enum RefType -{ - case FuncRef; - case ExternRef; -} diff --git a/src/WebAssembly/Structure/Types/ResultType.php b/src/WebAssembly/Structure/Types/ResultType.php index dff2e59..9f44e81 100644 --- a/src/WebAssembly/Structure/Types/ResultType.php +++ b/src/WebAssembly/Structure/Types/ResultType.php @@ -22,7 +22,7 @@ final readonly class ResultType return false; } foreach ($this->types as $i => $type) { - if (!$type->equals($other->types[$i])) { + if ($type !== $other->types[$i]) { return false; } } diff --git a/src/WebAssembly/Structure/Types/TableType.php b/src/WebAssembly/Structure/Types/TableType.php index 7e4b957..b812de4 100644 --- a/src/WebAssembly/Structure/Types/TableType.php +++ b/src/WebAssembly/Structure/Types/TableType.php @@ -6,9 +6,12 @@ namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types; final readonly class TableType { + /** + * @param ValType::FuncRef|ValType::ExternRef $refType + */ public function __construct( public Limits $limits, - public RefType $refType, + public ValType $refType, ) { } } diff --git a/src/WebAssembly/Structure/Types/ValType.php b/src/WebAssembly/Structure/Types/ValType.php index f6f3111..ace4604 100644 --- a/src/WebAssembly/Structure/Types/ValType.php +++ b/src/WebAssembly/Structure/Types/ValType.php @@ -4,22 +4,13 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types; -abstract readonly class ValType +enum ValType { - final public static function NumType(NumType $type): ValTypes\NumType - { - return new ValTypes\NumType($type); - } - - final public static function VecType(VecType $type): ValTypes\VecType - { - return new ValTypes\VecType($type); - } - - final public static function RefType(RefType $type): ValTypes\RefType - { - return new ValTypes\RefType($type); - } - - abstract public function equals(ValType $other): bool; + case I32; + case I64; + case F32; + case F64; + case V128; + case FuncRef; + case ExternRef; } diff --git a/src/WebAssembly/Structure/Types/ValTypes/NumType.php b/src/WebAssembly/Structure/Types/ValTypes/NumType.php deleted file mode 100644 index 1836679..0000000 --- a/src/WebAssembly/Structure/Types/ValTypes/NumType.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValTypes; - -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\NumType as OrigNumType; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; - -final readonly class NumType extends ValType -{ - protected function __construct(public OrigNumType $inner) - { - } - - public function equals(ValType $other): bool - { - return $other instanceof self - && $this->inner === $other->inner; - } -} diff --git a/src/WebAssembly/Structure/Types/ValTypes/RefType.php b/src/WebAssembly/Structure/Types/ValTypes/RefType.php deleted file mode 100644 index a31b7c3..0000000 --- a/src/WebAssembly/Structure/Types/ValTypes/RefType.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValTypes; - -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\RefType as OrigRefType; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; - -final readonly class RefType extends ValType -{ - protected function __construct(public OrigRefType $inner) - { - } - - public function equals(ValType $other): bool - { - return $other instanceof self - && $this->inner === $other->inner; - } -} diff --git a/src/WebAssembly/Structure/Types/ValTypes/VecType.php b/src/WebAssembly/Structure/Types/ValTypes/VecType.php deleted file mode 100644 index 03cc95e..0000000 --- a/src/WebAssembly/Structure/Types/ValTypes/VecType.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValTypes; - -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; -use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\VecType as OrigVecType; - -final readonly class VecType extends ValType -{ - protected function __construct(public OrigVecType $inner) - { - } - - public function equals(ValType $other): bool - { - return $other instanceof self - && $this->inner === $other->inner; // @phpstan-ignore-line - } -} diff --git a/src/WebAssembly/Structure/Types/VecType.php b/src/WebAssembly/Structure/Types/VecType.php deleted file mode 100644 index dcf7d68..0000000 --- a/src/WebAssembly/Structure/Types/VecType.php +++ /dev/null @@ -1,10 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types; - -enum VecType -{ - case V128; -} |
