|
| 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