File Coverage

blib/lib/Language/Expr/JS.pm
Criterion Covered Total %
statement 8 35 22.8
branch 0 10 0.0
condition 0 5 0.0
subroutine 3 5 60.0
pod 1 1 100.0
total 12 56 21.4


line stmt bran cond sub pod time code
1             package Language::Expr::JS;
2              
3             our $DATE = '2016-07-01'; # DATE
4             our $VERSION = '0.28'; # VERSION
5              
6 1     1   357 use 5.010;
  1         3  
7 1     1   5 use strict;
  1         1  
  1         29  
8 1     1   5 use warnings;
  1         1  
  1         353  
9             #use Log::Any qw($log);
10              
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(eval_expr_js);
14              
15             sub _comment {
16 0     0     my $str = shift;
17 0           $str =~ s!^!// !g;
18 0           $str;
19             }
20              
21             sub eval_expr_js {
22 0     0 1   require File::Temp;
23 0           require IPC::System::Options;
24 0           require JSON::MaybeXS;
25 0           require Language::Expr::Compiler::js;
26 0           require Nodejs::Util;
27              
28 0           my ($expr, $opts) = @_;
29 0   0       $opts //= {};
30              
31 0           state $nodejs_path = Nodejs::Util::get_nodejs_path();
32 0 0         die "Can't find node.js in PATH" unless $nodejs_path;
33              
34 0           state $default_jsc = Language::Expr::Compiler::js->new;
35              
36 0           state $json = JSON::MaybeXS->new->allow_nonref;
37              
38 0   0       my $jsc = $opts->{js_compiler} // $default_jsc;
39              
40             # code to be sent to nodejs
41             my $src = join(
42             "",
43             _comment("expr: $expr\n"),
44             ($opts->{vars} ?
45 0 0         _comment("declare vars\n") . join("", map { "let $_ = ".$json->encode($opts->{vars}{$_}).";\n" } sort keys %{$opts->{vars}})
  0            
  0            
46             : ""),
47             "console.log(JSON.stringify(",
48             $jsc->compile($expr),
49             "))",
50             );
51 0           my ($jsh, $jsfn) = File::Temp::tempfile();
52 0           print $jsh $src;
53 0 0         close($jsh) or die "Can't write JS code to file $jsfn: $!";
54              
55 0           my ($stdout, $stderr);
56 0           IPC::System::Options::system(
57             {capture_stdout => \$stdout, capture_stderr => \$stderr},
58             $nodejs_path, "--use_strict", "--harmony_scoping", $jsfn,
59             );
60 0 0         die "nodejs exists non-zero (".($? >> 8)."): $stderr" if $?;
61 0 0         if ($stdout eq "undefined\n") {
62 0           return undef;
63             }
64 0           $json->decode($stdout);
65             }
66              
67             1;
68             # ABSTRACT: Evaluate Expr JavaScript code
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Language::Expr::JS - Evaluate Expr JavaScript code
79              
80             =head1 VERSION
81              
82             This document describes version 0.28 of Language::Expr::JS (from Perl distribution Language-Expr), released on 2016-07-01.
83              
84             =head1 SYNOPSIS
85              
86             use Language::Expr::JS qw(eval_expr_js);
87              
88             say eval_expr_js('"a" . "b"'); # "ab"
89              
90             =head1 DESCRIPTION
91              
92             =head1 FUNCTIONS
93              
94             None exported by default.
95              
96             =head2 eval_expr_js($expr) => str
97              
98             Compile $expr to JavaScript code, then run the JavaScript code using Node.js,
99             and return the result.
100              
101             =head1 HOMEPAGE
102              
103             Please visit the project's homepage at L<https://metacpan.org/release/Language-Expr>.
104              
105             =head1 SOURCE
106              
107             Source repository is at L<https://github.com/perlancar/perl-Language-Expr>.
108              
109             =head1 BUGS
110              
111             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Language-Expr>
112              
113             When submitting a bug or request, please include a test-file or a
114             patch to an existing test-file that illustrates the bug or desired
115             feature.
116              
117             =head1 AUTHOR
118              
119             perlancar <perlancar@cpan.org>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2016 by perlancar@cpan.org.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut