Skip to content

Commit

Permalink
Introduce function reference value types
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Aug 3, 2023
1 parent d62ecf7 commit 688e7ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib"]

[dependencies]
cargo-php = "0.1.7"
ext-php-rs = "0.10.1"
ext-php-rs = {version = "0.10.1", features = ["closure"] }
wasmer = { version = "4.1.0", default-features = true, features = ["sys"] }
wasmer-compiler = "4.1.0"

Expand Down
2 changes: 1 addition & 1 deletion ext-wasm.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __set(string $accessor, mixed $value): void {}
class InstanceBuilder {
public static function fromWat(string $wat): \Wasm\InstanceBuilder {}

public function import(array $imports): void {}
public function import(Imports $imports): void {}

public function build(): \Wasm\WasmInstance {}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/ImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ public function test_it_imports_simple_globals() {
self::assertSame([32], $instance->read_g());
}


public function test_it_can_call_external_function() {
$result = (object)['current' => false];

$imports = Imports::create();
$imports->define('env', 'greet', Type\Global::immutable(
function () use ($result) {
$result->current = true;
}
));

$builder = $this->createBuilderFromWat(
<<<'EOWAT'
(module
(import "env" "greet" (func $greet))
(func
call $greet
)
(start 1) ;; run the first function automatically
)
EOWAT
);


$builder->import($imports);
$instance = $builder->build();

self::assertTrue($result->current);
}

private function createBuilderFromWat(?string $wat = null): InstanceBuilder
{
return InstanceBuilder::fromWat($wat ?? <<<'EOWAT'
Expand Down

0 comments on commit 688e7ff

Please sign in to comment.