Re: Type conversion between Java & Javascript, e.g. Java List to Javascript array

2621 views
Skip to first unread message

Johan Compagner

unread,
Jul 7, 2010, 5:51:56 AM7/7/10
to dan...@winterwell.com, dev-tech-js-...@lists.mozilla.org
see WrapFactory
You can have your own instance that you set in the context there you
can wrap/convert everything from java to js.

Dont know if there is a way the other way around (a JS Array to a List)


On Wed, Jul 7, 2010 at 09:50, Daniel Winterstein
<daniel.wi...@gmail.com> wrote:
>
> Is it possible to configure Rhino to do automatic conversion or wrapping
> between Java types and Javascript equivalents?
>
> In particular, I'd like to use javascript to call a Java method which
> returns a List. And have that List be mapped to a Javascript array.
>
> I understand why this isn't the normal behaviour, but it can be very useful
> in places. Is there a way to get this behaviour?
>
> This is probably an old question, but I couldn't find the answer in
> documentation or archives.
>
> Background: Our software allows users to write scripts in Javascript,
> executed using Rhino. These scripts are accessing Java objects. Our users
> expect lists to behave like Javascript arrays. Which leads to bugs,
> confusion, distress, etc.
>
> Thank you in advance for any help.
>
> Best regards,
> Daniel
>
> --------------------------------------------
> Dr Daniel Winterstein
> Winterwell Associates Ltd
> tel: 0772 5172 612
> http://www.winterwell.com
> Registered in Scotland, company no. SC342991
>
>
> _______________________________________________
> dev-tech-js-engine-rhino mailing list
> dev-tech-js-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
>

Attila Szegedi

unread,
Jul 7, 2010, 5:56:08 AM7/7/10
to dan...@winterwell.com, dev-tech-js-...@lists.mozilla.org
I have a homegrown WrapFactory for that. You can find the code at <http://markmail.org/message/xkmxywud3dgzfqlw>

A word of caution though: it will expose indexed access through the [] operator and for...in, as well as the .length property, but will not make the java.util.Lists otherwise act as arrays. i.e. they'll have no .forEach() and other methods. You could *probably* create a solution where the lists also get the Array.prototype as their prototype; not 100% sure about it though...

Attila.

kevin

unread,
Jul 8, 2010, 11:18:05 AM7/8/10
to
On Jul 7, 2:56 am, Attila Szegedi <szege...@gmail.com> wrote:
> I have a homegrown WrapFactory for that. You can find the code at <http://markmail.org/message/xkmxywud3dgzfqlw>
>
> A word of caution though: it will expose indexed access through the [] operator and for...in, as well as the .length property, but will not make the java.util.Lists otherwise act as arrays. i.e. they'll have no .forEach() and other methods. You could *probably* create a solution where the lists also get the Array.prototype as their prototype; not 100% sure about it though...
>
> Attila.
>

RingoJS has a very handy ScriptableList and ScriptableMap. The
ScriptableList wraps a java List and gets the Array.prototype so you
can use push, slice, etc.

http://github.com/ringo/ringojs/blob/master/src/org/ringojs/wrappers/ScriptableList.java
http://github.com/ringo/ringojs/blob/master/src/org/ringojs/wrappers/ScriptableMap.java

Kevin

Reply all
Reply to author
Forward
Message has been deleted
0 new messages