Extism
view release on metacpan or search on metacpan
t/02-extism.t view on Meta::CPAN
#url => "https://github.com/extism/plugins/releases/latest/download/count_vowels_kvstore.wasm"
path => 'count_vowels_kvstore.wasm',
}
],
});
# ... with low level api
my @lowlevel;
{
my %kv_store;
my $kv_read = Extism::Function->new("kv_read", [Extism_I64], [Extism_I64], sub {
my ($key_ptr) = @_;
my $key = Extism::CurrentPlugin::memory_load_from_handle($key_ptr);
if (!exists $kv_store{$key}) {
return Extism::CurrentPlugin::memory_alloc_and_store("\x00" x 4);
} else {
return Extism::CurrentPlugin::memory_alloc_and_store($kv_store{$key});
}
});
ok($kv_read);
my $kv_write = Extism::Function->new("kv_write", [Extism_I64, Extism_I64], [], sub {
my ($key_ptr, $value_ptr) = @_;
my $key = Extism::CurrentPlugin::memory_load_from_handle($key_ptr);
my $value = Extism::CurrentPlugin::memory_load_from_handle($value_ptr);
$kv_store{$key} = $value;
return;
});
ok($kv_write);
my $fplugin = Extism::Plugin->new($count_vowels_kv, {functions => [$kv_read, $kv_write], wasi => 1});
ok($fplugin);
my $hello = "Hello, World!";
$lowlevel[0] = $fplugin->call("count_vowels", $hello);
$lowlevel[1] = $fplugin->call("count_vowels", $hello);
}
# ... with high level api
my @highlevel;
{
my %kv_store;
my $kv_read = Extism::Function->new("kv_read", [Extism_String], [Extism_String], sub {
my ($key) = @_;
if (!exists $kv_store{$key}) {
return "\x00" x 4;
} else {
return $kv_store{$key};
}
});
ok($kv_read);
my $kv_write = Extism::Function->new("kv_write", [Extism_String, Extism_String], [], sub {
my ($key, $value) = @_;
$kv_store{$key} = $value;
return;
});
ok($kv_write);
my $fplugin = Extism::Plugin->new($count_vowels_kv, {functions => [$kv_read, $kv_write], wasi => 1});
ok($fplugin);
my $hello = "Hello, World!";
$highlevel[0] = $fplugin->call("count_vowels", $hello);
$highlevel[1] = $fplugin->call("count_vowels", $hello);
}
my @decoded = map {decode_json $_} @highlevel;
ok($decoded[0]{count} == 3);
ok($decoded[0]{count} == $decoded[1]{count});
ok($decoded[0]{total} == 3);
ok($decoded[1]{total} == 6);
# Verify both sets of results are the same
is($highlevel[0], $lowlevel[0]);
is($highlevel[1], $lowlevel[1]);
# test unreachable plugin
my $unreachable = encode_json({
wasm => [
{
path => 'unreachable.wasm',
}
],
});
my $uplugin = Extism::Plugin->new($unreachable, {wasi => 1});
ok($uplugin);
eval {
$uplugin->call('do_unreachable');
fail('calling do_unreachable should throw an exception');
};
ok($@);
ok($@->{code} != 0);
ok($@->{message});
# Test host_context
{
my $voidfunction = Extism::Function->new("hello_void", [], [], sub {
ok(! defined Extism::CurrentPlugin::host_context);
return;
});
my $paramsfunction = Extism::Function->new("hello_params", [Extism_F64, Extism_I32, Extism_F32, Extism_I64], [Extism_I64], sub {
# not called
});
my $fplugin = Extism::Plugin->new($hostwasm, {functions => [$voidfunction, $paramsfunction], wasi => 1});
$fplugin->call('call_hello_void');
}
{
my %context = ( abc => 123);
my $voidfunction = Extism::Function->new("hello_void", [], [], sub {
my $ctx = Extism::CurrentPlugin::host_context;
is_deeply($ctx, \%context);
ok($ctx == \%context);
return;
});
my $paramsfunction = Extism::Function->new("hello_params", [Extism_F64, Extism_I32, Extism_F32, Extism_I64], [Extism_I64], sub {
# not called
});
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 {
( run in 0.600 second using v1.01-cache-2.11-cpan-39bf76dae61 )