| 1 | package BarefootJS::Evaluator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 18 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use B (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Scalar::Util qw(looks_like_number refaddr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use BarefootJS::Undefined (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # JS `undefined` (BarefootJS/Undefined.pm). Perl's `undef` is JS `null`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 18 | my $UNDEF = $BarefootJS::Undefined::UNDEF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 688 | ▶ | sub _is_undef { BarefootJS::Undefined::is_undefined($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_undef: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 208 | ▶ | sub _is_nullish { BarefootJS::Undefined::is_nullish($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_nullish: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # Lightweight evaluator for the pure `ParsedExpr` subset, scoped to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # higher-order callback bodies (reduce / sort / map / filter / find | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # `(…) => expr`) — issue #2018. Templates cannot carry a lambda in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # expression position, which is why the adapters historically special-cased | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # these callbacks into fixed shapes (bf_sort's comparator catalogue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # bf_reduce's +/* fold). Instead, the callback BODY rides as a pure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # `ParsedExpr` subtree (the structured IR the compiler already produces) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # is evaluated here against an environment (`{acc, item, …captured free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # vars}`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # ONE shared implementation for both Perl backends (Mojo + Xslate), living | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # alongside SearchParams.pm in the engine-agnostic core (no CPAN deps beyond | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # core B / POSIX / Scalar::Util). The accepted subset and its semantics are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # documented in spec/compiler.md ("ParsedExpr Evaluator Semantics") and pinned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # isomorphically by the cross-language golden vectors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # (packages/adapter-tests/helper-vectors/eval-vectors.json), shared with the Go | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # evaluator (bf.go) — same input → same output. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # The coercion below is JS-faithful (ToNumber / ToString / ToBoolean, strict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # equality) and deliberately distinct from the divergent bf->string / number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # helpers in BarefootJS.pm, so the contract is unambiguous and the two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # template adapters stay byte-equal with each other and with Go. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # JS `undefined` is `$BarefootJS::UNDEF` and Perl's `undef` is JS `null`. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # missing field, an out-of-range or non-canonical index, the unbound | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # `undefined` identifier, an omitted builtin argument and a `find` with no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # result read as `UNDEF` (spec/js-undefined-value.md, section 2). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # evaluate($node, $env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Evaluate a decoded ParsedExpr node (a hashref keyed by `kind`) against the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # environment hashref ($env), returning a Perl value (number, string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # JSON::PP::Boolean, undef for null, `UNDEF` for undefined, arrayref, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # hashref). The matching JSON entry point is eval_json() below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 541 | ▶ | sub evaluate ($node, $env) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=56 SCAR=48.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 541 | ▶ | return undef unless ref $node eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 541 | ▶ | my $kind = $node->{kind} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"kind"} // ""
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 541 | ▶ | if ($kind eq 'literal') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 68 | return $node->{value}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 473 | ▶ | if ($kind eq 'identifier') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 211 | my $name = $node->{name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # The unshadowed `undefined` identifier is the constant. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 211 | ▶ | return $UNDEF if $name eq 'undefined' && !exists $env->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "undefined" and not exists $env->{$name}
MC/DC100%2/2
$name eq "undefined" and not exists $env->{$name}
$name eq "undefined"
not exists $env->{$name}
Truth table100%3/3
A and not(B)
A= $name eq "undefined"B= not exists $env->{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 210 | return $env->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 262 | ▶ | if ($kind eq 'binary') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | return _binary($node->{op}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 104 | evaluate($node->{left}, $env), evaluate($node->{right}, $env)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 158 | ▶ | if ($kind eq 'unary') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | return _unary($node->{op}, evaluate($node->{argument}, $env)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 157 | ▶ | if ($kind eq 'logical') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | my $op = $node->{op}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | my $left = evaluate($node->{left}, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if ($op eq '&&') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return _truthy($left) ? evaluate($node->{right}, $env) : $left; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if ($op eq '||') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return _truthy($left) ? $left : evaluate($node->{right}, $env); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # `??` coalesces both nils. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | return _is_nullish($left) ? evaluate($node->{right}, $env) : $left; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 154 | ▶ | if ($kind eq 'conditional') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | return _truthy(evaluate($node->{test}, $env)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ? evaluate($node->{consequent}, $env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | : evaluate($node->{alternate}, $env); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 154 | ▶ | if ($kind eq 'member') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 110 | return _read_property(evaluate($node->{object}, $env), $node->{property}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 44 | ▶ | if ($kind eq 'index-access') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 14 | return _read_index(evaluate($node->{object}, $env), evaluate($node->{index}, $env)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 30 | ▶ | if ($kind eq 'call') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # Nested `.map(cb)` / `.filter(cb)` (#2094) — e.g. a `.flatMap(p => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # p.tags.map(t => '#'+t))` projection body that itself contains a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # `.map`/`.filter` call. Checked BEFORE builtin-name resolution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # (the callee is a `member` node, never a bare/dotted builtin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # identifier, so the two can't collide). Mirrors Go's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # evalArrayCallbackCall / evalArrayCallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 14 | my ($method, $object_node, $arrow_node) = _array_callback_call($node); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 14 | ▶ | if (defined $method) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | return _array_callback($method, $object_node, $arrow_node, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | my $name = _builtin_name($node->{callee}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 9 | ▶ | return undef unless defined $name && $name ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and $name ne ""
MC/DC0%0/2
defined $name and $name ne ""
defined $name
$name ne ""
Truth table33%1/3
A and B
A= defined $nameB= $name ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | my @args = map { evaluate($_, $env) } @{ $node->{args} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"args"} // []
MC/DC0%0/1
$node->{"args"} // []
$node->{"args"}
Truth table50%1/2
A
A= $node->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 9 | return _call_builtin($name, \@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 16 | ▶ | if ($kind eq 'template-literal') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | for my $p (@{ $node->{parts} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"parts"} // []
MC/DC0%0/1
$node->{"parts"} // []
$node->{"parts"}
Truth table50%1/2
A
A= $node->{"parts"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | ▶ | if (($p->{type} // '') eq 'string') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$p->{"type"} // ""
MC/DC0%0/1
$p->{"type"} // ""
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | $out .= $p->{value} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$p->{"value"} // ""
MC/DC0%0/1
$p->{"value"} // ""
$p->{"value"}
Truth table50%1/2
A
A= $p->{"value"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | $out .= _to_string(evaluate($p->{expr}, $env)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 14 | ▶ | if ($kind eq 'array-literal') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | return [ map { evaluate($_, $env) } @{ $node->{elements} // [] } ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"elements"} // []
MC/DC0%0/1
$node->{"elements"} // []
$node->{"elements"}
Truth table50%1/2
A
A= $node->{"elements"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 12 | ▶ | if ($kind eq 'object-literal') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # Each entry carries its own `kind` ('prop' | 'spread', #2696 Step 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # — the SAME encoding the Go evaluator (eval.go) and the raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # ParsedExpr shape the eval-vectors.json golden corpus carry, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # this one decoder serves both a compiled `*_eval` payload and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # golden-vector JSON directly. A 'spread' evaluates its `expr` and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # shallow-merges the result's own keys (a non-hashref result — | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # including a null/undefined JS spread source — is a no-op); a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # 'prop' sets one key. Later entries win on a shared key, in source | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # order, matching JS object-spread exactly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my %out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | for my $prop (@{ $node->{properties} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$node->{"properties"} // []
MC/DC0%0/1
$node->{"properties"} // []
$node->{"properties"}
Truth table0%0/2
A
A= $node->{"properties"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (($prop->{kind} // '') eq 'spread') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$prop->{"kind"} // ""
MC/DC0%0/1
$prop->{"kind"} // ""
$prop->{"kind"}
Truth table0%0/2
A
A= $prop->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $spread = evaluate($prop->{expr}, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (ref $spread eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $out{$_} = $spread->{$_} for keys %$spread; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $out{ $prop->{key} } = evaluate($prop->{value}, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return \%out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 12 | ▶ | if ($kind eq 'array-method' && ($node->{method} // '') eq 'includes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition62%5/8
$kind eq "array-method" and ($node->{"method"} // "") eq "includes" and @{$node->{"args"} // [];} == 1
$kind eq "array-method" and ($node->{"method"} // "") eq "includes"
$node->{"method"} // ""
MC/DC0%0/3
$kind eq "array-method" and ($node->{"method"} // "") eq "includes" and @{$node->{"args"} // [];} == 1
$kind eq "array-method"
$node->{"method"}
@{$node->{"args"} // [];} == 1
Truth table25%1/4
A and B and C
A= $kind eq "array-method"B= $node->{"method"}C= @{$node->{"args"} // [];} == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | ▶ | && @{ $node->{args} // [] } == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"args"} // []
MC/DC0%0/1
$node->{"args"} // []
$node->{"args"}
Truth table50%1/2
A
A= $node->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # `.includes(x)` (#2075) — the one `array-method` in the evaluator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # subset, shared between `Array.prototype.includes` (SameValueZero | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # membership) and `String.prototype.includes` (substring search), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # matching the receiver-type dispatch the SSR template lowering does | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # at runtime (`bf_includes` / `$bf->includes`). Mirrors the JS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # reference's `includes()` (eval-reference.ts). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 8 | my $obj = evaluate($node->{object}, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | my $needle = evaluate($node->{args}[0], $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | ▶ | if (ref $obj eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | for my $el (@$obj) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 10 | ▶ | return _bool(1) if _same_value_zero($el, $needle); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | return _bool(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | if (_is_string($obj)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | return _bool(index($obj, _to_string($needle)) != -1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Any other receiver is not a JS `.includes` target — degrade to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # false rather than throwing, mirroring the reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | return _bool(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 4 | ▶ | if ($kind eq 'array-method' && ($node->{method} // '') eq 'join' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition37%3/8
$kind eq "array-method" and ($node->{"method"} // "") eq "join" and @{$node->{"args"} // [];} <= 1
$kind eq "array-method" and ($node->{"method"} // "") eq "join"
$node->{"method"} // ""
MC/DC0%0/3
$kind eq "array-method" and ($node->{"method"} // "") eq "join" and @{$node->{"args"} // [];} <= 1
$kind eq "array-method"
$node->{"method"}
@{$node->{"args"} // [];} <= 1
Truth table25%1/4
A and B and C
A= $kind eq "array-method"B= $node->{"method"}C= @{$node->{"args"} // [];} <= 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | && @{ $node->{args} // [] } <= 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"args"} // []
MC/DC0%0/1
$node->{"args"} // []
$node->{"args"}
Truth table50%1/2
A
A= $node->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # `.join(sep?)` (#2094), a sibling of `.includes` above in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # `array-method` node kind — needed for a composed case like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # `.flatMap(p => p.tags.map(...).join(' ')).join(', ')`. JS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # semantics: default separator is `,`; a `null`/`undefined` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # element joins as `''` (NOT the string "null"). Mirrors the Go | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # evaluator's evalJoin. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # An omitted or `undefined` separator is ","; a `null` one is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # ToString'd ("null"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | my $obj = evaluate($node->{object}, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | ▶ | my $args = $node->{args} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"args"} // []
MC/DC0%0/1
$node->{"args"} // []
$node->{"args"}
Truth table50%1/2
A
A= $node->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | my $sep_value = @$args == 1 ? evaluate($args->[0], $env) : $UNDEF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | my $sep = _is_undef($sep_value) ? ',' : _to_string($sep_value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | return _join_array($obj, $sep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # arrow-fn / higher-order / unsupported array-method: a callback body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # containing these is refused upstream (BF101); never reached here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # Nested `.map` / `.filter` callback recognition (#2094) — the evaluator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # widening that lets a callback body (already evaluated here for reduce / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # sort / filter / find / …) itself contain a `.map(cb)` or `.filter(cb)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # call, e.g. the #1938 blog-showcase `.flatMap(p => p.tags.map(t => '#'+t))` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # projection. Everything else nested (`.some`/`.find`/`.every`/`.sort`/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # `.reduce`/`.flat`/`.flatMap`, standalone arrows) stays refused upstream — | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # only `.map`/`.filter` are recognized here, mirroring Go's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # evalArrayCallbackCall / evalArrayCallback exactly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # _array_callback_call($node): if $node (a `call` node) is a `.map(arrow)` / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # `.filter(arrow)` call — i.e. its callee is an uncomputed `member` node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # whose property is "map"/"filter", and its first argument is an `arrow` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # node — returns `(method, object_node, arrow_node)`. Otherwise returns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # empty list (so `if (my (...) = _array_callback_call($node))` is false). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 14 | ▶ | sub _array_callback_call ($node) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_array_callback_call: called CC=13 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 14 | my $callee = $node->{callee}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 14 | ▶ | return () unless ref $callee eq 'HASH' && ($callee->{kind} // '') eq 'member'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $callee eq "HASH" and ($callee->{"kind"} // "") eq "member"
$callee->{"kind"} // ""
MC/DC0%0/2
ref $callee eq "HASH" and ($callee->{"kind"} // "") eq "member"
ref $callee eq "HASH"
$callee->{"kind"}
Truth table33%1/3
A and B
A= ref $callee eq "HASH"B= $callee->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 13 | ▶ | return () if $callee->{computed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 13 | ▶ | my $prop = $callee->{property} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$callee->{"property"} // ""
MC/DC0%0/1
$callee->{"property"} // ""
$callee->{"property"}
Truth table50%1/2
A
A= $callee->{"property"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 13 | ▶ | return () unless $prop eq 'map' || $prop eq 'filter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prop eq "map" or $prop eq "filter"
MC/DC100%2/2
$prop eq "map" or $prop eq "filter"
$prop eq "map"
$prop eq "filter"
Truth table100%3/3
A or B
A= $prop eq "map"B= $prop eq "filter"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 5 | ▶ | my $args = $node->{args} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"args"} // []
MC/DC0%0/1
$node->{"args"} // []
$node->{"args"}
Truth table50%1/2
A
A= $node->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 5 | ▶ | return () unless @$args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 5 | my $arrow = $args->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 5 | ▶ | return () unless ref $arrow eq 'HASH' && ($arrow->{kind} // '') eq 'arrow'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
ref $arrow eq "HASH" and ($arrow->{"kind"} // "") eq "arrow"
$arrow->{"kind"} // ""
MC/DC0%0/2
ref $arrow eq "HASH" and ($arrow->{"kind"} // "") eq "arrow"
ref $arrow eq "HASH"
$arrow->{"kind"}
Truth table33%1/3
A and B
A= ref $arrow eq "HASH"B= $arrow->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | return ($prop, $callee->{object}, $arrow); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # _array_callback($method, $object_node, $arrow_node, $env): evaluate the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # receiver (`$object_node`) against $env, then map/filter it through the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # arrow's body. `params` in the decoded JSON is an array ref of plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # strings (e.g. `["t"]` or `["t","i"]`) — 1 param binds the element, 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # params bind (element, index). The child env is a COPY of the parent env | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # (never mutated in place across sibling iterations), with the param | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # name(s) bound per call — matches Go's per-call `inner` env copy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | ▶ | sub _array_callback ($method, $object_node, $arrow_node, $env) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_array_callback: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 5 | my $arr = evaluate($object_node, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | ▶ | return undef unless ref $arr eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | ▶ | my @params = @{ $arrow_node->{params} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arrow_node->{"params"} // []
MC/DC0%0/1
$arrow_node->{"params"} // []
$arrow_node->{"params"}
Truth table50%1/2
A
A= $arrow_node->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 5 | my $body = $arrow_node->{body}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 11 | ▶ | my $call_cb = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 11 | my ($item, $index) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | my %inner = %$env; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 11 | ▶ | $inner{ $params[0] } = $item if @params >= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 11 | ▶ | $inner{ $params[1] } = $index if @params >= 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 11 | return evaluate($body, \%inner); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 5 | ▶ | if ($method eq 'map') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | for my $item (@$arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 5 | push @out, $call_cb->($item, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 5 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | for my $item (@$arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 6 | ▶ | push @out, $item if _truthy($call_cb->($item, $i)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 6 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # _join_array($obj, $sep): `Array.prototype.join` semantics — non-ARRAY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # receiver -> '' (the evaluator's degrade-rather-than-die convention, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # matching `.includes` above); a `null` (Perl `undef`) or `undefined` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # (`UNDEF`) element joins as '' (not the string "null"); every other element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # goes through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # `_to_string` (JS `String(x)` — a JS boolean stringifies "true"/"false", a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # number "42"/"NaN"/"Infinity", etc.). Mirrors Go's evalJoin. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 4 | ▶ | sub _join_array ($obj, $sep) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_join_array: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | ▶ | return '' unless ref $obj eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 4 | ▶ | return join($sep, map { _is_nullish($_) ? '' : _to_string($_) } @$obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # eval_json($json, $env): decode a ParsedExpr JSON string and evaluate it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # Mirrors the Go EvalExpr entry point. Requires JSON::PP only when used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | sub eval_json ($json, $env) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eval_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my $node = JSON::PP->new->decode($json); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | return evaluate($node, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # JS value classification. JSON-decoded strings carry the string flag (POK) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # but no numeric flag; JSON-decoded numbers carry IOK/NOK. This lets the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # evaluator tell the JS *string* "10" from the JS *number* 10 — essential for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # the `+` overload and relational comparison — which looks_like_number alone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # cannot (it is true for both). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 851 | ▶ | sub _is_string ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_string: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 851 | ▶ | return 0 if !defined $v || ref $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $v or ref $v
MC/DC100%2/2
not defined $v or ref $v
not defined $v
ref $v
Truth table100%3/3
not(A) or B
A= not defined $vB= ref $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 846 | my $f = B::svref_2object(\$v)->FLAGS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 846 | ▶ | return (($f & B::SVf_POK) && !($f & (B::SVf_IOK | B::SVf_NOK))) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$f & 1024 and not $f & 768
MC/DC50%1/2
$f & 1024 and not $f & 768
$f & 1024
not $f & 768
Truth table66%2/3
A and not(B)
A= $f & 1024B= not $f & 768
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 363 | ▶ | sub _is_number ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_number: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 363 | ▶ | return (defined $v && !ref $v && !_is_string($v)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $v and not ref $v and not _is_string($v)
defined $v and not ref $v
MC/DC100%3/3
defined $v and not ref $v and not _is_string($v)
defined $v
not ref $v
not _is_string($v)
Truth table100%4/4
A and not(B) and not(C)
A= defined $vB= not ref $vC= not _is_string($v)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # Perl's builtin `length` counts UTF-8 BYTES unless the scalar already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # carries the internal UTF8 flag (set when the caller decoded it, e.g. via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # `JSON::PP->new->utf8->decode`) — a byte count diverges from JS `.length` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # (UTF-16 code units) on ANY non-ASCII input, not just astral characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # (#2196 Level 1). Count Unicode codepoints instead: agrees with JS for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # every BMP character; astral characters (emoji, CJK-ext) still diverge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # from JS's 2-code-unit count (documented, separate known limitation) but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # now agree with the other four backends (Go/Ruby/Python/PHP), which all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | # already count codepoints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 1 | ▶ | sub _codepoint_length ($s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_codepoint_length: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 1 | ▶ | return length($s) if utf8::is_utf8($s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | my $copy = $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | utf8::decode($copy); # no-op on already-decoded or invalid-UTF-8 input | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | return length($copy); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 22 | ▶ | sub _nan { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nan: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 22 | my $inf = 9**9**9; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 22 | return $inf - $inf; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # JS coercion primitives (ToNumber / ToString / ToBoolean). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # _double: the IEEE double nearest $n. A JS number is a double, but a perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # built with -Duselongdouble or -Dusequadmath keeps a wider NV, so `0.1 + 0.2` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # is 0.3 there and a round-trip check passes at a precision JS doesn't have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # (#3445). Rounding where a number enters and after each arithmetic step keeps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # those perls on JS's values; with an 8-byte NV it is the identity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 18 | ▶ | use constant WIDE_NV => length(pack 'F', 0) > 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 705 | ▶ | sub _double ($n) { WIDE_NV ? unpack('d', pack('d', $n)) : $n } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_double: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 404 | ▶ | sub _to_number ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_number: called CC=7 SCAR=20.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 404 | ▶ | return 0 if !defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 392 | ▶ | return _nan() if _is_undef($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | if (ref $v eq 'JSON::PP::Boolean') { return $v ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 382 | ▶ | return _nan() if ref $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 382 | ▶ | return _string_to_number($v) if _is_string($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | # Already a number: no `+ 0`, which would turn a -0 into +0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 373 | return _double($v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # _string_to_number: JS StringToNumber (ECMA-262 StringNumericLiteral), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # ASCII digits only. The string is trimmed and `''` is 0; a signed decimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # literal with an optional exponent, the exact spelling `Infinity`, and an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # unsigned `0x` / `0o` / `0b` integer are their values; anything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # (`infinity`, `nan`, `1_000`, `-0x10`) is NaN. Perl's own numification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # (`looks_like_number`) accepts `inf` / `nan`, reads `"0x10"` as 0 and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | # `"-0"` as +0, so it is not used here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 18 | my %RADIX = (x => 16, o => 8, b => 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # _radix_to_number: the digits of a JS `0x` / `0o` / `0b` integer as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # double nearest its value, ties to even; NaN for a digit outside the base. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # JS rounds the mathematical value once (#3824 step 3), so the digits are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # read as bits: the first 53 significant bits, the next one (the round bit) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | # and whether any later bit is set (the sticky bit). Accumulating them in a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # Perl number rounds every prefix past 64 bits instead. A value past the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | # largest double is Infinity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 18 | my %RADIX_BITS = (16 => 4, 8 => 3, 2 => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | sub _radix_to_number ($digits, $base) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_radix_to_number: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | my $kept = 0; # the first 54 significant bits (53 + the round bit) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | my $significant = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | my $sticky = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | for my $digit (split //, $digits) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | my $d = hex $digit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | return _nan() if $d >= $base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | for (my $shift = $RADIX_BITS{$base} - 1; $shift >= 0; $shift--) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | my $bit = ($d >> $shift) & 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | next if $significant == 0 && $bit == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$significant == 0 and $bit == 0
MC/DC0%0/2
$significant == 0 and $bit == 0
$significant == 0
$bit == 0
Truth table0%0/3
A and B
A= $significant == 0B= $bit == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | if ($significant < 54) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $kept = $kept * 2 + $bit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | elsif ($bit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $sticky = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $significant++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return _double($kept) if $significant <= 53; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | my $round = $kept % 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my $mantissa = ($kept - $round) / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | $mantissa++ if $round && ($sticky || $mantissa % 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$round and $sticky || $mantissa % 2
$sticky or $mantissa % 2
MC/DC0%0/3
$round and $sticky || $mantissa % 2
$round
$sticky
$mantissa % 2
Truth table0%0/4
A and B or C
A= $roundB= $stickyC= $mantissa % 2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # mantissa <= 2^53 is exact, and a power of two scales it exactly or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # overflows to Infinity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | my $exponent = $significant - 53; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | return 9**9**9 if $exponent > 1100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | return _double($mantissa * (2**$exponent)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 9 | ▶ | sub _string_to_number ($s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string_to_number: called CC=8 SCAR=30.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 9 | my $t = $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 9 | $t =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 9 | $t =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 9 | ▶ | return 0 if $t eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 9 | ▶ | if ($t =~ /\A0([xXoObB])([0-9a-fA-F]+)\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | return _radix_to_number($2, $RADIX{lc $1}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 9 | ▶ | if ($t =~ /\A([+-]?)Infinity\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | return $1 eq '-' ? -(9**9**9) : 9**9**9; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 9 | ▶ | return _nan() unless $t =~ /\A([+-]?)(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my $n = _double($t + 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # `"-0"` (and `"-0.0"`, `"-0e5"`) is negative zero; Perl numifies it to +0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | return ($1 eq '-' && $n == 0) ? -(unpack('d', pack('d', 0))) : $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$1 eq "-" and $n == 0
MC/DC0%0/2
$1 eq "-" and $n == 0
$1 eq "-"
$n == 0
Truth table0%0/3
A and B
A= $1 eq "-"B= $n == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 197 | ▶ | sub _to_string ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_string: called CC=9 SCAR=22.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 197 | ▶ | return 'null' if !defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 190 | ▶ | return 'undefined' if _is_undef($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 186 | ▶ | if (ref $v eq 'JSON::PP::Boolean') { return $v ? 'true' : 'false' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | # Array.prototype.toString is join(","): a nil element (`null` or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # `undefined`) prints as "", and a nested array flattens through its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # join. A plain object prints as "[object Object]". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | return join(',', map { _is_nullish($_) ? '' : _to_string($_) } @$v) if ref $v eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 182 | ▶ | return '[object Object]' if ref $v eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 182 | ▶ | return _format_number($v + 0) if _is_number($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 147 | return "$v"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # _is_object: an array or a plain object, which ToPrimitive turns into its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # ToString, so a `+` over it concatenates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 36 | ▶ | sub _is_object ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_object: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 36 | my $r = ref $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 36 | ▶ | return ($r eq 'ARRAY' || $r eq 'HASH') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$r eq "ARRAY" or $r eq "HASH"
MC/DC0%0/2
$r eq "ARRAY" or $r eq "HASH"
$r eq "ARRAY"
$r eq "HASH"
Truth table33%1/3
A or B
A= $r eq "ARRAY"B= $r eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | # _format_number: JS `Number::toString`. Perl's own stringification is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | # `%.15g`, which drops digits (`0.1 + 0.2` -> "0.3"), spells the non-finite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # doubles "Inf" / "NaN", and pads a small exponent (`1.23456789e-06`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # Integers below 2**53 print exactly; everything else takes the shortest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # `%.Ng` that round-trips as a double and re-spells it with ECMA-262's notation rules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # decimal within [1e-6, 1e21), otherwise an unpadded lower-case exponent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # (`1.23456789e-7`, `1.23456789e+21`) (#3380). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 93 | ▶ | sub _format_number ($n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_number: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 93 | $n = _double($n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 93 | ▶ | return 'NaN' if $n != $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 88 | ▶ | return 'Infinity' if $n == 9**9**9; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 84 | ▶ | return '-Infinity' if $n == -(9**9**9); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 78 | ▶ | return '0' if $n == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 63 | ▶ | return sprintf('%.0f', $n) if $n == int($n) && abs($n) < 9007199254740992; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$n == int $n and abs $n < '9007199254740992'
MC/DC100%2/2
$n == int $n and abs $n < '9007199254740992'
$n == int $n
abs $n < '9007199254740992'
Truth table100%3/3
A and B
A= $n == int $nB= abs $n < '9007199254740992'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 21 | my $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 21 | for my $p (1 .. 17) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 127 | $s = sprintf('%.*g', $p, $n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 127 | ▶ | last if _double($s + 0) == $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 21 | return _js_notation($s); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # _js_notation: re-spell a shortest round-trip decimal (`[-]d[.ddd][e[+-]x]`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | # with ECMA-262's Number::toString notation rules. `$digits` holds the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | # significant digits, `$point` where the decimal point falls in them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 21 | ▶ | sub _js_notation ($shortest) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_js_notation: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 21 | ▶ | my $sign = $shortest =~ s/\A-// ? '-' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 21 | my ($mantissa, $exp) = split /e/i, $shortest, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 21 | my ($int, $frac) = split /\./, $mantissa, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 21 | ▶ | $frac //= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$frac //= ""
MC/DC100%1/1
$frac //= ""
$frac
Truth table100%2/2
A
A= $frac
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 21 | my $raw = $int . $frac; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 21 | ▶ | my $point = length($int) + (defined $exp ? $exp + 0 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 21 | (my $digits = $raw) =~ s/\A0+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 21 | $point -= length($raw) - length($digits); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 21 | $digits =~ s/0+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 21 | ▶ | $digits = '0' if $digits eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 21 | my $k = length $digits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 21 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 21 | ▶ | if ($k <= $point && $point <= 21) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$k <= $point and $point <= 21
MC/DC100%2/2
$k <= $point and $point <= 21
$k <= $point
$point <= 21
Truth table100%3/3
A and B
A= $k <= $pointB= $point <= 21
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 3 | $out = $digits . ('0' x ($point - $k)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | ▶ | } elsif ($point > 0 && $point <= 21) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$point > 0 and $point <= 21
MC/DC100%2/2
$point > 0 and $point <= 21
$point > 0
$point <= 21
Truth table100%3/3
A and B
A= $point > 0B= $point <= 21
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 2 | $out = substr($digits, 0, $point) . '.' . substr($digits, $point); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | ▶ | } elsif ($point > -6 && $point <= 0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$point > -6 and $point <= 0
MC/DC100%2/2
$point > -6 and $point <= 0
$point > -6
$point <= 0
Truth table100%3/3
A and B
A= $point > -6B= $point <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 7 | $out = '0.' . ('0' x -$point) . $digits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 9 | my $e = $point - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 9 | ▶ | $out = substr($digits, 0, 1) . ($k > 1 ? '.' . substr($digits, 1) : '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | . 'e' . ($e >= 0 ? '+' : '-') . abs($e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 21 | return $sign . $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 39 | ▶ | sub _truthy ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_truthy: called CC=9 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 39 | ▶ | return 0 if _is_nullish($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 38 | ▶ | if (ref $v eq 'JSON::PP::Boolean') { return $v ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 2 | ▶ | return 1 if ref $v; # arrays / objects are always truthy in JS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 2 | ▶ | if (_is_string($v)) { return $v ne '' ? 1 : 0 } # incl. the truthy "0" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | my $n = $v + 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | return ($n != 0 && $n == $n) ? 1 : 0; # nonzero and not NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$n != 0 and $n == $n
MC/DC0%0/2
$n != 0 and $n == $n
$n != 0
$n == $n
Truth table0%0/3
A and B
A= $n != 0B= $n == $n
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | # _bool: wrap a Perl truthy/falsy into a JS boolean (JSON::PP::Boolean), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | # boolean-valued operators (relational, ===, !, Boolean()) return a real | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # boolean rather than 1/0 — matching the Go evaluator's bool (e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # String(a < b) is "true", and `'x' + (a < b)` is "xtrue"). The coercions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # above already treat JSON::PP::Boolean correctly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 49 | ▶ | sub _bool ($t) { $t ? JSON::PP::true() : JSON::PP::false() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bool: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 113 | ▶ | sub _binary ($op, $l, $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_binary: called CC=19 SCAR=30.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 113 | ▶ | if ($op eq '+') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # JS `+`: string concatenation once either operand is a string after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # ToPrimitive (an array or object converts to its ToString), numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | # addition otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 34 | ▶ | return _to_string($l) . _to_string($r) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
_is_string($l) or _is_string($r) or _is_object($l) or _is_object($r)
_is_string($l) or _is_string($r) or _is_object($l)
_is_string($l) or _is_string($r)
MC/DC50%2/4
_is_string($l) or _is_string($r) or _is_object($l) or _is_object($r)
_is_string($l)
_is_string($r)
_is_object($l)
_is_object($r)
Truth table60%3/5
A or B or C or D
A= _is_string($l)B= _is_string($r)C= _is_object($l)D= _is_object($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | if _is_string($l) || _is_string($r) || _is_object($l) || _is_object($r); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 18 | my ($ln, $rn) = (_to_number($l), _to_number($r)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 18 | my $sum = _double($ln + $rn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | # Perl adds two integral doubles as integers, which drops the sign of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | # `-0 + -0`; IEEE addition keeps it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 18 | ▶ | return -(unpack('d', pack('d', 0))) if $sum == 0 && POSIX::signbit($ln) && POSIX::signbit($rn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$sum == 0 and POSIX::signbit($ln) and POSIX::signbit($rn)
$sum == 0 and POSIX::signbit($ln)
MC/DC0%0/3
$sum == 0 and POSIX::signbit($ln) and POSIX::signbit($rn)
$sum == 0
POSIX::signbit($ln)
POSIX::signbit($rn)
Truth table25%1/4
A and B and C
A= $sum == 0B= POSIX::signbit($ln)C= POSIX::signbit($rn)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 18 | return $sum; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 79 | ▶ | return _double(_to_number($l) - _to_number($r)) if $op eq '-'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 54 | ▶ | return _double(_to_number($l) * _to_number($r)) if $op eq '*'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 47 | ▶ | if ($op eq '/') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 8 | return _divide(_to_number($l), _to_number($r)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 39 | ▶ | return _remainder(_to_number($l), _to_number($r)) if $op eq '%'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 37 | ▶ | return _relational($op, $l, $r) if $op eq '<' || $op eq '<=' || $op eq '>' || $op eq '>='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$op eq "<" or $op eq "<=" or $op eq ">" or $op eq ">="
$op eq "<" or $op eq "<=" or $op eq ">"
$op eq "<" or $op eq "<="
MC/DC75%3/4
$op eq "<" or $op eq "<=" or $op eq ">" or $op eq ">="
$op eq "<"
$op eq "<="
$op eq ">"
$op eq ">="
Truth table80%4/5
A or B or C or D
A= $op eq "<"B= $op eq "<="C= $op eq ">"D= $op eq ">="
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 3 | ▶ | return _bool(_strict_eq($l, $r)) if $op eq '==='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 1 | ▶ | return _bool(!_strict_eq($l, $r)) if $op eq '!=='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # Loose equality / bitwise / shift are out of the subset. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # _divide: JS `/` over two numbers, as a double. Division by zero is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # finite-valued, not an error: x/0 is ±Infinity, signed by both operands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | # (`1 / -0` is -Infinity), and 0/0 or NaN/0 is NaN. Perl's native `/` dies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | # on a zero divisor. Shared with the template runtime's `divide` (#3454). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 15 | ▶ | sub _divide ($ln, $rn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_divide: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 15 | ▶ | if ($rn == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 12 | ▶ | return _nan() if $ln == 0 || $ln != $ln; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ln == 0 or $ln != $ln
MC/DC50%1/2
$ln == 0 or $ln != $ln
$ln == 0
$ln != $ln
Truth table66%2/3
A or B
A= $ln == 0B= $ln != $ln
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 10 | ▶ | my $negative = ($ln < 0) != (POSIX::signbit($rn) ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 10 | ▶ | return $negative ? -(9**9**9) : 9**9**9; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 3 | return _double($ln / $rn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # _remainder: JS `%` over two numbers, the truncated remainder: it takes the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # dividend's sign (`-7 % 2` is -1) and keeps a fraction (`7.5 % 2` is 1.5), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | # where Perl's native `%` floors and works on integers. A zero divisor, or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # NaN / infinite dividend, gives NaN. Shared with the template runtime's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # `mod` (#3474). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 14 | ▶ | sub _remainder ($ln, $rn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_remainder: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 14 | ▶ | return _nan() if $rn == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 13 | return POSIX::fmod($ln, $rn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 36 | ▶ | sub _relational ($op, $l, $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_relational: called CC=11 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | # JS Abstract Relational Comparison: both strings → compare by code unit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # otherwise coerce both to numbers (a NaN operand makes it false). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 36 | my $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 36 | ▶ | if (_is_string($l) && _is_string($r)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_is_string($l) and _is_string($r)
MC/DC0%0/2
_is_string($l) and _is_string($r)
_is_string($l)
_is_string($r)
Truth table33%1/3
A and B
A= _is_string($l)B= _is_string($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 0 | $c = _compare_utf16($l, $r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 36 | my $ln = _to_number($l); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 36 | my $rn = _to_number($r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 36 | ▶ | return _bool(0) if $ln != $ln || $rn != $rn; # NaN → false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ln != $ln or $rn != $rn
MC/DC50%1/2
$ln != $ln or $rn != $rn
$ln != $ln
$rn != $rn
Truth table66%2/3
A or B
A= $ln != $lnB= $rn != $rn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 35 | ▶ | $c = $ln < $rn ? -1 : $ln > $rn ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 35 | ▶ | return _bool($c < 0) if $op eq '<'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 33 | ▶ | return _bool($c <= 0) if $op eq '<='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 32 | ▶ | return _bool($c > 0) if $op eq '>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 30 | ▶ | return _bool($c >= 0) if $op eq '>='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | return _bool(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | # _compare_utf16: JS string order, by UTF-16 code unit. Perl's `cmp` orders | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # characters by code point, which differs where an astral character (whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | # leading surrogate is 0xD800-0xDBFF) meets a BMP character above the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # surrogates (`"😀" lt "。"` is true in JS); and a string without Perl's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # UTF8 flag holds UTF-8 bytes, which `cmp` would read as Latin-1 against a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # flagged one. Both are decoded and re-packed as big-endian UTF-16, whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # bytes compare in code-unit order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | ▶ | sub _compare_utf16 ($l, $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compare_utf16: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | ▶ | return $l cmp $r unless $l =~ /[^\x00-\x7f]/ || $r =~ /[^\x00-\x7f]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$l =~ /[^\x00-\x7f]/ or $r =~ /[^\x00-\x7f]/
MC/DC0%0/2
$l =~ /[^\x00-\x7f]/ or $r =~ /[^\x00-\x7f]/
$l =~ /[^\x00-\x7f]/
$r =~ /[^\x00-\x7f]/
Truth table0%0/3
A or B
A= $l =~ /[^\x00-\x7f]/B= $r =~ /[^\x00-\x7f]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | return _utf16_be($l) cmp _utf16_be($r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | ▶ | sub _utf16_be ($s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_utf16_be: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | utf8::decode($s) unless utf8::is_utf8($s); # no-op on invalid UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | return join '', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my $o = ord; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | $o > 0xFFFF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | ? pack('n2', 0xD800 + (($o - 0x10000) >> 10), 0xDC00 + (($o - 0x10000) & 0x3FF)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | : pack('n', $o) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | } split //, $s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 57 | ▶ | sub _strict_eq ($l, $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strict_eq: called CC=27 SCAR=34.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | # Strict `===`: equal JS type and value, no coercion. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 57 | my $ln = _is_number($l); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 57 | my $rn = _is_number($r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 57 | ▶ | if ($ln && $rn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ln and $rn
MC/DC100%2/2
$ln and $rn
$ln
$rn
Truth table100%3/3
A and B
A= $lnB= $rn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | # Compared as doubles: an arithmetic result is already rounded to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | # one, but a literal or env value on a wide-NV perl is not (#3445). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 14 | my ($lf, $rf) = (_double($l + 0), _double($r + 0)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 14 | ▶ | return 0 if $lf != $lf || $rf != $rf; # NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lf != $lf or $rf != $rf
MC/DC50%1/2
$lf != $lf or $rf != $rf
$lf != $lf
$rf != $rf
Truth table66%2/3
A or B
A= $lf != $lfB= $rf != $rf
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 13 | ▶ | return $lf == $rf ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 43 | ▶ | return 0 if $ln != $rn; # one numeric, one not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | # `null` is only itself and `undefined` only itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 35 | ▶ | if (!defined $l) { return !defined $r ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 32 | ▶ | return 0 if !defined $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 27 | ▶ | return (_is_undef($l) && _is_undef($r)) ? 1 : 0 if _is_undef($l) || _is_undef($r); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
_is_undef($l) or _is_undef($r)
_is_undef($l) and _is_undef($r)
MC/DC50%1/2
_is_undef($l) or _is_undef($r)
_is_undef($l)
_is_undef($r)
MC/DC0%0/2
_is_undef($l) and _is_undef($r)
_is_undef($l)
_is_undef($r)
Truth table66%2/3
A or B
A= _is_undef($l)B= _is_undef($r)
Truth table33%1/3
A and B
A= _is_undef($l)B= _is_undef($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 25 | my $lb = ref $l eq 'JSON::PP::Boolean'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 25 | my $rb = ref $r eq 'JSON::PP::Boolean'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 25 | ▶ | if ($lb || $rb) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lb or $rb
MC/DC50%1/2
$lb or $rb
$lb
$rb
Truth table66%2/3
A or B
A= $lbB= $rb
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 2 | ▶ | return 0 unless $lb && $rb; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$lb and $rb
MC/DC0%0/2
$lb and $rb
$lb
$rb
Truth table33%1/3
A and B
A= $lbB= $rb
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 2 | ▶ | return ((!!$l) == (!!$r)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | # An object or array is equal only to itself: the same reference, not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | # another structure with the same contents. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 23 | ▶ | if (ref $l || ref $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $l or ref $r
MC/DC50%1/2
ref $l or ref $r
ref $l
ref $r
Truth table66%2/3
A or B
A= ref $lB= ref $r
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 9 | ▶ | return (ref $l && ref $r && refaddr($l) == refaddr($r)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
ref $l and ref $r and builtin::refaddr($l) == builtin::refaddr($r)
ref $l and ref $r
MC/DC66%2/3
ref $l and ref $r and builtin::refaddr($l) == builtin::refaddr($r)
ref $l
ref $r
builtin::refaddr($l) == builtin::refaddr($r)
Truth table75%3/4
A and B and C
A= ref $lB= ref $rC= builtin::refaddr($l) == builtin::refaddr($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 14 | ▶ | return ($l eq $r ? 1 : 0) if _is_string($l) && _is_string($r); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
_is_string($l) and _is_string($r)
MC/DC0%0/2
_is_string($l) and _is_string($r)
_is_string($l)
_is_string($r)
Truth table33%1/3
A and B
A= _is_string($l)B= _is_string($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | # _same_value_zero: `Array.prototype.includes` membership test — `===` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | # except `NaN` equals itself (and +0/-0 are not distinguished, which the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | # JSON-decoded values here can't represent anyway). Reuses `_strict_eq`'s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # type/value rules and only special-cases the two-NaN case that `_strict_eq` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | # (deliberately, for `===`) reports as unequal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 28 | ▶ | sub _same_value_zero ($l, $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_same_value_zero: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 28 | ▶ | if (_is_number($l) && _is_number($r)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_number($l) and _is_number($r)
MC/DC100%2/2
_is_number($l) and _is_number($r)
_is_number($l)
_is_number($r)
Truth table100%3/3
A and B
A= _is_number($l)B= _is_number($r)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 6 | my ($lf, $rf) = ($l + 0, $r + 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 6 | ▶ | return 1 if $lf != $lf && $rf != $rf; # NaN sameValueZero NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$lf != $lf and $rf != $rf
MC/DC0%0/2
$lf != $lf and $rf != $rf
$lf != $lf
$rf != $rf
Truth table33%1/3
A and B
A= $lf != $lfB= $rf != $rf
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 28 | return _strict_eq($l, $r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 1 | ▶ | sub _unary ($op, $v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unary: called CC=4 SCAR=25.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 1 | ▶ | return _bool(!_truthy($v)) if $op eq '!'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | # JS `-x` keeps the sign of zero: negate the double, never a Perl IV, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # whose zero has no sign (`-0` is 0). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | ▶ | return -(unpack('d', pack('d', _to_number($v)))) if $op eq '-'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | ▶ | return _to_number($v) if $op eq '+'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | # Built-in calls (the deterministic allowlist). Locale-sensitive builtins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | # (localeCompare) are deliberately excluded to keep the backends isomorphic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | # _builtin_name: resolve a `call` callee to its builtin name (e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | # "Math.max"), or '' when the callee is not an allowlisted builtin reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 9 | ▶ | sub _builtin_name ($callee) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_builtin_name: called CC=12 SCAR=30.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 9 | ▶ | return '' unless ref $callee eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 9 | ▶ | my $kind = $callee->{kind} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$callee->{"kind"} // ""
MC/DC0%0/1
$callee->{"kind"} // ""
$callee->{"kind"}
Truth table50%1/2
A
A= $callee->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 9 | ▶ | if ($kind eq 'identifier') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 1 | ▶ | return $callee->{name} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$callee->{"name"} // ""
MC/DC0%0/1
$callee->{"name"} // ""
$callee->{"name"}
Truth table50%1/2
A
A= $callee->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 8 | ▶ | if ($kind eq 'member' && !$callee->{computed}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kind eq "member" and not $callee->{"computed"}
MC/DC0%0/2
$kind eq "member" and not $callee->{"computed"}
$kind eq "member"
not $callee->{"computed"}
Truth table33%1/3
A and not(B)
A= $kind eq "member"B= not $callee->{"computed"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 8 | my $obj = $callee->{object}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 8 | ▶ | return '' unless ref $obj eq 'HASH' && ($obj->{kind} // '') eq 'identifier'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
ref $obj eq "HASH" and ($obj->{"kind"} // "") eq "identifier"
$obj->{"kind"} // ""
MC/DC0%0/2
ref $obj eq "HASH" and ($obj->{"kind"} // "") eq "identifier"
ref $obj eq "HASH"
$obj->{"kind"}
Truth table33%1/3
A and B
A= ref $obj eq "HASH"B= $obj->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 8 | ▶ | return ($obj->{name} // '') . '.' . ($callee->{property} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$obj->{"name"} // ""
$callee->{"property"} // ""
MC/DC0%0/1
$obj->{"name"} // ""
$obj->{"name"}
MC/DC0%0/1
$callee->{"property"} // ""
$callee->{"property"}
Truth table50%1/2
A
A= $obj->{"name"}
Truth table50%1/2
A
A= $callee->{"property"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | # _math_round: half rounds toward +Infinity (JS Math.round: 2.5→3, -2.5→-2), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | # matching the existing round helper rather than half-away-from-zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | ▶ | sub _math_round ($n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_math_round: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | return POSIX::floor($n + 0.5); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 13 | ▶ | sub _call_builtin ($name, $args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call_builtin: called CC=18 SCAR=40.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # An omitted argument is JS `undefined`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 13 | ▶ | my $arg = sub ($i) { $i < @$args ? $args->[$i] : $UNDEF }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 13 | ▶ | if ($name eq 'Math.max') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 0 | my $m = -(9**9**9); # JS Math.max() with no args is -Infinity | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 0 | for my $a (@$args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | my $n = _to_number($a); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 0 | ▶ | return $n if $n != $n; # any NaN argument ⇒ NaN (JS / Go) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | ▶ | $m = $n if $n > $m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 0 | return $m; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 13 | ▶ | if ($name eq 'Math.min') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | my $m = 9**9**9; # JS Math.min() with no args is +Infinity | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 0 | for my $a (@$args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | my $n = _to_number($a); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 0 | ▶ | return $n if $n != $n; # any NaN argument ⇒ NaN (JS / Go) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 0 | ▶ | $m = $n if $n < $m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 0 | return $m; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 13 | ▶ | return abs(_to_number($arg->(0))) if $name eq 'Math.abs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 5 | ▶ | return POSIX::floor(_to_number($arg->(0))) if $name eq 'Math.floor'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 5 | ▶ | return POSIX::ceil(_to_number($arg->(0))) if $name eq 'Math.ceil'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 5 | ▶ | return _math_round(_to_number($arg->(0))) if $name eq 'Math.round'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | # `String()` / `Number()` with no argument are "" / 0; `String(undefined)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | # is "undefined" and `Number(undefined)` NaN. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 5 | ▶ | return @$args ? _to_string($args->[0]) : '' if $name eq 'String'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 3 | ▶ | return @$args ? _to_number($args->[0]) : 0 if $name eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 1 | ▶ | return _bool(_truthy($arg->(0))) if $name eq 'Boolean'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | # Any other callee is outside the subset (refused upstream). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | # Member / index access | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | # _read_property: JS `obj.key`. A key the object does not have, and any read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | # of a receiver with no such property (a nil, a number), is `undefined`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 110 | ▶ | sub _read_property ($obj, $key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_property: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 110 | ▶ | return $UNDEF if _is_nullish($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 110 | ▶ | if (ref $obj eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 106 | ▶ | return exists $obj->{$key} ? $obj->{$key} : $UNDEF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 4 | ▶ | if (ref $obj eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 2 | ▶ | return $key eq 'length' ? scalar(@$obj) : $UNDEF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 2 | ▶ | return $UNDEF if ref $obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | # `.length` is a string property only — a numeric scalar (123) has no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | # `.length` in the subset (JS `(123).length` is undefined), so guard on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | # _is_string rather than coercing the number to a string. Matches the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | # Go evaluator (numbers fall through to nil there). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 2 | ▶ | return _codepoint_length($obj) if $key eq 'length' && _is_string($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "length" and _is_string($obj)
MC/DC50%1/2
$key eq "length" and _is_string($obj)
$key eq "length"
_is_string($obj)
Truth table66%2/3
A and B
A= $key eq "length"B= _is_string($obj)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 1 | return $UNDEF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | # _read_index: JS `obj[index]`, and the template helper `BarefootJS::get` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | # (which delegates here, so a template read and a callback read cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | # disagree on a key). The key goes through ToPropertyKey (`_to_string`: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | # `undef` is "null", a JSON boolean "true" / "false", `-0` is "0", `1.0` is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | # "1"). An array, and a string, are read only through a canonical array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | # index: "1" reads element 1, while "01", "1e0", "-0", "1.1" and "-1" are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | # ordinary property names and read `undef`. A string reads one UTF-16 code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | # unit. A hash reads its own key (spec/js-primitive-semantics.md, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | # "ToPropertyKey and index read"). A key the receiver does not have, and any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | # read of a receiver with no properties, is `undefined` (`UNDEF`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 48 | ▶ | sub _read_index ($obj, $index) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_index: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 48 | ▶ | return $UNDEF if _is_nullish($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 46 | my $key = _to_string($index); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 46 | ▶ | if (ref $obj eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 11 | ▶ | return exists $obj->{$key} ? $obj->{$key} : $UNDEF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 35 | ▶ | return $UNDEF unless $key =~ /\A(?:0|[1-9][0-9]*)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 32 | ▶ | if (ref $obj eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 4 | ▶ | return $key < @$obj ? $obj->[$key] : $UNDEF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 28 | ▶ | return $UNDEF if ref $obj || !_is_string($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $obj or not _is_string($obj)
MC/DC0%0/2
ref $obj or not _is_string($obj)
ref $obj
not _is_string($obj)
Truth table33%1/3
A or not(B)
A= ref $objB= not _is_string($obj)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 28 | return _utf16_unit_at($obj, $key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | # _utf16_unit_at: the UTF-16 code unit of string $s at index $i, as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | # one-character string, or `undefined` past the end. A unit that is one half of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | # a surrogate pair reads as U+FFFD: a lone surrogate cannot be written out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | # as UTF-8 (spec/js-primitive-semantics.md, "Out of scope"). A string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | # without Perl's UTF8 flag holds UTF-8 bytes, so a decoded copy is iterated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | # (as `_codepoint_length` does): otherwise `"\xc3\xa9x"` would read one byte | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | # per index instead of `é`, `x`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 28 | ▶ | sub _utf16_unit_at ($s, $i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_utf16_unit_at: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 28 | ▶ | unless (utf8::is_utf8($s)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 14 | utf8::decode($s); # no-op on invalid-UTF-8 input | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 28 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 28 | for my $ch (split //, $s) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 44 | ▶ | my $width = ord($ch) > 0xFFFF ? 2 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 44 | ▶ | if ($i < $pos + $width) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 20 | ▶ | return $width == 1 ? $ch : "\x{FFFD}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 24 | $pos += $width; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 8 | return $UNDEF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | # Evaluator-driven higher-order folds (the generalization of bf_reduce / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | # bf_sort onto the evaluator) — the runtime half both Perl backends share. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | # fold($items, $body, $acc_name, $item_name, $init, $direction, $base_env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | # Fold an arrayref into a value via the evaluator. The reducer $body is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | # pure ParsedExpr node evaluated against `{$acc_name => acc, $item_name => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | # item}` plus the captured free vars in $base_env per element; $init seeds the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | # accumulator and $direction is "left" (reduce) or "right" (reduceRight). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | # Generalizes bf_reduce — any reducer body, not just the +/* arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | # catalogue, and acc may appear anywhere. $base_env is optional; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | # acc/item keys shadow any same-named base key. Mirrors Go's FoldEval. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 7 | ▶ | sub fold ($items, $body, $acc_name, $item_name, $init, $direction = 'left', $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fold: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 7 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 7 | ▶ | @arr = reverse @arr if ($direction // '') eq 'right'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$direction // ""
MC/DC0%0/1
$direction // ""
$direction
Truth table50%1/2
A
A= $direction
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | # Seed the env from the captured free vars once; acc / item are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | # overwritten each iteration (constant base keys carry through). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 7 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 7 | my $acc = $init; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | 7 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 20 | $env{$acc_name} = $acc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | 20 | $env{$item_name} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 20 | $acc = evaluate($body, \%env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 7 | return $acc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | # sort_by($items, $cmp, $param_a, $param_b, $base_env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | # Return a new arrayref ordered by a ParsedExpr comparator $cmp evaluated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | # against `{$param_a => a, $param_b => b}` plus the captured free vars in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | # $base_env to a number (negative / zero / positive, like a JS comparator). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | # Generalizes bf_sort — any comparator body. $base_env is optional. Stable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | # and non-mutating. Mirrors Go's SortEval. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | # JS moves every `undefined` element to the end without handing it to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | # comparator. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | 9 | ▶ | sub sort_by ($items, $cmp, $param_a, $param_b, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_by: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # Non-array receiver → empty arrayref, matching the nil-tolerant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # BarefootJS->sort helper convention (and avoiding an undef-deref footgun | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | # for callers that use the result as an arrayref). Value-compatible with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | # the Go SortEval, whose nil slice iterates as empty too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 9 | ▶ | return [] unless ref $items eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | 7 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | # Decorate each element with its original index and tie-break on it when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | # the comparator returns 0, so stability is explicit and independent of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | # the `sort` pragma / build (portable to the declared minimum Perl, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | # matching Go's sort.SliceStable). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 7 | my @defined = grep { !_is_undef($_) } @$items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 7 | my @tail = grep { _is_undef($_) } @$items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 7 | my @decorated = map { [ $_, $defined[$_] ] } 0 .. $#defined; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | my @sorted = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | 7 | $env{$param_a} = $a->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 21 | $env{$param_b} = $b->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | 21 | my $c = _to_number(evaluate($cmp, \%env)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | # Explicit sign test rather than `<=> 0`: a NaN comparator result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | # warns / is undefined under `<=>`, whereas `< 0` / `> 0` are both | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | # false for NaN, yielding 0 (no reordering) — matching JS (NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | # comparator ⇒ keep order) and the Go SortEval sign test. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | # original-index tie-break then preserves input order for equal keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 21 | ▶ | ($c < 0 ? -1 : $c > 0 ? 1 : 0) || ($a->[0] <=> $b->[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
($c < 0 ? -1 : ($c > 0 ? 1 : 0)) || $BarefootJS::Evaluator::a->[0] <=> $BarefootJS::Evaluator::b->[0]
MC/DC0%0/2
($c < 0 ? -1 : ($c > 0 ? 1 : 0)) || $BarefootJS::Evaluator::a->[0] <=> $BarefootJS::Evaluator::b->[0]
($c < 0 ? -1 : ($c > 0 ? 1 : 0))
$BarefootJS::Evaluator::a->[0] <=> $BarefootJS::Evaluator::b->[0]
Truth table66%2/3
A || B
A= ($c < 0 ? -1 : ($c > 0 ? 1 : 0))B= $BarefootJS::Evaluator::a->[0] <=> $BarefootJS::Evaluator::b->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | } @decorated; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | 7 | return [ (map { $_->[1] } @sorted), @tail ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | # JSON entry points for the adapters: decode the callback body once, then fold / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | # sort. Mirror the Go `bf_reduce_eval` / `bf_sort_eval` template funcs, which the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | # adapters emit with a serialized-ParsedExpr body argument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 3 | ▶ | sub fold_json ($items, $body_json, $acc_name, $item_name, $init, $direction = 'left', $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fold_json: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 3 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 3 | return fold($items, JSON::PP->new->decode($body_json), $acc_name, $item_name, $init, $direction, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 2 | ▶ | sub sort_by_json ($items, $cmp_json, $param_a, $param_b, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_by_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 2 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 2 | return sort_by($items, JSON::PP->new->decode($cmp_json), $param_a, $param_b, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | # Higher-order predicates (#2018, P2) — the generalization of bf_filter / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | # bf_find / bf_find_index / bf_every / bf_some onto the evaluator. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | # predicate $pred is a pure ParsedExpr evaluated against `{$param => item}` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | # plus the captured free vars in $base_env per element, lifting the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | # field-equality / truthiness restriction to any pure predicate body. Each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | # mirrors the corresponding Go helper (FilterEval / EveryEval / SomeEval / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | # FindEval / FindIndexEval). $base_env is optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | # filter — new arrayref of the elements the predicate keeps. Non-array receiver | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | # → empty arrayref (the BarefootJS->filter nil-tolerant convention). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | 4 | ▶ | sub filter ($items, $pred, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | 4 | ▶ | return [] unless ref $items eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | 4 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 4 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 4 | for my $item (@$items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 12 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 12 | ▶ | push @out, $item if _truthy(evaluate($pred, \%env)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 4 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | # every — 1 iff the predicate holds for every element (vacuously 1 for an empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | # receiver, like JS). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 3 | ▶ | sub every ($items, $pred, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
every: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 3 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | 3 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | 3 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 2 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 2 | ▶ | return 0 unless _truthy(evaluate($pred, \%env)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | # some — 1 iff the predicate holds for any element (0 for an empty receiver). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 3 | ▶ | sub some ($items, $pred, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
some: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 3 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | 3 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 3 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | 4 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | 4 | ▶ | return 1 if _truthy(evaluate($pred, \%env)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | # find — first matching element, or `undefined`. $forward false searches from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | # the end (findLast). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | 4 | ▶ | sub find ($items, $pred, $param, $forward = 1, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 4 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 4 | ▶ | @arr = reverse @arr unless $forward; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | 4 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | 4 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | 5 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 5 | ▶ | return $item if _truthy(evaluate($pred, \%env)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 1 | return $UNDEF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | # find_index — index of the first matching element, or -1. $forward false → | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | # findLastIndex (the index is into the original array either way). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 5 | ▶ | sub find_index ($items, $pred, $param, $forward = 1, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_index: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 5 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 5 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | 5 | ▶ | my @idx = $forward ? ( 0 .. $#arr ) : reverse( 0 .. $#arr ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 5 | for my $i (@idx) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | 7 | $env{$param} = $arr[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | 7 | ▶ | return $i if _truthy(evaluate($pred, \%env)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | 2 | return -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | # flat_map — project each element through $proj (a pure ParsedExpr) and flatten | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | # the results one level. A projection yielding an arrayref contributes its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | # elements; any other value contributes itself (JS `.flatMap` keeps a non-array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | # return as a single element). Generalizes bf->flat_map / flat_map_tuple to any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | # pure projection. Mirrors Go's FlatMapEval. $base_env is optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 3 | ▶ | sub flat_map ($items, $proj, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flat_map: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 3 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | 3 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | 3 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 3 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | 6 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | 6 | my $v = evaluate($proj, \%env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 6 | ▶ | if (ref $v eq 'ARRAY') { push @out, @$v } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 0 | else { push @out, $v } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 3 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | # map_items — project each element through $proj (a pure ParsedExpr), keeping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | # each result as one element (no flatten): JS value-producing `.map(cb)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | # (#2073). Named map_items (not `map`) so the Perl builtin stays unshadowed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | # Mirrors Go's MapEval. $base_env is optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | 4 | ▶ | sub map_items ($items, $proj, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
map_items: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | 4 | ▶ | my @arr = ref $items eq 'ARRAY' ? @$items : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | 4 | ▶ | my %env = $base_env ? %$base_env : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | 4 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | 4 | for my $item (@arr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | 7 | $env{$param} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 7 | push @out, evaluate($proj, \%env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | 4 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | # JSON-string seams — the adapters emit `bf->filter_eval($recv, '<json>', …)`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | # the predicate body arrives as a JSON string here, decoded then handed to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | # helper above (mirroring fold_json / sort_by_json). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | 1 | ▶ | sub filter_json ($items, $pred_json, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | 1 | return filter($items, JSON::PP->new->decode($pred_json), $param, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | 1 | ▶ | sub every_json ($items, $pred_json, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
every_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | 1 | return every($items, JSON::PP->new->decode($pred_json), $param, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | 1 | ▶ | sub some_json ($items, $pred_json, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
some_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | 1 | return some($items, JSON::PP->new->decode($pred_json), $param, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | 1 | ▶ | sub find_json ($items, $pred_json, $param, $forward = 1, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_json: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 1 | return find($items, JSON::PP->new->decode($pred_json), $param, $forward, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 1 | ▶ | sub find_index_json ($items, $pred_json, $param, $forward = 1, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_index_json: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | 1 | return find_index($items, JSON::PP->new->decode($pred_json), $param, $forward, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | 1 | ▶ | sub flat_map_json ($items, $proj_json, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flat_map_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 1 | return flat_map($items, JSON::PP->new->decode($proj_json), $param, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | 1 | ▶ | sub map_json ($items, $proj_json, $param, $base_env = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
map_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | 1 | return map_items($items, JSON::PP->new->decode($proj_json), $param, $base_env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||