-
Notifications
You must be signed in to change notification settings - Fork 42
/
hello.php
48 lines (35 loc) · 1.2 KB
/
hello.php
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
echo 'Initializing...'.PHP_EOL;
$engine = wasm_engine_new();
$store = wasm_store_new($engine);
echo 'Loading binary...'.PHP_EOL;
$wasm = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'hello.wasm');
echo 'Compiling module...'.PHP_EOL;
$module = wasm_module_new($store, $wasm);
echo 'Creating callback...'.PHP_EOL;
function hello_callback()
{
echo 'Calling back...'.PHP_EOL;
echo '> Hello World!'.PHP_EOL;
return null;
}
$functype = wasm_functype_new(new Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = wasm_func_new($store, $functype, 'hello_callback');
wasm_functype_delete($functype);
echo 'Instantiating module...'.PHP_EOL;
$extern = wasm_func_as_extern($func);
$externs = new Wasm\Vec\Extern([$extern]);
$instance = wasm_instance_new($store, $module, $externs);
wasm_func_delete($func);
echo 'Extracting export...'.PHP_EOL;
$exports = wasm_instance_exports($instance);
$run = wasm_extern_as_func($exports[0]);
wasm_module_delete($module);
wasm_instance_delete($instance);
echo 'Calling export...'.PHP_EOL;
$args = new Wasm\Vec\Val();
$results = wasm_func_call($run, $args);
echo 'Shutting down...'.PHP_EOL;
wasm_store_delete($store);
wasm_engine_delete($engine);