Expr/JS.pm

filter: Statementstmt 36.8% 14 / 38 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 30.0% 18 / 60 CC 7 SCAR 35.9
CC
7
Cov
27%
eval_expr_js
56.0
_comment
2.0
BEGIN
1.0
CRAP 36.1
« Expr/Interpreter/default.pm ↑ Summary Expr/EvaluatorRole.pm »
1 package Language::Expr::JS;
2  
3 2 our $DATE = '2016-07-03'; # DATE
4 2 our $VERSION = '0.29'; # VERSION
5  
6 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Log::Any qw($log);
10  
11 2 require Exporter;
12 2 our @ISA = qw(Exporter);
13 2 our @EXPORT_OK = qw(eval_expr_js);
14  
15 0 ▶sub _comment {
Subroutine _comment: not called CC=1 SCAR=6.9
16 0 my $str = shift;
17 0 $str =~ s!^!// !g;
18 0 $str;
19 }
20  
21 0 ▶sub eval_expr_js {
Subroutine eval_expr_js: not called CC=7 SCAR=40.3
Pod documented
22 0 require File::Temp;
23 0 require JSON::MaybeXS;
24 0 require Language::Expr::Compiler::js;
25 0 require Nodejs::Util;
26  
27 0 my ($expr, $opts) = @_;
28 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
29  
30 0 state $default_jsc = Language::Expr::Compiler::js->new;
31  
32 0 state $json = JSON::MaybeXS->new->allow_nonref;
33  
34 0 ▶ my $jsc = $opts->{js_compiler} // $default_jsc;
Condition0%0/3
$opts->{"js_compiler"} // $default_jsc
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"js_compiler"} // $default_jsc
$opts->{"js_compiler"} $default_jsc
Truth table0%0/3
A // B
A= $opts->{"js_compiler"}B= $default_jsc
ABresult
1X1
011
000
35  
36 # code to be sent to nodejs
37 my $src = join(
38 "",
39 _comment("expr: $expr\n"),
40 ($opts->{vars} ?
41 0 ▶ _comment("declare vars\n") . join("", map { "let $_ = ".$json->encode($opts->{vars}{$_}).";\n" } sort keys %{$opts->{vars}})
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'vars'} ? : 0 0 0
42 : ""),
43 "console.log(JSON.stringify(",
44 $jsc->compile($expr),
45 "))",
46 );
47 0 my ($jsh, $jsfn) = File::Temp::tempfile();
48 0 print $jsh $src;
49 0 ▶ close($jsh) or die "Can't write JS code to file $jsfn: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $jsh or die "Can't write JS code to file ${jsfn}: $!" 0 0 0
50  
51 0 my ($stdout, $stderr);
52 0 Nodejs::Util::system_nodejs(
53 {
54 harmony_scoping => 1,
55 capture_stdout => \$stdout,
56 capture_stderr => \$stderr
57 },
58 $jsfn,
59 );
60 0 ▶ die "nodejs exists non-zero (".($? >> 8)."): $stderr" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
61 0 ▶ if ($stdout eq "undefined\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout eq "undefined\n") 0 0 0
62 0 return undef;
63 }
64 0 $json->decode($stdout);
65 }
66  
67 2 1;
68 # ABSTRACT: Evaluate Expr JavaScript code
69  
70 __END__
« Expr/Interpreter/default.pm ↑ Summary Expr/EvaluatorRole.pm »