1
1
<?php
2
- declare (strict_types=1 );
3
- namespace Extism ;
4
2
5
- require_once __DIR__ . "/LibExtism.php " ;
6
- require_once __DIR__ . "/CurrentPlugin.php " ;
3
+ declare (strict_types=1 );
7
4
5
+ namespace Extism ;
8
6
class ExtismValType
9
7
{
10
8
public const I32 = 0 ;
@@ -29,23 +27,23 @@ class ExtismValType
29
27
30
28
class HostFunction
31
29
{
32
- private \LibExtism $ lib ;
30
+ private \Extism \ Internal \ LibExtism $ lib ;
33
31
private $ callback ;
34
32
35
33
public \FFI \CData $ handle ;
36
34
37
35
/**
38
36
* Constructor
39
- *
37
+ *
40
38
* @param string $name Name of the function
41
39
* @param array $inputTypes Array of input types. @see ExtismValType
42
40
* @param array $outputTypes Array of output types
43
41
* @param callable $callback Callback to invoke when the function is called
44
- *
42
+ *
45
43
* @example ../tests/PluginTest.php 82 84 Simple Example
46
44
* @example ../tests/PluginTest.php 100 104 Manually read memory using CurrentPlugin
47
45
*/
48
- function __construct (string $ name , array $ inputTypes , array $ outputTypes , callable $ callback )
46
+ public function __construct (string $ name , array $ inputTypes , array $ outputTypes , callable $ callback )
49
47
{
50
48
$ reflection = new \ReflectionFunction ($ callback );
51
49
$ arguments = $ reflection ->getParameters ();
@@ -54,7 +52,7 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
54
52
global $ lib ;
55
53
56
54
if ($ lib == null ) {
57
- $ lib = new \LibExtism ();
55
+ $ lib = new \Extism \ Internal \ LibExtism ();
58
56
}
59
57
60
58
$ this ->lib = $ lib ;
@@ -80,9 +78,9 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
80
78
81
79
$ r = $ callback (...$ params );
82
80
83
- if ($ r == NULL ) {
81
+ if ($ r == null ) {
84
82
$ r = 0 ;
85
- } else if (gettype ($ r ) == "string " ) {
83
+ } elseif (gettype ($ r ) == "string " ) {
86
84
$ r = $ currentPlugin ->write_block ($ r );
87
85
}
88
86
@@ -106,7 +104,6 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
106
104
throw new \Exception ("Unsupported type for output: " . $ output ->t );
107
105
}
108
106
}
109
-
110
107
// Throwing an exception in FFI callback is not supported and
111
108
// causes a fatal error without a stack trace.
112
109
// So we catch it and print the exception manually
@@ -123,12 +120,12 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
123
120
$ this ->set_namespace ("extism:host/user " );
124
121
}
125
122
126
- function __destruct ()
123
+ public function __destruct ()
127
124
{
128
125
$ this ->lib ->extism_function_free ($ this ->handle );
129
126
}
130
127
131
- function set_namespace (string $ namespace )
128
+ public function set_namespace (string $ namespace )
132
129
{
133
130
$ this ->lib ->extism_function_set_namespace ($ this ->handle , $ namespace );
134
131
}
@@ -156,12 +153,12 @@ private static function get_type_name(\ReflectionParameter $param)
156
153
}
157
154
158
155
private static function get_parameters (
159
- CurrentPlugin $ currentPlugin ,
156
+ CurrentPlugin $ currentPlugin ,
160
157
\FFI \CData $ inputs ,
161
158
int $ n_inputs ,
162
- array $ arguments ,
163
- int $ offset) : array
164
- {
159
+ array $ arguments ,
160
+ int $ offset
161
+ ): array {
165
162
$ params = [];
166
163
167
164
if ($ offset == 1 ) {
@@ -219,7 +216,7 @@ private static function validate_arguments(array $arguments, array $inputTypes)
219
216
220
217
if ($ argType == null ) {
221
218
continue ;
222
- } else if ($ argType == "string " ) {
219
+ } elseif ($ argType == "string " ) {
223
220
// string is represented as a pointer to a block of memory
224
221
$ argType = "int " ;
225
222
}
0 commit comments