Skip to content

Commit 89cf427

Browse files
committed
Merge pull request #15 from mamod/v1.0.0_7
V1.0.0 7
2 parents 48e6404 + e73660a commit 89cf427

File tree

3 files changed

+173
-7
lines changed

3 files changed

+173
-7
lines changed

Makefile.PL

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ WriteMakefile(
2121
PREREQ_PM => {
2222
'Inline' => '0.80',
2323
'Inline::C' => '0.75',
24+
},
25+
TEST_REQUIRES => {
26+
'Test::Fatal' => '0.014',
27+
'Try::Tiny' => '0.24',
2428
}
2529
);

lib/JavaScript/Duktape.pm

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ sub new {
175175
return 1;
176176
};
177177

178-
$duk->push_perl_function($self->{call}, -1, 1);
178+
$duk->push_perl_function($self->{call}, -1);
179179
$duk->put_global_string('perlCall');
180-
181180
return $self;
182181
}
183182

@@ -265,11 +264,20 @@ sub eval {
265264
sub vm { shift->{duk}; }
266265
sub duk { shift->{duk}; }
267266

267+
sub destroy {
268+
my $self = shift;
269+
return if !$self->{duk};
270+
local $@; #duk_desatroy_heap mess with $@!!
271+
$self->{duk}->gc(0);
272+
$self->{duk}->destroy_heap();
273+
delete $self->{duk};
274+
}
275+
268276
sub DESTROY {
269277
my $self = shift;
270278
my $duk = $self->duk;
271279
if ($self->{pid} == $$){
272-
$duk->destroy_heap();
280+
$self->destroy();
273281
}
274282
}
275283

@@ -512,7 +520,6 @@ sub push_c_function {
512520
my $self = shift;
513521
my $sub = shift;
514522
my $nargs = shift || -1;
515-
my $nofinalizer = shift;
516523

517524
$GlobalRef->{"$sub"} = sub {
518525
my @args = @_;
@@ -540,16 +547,13 @@ sub push_c_function {
540547
}, $top, 1);
541548

542549
if ($died){
543-
$self->push_error_object(100, $died);
544550
croak $died;
545551
}
546552

547553
return $ret;
548554
};
549555

550556
$self->perl_push_function($GlobalRef->{"$sub"}, $nargs);
551-
552-
return if $nofinalizer;
553557
$self->eval_string("(function(){perlFinalizer('$sub')})");
554558
$self->set_finalizer(-2);
555559
}

t/nested.t

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use lib './lib';
2+
use strict;
3+
use warnings;
4+
use JavaScript::Duktape;
5+
use Test::More;
6+
use Test::Fatal;
7+
8+
use Try::Tiny;
9+
10+
subtest 'try-tiny interaction' => sub {
11+
sub eval_js {
12+
my $code = shift;
13+
14+
my $js = JavaScript::Duktape->new;
15+
16+
try {
17+
$js->eval( $code );
18+
} catch {
19+
my $err = $_;
20+
warn "CAUGHT ERR: $err";
21+
die "JS ERR: $err";
22+
};
23+
}
24+
25+
eval {
26+
eval_js(q{
27+
throw new Error('oh boy!');
28+
});
29+
};
30+
isnt $@, '', 'error message comes through';
31+
};
32+
33+
subtest 'simple nested thrown exception' => sub{
34+
my $js = JavaScript::Duktape->new();
35+
$js->set( each => sub{
36+
my $arr = shift;
37+
my $cb = shift;
38+
for(@$arr) {
39+
$cb->($_);
40+
}
41+
});
42+
like exception { $js->eval(q{
43+
var x=0; each([11,22], function(i){
44+
throw new Error('foo error!');
45+
});
46+
}) }, qr/foo error/;
47+
};
48+
49+
subtest 'try-catch caught nested thrown exception' => sub{
50+
my $js = JavaScript::Duktape->new();
51+
$js->set( each => sub{
52+
my $arr = shift;
53+
my $cb = shift;
54+
for(@$arr) {
55+
$cb->($_);
56+
}
57+
});
58+
my $ret;
59+
ok !exception { $ret =$js->eval(q{
60+
var x=0;
61+
try {
62+
each([11,22], function(i){
63+
x+=i;
64+
throw new Error('foo error!');
65+
});
66+
} catch(e) {
67+
}
68+
x;
69+
}) };
70+
is $ret, 11;
71+
};
72+
73+
subtest 'rewrap func with nested call' => sub{
74+
my $js = JavaScript::Duktape->new();
75+
$js->set( inc => sub{
76+
return 1 + shift();
77+
});
78+
$js->set( each => sub{
79+
my $arr = shift;
80+
my $cb = shift;
81+
for(@$arr) {
82+
$cb->($_);
83+
}
84+
});
85+
my $ret = $js->eval(q{
86+
var x=0; each([11,22], function(i){ x+=i; x=inc(x); }); x
87+
});
88+
is $ret, 35,
89+
};
90+
91+
subtest 'trap callback error' => sub{
92+
my $js = JavaScript::Duktape->new();
93+
$js->set( each => sub{
94+
die "foo here";
95+
});
96+
like exception { $js->eval(q{
97+
var x=0; each([11,22], function(i){ x+=i });
98+
}) }, qr/foo here/;
99+
};
100+
101+
subtest 'exception javascript' => sub{
102+
my $js = JavaScript::Duktape->new();
103+
$js->set( each => sub{
104+
my $arr = shift;
105+
my $cb = shift;
106+
for(@$arr) {
107+
$cb->($_);
108+
}
109+
});
110+
like exception { $js->eval(q{
111+
var x=0; var foo={};
112+
each([11,22], function(i){ foo.bar() });
113+
}) }, qr/not callable/;
114+
};
115+
116+
subtest 'exception in perl' => sub{
117+
my $js = JavaScript::Duktape->new();
118+
$js->set( each => sub{
119+
die('no good');
120+
});
121+
like exception { $js->eval(q{
122+
var x=0; var foo={};
123+
each([11,22], function(i){ return 33 });
124+
}) }, qr/no good/;
125+
};
126+
127+
subtest 'exception in callback function' => sub{
128+
my $js = JavaScript::Duktape->new();
129+
$js->set( each => sub{
130+
my $arr = shift;
131+
my $cb = shift;
132+
for(@$arr) {
133+
$cb->($_);
134+
}
135+
});
136+
like exception { $js->eval(q{
137+
var x=0; each([11,22], function(i){ not_a_function() });
138+
}) }, qr/not_a_function.*undefined/;
139+
};
140+
141+
subtest 'trap rewrap func error within nested call' => sub{
142+
my $js = JavaScript::Duktape->new();
143+
$js->set( inc => sub{
144+
die "error bar here";
145+
});
146+
$js->set( each => sub{
147+
my $arr = shift;
148+
my $cb = shift;
149+
for(@$arr) {
150+
$cb->($_);
151+
}
152+
});
153+
like exception { $js->eval(q{
154+
var x=0; each([11,22], function(i){ x+=i; x=inc(x); });
155+
}) }, qr/error bar here/;
156+
};
157+
158+
done_testing;

0 commit comments

Comments
 (0)