Template/JSAdapter/JavaScript/SpiderMonkey.pm

filter: Statementstmt 38.8% 26 / 67 Branchbran 4.5% 1 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 35.5% 37 / 104 CC 13 SCAR 35.4
CC
13
Cov
30%
__ANON__
90.0
__ANON__
6.0
__ANON__
6.0
CRAP 34.4
↑ Summary Template/JSAdapter/JavaScript.pm »
1 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 EJS::Template::JSAdapter::JavaScript::SpiderMonkey
8  
9 =cut
10  
11 package EJS::Template::JSAdapter::JavaScript::SpiderMonkey;
12 1 ▶use base 'EJS::Template::JSAdapter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use EJS::Template::Util qw(clean_text_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $ENCODE_UTF8 = 0;
18 1 our $SANITIZE_UTF8 = 0;
19 1 our $FORCE_UNTAINT = 0;
20 1 our $PRESERVE_UTF8 = 0;
21  
22 =head1 Methods
23  
24 =head2 new
25  
26 Creates an adapter object.
27  
28 =cut
29  
30 1 ▶sub new {
Subroutine new: called CC=2 SCAR=9.7
Pod documented
31 1 my ($class) = @_;
32 0 ▶ eval 'use JavaScript::SpiderMonkey';
Subroutine BEGIN: called CC=1 SCAR=1.2
33 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
34 0 my $engine = JavaScript::SpiderMonkey->new;
35 0 $engine->init();
36 0 return bless {engine => $engine}, $class;
37 }
38  
39 =head2 bind
40  
41 Implements the bind method.
42  
43 =cut
44  
45 0 ▶sub bind {
Subroutine bind: not called CC=1 SCAR=6.9
Pod documented
46 0 my ($self, $variables) = @_;
47 0 my $engine = $self->engine;
48
49 0 my $assign_value;
50 my $assign_hash;
51 0 my $assign_array;
52
53 0 ▶ $assign_value = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
54 0 my ($obj, $parent_path, $name, $source_ref, $in_array) = @_;
55
56 0 my $reftype = reftype $$source_ref;
57 0 ▶ my $path = $parent_path ne '' ? "$parent_path.$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$parent_path ne '' ? : 0 0 0
58
59 0 ▶ if ($reftype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype) { } 0 0 0
60 0 ▶ if ($reftype eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'CODE') { } 0 0 0
elsif ($reftype eq 'SCALAR') { } 0 0 0
61 0 my $new_obj = $engine->object_by_path($path);
62 0 $assign_hash->($new_obj, $$source_ref, $path);
63 } elsif ($reftype eq 'ARRAY') {
64 0 my $new_obj = $engine->array_by_path($path);
65 0 $assign_array->($new_obj, $$source_ref, $path);
66 } elsif ($reftype eq 'CODE') {
67 0 $engine->function_set($name, $$source_ref, $obj);
68 } elsif ($reftype eq 'SCALAR') {
69 0 $assign_array->($obj, $parent_path, $name, $$source_ref, $in_array);
70 } else {
71 # ignore?
72 }
73 } else {
74 0 my $text_ref = clean_text_ref($source_ref, $ENCODE_UTF8, $SANITIZE_UTF8, $FORCE_UNTAINT);
75
76 0 ▶ if ($in_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_array) { } 0 0 0
77 0 $engine->array_set_element($obj, $name, $$text_ref);
78 } else {
79 0 ▶ if ($parent_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_path) { } 0 0 0
80 0 $engine->property_by_path($path, $$text_ref);
81 } else {
82 JavaScript::SpiderMonkey::JS_DefineProperty(
83 0 $engine->{context}, $obj, $name, $$text_ref);
84 }
85 }
86 }
87 0 };
88
89 0 ▶ $assign_hash = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 my ($obj, $source, $parent_path) = @_;
91
92 0 for my $name (keys %$source) {
93 0 $assign_value->($obj, $parent_path, $name, \$source->{$name});
94 }
95 0 };
96
97 0 ▶ $assign_array = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
98 0 my ($obj, $source, $parent_path) = @_;
99 0 my $len = scalar(@$source);
100
101 0 ▶ for (my $i = 0; $i < $len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
102 0 $assign_value->($obj, $parent_path, $i, \$source->[$i], 1);
103 }
104 0 };
105
106 0 $assign_hash->($engine->{global_object}, $variables, '');
107 0 return $engine;
108 }
109  
110 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
111 0 my ($self) = @_;
112 0 ▶ $self->{engine}->destroy() if $self->{engine};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"engine"} 0 0 0
113 0 delete $self->{engine};
114 }
115  
116 =head1 SEE ALSO
117  
118 =over 4
119  
120 =item * L<EJS::Template>
121  
122 =item * L<EJS::Template::JSAdapter>
123  
124 =item * L<JavaScript::SpiderMonkey>
125  
126 =back
127  
128 =cut
129  
130 1 1;
↑ Summary Template/JSAdapter/JavaScript.pm »