gam3 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use JavaScript and would like to add a document class so that I can control document.write(). I am using Debian testing, and have try the Debian verison of JavaScript and built my own. In both cases the code below only gives me Segmentation fault as output.

Am I doing something silly. Do I need a different version of the javascript (Spider Monkey) library.

#!/usr/bin/perl use strict; use JavaScript qw(:all); my $runtime = JavaScript::Runtime->new(); my $context = $runtime->create_context(); $context->bind_class( name => 'Loo', constructor => sub { print STDERR "x = x\n"; my $x = Foo->new(@_); print STDERR "x = $x\n"; return $x; }, 'package' => 'Foo' ); print $context->eval("new Loo();"); { package Foo; sub new { my $ret = bless { @_ }, 'Foo'; print "-new- (@_) $ret\n"; return $ret; } }
Thanks

Update: removed Methods because they are not needed for example.

-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re: Segfault with JavaScript in Debian
by PodMaster (Abbot) on Apr 29, 2005 at 00:19 UTC
    Segfaults on windows also. At first I thought probably because your constructor doesn't create an object (returns undef), but it turns out it's due to the javascript. Try
    print $context->eval("var bong = new Loo();");

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Segfault with JavaScript in Debian
by injunjoel (Priest) on Apr 29, 2005 at 00:24 UTC
    Greetings all,
    Segfault usually indicated attempting to access space in memory that has not been allocated, like in C calling array elements beyond the bounds initially set...
    With that in mind I would focus attention on the
    methods=>{ bar=>\&Foo::bar, baz=>\&Foo::baz }

    portion of your code since from your posting I do not see those subs in your Foo package.
    Of course this could be a deeper issue that inheres in the module itself, can't really say for sure.
    Just a thought,

    -InjunJoel

    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Segfault with JavaScript in Debian
by Joost (Canon) on Apr 29, 2005 at 08:36 UTC
    Allright, this segfaults for me too, somewhere in the javascript -> perl data conversion code. I added some extra conversion stuff to the last release, and it's still not quite good enough, aparently.

    I'll try to get this fixed somewhere in the next couple of days - I need to get some other fixes done anyway.

    Joost.

Re: Segfault with JavaScript in Debian
by gam3 (Curate) on May 04, 2005 at 00:25 UTC
    I had built JavaScript without JS_THREADSAFE. Rebuilding JavaScript with it solved the problem.

    -- gam3
    A picture is worth a thousand words, but takes 200K.