Skip to content

Commit 2efbc90

Browse files
committed
feat: per call context
1 parent 204f54e commit 2efbc90

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

Extism/lib/Extism/CurrentPlugin.pm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Extism::XS qw(current_plugin_memory
88
current_plugin_memory_alloc
99
current_plugin_memory_length
1010
current_plugin_memory_free
11+
current_plugin_host_context
1112
CopyToPtr);
1213

1314
use version 0.77;
@@ -53,4 +54,8 @@ sub memory_alloc_and_store {
5354
return $ptr;
5455
}
5556

57+
sub host_context {
58+
current_plugin_host_context($Extism::CurrentPlugin::instance)
59+
}
60+
5661
1; # End of Extism::CurrentPlugin

Extism/lib/Extism/Plugin.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ sub new {
5959
# passed to the plugin. If INPUT is a reference, the referenced item will be
6060
# encoded with json and then passed to the plugin.
6161
sub call {
62-
my ($self, $func_name, $input) = @_;
62+
my ($self, $func_name, $input, $host_context) = @_;
6363
$input //= '';
6464
my $type = reftype($input);
6565
if ($type) {
6666
$input = $$input if($type eq 'SCALAR');
6767
$input = encode_json($input);
6868
}
69-
my $rc = plugin_call($$self, $func_name, $input, length($input));
69+
my $rc = plugin_call($$self, $func_name, $input, length($input), $host_context);
7070
if ($rc != 0) {
7171
die Extism::Plugin::CallException->new($rc, plugin_error($$self));
7272
}

Extism/lib/Extism/XS.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ our @EXPORT_OK = qw(
3333
current_plugin_memory_alloc
3434
current_plugin_memory_length
3535
current_plugin_memory_free
36+
current_plugin_host_context
3637
log_file
3738
log_custom
3839
log_drain

Extism/lib/Extism/XS.xs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ plugin_new_error_free(err);
109109
extism_plugin_new_error_free(err);
110110

111111
int32_t
112-
plugin_call(plugin, func_name, data, data_len)
112+
plugin_call(plugin, func_name, data, data_len, host_context=&PL_sv_undef)
113113
ExtismPlugin *plugin
114114
const char *func_name
115115
const uint8_t *data
116116
ExtismSize data_len
117+
SV *host_context
117118
CODE:
118-
RETVAL = extism_plugin_call(plugin, func_name, data, data_len);
119+
RETVAL = extism_plugin_call_with_host_context(plugin, func_name, data, data_len, host_context);
119120
OUTPUT:
120121
RETVAL
121122

@@ -261,6 +262,17 @@ current_plugin_memory_free(plugin, handle)
261262
CODE:
262263
extism_current_plugin_memory_free(plugin, handle);
263264

265+
SV *
266+
current_plugin_host_context(plugin)
267+
ExtismCurrentPlugin *plugin
268+
CODE:
269+
RETVAL = extism_current_plugin_host_context(plugin);
270+
if (RETVAL != &PL_sv_undef) {
271+
SvREFCNT_inc_simple_NN(RETVAL);
272+
}
273+
OUTPUT:
274+
RETVAL
275+
264276
void
265277
log_file(filename, log_level)
266278
const char *filename

Extism/t/02-extism.t

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Extism ':all';
77
use JSON::PP qw(encode_json decode_json);
88
use File::Temp qw(tempfile);
99
use Devel::Peek qw(Dump);
10-
plan tests => 45;
10+
plan tests => 48;
1111

1212
# ...
1313
ok(Extism::version());
@@ -213,3 +213,30 @@ eval {
213213
ok($@);
214214
ok($@->{code} != 0);
215215
ok($@->{message});
216+
217+
# Test host_context
218+
{
219+
my $voidfunction = Extism::Function->new("hello_void", [], [], sub {
220+
ok(! defined Extism::CurrentPlugin::host_context);
221+
return;
222+
});
223+
my $paramsfunction = Extism::Function->new("hello_params", [Extism_F64, Extism_I32, Extism_F32, Extism_I64], [Extism_I64], sub {
224+
# not called
225+
});
226+
my $fplugin = Extism::Plugin->new($hostwasm, {functions => [$voidfunction, $paramsfunction], wasi => 1});
227+
$fplugin->call('call_hello_void');
228+
}
229+
{
230+
my %context = ( abc => 123);
231+
my $voidfunction = Extism::Function->new("hello_void", [], [], sub {
232+
my $ctx = Extism::CurrentPlugin::host_context;
233+
is_deeply($ctx, \%context);
234+
ok($ctx == \%context);
235+
return;
236+
});
237+
my $paramsfunction = Extism::Function->new("hello_params", [Extism_F64, Extism_I32, Extism_F32, Extism_I64], [Extism_I64], sub {
238+
# not called
239+
});
240+
my $fplugin = Extism::Plugin->new($hostwasm, {functions => [$voidfunction, $paramsfunction], wasi => 1});
241+
$fplugin->call('call_hello_void', '', \%context);
242+
}

0 commit comments

Comments
 (0)