Subject: | corrupted argument values when passing a JavaScript function to a Perl sub |
In JavaScript, if a function is passed as the second or later argument to a function that is implemented
in Perl, the arguments before the function (in the argument list) will be corrupted.
Sample code:
use JavaScript;
use Data::Dumper;
sub foo { print Data::Dumper->Dump([\@_], ['args']) }
my $rt = JavaScript::Runtime->new;
my $ctx = $rt->create_context;
$ctx->bind_function(foo => sub { foo(@_) });
$ctx->eval(q[foo(function () {}, 'x','y','z')]);
$ctx->eval(q[foo('x', function () {}, 'y','z')]);
$ctx->eval(q[foo('x', 'y', function () {}, 'z')]);
__END__
The above generates the following output:
$args = [
bless( do{\(my $o = [
\136793560,
bless( {
'runtime' => bless( {
'_impl' => \135896808,
'_does' => []
}, 'JavaScript::Runtime' ),
'_impl' => \135774832
}, 'JavaScript::Context' ),
136822936
])}, 'JavaScript::Function' ),
'x',
'y',
'z'
];
$args = [
'z',
bless( do{\(my $o = [
\136727528,
bless( {
'runtime' => bless( {
'_impl' => \135896808,
'_does' => []
}, 'JavaScript::Runtime' ),
'_impl' => \135774832
}, 'JavaScript::Context' ),
136828840
])}, 'JavaScript::Function' ),
'y',
${\$args->[0]}
];
$args = [
$args,
undef,
bless( do{\(my $o = [
\136727528,
bless( {
'runtime' => bless( {
'_impl' => \135896808,
'_does' => []
}, 'JavaScript::Runtime' ),
'_impl' => \135774832
}, 'JavaScript::Context' ),
136822904
])}, 'JavaScript::Function' ),
'z'
];
Attempt to free unreferenced scalar: SV 0x8255ff0, Perl interpreter: 0x8153008 at
/usr/lib/perl5/JavaScript/Context.pm line 38.
In the above output, the first dump is correct. But the second should have read ['x', bless(...), 'y', 'z'].
And the third should have read ['x', 'y', bless(...), 'z'].
I've tried this on two different machines, both running Linux, one with v1.04 of the JS dist, and the
other with v1.11.
-TimK