It looks like when I bind a class and throw an error (such as a die()
statement or a failed DB connect) from within a static or object method, it
bubbles beautifully through JS as an exception.
However when I bind a class and register some perl sub as a *constructor*,
any dies or errors from there seem to make the constructor abort silently
instead.
Example: in the code attached below, the perl diagnostic displays a blessed
object. when you uncomment "test point 2" you get a well documented error
message. When you uncomment "Test point #1" however, the message goes
unreported and you get a blank (uncontructed) object instead of a blessed
hash.
Thank you for your consideration. :)
- - Jesse Thompson
Webformix, Bend OR
use JavaScript;
use Data::Dumper;
my $rt = JavaScript::Runtime->new();
my $cx = $rt->create_context();
$cx->bind_function(write => sub { print @_; });
$cx->bind_class(
name => 'JSDB',
package => 'JSDB',
constructor => "JSDB::new",
fs => { test => \&JSDB::test, },
);
$cx->set_error_handler( sub { die(Dumper(\@_)); } );
$ret = $cx->eval(q[
var dbh = new JSDB;
dbh.test();
]);
print "js_ret = ($ret), \$@ = ($@)\n";
die(Dumper($ret));
exit;
package JSDB;
sub new
{
my $class = shift;
my $self = {};
# die("Test point #1");
bless $self, $class;
return($self);
}
sub test
{
my $self = shift;
# die("Test point #2");
}
On 6 apr 2007, at 10.35, Jesse Thompson wrote:
> Am I approaching this the wrong way, or is this perhaps something
> to report
> in rt?
>
> It looks like when I bind a class and throw an error (such as a die()
> statement or a failed DB connect) from within a static or object
> method, it
> bubbles beautifully through JS as an exception.
>
> However when I bind a class and register some perl sub as a
> *constructor*,
> any dies or errors from there seem to make the constructor abort
> silently
> instead.
Yes, this seems to be a bug. I think the problem lies within this
piece of code from PJS_construct_perl_object (in JavaScript.xs)
if (SvROK(pcls->cons)) {
SV *rsv;
SV *pkg = newSVpv(pcls->pkg, 0);
perl_call_sv_with_jsvals_rsv(cx, obj,
pcls->cons, pkg,
argc, argv, &rsv);
SvREFCNT_inc(rsv);
JS_SetPrivate(cx, obj, (void *) rsv);
}
return JS_TRUE;
perl_call_sv_with_jsvals_rsv returns JS_FALSE if it throws an
exception so we should also return JS_FALSE.
Please report this as an bug in RT and I'll fix it in the next release.
Thanks,
Claes