aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/WebAssembly
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-11 04:20:25 +0900
committernsfisis <nsfisis@gmail.com>2024-07-11 04:20:25 +0900
commit5be97eadbb1065f842aad4341cd4732498e0553b (patch)
tree827560953ea0abf494b3bb7aae9a987d105a8da3 /src/WebAssembly
parent8a083ed74e9f4472441175e187208012927ed357 (diff)
downloadphp-waddiwasi-5be97eadbb1065f842aad4341cd4732498e0553b.tar.gz
php-waddiwasi-5be97eadbb1065f842aad4341cd4732498e0553b.tar.zst
php-waddiwasi-5be97eadbb1065f842aad4341cd4732498e0553b.zip
feat: simplify FuncType structure
Diffstat (limited to 'src/WebAssembly')
-rw-r--r--src/WebAssembly/BinaryFormat/Decoder.php8
-rw-r--r--src/WebAssembly/Debug/Debug.php10
-rw-r--r--src/WebAssembly/Execution/Runtime.php23
-rw-r--r--src/WebAssembly/Structure/Types/FuncType.php29
-rw-r--r--src/WebAssembly/Structure/Types/ResultType.php31
5 files changed, 43 insertions, 58 deletions
diff --git a/src/WebAssembly/BinaryFormat/Decoder.php b/src/WebAssembly/BinaryFormat/Decoder.php
index e6e8bfb..0ec342c 100644
--- a/src/WebAssembly/BinaryFormat/Decoder.php
+++ b/src/WebAssembly/BinaryFormat/Decoder.php
@@ -31,7 +31,6 @@ 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\ResultType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\TableType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
use function array_reduce;
@@ -284,9 +283,12 @@ final class Decoder
return $this->decodeU32();
}
- private function decodeResultType(): ResultType
+ /**
+ * @return list<ValType>
+ */
+ private function decodeResultType(): array
{
- return new ResultType($this->decodeVec($this->decodeValType(...)));
+ return $this->decodeVec($this->decodeValType(...));
}
private function decodeFuncType(): FuncType
diff --git a/src/WebAssembly/Debug/Debug.php b/src/WebAssembly/Debug/Debug.php
index 60ac99b..99eefb0 100644
--- a/src/WebAssembly/Debug/Debug.php
+++ b/src/WebAssembly/Debug/Debug.php
@@ -11,7 +11,6 @@ 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\ResultType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\TableType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
@@ -64,8 +63,8 @@ final readonly class Debug
private static function funcTypeToString(FuncType $type): string
{
- $params = self::resultTypeToString($type->params);
- $results = self::resultTypeToString($type->results);
+ $params = implode(', ', array_map(self::valTypeToString(...), $type->params));
+ $results = implode(', ', array_map(self::valTypeToString(...), $type->results));
return "($params) -> ($results)";
}
@@ -96,9 +95,4 @@ final readonly class Debug
Mut::Var => 'var',
};
}
-
- private static function resultTypeToString(ResultType $type): string
- {
- return implode(', ', array_map(self::valTypeToString(...), $type->types));
- }
}
diff --git a/src/WebAssembly/Execution/Runtime.php b/src/WebAssembly/Execution/Runtime.php
index 4e8f3e8..d0f90b5 100644
--- a/src/WebAssembly/Execution/Runtime.php
+++ b/src/WebAssembly/Execution/Runtime.php
@@ -13,7 +13,6 @@ 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\ResultType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\TableType;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
use RuntimeException;
@@ -209,8 +208,8 @@ final class Runtime
$funcInst = $this->store->funcs[$funcAddr];
assert($funcInst instanceof FuncInsts\Wasm);
- $paramTypes = $funcInst->type->params->types;
- $resultTypes = $funcInst->type->results->types;
+ $paramTypes = $funcInst->type->params;
+ $resultTypes = $funcInst->type->results;
if (count($paramTypes) !== count($vals)) {
throw new RuntimeException("invoke($name) invalid function arity: expected " . count($paramTypes) . ", got " . count($vals));
}
@@ -249,9 +248,9 @@ final class Runtime
private function doInvokeWasmFunc(FuncInsts\Wasm $fn, int $funcAddr): void
{
- $paramTypes = $fn->type->params->types;
+ $paramTypes = $fn->type->params;
$n = count($paramTypes);
- $resultTypes = $fn->type->results->types;
+ $resultTypes = $fn->type->results;
$m = count($resultTypes);
$ts = $fn->code->locals;
$vals = $this->stack->popNValues($n);
@@ -304,9 +303,9 @@ final class Runtime
private function doInvokeHostFunc(FuncInsts\Host $fn): void
{
- $paramTypes = $fn->type->params->types;
+ $paramTypes = $fn->type->params;
$n = count($paramTypes);
- $resultTypes = $fn->type->results->types;
+ $resultTypes = $fn->type->results;
$m = count($resultTypes);
$params = array_reverse($this->stack->popNValues($n));
@@ -2464,8 +2463,8 @@ final class Runtime
$instrs = $instr->body;
$f = $this->stack->currentFrame();
$bt = self::expandBlockType($blockType, $f->module);
- $params = array_reverse($this->stack->popNValues(count($bt->params->types)));
- $n = count($bt->results->types);
+ $params = array_reverse($this->stack->popNValues(count($bt->params)));
+ $n = count($bt->results);
$l = new Label($n);
$result = $this->execInstrs($instrs, $l, $params);
if ($result === null) {
@@ -2573,7 +2572,7 @@ final class Runtime
$instrs = $instr->body;
$f = $this->stack->currentFrame();
$bt = self::expandBlockType($blockType, $f->module);
- $m = count($bt->params->types);
+ $m = count($bt->params);
$l = new Label($m);
while (true) {
$params = array_reverse($this->stack->popNValues($m));
@@ -2654,8 +2653,8 @@ final class Runtime
} elseif ($bt instanceof BlockTypes\ValType) {
$t = $bt->inner;
return new FuncType(
- new ResultType([]),
- new ResultType($t === null ? [] : [$t]),
+ [],
+ $t === null ? [] : [$t],
);
} else {
throw new RuntimeException("expand(): invalid blocktype");
diff --git a/src/WebAssembly/Structure/Types/FuncType.php b/src/WebAssembly/Structure/Types/FuncType.php
index 6c53519..9ea2434 100644
--- a/src/WebAssembly/Structure/Types/FuncType.php
+++ b/src/WebAssembly/Structure/Types/FuncType.php
@@ -4,17 +4,38 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types;
+use function count;
+
final readonly class FuncType
{
+ /**
+ * @param list<ValType> $params
+ * @param list<ValType> $results
+ */
public function __construct(
- public ResultType $params,
- public ResultType $results,
+ public array $params,
+ public array $results,
) {
}
public function equals(FuncType $other): bool
{
- return $this->params->equals($other->params)
- && $this->results->equals($other->results);
+ if (count($this->params) !== count($other->params)) {
+ return false;
+ }
+ if (count($this->results) !== count($other->results)) {
+ return false;
+ }
+ foreach ($this->params as $i => $type) {
+ if ($type !== $other->params[$i]) {
+ return false;
+ }
+ }
+ foreach ($this->results as $i => $type) {
+ if ($type !== $other->results[$i]) {
+ return false;
+ }
+ }
+ return true;
}
}
diff --git a/src/WebAssembly/Structure/Types/ResultType.php b/src/WebAssembly/Structure/Types/ResultType.php
deleted file mode 100644
index 9f44e81..0000000
--- a/src/WebAssembly/Structure/Types/ResultType.php
+++ /dev/null
@@ -1,31 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Nsfisis\Waddiwasi\WebAssembly\Structure\Types;
-
-use function count;
-
-final readonly class ResultType
-{
- /**
- * @param list<ValType> $types
- */
- public function __construct(
- public array $types,
- ) {
- }
-
- public function equals(ResultType $other): bool
- {
- if (count($this->types) !== count($other->types)) {
- return false;
- }
- foreach ($this->types as $i => $type) {
- if ($type !== $other->types[$i]) {
- return false;
- }
- }
- return true;
- }
-}