Skip to content

Commit

Permalink
feat: add fuel_limit to Extism::Plugin->new
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Nov 27, 2024
1 parent 1331b26 commit 5aad512
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Extism/lib/Extism/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use warnings;
use Carp qw(croak);
use Extism::XS qw(
plugin_new
plugin_new_with_fuel_limit
plugin_new_error_free
plugin_call
plugin_error
Expand All @@ -31,20 +32,26 @@ sub new {
my ($name, $wasm, $options) = @_;
my $functions = [];
my $with_wasi = 0;
my $fuel_limit;
if ($options) {
if (exists $options->{functions}) {
$functions = $options->{functions};
}
if (exists $options->{wasi}) {
$with_wasi = $options->{wasi};
}
if (exists $options->{fuel_limit}) {
$fuel_limit = $options->{fuel_limit};
}
}
my $errptr = "\x00" x 8;
my $errptrptr = unpack('Q', pack('P', $errptr));
my @rawfunctions = map {$$_} @{$functions};
my $functionsarray = pack('Q*', @rawfunctions);
my $functionsptr = unpack('Q', pack('P', $functionsarray));
my $plugin = plugin_new($wasm, length($wasm), $functionsptr, scalar(@rawfunctions), $with_wasi, $errptrptr);
my $plugin = ! defined $fuel_limit
? plugin_new($wasm, length($wasm), $functionsptr, scalar(@rawfunctions), $with_wasi, $errptrptr)
: plugin_new_with_fuel_limit($wasm, length($wasm), $functionsptr, scalar(@rawfunctions), $with_wasi, $fuel_limit, $errptrptr);
if (! $plugin) {
my $errmsg = unpack('p', $errptr);
plugin_new_error_free(unpack('Q', $errptr));
Expand Down
1 change: 1 addition & 0 deletions Extism/lib/Extism/XS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ XSLoader::load('Extism::XS', $VERSION);
our @EXPORT_OK = qw(
version
plugin_new
plugin_new_with_fuel_limit
plugin_new_error_free
plugin_call
plugin_error
Expand Down
15 changes: 15 additions & 0 deletions Extism/lib/Extism/XS.xs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ExtismCurrentPlugin * T_PTR
ExtismMemoryHandle T_UV
const ExtismCancelHandle * T_PTR
PV T_PV
uint64_t T_UV
HERE

const char *
Expand All @@ -102,6 +103,20 @@ plugin_new(wasm, wasm_size, functions, n_functions, with_wasi, errmsg)
OUTPUT:
RETVAL

ExtismPlugin *
plugin_new_with_fuel_limit(wasm, wasm_size, functions, n_functions, with_wasi, fuel_limit, errmsg)
const uint8_t *wasm
ExtismSize wasm_size
const ExtismFunction **functions
ExtismSize n_functions
bool with_wasi
uint64_t fuel_limit
char **errmsg
CODE:
RETVAL = extism_plugin_new_with_fuel_limit(wasm, wasm_size, functions, n_functions, with_wasi, fuel_limit, errmsg);
OUTPUT:
RETVAL

void
plugin_new_error_free(err);
void *err
Expand Down
17 changes: 16 additions & 1 deletion Extism/t/02-extism.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Extism ':all';
use JSON::PP qw(encode_json decode_json);
use File::Temp qw(tempfile);
use Devel::Peek qw(Dump);
plan tests => 48;
plan tests => 50;

# ...
ok(Extism::version());
Expand Down Expand Up @@ -240,3 +240,18 @@ ok($@->{message});
my $fplugin = Extism::Plugin->new($hostwasm, {functions => [$voidfunction, $paramsfunction], wasi => 1});
$fplugin->call('call_hello_void', '', \%context);
}

# fuel
{
my $plugin = Extism::Plugin->new($wasm, {wasi => 1, fuel_limit => 100000});
my $output = $plugin->call('count_vowels', "this is a test");
my $outputhash = decode_json($output);
ok($outputhash->{count} == 4);
}
{
my $plugin = Extism::Plugin->new($wasm, {wasi => 1, fuel_limit => 5});
eval {
my $output = $plugin->call('count_vowels', "this is a test");
};
ok($@);
}

0 comments on commit 5aad512

Please sign in to comment.