@@ -7,6 +7,7 @@ use Carp qw(croak);
77use Extism::XS qw(
88 plugin_new
99 plugin_new_with_fuel_limit
10+ plugin_new_from_compiled
1011 plugin_allow_http_response_headers
1112 plugin_new_error_free
1213 plugin_call
@@ -22,6 +23,7 @@ use Extism::XS qw(
2223 ) ;
2324use Extism::Plugin::CallException;
2425use Extism::Plugin::CancelHandle;
26+ use Extism::CompiledPlugin qw( BuildPluginNewParams) ;
2527use Data::Dumper qw( Dumper) ;
2628use Devel::Peek qw( Dump) ;
2729use JSON::PP qw( encode_json) ;
@@ -31,38 +33,26 @@ our $VERSION = qv(v0.2.0);
3133
3234sub new {
3335 my ($name , $wasm , $options ) = @_ ;
34- my $functions = [];
35- my $with_wasi = 0;
36- my $fuel_limit ;
37- my $allow_http_response_headers ;
38- if ($options ) {
39- if (exists $options -> {functions }) {
40- $functions = $options -> {functions };
41- }
42- if (exists $options -> {wasi }) {
43- $with_wasi = $options -> {wasi };
44- }
45- if (exists $options -> {fuel_limit }) {
46- $fuel_limit = $options -> {fuel_limit };
47- }
48- if (exists $options -> {allow_http_response_headers }) {
49- $allow_http_response_headers = $options -> {allow_http_response_headers };
50- }
36+ my ($plugin , $opt , $errptr );
37+ if (!ref ($wasm ) || !$wasm -> isa(' Extism::CompiledPlugin' )) {
38+ $opt = defined $options ? {%$options } : {};
39+ my $p = BuildPluginNewParams($wasm , $opt );
40+ $plugin = ! defined $p -> {fuel_limit }
41+ ? plugin_new($p -> {wasm }, length ($p -> {wasm }), $p -> {functions }, $p -> {n_functions }, $p -> {wasi }, $p -> {errmsg })
42+ : plugin_new_with_fuel_limit($p -> {wasm }, length ($p -> {wasm }), $p -> {functions }, $p -> {n_functions }, $p -> {wasi }, $p -> {fuel_limit }, $p -> {errmsg });
43+ $errptr = $p -> {errptr };
44+ } else {
45+ $opt = $wasm -> {options };
46+ $errptr = " \x00 " x 8 ;
47+ my $errmsg = unpack (' Q' , pack (' P' , $errptr ));
48+ $plugin = plugin_new_from_compiled($wasm -> {compiled }, $errmsg );
5149 }
52- my $errptr = " \x00 " x 8 ;
53- my $errptrptr = unpack (' Q' , pack (' P' , $errptr ));
54- my @rawfunctions = map {$$_ } @{$functions };
55- my $functionsarray = pack (' Q*' , @rawfunctions );
56- my $functionsptr = unpack (' Q' , pack (' P' , $functionsarray ));
57- my $plugin = ! defined $fuel_limit
58- ? plugin_new($wasm , length ($wasm ), $functionsptr , scalar (@rawfunctions ), $with_wasi , $errptrptr )
59- : plugin_new_with_fuel_limit($wasm , length ($wasm ), $functionsptr , scalar (@rawfunctions ), $with_wasi , $fuel_limit , $errptrptr );
6050 if (! $plugin ) {
6151 my $errmsg = unpack (' p' , $errptr );
6252 plugin_new_error_free(unpack (' Q' , $errptr ));
6353 croak $errmsg ;
6454 }
65- if ($allow_http_response_headers ) {
55+ if ($opt -> { allow_http_response_headers } ) {
6656 plugin_allow_http_response_headers($plugin );
6757 }
6858 bless \$plugin , $name
0 commit comments