-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
<?php | ||
|
||
$instance = Wasm\InstanceBuilder::fromWat( | ||
<<<'EOWAT' | ||
(module | ||
(import "env" "global" (global $global (mut i32))) | ||
(func (export "read_g") (result i32) | ||
global.get $global) | ||
(func (export "write_g") (param i32) | ||
local.get 0 | ||
global.set $global)) | ||
EOWAT | ||
)->withImports([ | ||
'env' => [ | ||
'global' => 33 | ||
] | ||
])->build(); | ||
$instanceBuilder = Wasm\InstanceBuilder::fromWat( | ||
<<<'EOWAT' | ||
(module | ||
(import "env" "global" (global $global (mut i32))) | ||
(func (export "read_g") (result i32) | ||
global.get $global) | ||
(func (export "write_g") (param i32) | ||
local.get 0 | ||
global.set $global)) | ||
EOWAT | ||
); | ||
|
||
$imports = Wasm\Imports::create(); | ||
$imports->define('env', 'global', \Wasm\Type\Global::mutable(32)); | ||
$instanceBuilder->import($imports); | ||
|
||
$instance = $instanceBuilder->build(); | ||
|
||
var_dump($instance->read_g()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod value; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Wasm\InstanceBuilder; | ||
use Wasm\Imports; | ||
use Wasm\Type; | ||
|
||
class ImportsTest extends TestCase | ||
{ | ||
public function test_it_fails_on_missing_imports() { | ||
$this->expectException(\Exception::class); | ||
$this->createBuilderFromWat()->build(); | ||
} | ||
|
||
public function test_it_fails_on_unkown_env() { | ||
$imports = Imports::create(); | ||
$imports->define('unkown', 'global', Type\Global::mutable(32)); | ||
|
||
$builder = $this->createBuilderFromWat(); | ||
$builder->import($imports); | ||
|
||
$this->expectException(\Exception::class); | ||
$builder->build(); | ||
} | ||
|
||
public function test_it_fails_on_unkown_import_key() { | ||
$imports = Imports::create(); | ||
$imports->define('env', 'unkown', Type\Global::mutable(32)); | ||
|
||
$builder = $this->createBuilderFromWat(); | ||
$builder->import($imports); | ||
|
||
$this->expectException(\Exception::class); | ||
$builder->build(); | ||
} | ||
|
||
public function test_it_fails_on_invalid_type() { | ||
$imports = Imports::create(); | ||
$imports->define('env', 'global', Type\Global::mutable(1.1)); | ||
|
||
$builder = $this->createBuilderFromWat(); | ||
$builder->import($imports); | ||
|
||
$this->expectException(\Exception::class); | ||
$builder->build(); | ||
} | ||
|
||
public function test_it_fails_on_invalid_mutability() { | ||
$imports = Imports::create(); | ||
$imports->define('env', 'global', Type\Global::immutable(32)); | ||
|
||
$builder = $this->createBuilderFromWat(); | ||
$builder->import($imports); | ||
|
||
$this->expectException(\Exception::class); | ||
$builder->build(); | ||
} | ||
|
||
public function test_it_imports_simple_globals() { | ||
$imports = Imports::create(); | ||
$imports->define('env', 'global', Type\Global::mutable(32)); | ||
|
||
$builder = $this->createBuilderFromWat(); | ||
$builder->import($imports); | ||
|
||
$instance = $builder->build(); | ||
|
||
self::assertSame(32, $instance->read_g()); | ||
} | ||
|
||
private function createBuilderFromWat(?string $wat = null): InstanceBuilder | ||
{ | ||
return InstanceBuilder::fromWat($wat ?? <<<'EOWAT' | ||
(module | ||
(import "env" "global" (global $global (mut i32))) | ||
(func (export "read_g") (result i32) | ||
global.get $global) | ||
(func (export "write_g") (param i32) | ||
local.get 0 | ||
global.set $global)) | ||
EOWAT); | ||
} | ||
} |