-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rollup to libextism 1.9.1 (#32)
- [x] CompiledPlugin - [x] HTTP Response headers - [x] Call with Host Context - [x] Fuel limit support - [x] Update docs - [x] Add tests - [x] Check for backward compatibility
- Loading branch information
1 parent
d6d8bed
commit 43432a7
Showing
12 changed files
with
663 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Extism; | ||
|
||
/** | ||
* A pre-compiled plugin ready to be instantiated. | ||
*/ | ||
class CompiledPlugin | ||
{ | ||
private \FFI\CData $handle; | ||
private \Extism\Internal\LibExtism $lib; | ||
private array $functions; | ||
|
||
/** | ||
* Compile a plugin from a Manifest. | ||
* | ||
* @param Manifest $manifest A manifest that describes the Wasm binaries | ||
* @param array $functions Array of host functions | ||
* @param bool $withWasi Enable WASI support | ||
*/ | ||
public function __construct(Manifest $manifest, array $functions = [], bool $withWasi = false) | ||
{ | ||
global $lib; | ||
|
||
if ($lib === null) { | ||
$lib = new \Extism\Internal\LibExtism(); | ||
} | ||
|
||
$this->lib = $lib; | ||
$this->functions = $functions; | ||
|
||
$data = json_encode($manifest); | ||
if (!$data) { | ||
throw new \Extism\PluginLoadException("Failed to encode manifest"); | ||
} | ||
|
||
$errPtr = $lib->ffi->new($lib->ffi->type("char*")); | ||
|
||
$handle = $this->lib->extism_compiled_plugin_new( | ||
$data, | ||
strlen($data), | ||
$functions, | ||
count($functions), | ||
$withWasi, | ||
\FFI::addr($errPtr) | ||
); | ||
|
||
if (\FFI::isNull($errPtr) === false) { | ||
$error = \FFI::string($errPtr); | ||
$this->lib->extism_plugin_new_error_free($errPtr); | ||
throw new \Extism\PluginLoadException("Extism: unable to compile plugin: " . $error); | ||
} | ||
|
||
$this->handle = $handle; | ||
} | ||
|
||
/** | ||
* Instantiate a plugin from this compiled plugin. | ||
* | ||
* @return Plugin | ||
*/ | ||
public function instantiate(): Plugin | ||
{ | ||
$errPtr = $this->lib->ffi->new($this->lib->ffi->type("char*")); | ||
$nativeHandle = $this->lib->extism_plugin_new_from_compiled($this->handle, \FFI::addr($errPtr)); | ||
|
||
if (\FFI::isNull($errPtr) === false) { | ||
$error = \FFI::string($errPtr); | ||
$this->lib->extism_plugin_new_error_free($errPtr); | ||
throw new \Extism\PluginLoadException("Extism: unable to load plugin from compiled: " . $error); | ||
} | ||
|
||
$handle = new \Extism\Internal\PluginHandle($this->lib, $nativeHandle); | ||
|
||
return new Plugin($handle); | ||
} | ||
|
||
/** | ||
* Destructor to clean up resources | ||
*/ | ||
public function __destruct() | ||
{ | ||
$this->lib->extism_compiled_plugin_free($this->handle); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Extism\Internal; | ||
|
||
/** @internal */ | ||
class PluginHandle | ||
{ | ||
/** @internal */ | ||
public \Extism\Internal\LibExtism $lib; | ||
/** @internal */ | ||
public \FFI\CData $native; | ||
|
||
public function __construct($lib, \FFI\CData $handle) | ||
{ | ||
$this->native = $handle; | ||
$this->lib = $lib; | ||
} | ||
} |
Oops, something went wrong.