-
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: add Host Functions support (#13)
Fixes #11 - [x] Implement FFI calls - [x] Implement CurrentPlugin - [x] Implement HostFunction - [x] Fail fast when validating callback params - [x] Add PHPDoc comments - [x] Add README section - [x] Add tests - [x] Review for breaking changes - [ ] ~~User Data~~. Wasn't able to find a way to pin PHP objects and get their address. Users can use captured variables in the callback closures instead. --------- Co-authored-by: Steve Manuel <steve@dylib.so>
- Loading branch information
1 parent
b7a60b9
commit 0a522eb
Showing
12 changed files
with
611 additions
and
34 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 |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
/composer.lock | ||
**/vendor/ | ||
src/ExtismLib.php | ||
example/php_errors.log | ||
php_errors.log |
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,35 @@ | ||
<?php | ||
use Extism\PathWasmSource; | ||
use Extism\UrlWasmSource; | ||
use Extism\Manifest; | ||
use Extism\Plugin; | ||
use Extism\HostFunction; | ||
use Extism\ExtismValType; | ||
use Extism\CurrentPlugin; | ||
|
||
require_once __DIR__ . "/../src/Plugin.php"; | ||
require_once __DIR__ . "/../src/HostFunction.php"; | ||
|
||
$wasm = new PathWasmSource(__DIR__ . "/../wasm/count_vowels_kvstore.wasm"); | ||
$manifest = new Manifest($wasm); | ||
|
||
for ($i = 0; $i < 10_000; $i++){ | ||
$kvstore = []; | ||
|
||
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$kvstore) { | ||
return $kvstore[$key] ?? "\0\0\0\0"; | ||
}); | ||
|
||
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (string $key, string $value) use (&$kvstore) { | ||
$kvstore[$key] = $value; | ||
}); | ||
|
||
$plugin = new Plugin($manifest, true, [$kvRead, $kvWrite]); | ||
$output = $plugin->call("count_vowels", "Hello World!"); | ||
|
||
if ($i % 100 === 0) { | ||
echo "Iteration: $i\n"; | ||
} | ||
} | ||
|
||
readline(); |
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,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Extism; | ||
|
||
require_once __DIR__ . "/LibExtism.php"; | ||
|
||
/** | ||
* Represents a plugin that is calling the currently running host function. | ||
*/ | ||
class CurrentPlugin | ||
{ | ||
private \FFI\CData $handle; | ||
private \LibExtism $lib; | ||
|
||
/** | ||
* constructor. | ||
* | ||
* @param \LibExtism $lib | ||
* @param \FFI\CData $handle | ||
*/ | ||
function __construct($lib, \FFI\CData $handle) | ||
{ | ||
$this->handle = $handle; | ||
$this->lib = $lib; | ||
} | ||
|
||
/** | ||
* Reads a string from the plugin's memory at the given offset. | ||
* | ||
* @param int $offset Offset of the block to read. | ||
*/ | ||
function read_block(int $offset) : string | ||
{ | ||
$ptr = $this->lib->extism_current_plugin_memory($this->handle); | ||
$ptr = $this->lib->ffi->cast("char *", $ptr); | ||
$ptr = $this->lib->ffi->cast("char *", $ptr + $offset); | ||
|
||
$length = $this->lib->extism_current_plugin_memory_length($this->handle, $offset); | ||
|
||
return \FFI::string($ptr, $length); | ||
} | ||
|
||
/** | ||
* Allocates a block of memory in the plugin's memory and returns the offset. | ||
* | ||
* @param int $size Size of the block to allocate in bytes. | ||
*/ | ||
function allocate_block(int $size) : int | ||
{ | ||
return $this->lib->extism_current_plugin_memory_alloc($this->handle, $size); | ||
} | ||
|
||
/** | ||
* Writes a string to the plugin's memory, returning the offset of the block. | ||
* | ||
* @param string $data Buffer to write to the plugin's memory. | ||
*/ | ||
function write_block(string $data) : int | ||
{ | ||
$offset = $this->allocate_block(strlen($data)); | ||
$this->fill_block($offset, $data); | ||
return $offset; | ||
} | ||
|
||
/** | ||
* Fills a block of memory in the plugin's memory. | ||
* | ||
* @param int $offset Offset of the block to fill. | ||
* @param string $data Buffer to fill the block with. | ||
*/ | ||
function fill_block(int $offset, string $data) : void | ||
{ | ||
$ptr = $this->lib->extism_current_plugin_memory($this->handle); | ||
$ptr = $this->lib->ffi->cast("char *", $ptr); | ||
$ptr = $this->lib->ffi->cast("char *", $ptr + $offset); | ||
|
||
\FFI::memcpy($ptr, $data, strlen($data)); | ||
} | ||
|
||
/** | ||
* Frees a block of memory in the plugin's memory. | ||
* | ||
* @param int $offset Offset of the block to free. | ||
*/ | ||
function free_block(int $offset) : void | ||
{ | ||
$this->lib->extism_current_plugin_memory_free($this->handle, $offset); | ||
} | ||
} |
Oops, something went wrong.