lib/JavaScript/Duktape.pm

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