lib/BarefootJS/Undefined.pm

filter: Statementstmt 78.0% 32 / 41 Branchbran 55.0% 11 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 7 totaltotal 65.6% 65 / 99 CC 15 SCAR 17.0
CC
15
Cov
69%
decode_undefined
13.8
is_undefined_sentinel
10.8
is_nullish
3.0
CRAP 5.5
« blib/lib/BarefootJS/DevReload.pm ↑ Summary lib/BarefootJS/SearchParams.pm »
1 package BarefootJS::Undefined;
2 18 our $VERSION = "0.41.3";
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # JS `undefined` (spec/js-undefined-value.md). Perl's `undef` is JS `null`;
7 # JS `undefined` is one singleton, `$BarefootJS::Undefined::UNDEF` (also
8 # `$BarefootJS::UNDEF`), created only where JS reads one: a missing key, an
9 # out-of-range index, `.at()` / `.find()` with no result, `?.` over a
10 # nullish receiver, the `undefined` identifier (`bf->undefined` in a
11 # template), an omitted declared prop. No decoded data is walked to convert
12 # anything.
13 #
14 # It is a blessed scalar reference with overloads, so Perl's own operators
15 # see it as JS sees `undefined` where they can:
16 #
17 # - `bool` is false: a native `if` / `unless` / `?:` / `!` reads it as falsy.
18 # - `""` is `''`: printed in a text or attribute position it renders nothing,
19 # as JS renders `undefined` there.
20 # - `0+` is NaN: native arithmetic over it is NaN, as JS ToNumber.
21 # - `%{}` / `@{}` give a fresh empty hash / array, so a stray `->{k}` read or
22 # an `@{...}` loop over it reads nothing instead of dying.
23 # - any method call (Kolon's `$x.k`, `$x.size()`) returns the singleton.
24 #
25 # It is NOT `undef`: Perl's `defined` is true for it and `//` keeps it. So
26 # every runtime and emitter site that means JS "nullish" tests through
27 # `is_nullish` below (or `$bf->is_nullish` in a template), never `defined` /
28 # `//`. A strict `=== null` is `is_null`, `=== undefined` is `is_undefined`.
29 #
30 # Identity is the class, not the address: a host's deep copy of a props
31 # structure (Storable's `dclone`, ...) keeps reading as `undefined`.
32 use overload
33 1 ▶ 'bool' => sub { '' },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 ▶ '""' => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 ▶ '0+' => sub { my $inf = 9**9**9; $inf - $inf },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 ▶ '%{}' => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶ '@{}' => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 18 fallback => 1;
39  
40 18 our $UNDEF = bless \(my $slot = undef), __PACKAGE__;
41  
42 # The singleton survives a copy, and a constructor call returns it.
43 1 ▶sub new { return $UNDEF }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
44  
45 # Any other method (Kolon's member and method syntax on an object) reads as
46 # `undefined` itself, as Kolon reads a member of `nil` as `nil`.
47 18 our $AUTOLOAD;
48 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.2
49 1 ▶ return if $AUTOLOAD =~ /::DESTROY\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $BarefootJS::Undefined::AUTOLOAD =~ /::DESTROY\z/ 0 1 1
50 1 return $UNDEF;
51 }
52 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
53  
54 # A host encoder that honours TO_JSON writes `null`; the runtime's own JSON
55 # walk follows `JSON.stringify` instead (a member holding it is dropped).
56 0 ▶sub TO_JSON { return undef }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 # JS `v === undefined`.
59 1198 ▶sub is_undefined { return ref($_[0]) eq __PACKAGE__ ? 1 : 0 }
Subroutine is_undefined: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 109 1089 1198
60  
61 # JS `v === null`: Perl's `undef`.
62 7 ▶sub is_null { return defined($_[0]) ? 0 : 1 }
Subroutine is_null: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 6 7
63  
64 # JS `v == null`: `null` or `undefined`.
65 418 ▶sub is_nullish { return (!defined($_[0]) || ref($_[0]) eq __PACKAGE__) ? 1 : 0 }
Subroutine is_nullish: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[0]) || ref $_[0] eq __PACKAGE__ ? : 43 375 418
Condition100%3/3
not defined $_[0] or ref $_[0] eq __PACKAGE__
l!l&&r!l&&!r
3211375
MC/DC100%2/2
not defined $_[0] or ref $_[0] eq __PACKAGE__
not defined $_[0] ref $_[0] eq __PACKAGE__
Truth table100%3/3
not(A) or B
A= not defined $_[0]B= ref $_[0] eq __PACKAGE__
ABresult
1X1
011
000
66  
67 # Whether a JSON-decoded value is the `{"$undef": true}` sentinel, the
68 # manifest's (and the golden vectors') spelling of JS `undefined`
69 # (spec/js-undefined-value.md, "JSON encoding for vectors").
70 21 ▶sub is_undefined_sentinel {
Subroutine is_undefined_sentinel: called CC=5 SCAR=23.8
Pod undocumented
71 21 my ($v) = @_;
72 21 ▶ return 0 unless ref $v eq 'HASH' && keys %$v == 1 && exists $v->{'$undef'};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "HASH" and keys %$v == 1 and exists $v->{"\$undef"} 21 0 21
Condition33%2/6
ref $v eq "HASH" and keys %$v == 1 and exists $v->{"\$undef"}
!ll&&!rl&&r
2100
ref $v eq "HASH" and keys %$v == 1
!ll&&!rl&&r
2100
MC/DC0%0/3
ref $v eq "HASH" and keys %$v == 1 and exists $v->{"\$undef"}
ref $v eq "HASH" keys %$v == 1 exists $v->{"\$undef"}
Truth table25%1/4
A and B and C
A= ref $v eq "HASH"B= keys %$v == 1C= exists $v->{"\$undef"}
ABCresult
0XX0
10X0
1100
1111
73 0 ▶ return $v->{'$undef'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v->{'$undef'} ? : 0 0 0
74 }
75  
76 # `$v` with every sentinel, at any depth, replaced by `$UNDEF`; inside a
77 # hash it is a present key holding `undefined`. Every other hash key that
78 # begins with `$$` loses one leading `$`: the manifest encoder escapes a
79 # seed's own `$`-prefixed keys that way, so data shaped like the sentinel
80 # stays data.
81 21 ▶sub decode_undefined {
Subroutine decode_undefined: called CC=6 SCAR=26.2
Pod undocumented
82 21 my ($v) = @_;
83 21 ▶ return $UNDEF if is_undefined_sentinel($v);
Branch: 1 branch
BranchTrueFalseTotal
if is_undefined_sentinel($v) 0 21 21
84 0 ▶ return [ map { decode_undefined($_) } @$v ] if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 0 21 21
85 21 ▶ if (ref $v eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 0 21 21
86 0 my %out;
87 0 for my $k (keys %$v) {
88 0 my $key = $k;
89 0 ▶ $key = substr($key, 1) if index($key, '$$') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($key, "\$\$") == 0 0 0 0
90 0 $out{$key} = decode_undefined($v->{$k});
91 }
92 0 return \%out;
93 }
94 21 return $v;
95 }
96  
97 18 1;
98 __END__
« blib/lib/BarefootJS/DevReload.pm ↑ Summary lib/BarefootJS/SearchParams.pm »