aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/rubyvm-on-php-on-wasm/php-wasm.php
blob: 3f5b9d0990566608393f11a4bc822badeeeec70a (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\Execution\Runtime;

const PHP_HELLO_WORLD = <<<'EOS'
require_once '%DIR%/HelloWorld.php';
EOS;

$wasmBinaryStream = new FileStream(__DIR__ . '/php-wasm.wasm');

$linker = require_once __DIR__ . '/emscripten_bridge.php';

$runtime = Runtime::instantiateFromStream($wasmBinaryStream, $linker);
$codePtr = allocateStringOnWasmMemory($runtime, strtr(PHP_HELLO_WORLD, [
    '%DIR%' => __DIR__,
]));

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