lib/JavaScript/Embedded.pm

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