Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing Numbers to JavaScript

247 views
Skip to first unread message

Alan Williamson

unread,
Aug 16, 2009, 3:41:14 PM8/16/09
to
I have a java function:

--------------------
public Object func() throws Exception {
return Long.valueOf("123");
}
--------------------

i have reduced the complexity of it right down, so you can see.

When i access this in my Javascript:

-----------
x = $cf.func();
y = x + 1;
-----------

x = 123 and y = 1231. If i multiple x by *1 then its fine. but thats
a kludge.


How can i get Rhino to respect the fact i am returning a
java.lang.Number for it to keep it as that, and not convert it to a
string object?

thanks

Attila Szegedi

unread,
Aug 16, 2009, 4:35:09 PM8/16/09
to Alan Williamson, dev-tech-js-...@lists.mozilla.org
For your Context object, do
ctx.getWrapFactory().setJavaPrimitiveWrap(false);

Short explanation is that if you don't, your java.lang.Long will be
seen as a JS Object, not as a JS number primitive, and Objects will be
concatenated with other values as strings...

Attila.

Alan Williamson

unread,
Aug 17, 2009, 5:43:38 AM8/17/09
to
Fantastic. That worked beautifully.

So to play the other team -- under what circumstances would i want to
leave it as is?

0 new messages