File Coverage

blib/lib/Language/Expr/JS.pm
Criterion Covered Total %
statement 8 32 25.0
branch 0 8 0.0
condition 0 5 0.0
subroutine 3 5 60.0
pod 1 1 100.0
total 12 51 23.5


line stmt bran cond sub pod time code
1             package Language::Expr::JS;
2              
3             our $DATE = '2016-07-03'; # DATE
4             our $VERSION = '0.29'; # VERSION
5              
6 1     1   368 use 5.010;
  1         2  
7 1     1   3 use strict;
  1         2  
  1         16  
8 1     1   4 use warnings;
  1         1  
  1         332  
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 JSON::MaybeXS;
24 0           require Language::Expr::Compiler::js;
25 0           require Nodejs::Util;
26              
27 0           my ($expr, $opts) = @_;
28 0   0       $opts //= {};
29              
30 0           state $default_jsc = Language::Expr::Compiler::js->new;
31              
32 0           state $json = JSON::MaybeXS->new->allow_nonref;
33              
34 0   0       my $jsc = $opts->{js_compiler} // $default_jsc;
35              
36             # code to be sent to nodejs
37             my $src = join(
38             "",
39             _comment("expr: $expr\n"),
40             ($opts->{vars} ?
41 0 0         _comment("declare vars\n") . join("", map { "let $_ = ".$json->encode($opts->{vars}{$_}).";\n" } sort keys %{$opts->{vars}})
  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 0         close($jsh) or die "Can't write JS code to file $jsfn: $!";
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 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.29 of Language::Expr::JS (from Perl distribution Language-Expr), released on 2016-07-03.
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/sharyanto/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