diff options
| -rw-r--r-- | examples/hello-world/index.php | 5 | ||||
| -rw-r--r-- | examples/php-on-wasm/php-wasm.php | 4 | ||||
| -rw-r--r-- | examples/rubyvm-on-php-on-wasm/php-wasm.php | 4 | ||||
| -rw-r--r-- | src/WebAssembly/Execution/Runtime.php | 12 |
4 files changed, 16 insertions, 9 deletions
diff --git a/examples/hello-world/index.php b/examples/hello-world/index.php index 6129a07..74989a3 100644 --- a/examples/hello-world/index.php +++ b/examples/hello-world/index.php @@ -5,7 +5,6 @@ declare(strict_types=1); require_once __DIR__ . '/../../vendor/autoload.php'; use Nsfisis\Waddiwasi\Stream\BlobStream; -use Nsfisis\Waddiwasi\WebAssembly\BinaryFormat\Decoder; use Nsfisis\Waddiwasi\WebAssembly\Execution\Extern; use Nsfisis\Waddiwasi\WebAssembly\Execution\FuncInst; use Nsfisis\Waddiwasi\WebAssembly\Execution\Linker; @@ -23,13 +22,13 @@ $wasmBinary = ("" . "\x00\x41\xd7\x00\x10\x00\x41\xef\x00\x10\x00\x41\xf2\x00\x10\x00" . "\x41\xec\x00\x10\x00\x41\xe4\x00\x10\x00\x41\x21\x10\x00\x41\x0a" . "\x10\x00\x0b"); -$module = (new Decoder(new BlobStream($wasmBinary)))->decode(); +$wasmBinaryStream = new BlobStream($wasmBinary); $store = Store::empty(); $linker = new Linker($store); $linker->register('', 'putc', Extern::Func(FuncInst::Host(new FuncType([ValType::I32], []), function (Runtime $runtime, int $c) { printf('%c', $c); }))); -$runtime = Runtime::instantiate($module, $linker); +$runtime = Runtime::instantiateFromStream($wasmBinaryStream, $linker); $runtime->invoke('main', []); diff --git a/examples/php-on-wasm/php-wasm.php b/examples/php-on-wasm/php-wasm.php index 90a5c1a..c66b79c 100644 --- a/examples/php-on-wasm/php-wasm.php +++ b/examples/php-on-wasm/php-wasm.php @@ -5,7 +5,6 @@ declare(strict_types=1); require_once __DIR__ . '/../../vendor/autoload.php'; use Nsfisis\Waddiwasi\Stream\FileStream; -use Nsfisis\Waddiwasi\WebAssembly\BinaryFormat\Decoder; use Nsfisis\Waddiwasi\WebAssembly\Execution\Runtime; const PHP_HELLO_WORLD = <<<'EOS' @@ -15,9 +14,8 @@ EOS; $linker = (require_once __DIR__ . '/emscripten_bridge.php'); $wasmBinaryStream = new FileStream(__DIR__ . '/php-wasm.wasm'); -$module = (new Decoder($wasmBinaryStream))->decode(); -$runtime = Runtime::instantiate($module, $linker); +$runtime = Runtime::instantiateFromStream($wasmBinaryStream, $linker); $codePtr = allocateStringOnWasmMemory($runtime, PHP_HELLO_WORLD); $results = $runtime->invoke("php_wasm_run", [$codePtr]); diff --git a/examples/rubyvm-on-php-on-wasm/php-wasm.php b/examples/rubyvm-on-php-on-wasm/php-wasm.php index 148b98a..c89c8c4 100644 --- a/examples/rubyvm-on-php-on-wasm/php-wasm.php +++ b/examples/rubyvm-on-php-on-wasm/php-wasm.php @@ -5,7 +5,6 @@ declare(strict_types=1); require_once __DIR__ . '/../../vendor/autoload.php'; use Nsfisis\Waddiwasi\Stream\FileStream; -use Nsfisis\Waddiwasi\WebAssembly\BinaryFormat\Decoder; use Nsfisis\Waddiwasi\WebAssembly\Execution\Runtime; const PHP_HELLO_WORLD = <<<'EOS' @@ -13,11 +12,10 @@ require_once '%DIR%/HelloWorld.php'; EOS; $wasmBinaryStream = new FileStream(__DIR__ . '/php-wasm.wasm'); -$module = (new Decoder($wasmBinaryStream))->decode(); $linker = require_once __DIR__ . '/emscripten_bridge.php'; -$runtime = Runtime::instantiate($module, $linker); +$runtime = Runtime::instantiateFromStream($wasmBinaryStream, $linker); $codePtr = allocateStringOnWasmMemory($runtime, strtr(PHP_HELLO_WORLD, ['%DIR%' => __DIR__])); $results = $runtime->invoke("php_wasm_run", [$codePtr]); diff --git a/src/WebAssembly/Execution/Runtime.php b/src/WebAssembly/Execution/Runtime.php index 1bb440b..a120d7d 100644 --- a/src/WebAssembly/Execution/Runtime.php +++ b/src/WebAssembly/Execution/Runtime.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Execution; +use Nsfisis\Waddiwasi\Stream\StreamInterface; +use Nsfisis\Waddiwasi\WebAssembly\BinaryFormat\Decoder; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instr; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs; use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs\Control\BlockType; @@ -125,6 +127,16 @@ final class Runtime implements ExporterInterface return new self($store, $stack, $moduleInst); } + public static function instantiateFromStream( + StreamInterface $stream, + Linker $linker, + ): self { + return self::instantiate( + (new Decoder($stream))->decode(), + $linker, + ); + } + public function exports(): array { return $this->module->exports; |
