-
Notifications
You must be signed in to change notification settings - Fork 42
/
exports-function.php
43 lines (32 loc) · 1.1 KB
/
exports-function.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
<?php
declare(strict_types=1);
require_once __DIR__.'/../vendor/autoload.php';
// Let's declare the Wasm module.
//
// We are using the text representation of the module here.
$wasmBytes = Wasm\Wat::wasm(<<<'WAT'
(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add)
(export "sum" (func $sum_f)))
WAT);
// Create an Engine
$engine = Wasm\Engine::new();
// Create a Store
$store = Wasm\Store::new($engine);
echo 'Compiling module...'.PHP_EOL;
$module = Wasm\Module::new($store, $wasmBytes);
echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Instance::new($store, $module);
// Extracting export...
$exports = $instance->exports();
$sum = (new Wasm\Extern($exports[0]))->asFunc();
$firstArg = Wasm\Val::newI32(1);
$secondArg = Wasm\Val::newI32(2);
$args = new Wasm\Vec\Val([$firstArg->inner(), $secondArg->inner()]);
echo 'Calling `sum` function...'.PHP_EOL;
$result = $sum($args);
echo 'Results of `sum`: '.((new Wasm\Val($result[0]))->value()).PHP_EOL;