Subject: | JavaScript module breaks JS inheritance mechanism |
distribution name: JavaScript-0.55
perl version: v5.8.7 built for i386-linux-thread-multi
operation system: Linux hst2 2.6.8-1-386 #1 Mon Sep 13 23:29:55 EDT 2004 i686 GNU/Linux
It seems, that JS classes, exported from perl by JavaScript::bind_class(), cannot be used as base classes in JS.
The following code exposes the bug:
package PerlClass;
sub new {
my $class = shift;
$class = ref $class || $class;
my $self = { };
bless $self, $class;
$self->{prop_a} = '123';
return $self;
}
package main;
use strict;
use JavaScript;
my $runtime = new JavaScript::Runtime;
my $context = $runtime->create_context();
$context->bind_function( name => 'alert', func => sub { print((defined $_[0] ? $_[0] : 'undefined') . "\n"); });
$context->bind_class(
name => 'PerlClass',
constructor => sub { return new PerlClass(@_); },
properties => {
prop_a => {
flags => JS_PROP_READONLY,
},
},
package => 'PerlClass'
);
$context->eval(<<END
function test(Parent) {
alert('Parent: ' + (new Parent).prop_a);
var Child = function() { };
Child.prototype = new Parent;
alert('Child: ' + (new Child).prop_a);
}
/* prints
* Parent: 123
* Child: undefined
*
* Expected: both Parent and Child equal to 123
*/
test(PerlClass);
JSClass = function() { this.prop_a = 'aaa'; }
/* prints as expected:
* Parent: aaa
* Child: aaa
*/
test(JSClass);
END
);