File Coverage

blib/lib/Wasm/Hook.pm
Criterion Covered Total %
statement 47 48 97.9
branch 8 10 80.0
condition n/a
subroutine 11 11 100.0
pod n/a
total 66 69 95.6


line stmt bran cond sub pod time code
1             package Wasm::Hook;
2              
3 1     1   229692 use strict;
  1         7  
  1         34  
4 1     1   6 use warnings;
  1         2  
  1         23  
5 1     1   481 use autodie;
  1         14353  
  1         5  
6 1     1   6842 use 5.008001;
  1         6  
7 1     1   500 use Wasm;
  1         786  
  1         6  
8 1     1   509 use Ref::Util qw( is_ref );
  1         1710  
  1         80  
9 1     1   791 use Path::Tiny qw( path );
  1         11275  
  1         57  
10 1     1   8 use Scalar::Util qw( refaddr );
  1         2  
  1         460  
11              
12             # ABSTRACT: Automatically load WebAssembly modules without a Perl wrapper
13             our $VERSION = '0.01'; # VERSION
14              
15              
16             sub _hook
17             {
18 2     2   21598 my(undef, $file) = @_;
19 2         8 foreach my $inc (@INC)
20             {
21 12 100       32 next if ref $inc;
22 11         26 my $pm = path($inc)->child($file);
23 11 50       664 return () if -f $pm;
24 11         175 my $basename = $pm->basename;
25 11         302 $basename =~ s/\.pm$//;
26 0         0 my($wa) = sort { $b->stat->mtime <=> $a->stat->mtime }
27 22         932 grep { -f $_ }
28 11         24 map { $pm->parent->child($basename . $_) }
  22         1146  
29             qw( .wasm .wat );
30 11 100       186 next unless defined $wa;
31 1 50       4 if(-f $wa)
32             {
33 1         18 my $package = $file;
34 1         5 $package =~ s/\.pm$//;
35 1         4 $package =~ s/\//::/g;
36 1         5 my $perl = qq{package $package; use Wasm -api => 0, -file => "$wa"; 1;\n};
37 1         5 my $fh;
38 1         4 open $fh, '<', \$perl;
39 1         3598 return ($fh);
40             }
41             }
42 1         630 return ();
43             }
44              
45             sub import
46             {
47 1     1   9 __PACKAGE__->unimport;
48 1         31 push @INC, \&_hook;
49             }
50              
51             sub unimport
52             {
53 2 100   2   7610 @INC = grep { !is_ref($_) || refaddr($_) != refaddr(\&_hook) } @INC;
  22         68  
54             }
55              
56             1;
57              
58             __END__