diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-13 14:10:40 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-13 14:10:40 +0900 |
| commit | f26f497131923886889deb4b843b179518888b1f (patch) | |
| tree | 10ff8467135865d4aadb6bf4d3978dd1d011ed1a /examples | |
| parent | 1f4170811730477e9cd7d9620608c4ab619bdefc (diff) | |
| download | php-waddiwasi-f26f497131923886889deb4b843b179518888b1f.tar.gz php-waddiwasi-f26f497131923886889deb4b843b179518888b1f.tar.zst php-waddiwasi-f26f497131923886889deb4b843b179518888b1f.zip | |
feat: add examples/hello-world
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/hello-world/index.php | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/hello-world/index.php b/examples/hello-world/index.php new file mode 100644 index 0000000..9b0b1bd --- /dev/null +++ b/examples/hello-world/index.php @@ -0,0 +1,35 @@ +<?php + +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; +use Nsfisis\Waddiwasi\WebAssembly\Execution\Runtime; +use Nsfisis\Waddiwasi\WebAssembly\Execution\Store; +use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\FuncType; +use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType; + +$wasmBinary = ("" + . "\x00\x61\x73\x6d\x01\x00\x00\x00\x01\x08\x02\x60\x00\x00\x60\x01" + . "\x7f\x00\x02\x09\x01\x00\x04\x70\x75\x74\x63\x00\x01\x03\x02\x01" + . "\x00\x07\x08\x01\x04\x6d\x61\x69\x6e\x00\x01\x0a\x46\x01\x44\x00" + . "\x41\xc8\x00\x10\x00\x41\xe5\x00\x10\x00\x41\xec\x00\x10\x00\x41" + . "\xec\x00\x10\x00\x41\xef\x00\x10\x00\x41\x2c\x10\x00\x41\x20\x10" + . "\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(); + +$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($store, $module, $linker->resolve($module)); + +$runtime->invoke('main', []); |
