Menu

#1 JavaScript::SpiderMonkey object properties which are numbers

open
5
2006-02-04
2006-02-04
No

From: Andy <a@xygot.com>
To: m@perlmeister.com
Subject: JavaScript::SpiderMonkey object properties
which are numbers
Date: Sun, 20 Feb 2005 02:15:57 -0000

Hi,

I have just started using your module. It seems to be
working fine and doing
what I want except that I am having problems with
object properties which
are numbers rather than strings.

The small test script below should show my problem.

In Test 1, the property is treated as a string so the
output printed is
"Value is: 42.31"

In Test 2, the property still seems to be treated as a
string and gives the
same output.

In Test 3, the program (js32.dll) exits within the eval
code.

Not sure what I am doing wrong or whether this is a
fundemental error in my
understanding of what is going on here.

Thanks for the help.

Best regards,

andy

################################
use JavaScript::SpiderMonkey;

print "Test 1 - property is a string\n";

my $js = JavaScript::SpiderMonkey->new();
$js->init();

$js->property_by_path("doc.size", "42.3");

# Execute some code
my $rc = $js->eval(q�
doc.size = doc.size + 1;
�);

# Get the value of a property set/modified in JS
my $size = $js->property_get("doc.size");
print "Value is: $size\n";

$js->destroy();

sleep(2);

print "Test 2 - property is a number but treated as a
string\n";

my $js = JavaScript::SpiderMonkey->new();
$js->init(); # Initialize Runtime/Context

$js->property_by_path("doc.size", 42.3);

# Execute some code
my $rc = $js->eval(q�
doc.size = doc.size + 1;
�);

# Get the value of a property set/modified in JS
my $size = $js->property_get("doc.size");
print "Value is: $size\n";

$js->destroy();

sleep(2);

print "Test 3 - property is set to a number in JS\n";

my $js = JavaScript::SpiderMonkey->new();
$js->init();

my $number = 45.3;
$js->property_by_path("doc.size", $number);

# Execute some code
my $rc = $js->eval(q�
var j = 15.3;
j = j+1;
doc.size = j;
�);

# Get the value of a property set in JS
#my $size = $js->property_get("doc.size");
print "Value is: $size\n";

$js->destroy();

sleep(10);
##############################################

I am running ActiveState 5.8.4 on Windows XP SP2.
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 810 provided by ActiveState Corp.
http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun 1 2004 11:52:21

JavaScript::SpiderMonkey 0.11 was installed from
ActiveState repository
using PPM.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.