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
>
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