Skip to content

Commit 84970e3

Browse files
committed
Merge pull request #8 from mamod/fix_nested
Fix nested perl subs when throwing an error
2 parents 972defd + b8e63d6 commit 84970e3

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/JavaScript/Duktape.pm

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ sub to_perl {
407407
for (my $i = 0; $i < $len; $i++){
408408
$self->push_perl($_[$i]);
409409
}
410-
$self->call_method($len);
410+
if ($self->pcall_method($len) == 1) {
411+
croak $self->safe_to_string(-1);
412+
}
411413
my $ret = $self->to_perl(-1);
412414
$self->pop();
413415
return $ret;
@@ -510,25 +512,36 @@ sub push_c_function {
510512
my $self = shift;
511513
my $sub = shift;
512514
my $nargs = shift || -1;
513-
514515
$GlobalRef->{"$sub"} = sub {
515516
my @args = @_;
516517
my $top = $self->get_top();
517518
my $ret = 1;
519+
my $died;
518520
$self->perl_duk_safe_call(sub {
519521
eval { $ret = $sub->(@args) };
520522
my $error = $@;
521523
if ($error){
522524
if ($error =~ /^Duk::Error/){
523525
croak $@;
524526
} else {
525-
$self->eval_string('(function (e){ throw new Error(e) })');
527+
## don't throw error inside perl sub safe call
528+
## just tell our sub that there is an error and
529+
## let it exit this calling stack then throw
530+
## Fixes issue #6
531+
$died = $error;
532+
$self->eval_string('(function (e){ return new Error(e) })');
526533
$self->push_string($error);
527534
$self->call(1);
528535
}
529536
}
530537
return $ret;
531538
}, $top, 1);
539+
540+
if ($died){
541+
$self->push_error_object(100, $died);
542+
croak $died;
543+
}
544+
532545
return $ret;
533546
};
534547

0 commit comments

Comments
 (0)