@@ -2,10 +2,49 @@ use lib './lib';
22use strict;
33use warnings;
44use JavaScript::Duktape;
5- use Data::Dumper;
65use Test::More;
76use Test::Fatal;
87
8+ subtest ' simple nested thrown exception' => sub{
9+ my $js = JavaScript::Duktape-> new();
10+ $js -> set( each => sub{
11+ my $arr = shift ;
12+ my $cb = shift ;
13+ for (@$arr ) {
14+ $cb -> ($_ );
15+ }
16+ });
17+ like exception { $js -> eval (q{
18+ var x=0; each([11,22], function(i){
19+ throw new Error('foo error!');
20+ });
21+ } ) }, qr / foo error/ ;
22+ };
23+
24+ subtest ' try-catch caught nested thrown exception' => sub{
25+ my $js = JavaScript::Duktape-> new();
26+ $js -> set( each => sub{
27+ my $arr = shift ;
28+ my $cb = shift ;
29+ for (@$arr ) {
30+ $cb -> ($_ );
31+ }
32+ });
33+ my $ret ;
34+ ok !exception { $ret =$js -> eval (q{
35+ var x=0;
36+ try {
37+ each([11,22], function(i){
38+ x+=i;
39+ throw new Error('foo error!');
40+ });
41+ } catch(e) {
42+ }
43+ x;
44+ } ) };
45+ is $ret , 11;
46+ };
47+
948subtest ' rewrap func with nested call' => sub{
1049 my $js = JavaScript::Duktape-> new();
1150 $js -> set( inc => sub{
@@ -34,10 +73,50 @@ subtest 'trap callback error' => sub{
3473 } ) }, qr / foo here/ ;
3574};
3675
76+ subtest ' exception javascript' => sub{
77+ my $js = JavaScript::Duktape-> new();
78+ $js -> set( each => sub{
79+ my $arr = shift ;
80+ my $cb = shift ;
81+ for (@$arr ) {
82+ $cb -> ($_ );
83+ }
84+ });
85+ like exception { $js -> eval (q{
86+ var x=0; var foo={};
87+ each([11,22], function(i){ foo.bar() });
88+ } ) }, qr / not callable/ ;
89+ };
90+
91+ subtest ' exception in perl' => sub{
92+ my $js = JavaScript::Duktape-> new();
93+ $js -> set( each => sub{
94+ die (' no good' );
95+ });
96+ like exception { $js -> eval (q{
97+ var x=0; var foo={};
98+ each([11,22], function(i){ return 33 });
99+ } ) }, qr / no good/ ;
100+ };
101+
102+ subtest ' exception in callback function' => sub{
103+ my $js = JavaScript::Duktape-> new();
104+ $js -> set( each => sub{
105+ my $arr = shift ;
106+ my $cb = shift ;
107+ for (@$arr ) {
108+ $cb -> ($_ );
109+ }
110+ });
111+ like exception { $js -> eval (q{
112+ var x=0; each([11,22], function(i){ not_a_function() });
113+ } ) }, qr / not_a_function.*undefined/ ;
114+ };
115+
37116subtest ' trap rewrap func error within nested call' => sub{
38117 my $js = JavaScript::Duktape-> new();
39118 $js -> set( inc => sub{
40- die " bar here" ;
119+ die " error bar here" ;
41120 });
42121 $js -> set( each => sub{
43122 my $arr = shift ;
@@ -48,7 +127,7 @@ subtest 'trap rewrap func error within nested call' => sub{
48127 });
49128 like exception { $js -> eval (q{
50129 var x=0; each([11,22], function(i){ x+=i; x=inc(x); });
51- } ) }, qr / bar here/ ;
130+ } ) }, qr /error bar here/ ;
52131};
53132
54133done_testing;
0 commit comments