lib/JavaScript/Embedded.pm

filter: Statementstmt 94.5% 525 / 555 Branchbran 83.1% 153 / 184 Conditioncond 72.2% 26 / 36 MC/DCmcdc 52.3% 11 / 21 Subroutinesub 96.8% 93 / 96 Podpod 64.2% 9 / 14 totaltotal 90.1% 817 / 906 CC 108 SCAR 19.7
CC
108
Cov
91%
push_perl
23.2
to_perl
18.0
__ANON__
12.9
CRAP 7.1
« blib/lib/JavaScript/Embedded.pm ↑ Summary blib/lib/JavaScript/Embedded/C/FunctionsMap.pl »
1 package JavaScript::Embedded;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 57 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
10 # Create Inline's build directory:
11 57 ▶ mkdir "$Config{installsitelib}/_Inline"
Branch: 1 branch
BranchTrueFalseTotal
unless -d qq[$JavaScript::Embedded::Config{"installsitelib"}/_Inline] 1 56 57
12 unless -d "$Config{installsitelib}/_Inline";
13 };
14  
15 57 our $VERSION = '2.7.3';
16  
17 57 my $GlobalRef = {};
18  
19 57 my $THIS;
20 my $DUKTAPE;
21 57 my $isNew = bless [], "NEW";
22 57 my $HEAP = bless [], "HEAP";
23 57 my $DUK = bless [], "DUK";
24 57 my $NOARGS = bless [], "NOARGS";
25  
26 57 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 57 our @EXPORT = qw (
28 DUK_TYPE_NONE
29 DUK_TYPE_UNDEFINED
30 DUK_TYPE_NULL
31 DUK_TYPE_BOOLEAN
32 DUK_TYPE_NUMBER
33 DUK_TYPE_STRING
34 DUK_TYPE_OBJECT
35 DUK_TYPE_BUFFER
36 DUK_TYPE_POINTER
37 DUK_TYPE_LIGHTFUNC
38 DUK_ENUM_INCLUDE_NONENUMERABLE
39 DUK_ENUM_INCLUDE_HIDDEN
40 DUK_ENUM_INCLUDE_SYMBOLS
41 DUK_ENUM_EXCLUDE_STRINGS
42 DUK_ENUM_INCLUDE_INTERNAL
43 DUK_ENUM_OWN_PROPERTIES_ONLY
44 DUK_ENUM_ARRAY_INDICES_ONLY
45 DUK_ENUM_SORT_ARRAY_INDICES
46 DUK_ENUM_NO_PROXY_BEHAVIOR
47 DUK_TYPE_MASK_NONE
48 DUK_TYPE_MASK_UNDEFINED
49 DUK_TYPE_MASK_NULL
50 DUK_TYPE_MASK_BOOLEAN
51 DUK_TYPE_MASK_NUMBER
52 DUK_TYPE_MASK_STRING
53 DUK_TYPE_MASK_OBJECT
54 DUK_TYPE_MASK_BUFFER
55 DUK_TYPE_MASK_POINTER
56 DUK_TYPE_MASK_LIGHTFUNC
57 DUK_TYPE_MASK_THROW
58 DUK_COMPILE_EVAL
59 DUK_COMPILE_FUNCTION
60 DUK_COMPILE_STRICT
61 DUK_COMPILE_SAFE
62 DUK_COMPILE_NORESULT
63 DUK_COMPILE_NOSOURCE
64 DUK_COMPILE_STRLEN
65 DUK_DEFPROP_WRITABLE
66 DUK_DEFPROP_ENUMERABLE
67 DUK_DEFPROP_CONFIGURABLE
68 DUK_DEFPROP_HAVE_WRITABLE
69 DUK_DEFPROP_HAVE_ENUMERABLE
70 DUK_DEFPROP_HAVE_CONFIGURABLE
71 DUK_DEFPROP_HAVE_VALUE
72 DUK_DEFPROP_HAVE_GETTER
73 DUK_DEFPROP_HAVE_SETTER
74 DUK_DEFPROP_FORCE
75 DUK_VARARGS
76 null
77 true
78 false
79 _
80 this
81 );
82  
83 ##constants
84 57 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
85 57 DUK_TYPE_NONE => 0,
86 DUK_TYPE_UNDEFINED => 1,
87 DUK_TYPE_NULL => 2,
88 DUK_TYPE_BOOLEAN => 3,
89 DUK_TYPE_NUMBER => 4,
90 DUK_TYPE_STRING => 5,
91 DUK_TYPE_OBJECT => 6,
92 DUK_TYPE_BUFFER => 7,
93 DUK_TYPE_POINTER => 8,
94 DUK_TYPE_LIGHTFUNC => 9,
95  
96 DUK_TYPE_MASK_NONE => ( 1 << 0 ),
97 DUK_TYPE_MASK_UNDEFINED => ( 1 << 1 ),
98 DUK_TYPE_MASK_NULL => ( 1 << 2 ),
99 DUK_TYPE_MASK_BOOLEAN => ( 1 << 3 ),
100 DUK_TYPE_MASK_NUMBER => ( 1 << 4 ),
101 DUK_TYPE_MASK_STRING => ( 1 << 5 ),
102 DUK_TYPE_MASK_OBJECT => ( 1 << 6 ),
103 DUK_TYPE_MASK_BUFFER => ( 1 << 7 ),
104 DUK_TYPE_MASK_POINTER => ( 1 << 8 ),
105 DUK_TYPE_MASK_LIGHTFUNC => ( 1 << 9 ),
106 DUK_TYPE_MASK_THROW => ( 1 << 10 ),
107  
108 # Enumeration flags for duk_enum()
109 DUK_ENUM_INCLUDE_NONENUMERABLE => ( 1 << 0 ),
110 DUK_ENUM_INCLUDE_HIDDEN => ( 1 << 1 ),
111 DUK_ENUM_INCLUDE_SYMBOLS => ( 1 << 2 ),
112 DUK_ENUM_EXCLUDE_STRINGS => ( 1 << 3 ),
113 DUK_ENUM_OWN_PROPERTIES_ONLY => ( 1 << 4 ),
114 DUK_ENUM_ARRAY_INDICES_ONLY => ( 1 << 5 ),
115 DUK_ENUM_SORT_ARRAY_INDICES => ( 1 << 6 ),
116 DUK_ENUM_NO_PROXY_BEHAVIOR => ( 1 << 7 ),
117  
118 DUK_COMPILE_EVAL => ( 1 << 3 ),
119 DUK_COMPILE_FUNCTION => ( 1 << 4 ),
120 DUK_COMPILE_STRICT => ( 1 << 5 ),
121 DUK_COMPILE_SAFE => ( 1 << 6 ),
122 DUK_COMPILE_NORESULT => ( 1 << 7 ),
123 DUK_COMPILE_NOSOURCE => ( 1 << 8 ),
124 DUK_COMPILE_STRLEN => ( 1 << 9 ),
125  
126 #Flags for duk_def_prop() and its variants
127 DUK_DEFPROP_WRITABLE => ( 1 << 0 ),
128 DUK_DEFPROP_ENUMERABLE => ( 1 << 1 ),
129 DUK_DEFPROP_CONFIGURABLE => ( 1 << 2 ),
130 DUK_DEFPROP_HAVE_WRITABLE => ( 1 << 3 ),
131 DUK_DEFPROP_HAVE_ENUMERABLE => ( 1 << 4 ),
132 DUK_DEFPROP_HAVE_CONFIGURABLE => ( 1 << 5 ),
133 DUK_DEFPROP_HAVE_VALUE => ( 1 << 6 ),
134 DUK_DEFPROP_HAVE_GETTER => ( 1 << 7 ),
135 DUK_DEFPROP_HAVE_SETTER => ( 1 << 8 ),
136 DUK_DEFPROP_FORCE => ( 1 << 9 ),
137 DUK_VARARGS => -1
138 57 };
139  
140 75 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod undocumented
141 75 my $class = shift;
142 75 my %options = @_;
143  
144 75 ▶ my $max_memory = $options{max_memory} || 0;
Condition100%2/2
$options{'max_memory'} || 0
l!l
669
MC/DC100%1/1
$options{'max_memory'} || 0
$options{'max_memory'}
Truth table100%2/2
A
A= $options{'max_memory'}
Aresult
11
00
145 75 ▶ my $timeout = $options{timeout} || 0;
Condition100%2/2
$options{'timeout'} || 0
l!l
372
MC/DC100%1/1
$options{'timeout'} || 0
$options{'timeout'}
Truth table100%2/2
A
A= $options{'timeout'}
Aresult
11
00
146  
147 75 ▶ if ($timeout){
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 3 72 75
148 3 ▶ croak "timeout option must be a number" if !JavaScript::Embedded::Vm::duk_sv_is_number( $timeout );
Branch: 1 branch
BranchTrueFalseTotal
unless JavaScript::Embedded::Vm::duk_sv_is_number($timeout) 1 2 3
149 }
150  
151 74 ▶ if ( $max_memory ){
Branch: 1 branch
BranchTrueFalseTotal
if ($max_memory) 6 68 74
152 6 ▶ croak "max_memory option must be a number" if !JavaScript::Embedded::Vm::duk_sv_is_number( $max_memory );
Branch: 1 branch
BranchTrueFalseTotal
unless JavaScript::Embedded::Vm::duk_sv_is_number($max_memory) 1 5 6
153 5 ▶ croak "max_memory must be at least 256k (256 * 1024)" if $max_memory < 256 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $max_memory < 262144 1 4 5
154 }
155  
156 72 my $self = bless {}, $class;
157  
158 72 my $duk = $self->{duk} = JavaScript::Embedded::Vm->perl_duk_new( $max_memory, $timeout );
159  
160 72 $self->{pid} = $$;
161 72 $self->{max_memory} = $max_memory;
162  
163 # Initialize global stash 'PerlGlobalStash'
164 # this will be used to store some perl refs
165 72 $duk->push_global_stash();
166 72 $duk->push_object();
167 72 $duk->put_prop_string( -2, "PerlGlobalStash" );
168 72 $duk->pop();
169  
170 72 $THIS = bless { duk => $duk, heapptr => 0 }, "JavaScript::Embedded::Object";
171  
172 ##finalizer method
173 155 ▶ $self->{finalizer} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
174 155 my $ref = $duk->get_string(0);
175 155 delete $GlobalRef->{$ref};
176 155 return 1;
177 72 };
178  
179 72 weaken $GlobalRef;
180  
181 72 $duk->perl_push_function( $self->{finalizer}, 1 );
182 72 $duk->put_global_string('perlFinalizer');
183  
184 72 return $self;
185 }
186  
187 2 ▶sub null { $JavaScript::Embedded::NULL::null; }
Subroutine null: called CC=1 SCAR=0.0
Pod documented
188 9 ▶sub true { $JavaScript::Embedded::Bool::true; }
Subroutine true: called CC=1 SCAR=0.0
Pod documented
189 7 ▶sub false { $JavaScript::Embedded::Bool::false }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
190 3 ▶sub JavaScript::Embedded::_ { $NOARGS }
Subroutine _: called CC=1 SCAR=0.0
191 4 ▶sub this { $THIS }
Subroutine this: called CC=1 SCAR=0.0
Pod documented
192  
193 46 ▶sub set {
Subroutine set: called CC=4 SCAR=14.0
Pod documented
194 46 my $self = shift;
195 46 my $name = shift;
196 46 my $val = shift;
197 46 my $duk = $self->vm;
198  
199 46 ▶ if ( $name =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\./) 2 44 46
200  
201 2 my @props = split /\./, $name;
202 2 my $last = pop @props;
203 2 my $others = join '.', @props;
204  
205 2 $duk->perl_duk_arm_timeout();
206 2 ▶ if ( $duk->peval_string($others) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->peval_string($others) != 0) 0 2 2
207 0 croak $others . " is not a javascript object ";
208 }
209  
210 2 my $type = $duk->get_type(-1);
211 2 ▶ if ( $type != DUK_TYPE_OBJECT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 6) 0 2 2
212 0 croak $others . " isn't an object";
213 }
214  
215 2 $duk->push_string($last);
216 2 $duk->push_perl($val);
217 2 $duk->put_prop(-3);
218 2 $duk->pop();
219 2 return 1;
220 }
221  
222 44 $duk->push_perl($val);
223 44 $duk->put_global_string($name);
224 44 return 1;
225 }
226  
227 1 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod documented
228 1 my $self = shift;
229 1 my $name = shift;
230 1 my $duk = $self->vm;
231 1 $duk->push_string($name);
232 1 $duk->perl_duk_arm_timeout();
233 1 ▶ if ( $duk->peval() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->peval != 0) 0 1 1
234 0 croak $duk->last_error_string();
235 }
236 1 my $ret = $duk->to_perl(-1);
237 1 $duk->pop();
238 1 return $ret;
239 }
240  
241 8 ▶sub get_object {
Subroutine get_object: called CC=2 SCAR=7.0
Pod documented
242 8 my $self = shift;
243 8 my $name = shift;
244 8 my $duk = $self->vm;
245 8 $duk->push_string($name);
246 8 $duk->perl_duk_arm_timeout();
247 8 ▶ if ( $duk->peval() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->peval != 0) 0 8 8
248 0 croak $duk->last_error_string();
249 }
250 8 my $ret = $duk->to_perl_object(-1);
251 8 $duk->pop();
252 8 return $ret;
253 }
254  
255 ##FIXME : should pop here?
256 39 ▶sub eval {
Subroutine eval: called CC=2 SCAR=6.9
Pod documented
257 39 my $self = shift;
258 39 my $string = shift;
259 39 my $duk = $self->duk;
260  
261 39 $duk->perl_duk_arm_timeout();
262 39 ▶ if ( $duk->peval_string($string) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->peval_string($string) != 0) 11 28 39
263 11 croak $duk->last_error_string();
264 }
265  
266 28 return $duk->to_perl(-1);
267 }
268  
269 55 ▶sub vm { shift->{duk}; }
Subroutine vm: called CC=1 SCAR=0.0
Pod undocumented
270 93 ▶sub duk { shift->{duk}; }
Subroutine duk: called CC=1 SCAR=0.0
Pod undocumented
271  
272 4 ▶sub set_timeout {
Subroutine set_timeout: called CC=1 SCAR=0.0
Pod undocumented
273 4 my $self = shift;
274 4 $self->duk->set_timeout( shift );
275 }
276  
277 2 ▶sub resize_memory {
Subroutine resize_memory: called CC=1 SCAR=0.0
Pod documented
278 2 my $self = shift;
279 2 $self->duk->resize_memory( shift );
280 }
281  
282 72 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=7.0
Pod undocumented
283 72 local $@;
284 72 my $self = shift;
285 72 my $duk = delete $self->{duk};
286 72 ▶ return if !$duk;
Branch: 1 branch
BranchTrueFalseTotal
unless $duk 0 72 72
287 72 $duk->free_perl_duk();
288 72 $duk->destroy_heap();
289 }
290  
291 72 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=12.5
292 72 my $self = shift;
293 72 ▶ if ( $self->{pid} && $self->{pid} == $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} and $self->{"pid"} == $$) 72 0 72
Condition33%1/3
$self->{"pid"} and $self->{"pid"} == $$
!ll&&!rl&&r
0072
MC/DC0%0/2
$self->{"pid"} and $self->{"pid"} == $$
$self->{"pid"} $self->{"pid"} == $$
Truth table33%1/3
A and B
A= $self->{"pid"}B= $self->{"pid"} == $$
ABresult
0X0
100
111
294 72 $self->destroy();
295 }
296 }
297  
298 package JavaScript::Embedded::Vm;
299 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
301 57 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
302 57 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
303 57 ▶use Config qw( %Config );
Subroutine BEGIN: called CC=1 SCAR=0.0
304 57 ▶use JavaScript::Embedded::C::libPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
305 57 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 57 my $Duklib;
308  
309 57 my $BOOL_PACKAGES = {
310 'JavaScript::Embedded::Bool' => 1,
311 'boolean' => 1,
312 'JSON::PP::Boolean' => 1,
313 'JSON::Tiny::_Bool' => 1,
314 'Data::MessagePack::Boolean' => 1
315 };
316  
317 57 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
318 57 my $FunctionsMap = _get_path("FunctionsMap.pl");
319 57 require $FunctionsMap;
320  
321 342 ▶ sub _get_path { &JavaScript::Embedded::C::libPath::getPath }
Subroutine _get_path: called CC=1 SCAR=0.0
322  
323 57 ▶ $Duklib =
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 57 57
324 $^O eq 'MSWin32'
325 ? _get_path('duktape.dll')
326 : _get_path('duktape.so');
327 }
328  
329 57 ▶use Inline C => config =>
Subroutine BEGIN: called CC=1 SCAR=0.0
330 build_noisy => 1,
331 clean_after_build => 0,
332 directory => "$Config{installsitelib}/_Inline",
333 typemaps => _get_path('typemap'),
334 57 INC => '-I' . _get_path('../C') . ' -I' . _get_path('../C/lib');
335 # myextlib => $Duklib,
336 # LIBS => '-L'. _get_path('../C/lib') . ' -lduktape';
337  
338 57 ▶use Inline C => _get_path('duk_perl.c');
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 57 ▶use Inline C => q{
Subroutine BEGIN: called CC=1 SCAR=0.0
341 void poke_buffer(IV to, IV from, IV sz) {
342 memcpy( to, from, sz );
343 }
344 57 };
345  
346 57 my $ptr_format = do {
347 57 my $ptr_size = $Config{ptrsize};
348 57 ▶ $ptr_size == 4 ? "L"
Branch: 2 branches
BranchTrueFalseTotal
$ptr_size == 4 ? : 0 57 57
$ptr_size == 8 ? : 57 0 57
349 : $ptr_size == 8 ? "Q"
350 : die("Unrecognized pointer size");
351 };
352  
353 5 ▶sub peek { unpack 'P' . $_[1], pack $ptr_format, $_[0] }
Subroutine peek: called CC=1 SCAR=0.0
354 6 ▶sub pv_address { unpack( $ptr_format, pack( "p", $_[0] ) ) }
Subroutine pv_address: called CC=1 SCAR=0.0
355  
356 613635 ▶sub push_perl {
Subroutine push_perl: called CC=23 SCAR=31.4
357 613635 my $self = shift;
358 613635 my $val = shift;
359 613635 ▶ my $stash = shift || {};
Condition100%2/2
shift() || {}
l!l
58613577
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
360  
361 613635 ▶ if ( my $ref = ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $val) { } 100 613535 613635
362 100 ▶ if ( $ref eq 'JavaScript::Embedded::NULL' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($ref eq 'JavaScript::Embedded::NULL') { } 1 99 100
elsif ($BOOL_PACKAGES->{$ref}) { } 6 93 99
elsif ($ref eq 'ARRAY') { } 12 81 93
elsif ($ref eq 'HASH') { } 16 65 81
elsif ($ref eq 'CODE') { } 46 19 65
elsif ($ref eq 'JavaScript::Embedded::Object') { } 11 8 19
elsif ($ref eq 'JavaScript::Embedded::Function') { } 0 8 8
elsif ($ref eq 'JavaScript::Embedded::Pointer') { } 0 8 8
elsif ($ref eq 'JavaScript::Embedded::Buffer') { } 6 2 8
elsif ($ref eq 'SCALAR') { } 2 0 2
363 1 $self->push_null();
364 }
365  
366 elsif ( $BOOL_PACKAGES->{$ref} ) {
367 6 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 3 3 6
368 3 $self->push_true();
369 }
370 else {
371 3 $self->push_false();
372 }
373 }
374  
375 elsif ( $ref eq 'ARRAY' ) {
376 12 my $arr_idx = $self->push_array();
377 12 $stash->{$val} = $self->get_heapptr(-1);
378 12 my $len = scalar @{$val};
379 12 ▶ for ( my $idx = 0 ; $idx < $len ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $len) 32 12 44
380 32 ▶ if ( $stash->{ $val->[$idx] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$stash{$val->[$idx]}) { } 2 30 32
381 2 $self->push_heapptr( $stash->{ $val->[$idx] } );
382 }
383 else {
384 30 $self->push_perl( $val->[$idx], $stash );
385 }
386 32 $self->put_prop_index( $arr_idx, $idx );
387 }
388 }
389  
390 elsif ( $ref eq 'HASH' ) {
391 16 $self->push_object();
392 16 $stash->{$val} = $self->get_heapptr(-1);
393 16 ▶ while ( my ( $k, $v ) = each %{$val} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$val;}) 29 16 45
394 29 $self->push_string($k);
395 29 ▶ if ( $v && $stash->{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v and $stash->{$v}) { } 1 28 29
Condition100%3/3
$v and $stash->{$v}
!ll&&!rl&&r
5231
MC/DC100%2/2
$v and $stash->{$v}
$v $stash->{$v}
Truth table100%3/3
A and B
A= $vB= $stash->{$v}
ABresult
0X0
100
111
396 1 $self->push_heapptr( $stash->{$v} );
397 }
398 else {
399 28 $self->push_perl( $v, $stash );
400 }
401 29 $self->put_prop(-3);
402 }
403 }
404  
405 elsif ( $ref eq 'CODE' ) {
406 46 $self->push_function($val);
407 }
408  
409 elsif ( $ref eq 'JavaScript::Embedded::Object' ) {
410 11 $self->push_heapptr( $val->{heapptr} );
411 }
412  
413 elsif ( $ref eq 'JavaScript::Embedded::Function' ) {
414 0 $self->push_heapptr( $val->($HEAP) );
415 }
416  
417 elsif ( $ref eq 'JavaScript::Embedded::Pointer' ) {
418 0 $self->push_pointer($$val);
419 }
420  
421 elsif ( $ref eq 'JavaScript::Embedded::Buffer' ) {
422 6 ▶ my $len = defined $$val ? length($$val) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $$val ? : 5 1 6
423 6 my $ptr = $self->push_fixed_buffer($len);
424 6 poke_buffer( $ptr, pv_address($$val), $len );
425 }
426  
427 elsif ( $ref eq 'SCALAR' ) {
428 2 ▶ $$val ? $self->push_true() : $self->push_false()
Branch: 1 branch
BranchTrueFalseTotal
$$val ? : 1 1 2
429 }
430  
431 else {
432 0 $self->push_undefined();
433 }
434 }
435 else {
436 613535 ▶ if ( !defined $val ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 3 613532 613535
elsif (duk_sv_is_number($val)) { } 613102 430 613532
437 3 $self->push_undefined();
438 }
439 elsif ( duk_sv_is_number($val) ) {
440 613102 $self->push_number($val);
441 }
442 else {
443 430 $self->push_string($val);
444 }
445 }
446 }
447  
448 239 ▶sub to_perl_object {
Subroutine to_perl_object: called CC=2 SCAR=7.2
449 239 my $self = shift;
450 239 my $index = shift;
451 239 my $heapptr = $self->get_heapptr($index);
452 239 ▶ if ( !$heapptr ) { croak "value at stack $index is not an object" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($heapptr) 0 239 239
453 239 return JavaScript::Embedded::Util::jsObject(
454 {
455 duk => $self,
456 heapptr => $heapptr
457 }
458 );
459 }
460  
461 12339 ▶sub to_perl {
Subroutine to_perl: called CC=18 SCAR=28.9
462 12339 my $self = shift;
463 12339 my $index = shift;
464 12339 ▶ my $stash = shift || {};
Condition100%2/2
shift() || {}
l!l
124611093
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
465  
466 12339 my $ret;
467  
468 12339 my $type = $self->get_type($index);
469  
470 12339 ▶ if ( $type == JavaScript::Embedded::DUK_TYPE_UNDEFINED ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type == 1) { } 174 12165 12339
elsif ($type == 5) { } 8505 3660 12165
elsif ($type == 4) { } 2322 1338 3660
elsif ($type == 7) { } 5 1333 1338
elsif ($type == 6) { } 1288 45 1333
elsif ($type == 3) { } 39 6 45
elsif ($type == 2) { } 3 3 6
elsif ($type == 8) { } 3 0 3
471 174 $ret = undef;
472 }
473  
474 elsif ( $type == JavaScript::Embedded::DUK_TYPE_STRING ) {
475 8505 $ret = $self->get_utf8_string($index);
476 }
477  
478 elsif ( $type == JavaScript::Embedded::DUK_TYPE_NUMBER ) {
479 2322 $ret = $self->get_number($index);
480 }
481  
482 elsif ( $type == JavaScript::Embedded::DUK_TYPE_BUFFER ) {
483 5 my $ptr = $self->get_buffer_data( $index, my $sz );
484 5 $ret = peek( $ptr, $sz );
485 }
486  
487 elsif ( $type == JavaScript::Embedded::DUK_TYPE_OBJECT ) {
488  
489 1288 ▶ if ( $self->is_function($index) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_function($index)) 444 844 1288
490 444 my $ptr = $self->get_heapptr($index);
491 9 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
492 9 $self->push_heapptr($ptr);
493 9 $self->push_this();
494 9 my $len = 0 + @_;
495 9 ▶ for ( my $i = 0 ; $i < $len ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 9 9 18
496 9 $self->push_perl( $_[$i] );
497 }
498 9 $self->perl_duk_arm_timeout();
499 9 ▶ if ( $self->pcall_method($len) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pcall_method($len) == 1) 5 4 9
500 5 croak $self->last_error_string();
501 }
502 4 my $ret = $self->to_perl(-1);
503 4 $self->pop();
504 4 return $ret;
505 444 };
506 }
507  
508 844 my $isArray = $self->is_array($index);
509  
510 844 my $heapptr = $self->require_heapptr($index);
511 844 ▶ if ( $stash->{$heapptr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{$heapptr}) { } 0 844 844
512 0 $ret = $stash->{$heapptr};
513 }
514 else {
515 844 ▶ $ret = $isArray ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$isArray ? : 30 814 844
516 844 $stash->{$heapptr} = $ret;
517 }
518  
519 844 $self->enum( $index, JavaScript::Embedded::DUK_ENUM_OWN_PROPERTIES_ONLY );
520  
521 844 ▶ while ( $self->next( -1, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next(-1, 1)) 5750 844 6594
522 5750 my ( $key, $val );
523  
524 5750 $key = $self->to_perl(-2);
525  
526 5750 ▶ if ( $self->get_type(-1) == JavaScript::Embedded::DUK_TYPE_OBJECT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_type(-1) == 6) { } 1309 4441 5750
527 1309 my $heapptr = $self->get_heapptr(-1);
528 1309 ▶ if ( $stash->{$heapptr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{$heapptr}) { } 63 1246 1309
529 63 $val = $stash->{$heapptr};
530 }
531 else {
532 1246 $val = $self->to_perl( -1, $stash );
533 }
534 }
535 else {
536 4441 $val = $self->to_perl(-1);
537 }
538  
539 5750 $self->pop_n(2);
540  
541 5750 ▶ if ($isArray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isArray) { } 133 5617 5750
542 133 $ret->[$key] = $val;
543 }
544 else {
545 5617 $ret->{$key} = $val;
546 }
547 }
548  
549 844 $self->pop();
550 }
551  
552 elsif ( $type == JavaScript::Embedded::DUK_TYPE_BOOLEAN ) {
553 39 my $bool = $self->get_boolean($index);
554 39 ▶ if ( $bool == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bool == 1) { } 31 8 39
555 31 $ret = JavaScript::Embedded::Bool::true();
556 }
557 else {
558 8 $ret = JavaScript::Embedded::Bool::false();
559 }
560 }
561  
562 elsif ( $type == JavaScript::Embedded::DUK_TYPE_NULL ) {
563 3 $ret = JavaScript::Embedded::NULL::null();
564 }
565  
566 elsif ( $type == JavaScript::Embedded::DUK_TYPE_POINTER ) {
567 3 my $p = $self->get_pointer($index);
568 3 $ret = bless \$p, 'JavaScript::Embedded::Pointer';
569 }
570  
571 11895 return $ret;
572 }
573  
574 ##############################################
575 # push functions
576 ##############################################
577 271 ▶sub push_function {
Subroutine push_function: called CC=2 SCAR=6.9
578 271 my $self = shift;
579 271 my $sub = shift;
580 271 ▶ my $nargs = shift || -1;
Condition100%2/2
shift() || -1
l!l
12259
MC/DC100%1/1
shift() || -1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
581  
582 $self->push_c_function(
583 613202 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
584 613202 my @args;
585 613202 my $top = $self->get_top();
586 613202 ▶ for ( my $i = 0 ; $i < $top ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $top) 409 613202 613611
587 409 push @args, $self->to_perl($i);
588 }
589  
590 613202 $self->push_this();
591 613202 my $heap = $self->get_heapptr(-1);
592 613202 $self->pop();
593  
594 613202 ▶ if ( !$heap ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heap) 613094 108 613202
595 613094 $self->push_global_object();
596 613094 $heap = $self->get_heapptr(-1);
597 613094 $self->pop();
598 }
599  
600 613202 $THIS->{heapptr} = $heap;
601 613202 $THIS->{duk} = $self;
602  
603 613202 my $ret = $sub->(@args);
604 613179 $self->push_perl($ret);
605 613179 return 1;
606 },
607 271 $nargs
608 );
609 }
610  
611 #####################################################################
612 # safe call
613 #####################################################################
614 274 ▶sub push_c_function {
Subroutine push_c_function: called CC=2 SCAR=6.9
615 274 my $self = shift;
616 274 my $sub = shift;
617 274 ▶ my $nargs = shift || -1;
Condition100%2/2
shift() || -1
l!l
2722
MC/DC100%1/1
shift() || -1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
618  
619 613215 ▶ $GlobalRef->{"$sub"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
620 613215 my @args = @_;
621 613215 my $top = $self->get_top();
622 613215 my $ret = 1;
623  
624 my $err = $self->safe_call(
625 613215 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
626 613215 $ret = $sub->(@args);
627 613192 return 1;
628 },
629 613215 $top,
630 1
631 );
632  
633 613215 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 23 613192 613215
634 23 croak $self->last_error_string();
635 }
636 613192 return $ret;
637 274 };
638  
639 274 $self->perl_push_function( $GlobalRef->{"$sub"}, $nargs );
640 274 $self->eval_string("(function(){perlFinalizer('$sub')})");
641 274 $self->set_finalizer(-2);
642 }
643  
644 #####################################################################
645 # safe call
646 #####################################################################
647 613262 ▶sub safe_call {
Subroutine safe_call: called CC=2 SCAR=6.9
648 613262 my $self = shift;
649 613262 my $sub = shift;
650 613262 my $ret;
651 613262 ▶ my $safe = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
652 613262 local $@;
653 613262 eval { $ret = $sub->($self) };
654 613262 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 39 613223 613262
655 39 ▶ if ( $error =~ /^Duk::Error/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /^Duk::Error/i) { } 22 17 39
656 22 croak $self->last_error_string();
657 }
658 else {
659 17 $self->eval_string('(function (e){ throw new Error(e) })');
660 17 $self->push_string($error);
661 17 $self->call(1);
662 }
663 }
664  
665 613223 ▶ return defined $ret ? $ret : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $ret ? : 613223 0 613223
666 613262 };
667  
668 613262 $self->perl_duk_arm_timeout();
669 613262 eval { $ret = $self->perl_duk_safe_call( $safe, @_ ) };
670 613262 ▶ return defined $ret ? $ret : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $ret ? : 613223 39 613262
671 }
672  
673 6 ▶sub set_timeout {
Subroutine set_timeout: called CC=2 SCAR=6.9
674 6 my $self = shift;
675 6 my $timeout = shift;
676  
677 6 ▶ croak "timeout must be a number" if !duk_sv_is_number($timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless duk_sv_is_number($timeout) 1 5 6
678 5 $self->perl_duk_set_timeout($timeout);
679 }
680  
681 2 ▶sub resize_memory {
Subroutine resize_memory: called CC=4 SCAR=14.1
682 2 my $self = shift;
683 2 ▶ my $max_memory = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
684  
685 2 ▶ croak "max_memory should be a number" if !duk_sv_is_number( $max_memory );
Branch: 1 branch
BranchTrueFalseTotal
unless duk_sv_is_number($max_memory) 0 2 2
686 2 ▶ croak "max_memory must be at least 256k (256 * 1024)" if $max_memory < 256 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $max_memory < 262144 1 1 2
687  
688 1 $self->perl_duk_resize_memory($max_memory);
689 }
690  
691 ##############################################
692 # custom functions
693 ##############################################
694 57 *get_utf8_string = \&perl_duk_get_utf8_string;
695 57 *push_perl_function = \&push_c_function;
696 57 *push_light_function = \&perl_push_function;
697  
698 ##############################################
699 # overridden functions
700 ##############################################
701 57 *require_context = \&perl_duk_require_context;
702  
703 ##############################################
704 # helper functions
705 ##############################################
706 57 *reset_top = \&perl_duk_reset_top;
707  
708 61 ▶sub last_error_string {
Subroutine last_error_string: called CC=1 SCAR=0.0
709 61 my $self = shift;
710 61 $self->dup(-1);
711 61 my $error_str = $self->safe_to_string(-1);
712 61 $self->pop();
713 61 return $error_str;
714 }
715  
716 3 ▶sub dump {
Subroutine dump: called CC=4 SCAR=13.9
717 3 my $self = shift;
718 3 ▶ my $name = shift || "Duktape";
Condition100%2/2
shift() || 'Duktape'
l!l
21
MC/DC100%1/1
shift() || 'Duktape'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
719 3 ▶ my $fh = shift || \*STDOUT;
Condition50%1/2
shift() || \*STDOUT
l!l
03
MC/DC0%0/1
shift() || \*STDOUT
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
720 3 my $n = $self->get_top();
721 3 printf $fh "%s (top=%ld):", $name, $n;
722 3 ▶ for ( my $i = 0 ; $i < $n ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 4 3 7
723 4 printf $fh " ";
724 4 $self->dup($i);
725 4 printf $fh "%s", $self->safe_to_string(-1);
726 4 $self->pop();
727 }
728 3 printf $fh "\n";
729 }
730  
731 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
732  
733 package JavaScript::Embedded::Bool;
734 {
735 57 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
736 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
737 57 our ( $true, $false );
738 use overload
739 18 ▶ '""' => sub { ${ $_[0] } },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
740 33 ▶ 'bool' => sub { ${ $_[0] } ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 24 9 33
741 57 fallback => 1;
742  
743 57 ▶ BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.6
744 57 my $use_boolean = eval { require boolean; 1; };
745 57 my $t = 1;
746 57 my $f = 0;
747 57 ▶ $true = $use_boolean ? boolean::true() : bless \$t, 'JavaScript::Embedded::Bool';
Branch: 1 branch
BranchTrueFalseTotal
$use_boolean ? : 0 57 57
748 57 ▶ $false = $use_boolean ? boolean::false() : bless \$f, 'JavaScript::Embedded::Bool';
Branch: 1 branch
BranchTrueFalseTotal
$use_boolean ? : 0 57 57
749 }
750  
751 31 ▶ sub true { $true }
Subroutine true: called CC=1 SCAR=0.0
752 8 ▶ sub false { $false }
Subroutine false: called CC=1 SCAR=0.0
753  
754 2 ▶ sub TO_JSON { ${$_[0]} ? \1 : \0 }
Subroutine TO_JSON: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 1 1 2
755 }
756  
757 57 package JavaScript::Embedded::NULL;
758 {
759 57 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
760 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
761 57 our ($null);
762 use overload
763 2 ▶ '""' => sub { ${ $_[0] } },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
764 3 ▶ 'bool' => sub { ${ $_[0] } ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 0 3 3
765 57 fallback => 1;
766  
767 57 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
768 57 my $n = '';
769 57 $null = bless \$n, 'JavaScript::Embedded::NULL';
770 }
771  
772 3 ▶ sub null { $null }
Subroutine null: called CC=1 SCAR=0.0
773 }
774  
775 57 package JavaScript::Embedded::Object;
776 {
777 57 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
778 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
779 57 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
780 57 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
781 57 my $CONSTRUCTORS = {};
782 57 ▶ use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
783 57 ▶ use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
784 3 my $self = shift;
785 3 $self->inspect();
786 },
787 57 fallback => 1;
788  
789 3 ▶ sub inspect {
Subroutine inspect: called CC=1 SCAR=0.0
790 3 my $self = shift;
791 3 my $heapptr = $self->{heapptr};
792 3 my $duk = $self->{duk};
793 3 $duk->push_heapptr($heapptr);
794 3 my $ret = $duk->to_perl(-1);
795 3 $duk->pop();
796 3 return $ret;
797 }
798  
799 57 our $AUTOLOAD;
800  
801 521 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
802 521 my $self = shift;
803 521 my $heapptr = $self->{heapptr};
804 521 my $duk = $self->{duk};
805 521 my ($method) = ( $AUTOLOAD =~ /([^:']+$)/ );
806 521 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 521 521
807 521 return JavaScript::Embedded::Util::autoload( $self, $method, $duk, $heapptr, @_ );
808 }
809  
810 249 ▶ DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
811 249 my $self = shift;
812 249 my $duk = $self->{duk};
813  
814 249 my $refcount = delete $self->{refcount};
815 249 ▶ return if ( !$refcount );
Branch: 1 branch
BranchTrueFalseTotal
unless $refcount 17 232 249
816 232 $duk->push_global_stash();
817 232 $duk->get_prop_string( -1, "PerlGlobalStash" );
818 232 $duk->push_number($refcount);
819 232 $duk->del_prop(-2);
820 232 $duk->pop_2();
821 }
822 }
823  
824 57 package JavaScript::Embedded::Function;
825 {
826 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
827 57 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
828 57 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
829  
830 136 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
831 136 my $self = shift;
832 136 $self->( $isNew, @_ );
833 }
834  
835 57 our $AUTOLOAD;
836  
837 0 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
838 0 my $self = shift;
839 0 my $heapptr = $self->($HEAP);
840 0 my $duk = $self->($DUK);
841  
842 0 my ($method) = ( $AUTOLOAD =~ /([^:']+$)/ );
843 0 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
844 0 return JavaScript::Embedded::Util::autoload( $self, $method, $duk, $heapptr, @_ );
845 }
846  
847 0 ▶ sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
848 57 };
849  
850 package JavaScript::Embedded::Util;
851 {
852 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
853 57 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
854 57 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
855 57 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
856  
857 521 ▶ sub autoload {
Subroutine autoload: called CC=9 SCAR=25.0
858 521 my $self = shift;
859 521 my $method = shift;
860 521 my $duk = shift;
861 521 my $heapptr = shift;
862  
863 521 $duk->push_heapptr($heapptr);
864 521 ▶ if ( $method eq 'new' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "new") 0 521 521
865 0 my $len = @_ + 0;
866 0 foreach my $val (@_) {
867 0 $duk->push_perl($val);
868 }
869 0 $duk->perl_duk_arm_timeout();
870 0 ▶ if ( $duk->pnew($len) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->pnew($len) != 0) 0 0 0
871 0 croak $duk->last_error_string();
872 }
873 0 my $val = $duk->to_perl_object(-1);
874 0 $duk->pop();
875 0 return $val;
876 }
877  
878 521 my $val = undef;
879 521 $duk->get_prop_string( -1, $method );
880  
881 521 my $type = $duk->get_type(-1);
882 521 ▶ if ( $type == JavaScript::Embedded::DUK_TYPE_OBJECT
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 6 or $type == 7) { } 378 143 521
Condition66%2/3
$type == 6 or $type == 7
l!l&&r!l&&!r
3780143
MC/DC50%1/2
$type == 6 or $type == 7
$type == 6 $type == 7
Truth table66%2/3
A or B
A= $type == 6B= $type == 7
ABresult
1X1
011
000
883 || $type == JavaScript::Embedded::DUK_TYPE_BUFFER )
884 {
885  
886 378 ▶ if ( $duk->is_function(-1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->is_function(-1)) { } 378 0 378
887 378 my $function_heap = $duk->get_heapptr(-1);
888  
889 378 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 376 2 378
890 #called with special no arg _
891 376 ▶ shift if ( ref $_[0] eq 'NOARGS' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "NOARGS" 3 373 376
892 376 $val = jsFunction( $method, $duk, $function_heap, $heapptr, 'call', @_ );
893 }
894 else {
895 2 $val = jsFunction( $method, $duk, $function_heap, $heapptr );
896 }
897 }
898 else {
899 0 $val = $duk->to_perl_object(-1);
900 }
901 }
902 else {
903 143 $val = $duk->to_perl(-1);
904 }
905 521 $duk->pop_2();
906 521 return $val;
907 }
908  
909 385 ▶ sub jsFunction {
Subroutine jsFunction: called CC=3 SCAR=11.2
910 385 my $methodname = shift;
911 385 my $duk = shift;
912 385 my $heapptr = shift;
913 385 ▶ my $constructor = shift || $heapptr;
Condition33%1/3
shift() || $heapptr
l!l&&r!l&&!r
38500
MC/DC0%0/2
shift() || $heapptr
shift() $heapptr
Truth table33%1/3
A || B
A= shift()B= $heapptr
ABresult
1X1
011
000
914 385 my $doCall = shift;
915 515 ▶ my $sub = sub {
Subroutine __ANON__: called CC=12 SCAR=25.6
916  
917 # check first value, if it a ref of NEW
918 # then this is a constructor call, other wise
919 # it's just a normal call
920 515 my $isNew;
921 515 my $ref = ref $_[0];
922 515 ▶ if ( $ref eq "NEW" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'NEW') { } 136 379 515
elsif ($ref eq 'HEAP') { } 0 379 379
elsif ($ref eq 'DUK') { } 0 379 379
923 136 shift;
924 136 $isNew = 1;
925 }
926 elsif ( $ref eq "HEAP" ) {
927 0 return $heapptr;
928 }
929 elsif ( $ref eq "DUK" ) {
930 0 return $duk;
931 }
932  
933 515 my $len = @_ + 0;
934 515 $duk->push_heapptr($heapptr);
935 515 ▶ $duk->push_heapptr($constructor) if !$isNew;
Branch: 1 branch
BranchTrueFalseTotal
unless $isNew 379 136 515
936 515 foreach my $val (@_) {
937 542 ▶ if ( ref $val eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'CODE') { } 211 331 542
938 211 $duk->push_function($val);
939 }
940 else {
941 331 $duk->push_perl($val);
942 }
943 }
944  
945 515 $duk->perl_duk_arm_timeout();
946 515 ▶ if ($isNew) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isNew) { } 136 379 515
947 136 ▶ if ( $duk->pnew($len) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->pnew($len) != 0) 0 136 136
948 0 croak $duk->last_error_string();
949 }
950 }
951 else {
952 379 ▶ if ( $duk->pcall_method($len) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->pcall_method($len) != 0) 0 379 379
953 0 croak $duk->last_error_string();
954 }
955 }
956  
957 515 my $ret;
958 ##getting function call values
959 515 my $type = $duk->get_type(-1);
960 515 ▶ if ( $type == JavaScript::Embedded::DUK_TYPE_OBJECT
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 6 or $type == 7) { } 205 310 515
Condition66%2/3
$type == 6 or $type == 7
l!l&&r!l&&!r
2050310
MC/DC50%1/2
$type == 6 or $type == 7
$type == 6 $type == 7
Truth table66%2/3
A or B
A= $type == 6B= $type == 7
ABresult
1X1
011
000
961 || $type == JavaScript::Embedded::DUK_TYPE_BUFFER )
962 {
963 205 $ret = $duk->to_perl_object(-1);
964 }
965 else {
966 310 $ret = $duk->to_perl(-1);
967 }
968 515 $duk->pop();
969 515 return $ret;
970 385 };
971  
972 385 ▶ return $sub->(@_) if $doCall;
Branch: 1 branch
BranchTrueFalseTotal
if $doCall 376 9 385
973 9 return bless $sub, "JavaScript::Embedded::Function";
974 }
975  
976 57 my $REFCOUNT = 0;
977  
978 239 ▶ sub jsObject {
Subroutine jsObject: called CC=3 SCAR=11.0
979 239 my $options = shift;
980  
981 239 my $duk = $options->{duk};
982 239 my $heapptr = $options->{heapptr};
983 239 ▶ my $constructor = $options->{constructor} || $heapptr;
Condition33%1/3
$options->{'constructor'} || $heapptr
l!l&&r!l&&!r
02390
MC/DC0%0/2
$options->{'constructor'} || $heapptr
$options->{'constructor'} $heapptr
Truth table33%1/3
A || B
A= $options->{'constructor'}B= $heapptr
ABresult
1X1
011
000
984  
985 #We may push same heapptr on the global stack more
986 #than once, this results in segmentation fault when
987 #we destroy the object and delete heapptr from the
988 #global stash then trying to use it again
989 #TODO : this is really a poor man solution
990 #for this problem, we use a refcounter to create
991 #a unique id for each heapptr, a better solution
992 #would be making sure same heapptr pushed once and not to
993 #be free unless all gone
994 239 my $refcount = ( ++$REFCOUNT ) + ( rand(3) );
995  
996 239 $duk->push_global_stash();
997 239 $duk->get_prop_string( -1, "PerlGlobalStash" );
998 239 $duk->push_number($refcount);
999 239 $duk->push_heapptr($heapptr);
1000 239 $duk->put_prop(-3); #PerlGlobalStash[heapptr] = object
1001 239 $duk->pop_2();
1002  
1003 239 my $type = $duk->get_type(-1);
1004  
1005 239 ▶ if ( $duk->is_function(-1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duk->is_function(-1)) 7 232 239
1006 7 return JavaScript::Embedded::Util::jsFunction( 'anon', $duk, $heapptr, $constructor );
1007 }
1008  
1009 232 return bless {
1010 refcount => $refcount,
1011 duk => $duk,
1012 heapptr => $heapptr
1013 }, "JavaScript::Embedded::Object";
1014 }
1015 }
1016  
1017 57 1;
1018  
1019 __END__
« blib/lib/JavaScript/Embedded.pm ↑ Summary blib/lib/JavaScript/Embedded/C/FunctionsMap.pl »