File Coverage

blib/lib/PJVM/Class/ConstantPool.pm
Criterion Covered Total %
statement 36 36 100.0
branch 5 8 62.5
condition n/a
subroutine 7 7 100.0
pod 0 3 0.0
total 48 54 88.8


line stmt bran cond sub pod time code
1             package PJVM::Class::ConstantPool;
2              
3 4     4   44233 use strict;
  4         9  
  4         139  
4 4     4   22 use warnings;
  4         15  
  4         100  
5              
6 4     4   2603 use PJVM::Class::ConstantPool::Types;
  4         9  
  4         251  
7              
8             use constant {
9 4         2678 CONSTANT_Class => 7,
10             CONSTANT_Fieldref => 9,
11             CONSTANT_Methodref => 10,
12             CONSTANT_InterfaceMethodref => 11,
13             CONSTANT_String => 8,
14             CONSTANT_Integer => 3,
15             CONSTANT_Float => 4,
16             CONSTANT_Long => 5,
17             CONSTANT_Double => 6,
18             CONSTANT_NameAndType => 12,
19             CONSTANT_Utf8 => 1,
20 4     4   26 };
  4         5  
21              
22             {
23             # Don't use fat comma (=>) here because we want to key to refer to a constant
24             my %constant_readers = (
25             CONSTANT_Class, "PJVM::Class::ConstantPool::Class",
26             CONSTANT_Fieldref, "PJVM::Class::ConstantPool::FieldRef",
27             CONSTANT_Methodref, "PJVM::Class::ConstantPool::MethodRef",
28             CONSTANT_InterfaceMethodref, "PJVM::Class::ConstantPool::InterfaceMethodRef",
29             CONSTANT_String, "PJVM::Class::ConstantPool::String",
30             CONSTANT_Integer, "PJVM::Class::ConstantPool::Integer",
31             CONSTANT_Float, "PJVM::Class::ConstantPool::Float",
32             CONSTANT_Long, "PJVM::Class::ConstantPool::Long",
33             CONSTANT_Double, "PJVM::Class::ConstantPool::Double",
34             CONSTANT_NameAndType, "PJVM::Class::ConstantPool::NameAndType",
35             CONSTANT_Utf8, "PJVM::Class::ConstantPool::Utf8",
36             );
37              
38             sub new_from_io {
39 4     4 0 100 my ($pkg, $io) = @_;
40            
41 4         11 my $buff;
42 4         37 read $io, $buff, 2;
43 4         18 my $count = unpack("n", $buff) - 1;
44              
45 4         11 my $constants = [];
46 4 50       40 if ($count) {
47 4         10 my $ix = 1;
48 4         6 my $tag;
49 4         16 while ($count--) {
50 96         159 read $io, $buff, 1;
51 96         160 my $tag = unpack("C", $buff);
52 96 50       224 die "Don't know how to read tag '${tag}'" unless exists $constant_readers{$tag};
53            
54 96         421 my $constant = $constant_readers{$tag}->new_from_io($tag, $io);
55 96         182 $constants->[$ix++] = $constant;
56 96 100       752 if ($constant->isa("PJVM::Class::ConstantPool::Extended")) {
57 4         11 $constants->[$ix++] = $constant;
58 4         11 $count--;
59             }
60             }
61             }
62            
63 4         13 my $self = bless $constants, $pkg;
64 4         15 return $self;
65             }
66             }
67              
68             sub length {
69 1     1 0 455 my $self = shift;
70 1         5 my $length = scalar @$self;
71 1 50       8 return $length ? $length - 1 : 0;
72             }
73              
74             sub get {
75 55     55 0 1523 my ($self, $ix) = @_;
76 55         654 return $self->[$ix];
77             }
78              
79             1;
80             __END__