-
Notifications
You must be signed in to change notification settings - Fork 42
/
instance.php
42 lines (31 loc) · 1.06 KB
/
instance.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
<?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 $add_one_t (func (param i32) (result i32)))
(func $add_one_f (type $add_one_t) (param $value i32) (result i32)
local.get $value
i32.const 1
i32.add)
(export "add_one" (func $add_one_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();
$addOne = (new Wasm\Extern($exports[0]))->asFunc();
$arg = Wasm\Val::newI32(1);
$args = new Wasm\Vec\Val([$arg->inner()]);
echo 'Calling `add_one` function...'.PHP_EOL;
$result = $addOne($args);
echo 'Results of `add_one`: '.((new Wasm\Val($result[0]))->value()).PHP_EOL;