aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-10 23:01:23 +0900
committernsfisis <nsfisis@gmail.com>2024-07-10 23:01:23 +0900
commita84a42244583a450686add2af2e0c9cb8a8f05b2 (patch)
tree6964092cdc61d2dddd04da858a5739b75738dfbe /examples
parent5a07cfb8f4eab8c63bdedb73cd3dbce270444d24 (diff)
downloadphp-waddiwasi-a84a42244583a450686add2af2e0c9cb8a8f05b2.tar.gz
php-waddiwasi-a84a42244583a450686add2af2e0c9cb8a8f05b2.tar.zst
php-waddiwasi-a84a42244583a450686add2af2e0c9cb8a8f05b2.zip
refactor: example code
Diffstat (limited to 'examples')
-rw-r--r--examples/php-on-wasm/php-wasm.php16
-rw-r--r--examples/rubyvm-on-php-on-wasm/php-wasm.php16
2 files changed, 10 insertions, 22 deletions
diff --git a/examples/php-on-wasm/php-wasm.php b/examples/php-on-wasm/php-wasm.php
index 336602c..92871cd 100644
--- a/examples/php-on-wasm/php-wasm.php
+++ b/examples/php-on-wasm/php-wasm.php
@@ -7,6 +7,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';
use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\BinaryFormat\InvalidBinaryFormatException;
use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\Externs;
use Nsfisis\Waddiwasi\Execution\FuncInst;
use Nsfisis\Waddiwasi\Execution\Refs;
use Nsfisis\Waddiwasi\Execution\Runtime;
@@ -32,7 +33,7 @@ try {
exit(1);
}
-$importModules = [
+$imports = [
'env' => [
'invoke_iii' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
'invoke_iiiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
@@ -132,14 +133,7 @@ $importModules = [
],
];
-$store = Store::empty();
-$importObj = [];
-foreach ($importModules as $importModuleName => $importModule) {
- foreach ($importModule as $importName => $import) {
- $importObj[$importModuleName][$importName] = Extern::Func($import);
- }
-}
-$runtime = Runtime::instantiate($store, $module, $importObj);
+$runtime = Runtime::instantiate(Store::empty(), $module, $imports);
$codePtr = allocateStringOnWasmMemory($runtime, PHP_HELLO_WORLD);
$results = $runtime->invoke("php_wasm_run", [$codePtr]);
@@ -187,7 +181,7 @@ function copyStringToWasmMemory(Runtime $runtime, int $dst, string $src): void
\assert($ok);
}
-function makeHostFunc(string $typeDef, callable $fn): FuncInst
+function makeHostFunc(string $typeDef, callable $fn): Externs\Func
{
$stringToType = fn (string $s) => match ($s) {
'i32' => ValType::NumType(NumType::I32),
@@ -202,7 +196,7 @@ function makeHostFunc(string $typeDef, callable $fn): FuncInst
$params = array_map($stringToType, $paramsDef === '' ? [] : explode(', ', $paramsDef));
$results = array_map($stringToType, $resultsDef === '' ? [] : explode(', ', $resultsDef));
$type = new FuncType(new ResultType($params), new ResultType($results));
- return FuncInst::Host($type, $fn);
+ return Extern::Func(FuncInst::Host($type, $fn));
}
function getWasmTableEntry(Runtime $runtime, int $funcPtr): int
diff --git a/examples/rubyvm-on-php-on-wasm/php-wasm.php b/examples/rubyvm-on-php-on-wasm/php-wasm.php
index 5d411cc..b24641d 100644
--- a/examples/rubyvm-on-php-on-wasm/php-wasm.php
+++ b/examples/rubyvm-on-php-on-wasm/php-wasm.php
@@ -7,6 +7,7 @@ require_once __DIR__ . '/vendor/autoload.php';
use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\BinaryFormat\InvalidBinaryFormatException;
use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\Externs;
use Nsfisis\Waddiwasi\Execution\FuncInst;
use Nsfisis\Waddiwasi\Execution\MemInst;
use Nsfisis\Waddiwasi\Execution\Refs;
@@ -33,7 +34,7 @@ try {
exit(1);
}
-$importModules = [
+$imports = [
'env' => [
'invoke_iii' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
'invoke_iiiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
@@ -133,14 +134,7 @@ $importModules = [
],
];
-$store = Store::empty();
-$importObj = [];
-foreach ($importModules as $importModuleName => $importModule) {
- foreach ($importModule as $importName => $import) {
- $importObj[$importModuleName][$importName] = Extern::Func($import);
- }
-}
-$runtime = Runtime::instantiate($store, $module, $importObj);
+$runtime = Runtime::instantiate(Store::empty(), $module, $imports);
$codePtr = allocateStringOnWasmMemory($runtime, strtr(PHP_HELLO_WORLD, ['%DIR%' => __DIR__]));
$results = $runtime->invoke("php_wasm_run", [$codePtr]);
@@ -204,7 +198,7 @@ function copyStringToWasmMemory(Runtime $runtime, int $dst, string $src): void
\assert($ok);
}
-function makeHostFunc(string $typeDef, callable $fn): FuncInst
+function makeHostFunc(string $typeDef, callable $fn): Externs\Func
{
$stringToType = fn (string $s) => match ($s) {
'i32' => ValType::NumType(NumType::I32),
@@ -219,7 +213,7 @@ function makeHostFunc(string $typeDef, callable $fn): FuncInst
$params = array_map($stringToType, $paramsDef === '' ? [] : explode(', ', $paramsDef));
$results = array_map($stringToType, $resultsDef === '' ? [] : explode(', ', $resultsDef));
$type = new FuncType(new ResultType($params), new ResultType($results));
- return FuncInst::Host($type, $fn);
+ return Extern::Func(FuncInst::Host($type, $fn));
}
function getWasmTableEntry(Runtime $runtime, int $funcPtr): int