From af41dac9ce52a3d9ae3ac875c32a96d0508068e2 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Wed, 16 Oct 2024 15:18:35 +0300 Subject: [PATCH 1/2] fix: fix some of the papercuts and bugs i encoutered in the akaunting demo --- src/ExtismCallException.php | 15 +++++++++++++++ src/ExtismValType.php | 2 +- src/Internal/LibExtism.php | 2 +- src/Manifest/PathWasmSource.php | 2 +- src/Plugin.php | 8 ++++++-- src/PluginLoadException.php | 9 +++++++++ 6 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/ExtismCallException.php create mode 100644 src/PluginLoadException.php diff --git a/src/ExtismCallException.php b/src/ExtismCallException.php new file mode 100644 index 0000000..81047d1 --- /dev/null +++ b/src/ExtismCallException.php @@ -0,0 +1,15 @@ +error = $error; + $this->functionName = $functionName; + } +} \ No newline at end of file diff --git a/src/ExtismValType.php b/src/ExtismValType.php index 0a78b0f..f3d5084 100644 --- a/src/ExtismValType.php +++ b/src/ExtismValType.php @@ -6,7 +6,7 @@ class ExtismValType { public const I32 = 0; public const I64 = 1; - public const PTR = I64; + public const PTR = ExtismValType::I64; public const F32 = 2; public const F64 = 3; public const V128 = 4; diff --git a/src/Internal/LibExtism.php b/src/Internal/LibExtism.php index c145469..f0bda39 100644 --- a/src/Internal/LibExtism.php +++ b/src/Internal/LibExtism.php @@ -52,7 +52,7 @@ private function soname() case "Windows NT": return "extism.dll"; default: - throw new \Exception("Extism: unsupported platform " . $platform); + throw new \RuntimeException("Extism: unsupported platform " . $platform); } } diff --git a/src/Manifest/PathWasmSource.php b/src/Manifest/PathWasmSource.php index 72b243d..22b5217 100644 --- a/src/Manifest/PathWasmSource.php +++ b/src/Manifest/PathWasmSource.php @@ -19,7 +19,7 @@ public function __construct($path, $name = null, $hash = null) $this->path = realpath($path); if (!$this->path) { - throw new \Exception("Path not found: '" . $path . "'"); + throw new \PluginLoadException("Path not found: '" . $path . "'"); } $this->name = $name ?? pathinfo($path, PATHINFO_FILENAME); diff --git a/src/Plugin.php b/src/Plugin.php index cb64314..4299708 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -57,7 +57,7 @@ public function __construct(Manifest $manifest, bool $with_wasi = false, array $ if (\FFI::isNull($errPtr) == false) { $error = \FFI::string($errPtr); $this->lib->extism_plugin_new_error_free($errPtr); - throw new \Exception("Extism: unable to load plugin: " . $error); + throw new \PluginLoadException("Extism: unable to load plugin: " . $error); } $this->handle = $handle; @@ -93,13 +93,17 @@ public function functionExists(string $name) */ public function call(string $name, string $input = null): string { + if ($input == null) { + $input = ""; + } + $rc = $this->lib->extism_plugin_call($this->handle, $name, $input, strlen($input)); $msg = "code = " . $rc; $err = $this->lib->extism_error($this->handle); if ($err) { $msg = $msg . ", error = " . $err; - throw new \Exception("Extism: call to '" . $name . "' failed with " . $msg); + throw new \ExtismCallException("Extism: call to '" . $name . "' failed with " . $msg, $err, $name); } return $this->lib->extism_plugin_output_data($this->handle); diff --git a/src/PluginLoadException.php b/src/PluginLoadException.php new file mode 100644 index 0000000..a8260de --- /dev/null +++ b/src/PluginLoadException.php @@ -0,0 +1,9 @@ + Date: Wed, 16 Oct 2024 15:28:16 +0300 Subject: [PATCH 2/2] fix tests --- src/{ExtismCallException.php => FunctionCallException.php} | 4 +++- src/Manifest/PathWasmSource.php | 2 +- src/Plugin.php | 4 ++-- src/PluginLoadException.php | 4 +++- 4 files changed, 9 insertions(+), 5 deletions(-) rename src/{ExtismCallException.php => FunctionCallException.php} (81%) diff --git a/src/ExtismCallException.php b/src/FunctionCallException.php similarity index 81% rename from src/ExtismCallException.php rename to src/FunctionCallException.php index 81047d1..ff348a7 100644 --- a/src/ExtismCallException.php +++ b/src/FunctionCallException.php @@ -1,6 +1,8 @@ path = realpath($path); if (!$this->path) { - throw new \PluginLoadException("Path not found: '" . $path . "'"); + throw new \Extism\PluginLoadException("Path not found: '" . $path . "'"); } $this->name = $name ?? pathinfo($path, PATHINFO_FILENAME); diff --git a/src/Plugin.php b/src/Plugin.php index 4299708..4c88394 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -57,7 +57,7 @@ public function __construct(Manifest $manifest, bool $with_wasi = false, array $ if (\FFI::isNull($errPtr) == false) { $error = \FFI::string($errPtr); $this->lib->extism_plugin_new_error_free($errPtr); - throw new \PluginLoadException("Extism: unable to load plugin: " . $error); + throw new \Extism\PluginLoadException("Extism: unable to load plugin: " . $error); } $this->handle = $handle; @@ -103,7 +103,7 @@ public function call(string $name, string $input = null): string $err = $this->lib->extism_error($this->handle); if ($err) { $msg = $msg . ", error = " . $err; - throw new \ExtismCallException("Extism: call to '" . $name . "' failed with " . $msg, $err, $name); + throw new \Extism\FunctionCallException("Extism: call to '" . $name . "' failed with " . $msg, $err, $name); } return $this->lib->extism_plugin_output_data($this->handle); diff --git a/src/PluginLoadException.php b/src/PluginLoadException.php index a8260de..e895a11 100644 --- a/src/PluginLoadException.php +++ b/src/PluginLoadException.php @@ -1,6 +1,8 @@