Template/JSAdapter.pm

filter: Statementstmt 76.1% 32 / 42 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 100% 5 / 5 totaltotal 70.6% 53 / 75 CC 12 SCAR 18.8
CC
12
Cov
68%
bind
12.0
create
9.1
eval
3.1
CRAP 6.5
« Template/Util.pm ↑ Summary Template/Parser/Context.pm »
1 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 EJS::Template::JSAdapter - JavaScript engine adapter for EJS::Template
8  
9 =cut
10  
11 package EJS::Template::JSAdapter;
12  
13 =head1 Variables
14  
15 =head2 @SUPPORTED_ENGINES
16  
17 Supported JavaScript engine classes
18  
19 =over 4
20  
21 =item * L<JavaScript::V8>
22  
23 =item * L<JavaScript>
24  
25 =item * L<JavaScript::SpiderMonkey>
26  
27 =item * L<JE>
28  
29 =back
30  
31 =cut
32  
33 8 our @SUPPORTED_ENGINES = qw(
34 JavaScript::V8
35 JavaScript
36 JavaScript::SpiderMonkey
37 JE
38 );
39  
40 8 my $default_adapter_class;
41  
42 =head1 Methods
43  
44 =head2 create
45  
46 Instantiates a JavaScript engine adapter object.
47  
48 my $adapter = EJS::Template::JSAdapter->create();
49  
50 If no argument is passed, an engine is selected from the available ones.
51  
52 An explicit engine can also be specified. E.g.
53  
54 my $je_engine = EJS::Template::JSAdapter->create('JE');
55 my $v8_engine = EJS::Template::JSAdapter->create('JavaScript::V8');
56  
57 =cut
58  
59 58 ▶sub create {
Subroutine create: called CC=8 SCAR=22.1
Pod documented
60 58 my ($class, $engine) = @_;
61
62 58 ▶ if ($engine) {
Branch: 2 branches
BranchTrueFalseTotal
if ($engine) { } 33 25 58
elsif ($default_adapter_class) { } 20 5 25
63 33 my $adapter_class = $class.'::'.$engine;
64 33 eval "require $adapter_class";
65
66 33 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 33 33
67 0 $adapter_class = $engine;
68 0 eval "require $adapter_class";
69 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
70 }
71
72 33 return $adapter_class->new();
73 } elsif ($default_adapter_class) {
74 20 return $default_adapter_class->new();
75 } else {
76 5 for my $candidate (@SUPPORTED_ENGINES) {
77 20 eval "require $candidate";
78 20 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 15 5 20
79
80 5 my $adapter_class = $class.'::'.$candidate;
81 5 eval "require $adapter_class";
82 5 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
83
84 5 $default_adapter_class = $adapter_class;
85 5 return $adapter_class->new();
86 }
87
88 0 die "No JavaScript engine modules are found. ".
89 "Consider to install JavaScript::V8";
90 }
91 }
92  
93 =head2 new
94  
95 Creates an adapter object.
96  
97 This method should be overridden, and a property named 'engine' is expected to be set up.
98  
99 package Some::Extended::JSAdapter;
100 use base 'EJS::Template::JSAdapter';
101
102 sub new {
103 my ($class) = @_;
104 my $engine = Some::Underlying::JavaScript::Engine->new();
105 return bless {engine => $engine}, $class;
106 }
107  
108 =cut
109  
110 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($class) = @_;
112 0 return bless {engine => undef}, $class;
113 }
114  
115 =head2 engine
116  
117 Retrieves the underlying engine object.
118  
119 =cut
120  
121 256 ▶sub engine {
Subroutine engine: called CC=1 SCAR=0.0
Pod documented
122 256 my ($self) = @_;
123 256 return $self->{engine};
124 }
125  
126 =head2 bind
127  
128 Binds variable mapping to JavaScript objects.
129  
130 This method should be overridden in a way that it can be invoked like this:
131  
132 $engine->bind({
133 varname1 => $object1,
134 funcname2 => sub {...},
135 ...
136 });
137  
138 =cut
139  
140 0 ▶sub bind {
Subroutine bind: not called CC=3 SCAR=24.8
Pod documented
141 0 my ($self, $variables) = @_;
142
143 0 ▶ if (my $engine = $self->engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $engine = $self->engine) 0 0 0
144 0 ▶ if ($engine->can('bind')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->can("bind")) 0 0 0
145 0 return $engine->bind($variables);
146 }
147 }
148 }
149  
150 =head2 eval
151  
152 Evaluates a JavaScript code.
153  
154 This method should be overridden in a way that it can be invoked like this:
155  
156 $engine->eval('print("ok\n")');
157  
158 =cut
159  
160 62 ▶sub eval {
Subroutine eval: called CC=3 SCAR=11.4
Pod documented
161 62 my ($self) = @_;
162
163 62 ▶ if (my $engine = $self->engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $engine = $self->engine) 62 0 62
164 62 ▶ if ($engine->can('eval')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->can("eval")) 62 0 62
165 62 return $engine->eval($_[1]);
166 }
167 }
168 }
169  
170 =head1 SEE ALSO
171  
172 =over 4
173  
174 =item * L<EJS::Template>
175  
176 =back
177  
178 =cut
179  
180 8 1;
« Template/Util.pm ↑ Summary Template/Parser/Context.pm »