lib/Devel/ebug/Wire.pm

filter: Statementstmt 64.0% 91 / 142 Branchbran 51.1% 43 / 84 Conditioncond 35.2% 12 / 34 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 81.2% 13 / 16 Podpod 100% 6 / 6 totaltotal 56.6% 171 / 302 CC 57 SCAR 29.1
CC
57
Cov
56%
_inflate
34.0
_inflate
34.0
_deflate
20.0
CRAP 18.4
« blib/lib/Devel/ebug/Backend.pm ↑ Summary lib/Devel/ebug/Plugin/StackTrace.pm »
1 package Devel::ebug::Wire;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Serialization for the Devel::ebug wire protocol
9 38 our $VERSION = '0.69'; # VERSION
10  
11  
12 38 our $JSON;
13  
14 # Tried in order; the first that loads wins. The XS module is much faster,
15 # and the protocol is chatty enough that it shows.
16 38 our @JSON_CLASSES = qw( Cpanel::JSON::XS JSON::PP );
17  
18 # The two formats cannot be confused: a hex packed YAML line is made up
19 # entirely of [0-9a-f], and a JSON object always opens with a brace. That
20 # lets the backend answer each request in the format it arrived in, which
21 # in turn means a frontend can choose freely without arranging anything
22 # with the backend beforehand.
23 487 ▶sub detect {
Subroutine detect: called CC=3 SCAR=12.5
Pod documented
24 0 ▶ my($line) = @_;
Subroutine detect: called CC=3 SCAR=12.5
Pod documented
25 0 ▶ return 'yaml' unless defined $line;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $line 1 486 487
unless defined $line 0 5 5
26 0 $line =~ s/^\s+//;
27 0 ▶ return substr($line, 0, 1) eq '{' ? 'json' : 'yaml';
Branch: 2 branches
BranchTrueFalseTotal
substr($line, 0, 1) eq '{' ? : 10 476 486
substr($line, 0, 1) eq '{' ? : 0 5 5
28 }
29  
30 45 ▶sub _json {
Subroutine _json: called CC=5 SCAR=22.9
31 0 ▶ return $JSON if $JSON;
Subroutine _json: not called CC=5 SCAR=22.9
Branch: 2 branches
BranchTrueFalseTotal
if $Devel::ebug::Wire::JSON 42 3 45
if $Devel::ebug::Wire::JSON 0 0 0
32 0 my $class;
33 0 foreach my $try (@JSON_CLASSES) {
34 0 (my $pm = "$try.pm") =~ s{::}{/}g;
35 0 ▶ if (eval { require $pm; 1 }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require $pm; 1 } }) 3 0 3
if (eval { do { require $pm; 1 } }) 0 0 0
36 0 $class = $try;
37 0 last;
38 }
39 }
40 0 ▶ croak "the json serializer needs one of @{[ join ' or ', @JSON_CLASSES ]}, none of which could be loaded"
Branch: 2 branches
BranchTrueFalseTotal
unless $class 0 3 3
unless $class 0 0 0
41 unless $class;
42 # canonical keeps the output stable, which makes the protocol diffable
43 # and the tests repeatable; allow_nonref so a bare scalar is legal.
44 0 $JSON = $class->new->utf8->canonical->allow_nonref;
45 0 return $JSON;
46 }
47  
48 # Walk a structure into something JSON can hold, tagging what it cannot.
49 69 ▶sub _deflate {
Subroutine _deflate: called CC=9 SCAR=30.0
50 0 ▶ my($data, $seen) = @_;
Subroutine _deflate: not called CC=9 SCAR=30.0
51  
52 0 my $type = reftype $data;
53 0 ▶ return $data unless defined $type;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $type 35 33 68
unless defined $type 0 0 0
54  
55 # Cycles have no JSON representation. YAML would emit an anchor; here the
56 # best that can be done is to break the loop visibly rather than recurse
57 # until the stack gives out.
58 0 ▶ $seen ||= {};
Condition50%2/4
$seen ||= {}
l!l
1221
$seen ||= {}
l!l
00
MC/DC100%1/1
$seen ||= {}
$seen
MC/DC0%0/1
$seen ||= {}
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
Truth table0%0/2
A
A= $seen
Aresult
11
00
59 0 my $addr = refaddr $data;
60 0 ▶ return '*CYCLE*' if $seen->{$addr};
Branch: 2 branches
BranchTrueFalseTotal
if $seen->{$addr} 2 31 33
if $seen->{$addr} 0 0 0
61 0 local $seen->{$addr} = 1;
62  
63 0 my $class = blessed $data;
64 0 my $out;
65  
66 0 ▶ if ($type eq 'HASH') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 23 8 31
elsif ($type eq 'ARRAY') { } 4 4 8
elsif ($type eq 'SCALAR' or $type eq 'REF') { } 2 2 4
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR' or $type eq 'REF') { } 0 0 0
67 0 $out = { map { $_ => _deflate($data->{$_}, $seen) } keys %$data };
68 } elsif ($type eq 'ARRAY') {
69 0 $out = [ map { _deflate($_, $seen) } @$data ];
70 ▶ } elsif ($type eq 'SCALAR' || $type eq 'REF') {
Condition33%2/6
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
202
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
000
MC/DC50%1/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
MC/DC0%0/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table66%2/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
71 0 $out = { __scalarref__ => _deflate($$data, $seen) };
72 } else {
73 # CODE, GLOB, IO and anything else: a name is better than a failure.
74 0 return "$data";
75 }
76  
77 0 ▶ return $class ? { __bless__ => $class, __value__ => $out } : $out;
Branch: 2 branches
BranchTrueFalseTotal
$class ? : 2 27 29
$class ? : 0 0 0
78 }
79  
80 # The reverse: put the classes back on.
81 79 ▶sub _inflate {
Subroutine _inflate: called CC=12 SCAR=35.3
82 0 ▶ my($data) = @_;
Subroutine _inflate: not called CC=12 SCAR=35.3
83  
84 0 my $ref = ref $data;
85 0 ▶ return $data unless $ref;
Branch: 2 branches
BranchTrueFalseTotal
unless $ref 37 41 78
unless $ref 0 0 0
86  
87 # Current JSON::PP and Cpanel::JSON::XS both decode true and false into
88 # JSON::PP::Boolean; accept Cpanel's own boolean class too, to be safe.
89 0 ▶ if (blessed $data && ($data->isa('JSON::PP::Boolean') || $data->isa('Cpanel::JSON::XS::Boolean'))) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")) 8 33 41
if (builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")) 0 0 0
Condition25%3/12
builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")
!ll&&!rl&&r
3308
$data->isa("JSON::PP::Boolean") or $data->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
800
builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")
!ll&&!rl&&r
000
$data->isa("JSON::PP::Boolean") or $data->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
000
MC/DC33%1/3
builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")
builtin::blessed($data) $data->isa("JSON::PP::Boolean") $data->isa("Cpanel::JSON::XS::Boolean")
MC/DC0%0/3
builtin::blessed($data) and $data->isa("JSON::PP::Boolean") || $data->isa("Cpanel::JSON::XS::Boolean")
builtin::blessed($data) $data->isa("JSON::PP::Boolean") $data->isa("Cpanel::JSON::XS::Boolean")
Truth table50%2/4
A and B or C
A= builtin::blessed($data)B= $data->isa("JSON::PP::Boolean")C= $data->isa("Cpanel::JSON::XS::Boolean")
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= builtin::blessed($data)B= $data->isa("JSON::PP::Boolean")C= $data->isa("Cpanel::JSON::XS::Boolean")
ABCresult
0XX0
1000
11X1
1011
90 0 ▶ return $data ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$data ? : 4 4 8
$data ? : 0 0 0
91 }
92  
93 0 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq "ARRAY") 4 29 33
if ($ref eq "ARRAY") 0 0 0
94 0 return [ map { _inflate($_) } @$data ];
95 }
96  
97 0 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq "HASH") 29 0 29
if ($ref eq "HASH") 0 0 0
98 0 ▶ if (exists $data->{__bless__} && exists $data->{__value__}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{"__bless__"} and exists $data->{"__value__"}) 2 27 29
if (exists $data->{"__bless__"} and exists $data->{"__value__"}) 0 0 0
Condition33%2/6
exists $data->{"__bless__"} and exists $data->{"__value__"}
!ll&&!rl&&r
2702
exists $data->{"__bless__"} and exists $data->{"__value__"}
!ll&&!rl&&r
000
MC/DC50%1/2
exists $data->{"__bless__"} and exists $data->{"__value__"}
exists $data->{"__bless__"} exists $data->{"__value__"}
MC/DC0%0/2
exists $data->{"__bless__"} and exists $data->{"__value__"}
exists $data->{"__bless__"} exists $data->{"__value__"}
Truth table66%2/3
A and B
A= exists $data->{"__bless__"}B= exists $data->{"__value__"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= exists $data->{"__bless__"}B= exists $data->{"__value__"}
ABresult
0X0
100
111
99 0 return bless _inflate($data->{__value__}), $data->{__bless__};
100 }
101 0 ▶ if (keys %$data == 1 && exists $data->{__scalarref__}) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"__scalarref__"}) 2 25 27
if (keys %$data == 1 and exists $data->{"__scalarref__"}) 0 0 0
Condition50%3/6
keys %$data == 1 and exists $data->{"__scalarref__"}
!ll&&!rl&&r
1692
keys %$data == 1 and exists $data->{"__scalarref__"}
!ll&&!rl&&r
000
MC/DC100%2/2
keys %$data == 1 and exists $data->{"__scalarref__"}
keys %$data == 1 exists $data->{"__scalarref__"}
MC/DC0%0/2
keys %$data == 1 and exists $data->{"__scalarref__"}
keys %$data == 1 exists $data->{"__scalarref__"}
Truth table100%3/3
A and B
A= keys %$data == 1B= exists $data->{"__scalarref__"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= keys %$data == 1B= exists $data->{"__scalarref__"}
ABresult
0X0
100
111
102 0 my $value = _inflate($data->{__scalarref__});
103 2 return \$value;
104 }
105 0 return { map { $_ => _inflate($data->{$_}) } keys %$data };
106 }
107  
108 0 return $data;
109 }
110  
111  
112 508 ▶sub encode {
Subroutine encode: called CC=2 SCAR=7.0
Pod documented
113 508 ▶ my($format, $data) = @_;
Subroutine encode: called CC=2 SCAR=7.0
Pod documented
114  
115 508 ▶ if ($format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq "json") 21 486 507
if ($format eq "json") 0 5 5
116 # No pretty printing, so the result never contains a raw newline and
117 # stays one line on the wire.
118 22 return _json()->encode(_deflate($data));
119 }
120  
121 487 require YAML;
122 0 return unpack('h*', YAML::Dump($data));
123 }
124  
125 499 ▶sub decode {
Subroutine decode: called CC=3 SCAR=11.1
Pod documented
126 499 ▶ my($format, $line) = @_;
Subroutine decode: called CC=3 SCAR=11.1
Pod documented
127 499 ▶ return undef unless defined $line;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $line 0 498 498
unless defined $line 0 5 5
128  
129 499 ▶ if ($format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq "json") 22 476 498
if ($format eq "json") 0 5 5
130 23 return _inflate(_json()->decode($line));
131 }
132  
133 477 require YAML;
134 477 local $YAML::LoadBlessed = 1;
135 477 return YAML::Load(pack('h*', $line));
136 }
137  
138  
139 38 1;
140  
141 __END__
« blib/lib/Devel/ebug/Backend.pm ↑ Summary lib/Devel/ebug/Plugin/StackTrace.pm »