aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/php-on-wasm/php-wasm.php
blob: 90a5c1a3534bd777eb26c1c262ee6e89de217e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

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'
echo "Hello, World!\n";
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);
$codePtr = allocateStringOnWasmMemory($runtime, PHP_HELLO_WORLD);

$results = $runtime->invoke("php_wasm_run", [$codePtr]);
\assert(\count($results) === 1);
$exitCode = $results[0];
\assert(\is_int($exitCode));