-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Host Functions compatible with PHP 7 #22
Comments
So I've poked at this a little bit, from what I can tell the following function is the culprit here: function toCArray(array $array, string $type): ?FFI\CData
{
if (count($array) == 0) {
return $this->ffi->new($type . "*");
}
$cArray = $this->ffi->new($type . "[" . count($array) . "]");
for ($i = 0; $i < count($array); $i++) {
$cArray[$i] = $array[$i];
}
return $cArray;
} But more specifically line 165, it's not exactly well documented from what I can tell, but PHP 7.4's FFI doesn't allow for assigning CData to structs or fields (see https://www.php.net/manual/en/class.ffi-cdata.php#Changelog). Where this applies to host functions is when converting the handle to CData when mapping the inputs / outputs here: https://github.com/extism/php-sdk/blob/main/src/LibExtism.php#L136 function extism_function_new(string $name, array $inputTypes, array $outputTypes, callable $callback, $userData, $freeUserData) : \FFI\CData
{
$inputs = $this->toCArray($inputTypes, "ExtismValType");
$outputs = $this->toCArray($outputTypes, "ExtismValType");
$handle = $this->ffi->extism_function_new($name, $inputs, count($inputTypes), $outputs, count($outputTypes), $callback, $userData, $freeUserData);
return $handle;
} Since the problematic line in $a = 0;
$increment_a = new HostFunction('increment_a', [], [],
function () use (&$a) {
$a++;
}
);
...
echo $a; I'll do some more digging into if there's an effective workaround for the CData assignment issue, but hopefully this gives better visibility on where the issue actually lies 😄 |
I believe I have a fix. Changing the following block from this: $inputs = [];
for ($i = 0; $i < count($inputTypes); $i++) {
$inputs[$i] = $this->lib->ffi->cast("ExtismValType", $inputTypes[$i]);
}
$outputs = [];
for ($i = 0; $i < count($outputTypes); $i++) {
$outputs[$i] = $this->lib->ffi->cast("ExtismValType", $outputTypes[$i]);
} To this: public const VAL_TYPE_MAP = [
0 => 'I32',
1 => 'I64',
2 => 'F32',
3 => 'F64',
4 => 'V128',
5 => 'FUNC_REF',
6 => 'EXTERN_REF',
]; $inputs = [];
for ($i = 0; $i < count($inputTypes); $i++) {
$enum = ExtismValType::VAL_TYPE_MAP[$inputTypes[$i]];
$inputs[$i] = $this->lib->ffi->$enum;
}
$outputs = [];
for ($i = 0; $i < count($outputTypes); $i++) {
$enum = ExtismValType::VAL_TYPE_MAP[$outputTypes[$i]];
$outputs[$i] = $this->lib->ffi->$enum;
} Allows the host functions to work as expected :) |
Fixes issue #22 that causes host functions with arguments to not work on PHP 7.4. See issue for context.
This is fixed in #23 |
When running the tests with PHPUnit 9 and PHP 7.4, this is one of our community members got:
The text was updated successfully, but these errors were encountered: