diff --git a/Cargo.toml b/Cargo.toml index 338e9a5..56c7b8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/ext-wasm.stubs.php b/ext-wasm.stubs.php index e10270e..840394c 100644 --- a/ext-wasm.stubs.php +++ b/ext-wasm.stubs.php @@ -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 {} } diff --git a/tests/ImportsTest.php b/tests/ImportsTest.php index bd92c3e..482d618 100644 --- a/tests/ImportsTest.php +++ b/tests/ImportsTest.php @@ -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'