convert org.mozilla.javascript.NativeArray to java.lang.reflect.Array

Hi,
 I am getting an array as input to one of the functions in a subclass
of ScriptableObject.
 If I try to pass Object[] as an argument , I get exception.
If I give it as Object and then try to see if it is native array and
process it , somehow I cant get the actual values out pf it.
 example of javascript is --
  var arr = new Array();
    var f = this.getField("lsBox1");
    var l = this.getField("txt0");
    l.value = "Orig List:";

    for (var i = 0; i < f.numItems; i++){
        l.value = l.value +"\n"+f.getItemAt(i, false);
        arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
    arr.sort(compare);
    f.setItems(arr);

The function set Items is in the ScriptableObject subclass . Current
implementation is like this - which adds native array object to my
text.
public void jsFunction_setItems(Object result)
	{
		if (result instanceof NativeArray) {
			// Convert JavaScript array to Collection
			NativeArray arr = (NativeArray) result;
			int len = (int) arr.getLength();
			List list = new ArrayList(len);
			for (int i = 0; i < len; i++) {
				Object obj = arr.get(i, arr);
				Context.toObject(obj, arr);
				if (obj == Context.getUndefinedValue()) {
					obj = null;
				}
				list.add(obj);
			}
			Object[] arr1 = list.toArray();

			for (int i = 0; i < arr1.length; i++) {
				if (arr1[i] instanceof Object[]) {
					Object[] arr2 = (Object[]) arr1[i];
					jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
							.toString(), i);
				} else {
					jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
							.toString(), i);
				}
			}
		}
	}

0
Rhino
9/18/2007 4:45:18 AM
mozilla.dev.tech.js-engine 2010 articles. 0 followers. Post Follow

4 Replies
979 Views

Similar Articles

[PageSpeed] 59

On Sep 18, 12:45 am, Rhino user <anupama.jo...@gmail.com> wrote:
> Hi,
>  I am getting an array as input to one of the functions in a subclass
> of ScriptableObject.
>  If I try to pass Object[] as an argument , I get exception.
> If I give it as Object and then try to see if it is native array and
> process it , somehow I cant get the actual values out pf it.
>  example of javascript is --
>   var arr = new Array();
>     var f = this.getField("lsBox1");
>     var l = this.getField("txt0");
>     l.value = "Orig List:";
>
>     for (var i = 0; i < f.numItems; i++){
>         l.value = l.value +"\n"+f.getItemAt(i, false);
>         arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
>     arr.sort(compare);
>     f.setItems(arr);
>
> The function set Items is in the ScriptableObject subclass . Current
> implementation is like this - which adds native array object to my
> text.
> public void jsFunction_setItems(Object result)
>         {
>                 if (result instanceof NativeArray) {
>                         // Convert JavaScript array to Collection
>                         NativeArray arr = (NativeArray) result;
>                         int len = (int) arr.getLength();
>                         List list = new ArrayList(len);
>                         for (int i = 0; i < len; i++) {
>                                 Object obj = arr.get(i, arr);
>                                 Context.toObject(obj, arr);
>                                 if (obj == Context.getUndefinedValue()) {
>                                         obj = null;
>                                 }
>                                 list.add(obj);
>                         }
>                         Object[] arr1 = list.toArray();
>
>                         for (int i = 0; i < arr1.length; i++) {
>                                 if (arr1[i] instanceof Object[]) {
>                                         Object[] arr2 = (Object[]) arr1[i];
>                                         jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
>                                                         .toString(), i);
>                                 } else {
>                                         jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
>                                                         .toString(), i);
>                                 }
>                         }
>                 }
>         }

Have you tried using Context.getElements?

--N

0
Norris
9/18/2007 3:24:46 PM
On Sep 18, 8:24 am, Norris Boyd <norrisb...@gmail.com> wrote:
> On Sep 18, 12:45 am, Rhino user <anupama.jo...@gmail.com> wrote:
>
>
>
> > Hi,
> >  I am getting an array as input to one of the functions in a subclass
> > of ScriptableObject.
> >  If I try to pass Object[] as an argument , I get exception.
> > If I give it as Object and then try to see if it is native array and
> > process it , somehow I cant get the actual values out pf it.
> >  example of javascript is --
> >   var arr = new Array();
> >     var f = this.getField("lsBox1");
> >     var l = this.getField("txt0");
> >     l.value = "Orig List:";
>
> >     for (var i = 0; i < f.numItems; i++){
> >         l.value = l.value +"\n"+f.getItemAt(i, false);
> >         arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
> >     arr.sort(compare);
> >     f.setItems(arr);
>
> > The function set Items is in the ScriptableObject subclass . Current
> > implementation is like this - which adds native array object to my
> > text.
> > public void jsFunction_setItems(Object result)
> >         {
> >                 if (result instanceof NativeArray) {
> >                         // Convert JavaScript array to Collection
> >                         NativeArray arr = (NativeArray) result;
> >                         int len = (int) arr.getLength();
> >                         List list = new ArrayList(len);
> >                         for (int i = 0; i < len; i++) {
> >                                 Object obj = arr.get(i, arr);
> >                                 Context.toObject(obj, arr);
> >                                 if (obj == Context.getUndefinedValue()) {
> >                                         obj = null;
> >                                 }
> >                                 list.add(obj);
> >                         }
> >                         Object[] arr1 = list.toArray();
>
> >                         for (int i = 0; i < arr1.length; i++) {
> >                                 if (arr1[i] instanceof Object[]) {
> >                                         Object[] arr2 = (Object[]) arr1[i];
> >                                         jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
> >                                                         .toString(), i);
> >                                 } else {
> >                                         jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
> >                                                         .toString(), i);
> >                                 }
> >                         }
> >                 }
> >         }
>
> Have you tried using Context.getElements?
>
> --N

It still has array of nativeArray objects. I am not able to to the
actual data in it.
Thanks

0
Rhino
9/18/2007 6:15:10 PM
On Sep 18, 2:15 pm, Rhino user <anupama.jo...@gmail.com> wrote:
> On Sep 18, 8:24 am, Norris Boyd <norrisb...@gmail.com> wrote:
>
> > On Sep 18, 12:45 am, Rhino user <anupama.jo...@gmail.com> wrote:
>
> > > Hi,
> > >  I am getting an array as input to one of the functions in a subclass
> > > of ScriptableObject.
> > >  If I try to pass Object[] as an argument , I get exception.
> > > If I give it as Object and then try to see if it is native array and
> > > process it , somehow I cant get the actual values out pf it.
> > >  example of javascript is --
> > >   var arr = new Array();
> > >     var f = this.getField("lsBox1");
> > >     var l = this.getField("txt0");
> > >     l.value = "Orig List:";
>
> > >     for (var i = 0; i < f.numItems; i++){
> > >         l.value = l.value +"\n"+f.getItemAt(i, false);
> > >         arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
> > >     arr.sort(compare);
> > >     f.setItems(arr);
>
> > > The function set Items is in the ScriptableObject subclass . Current
> > > implementation is like this - which adds native array object to my
> > > text.
> > > public void jsFunction_setItems(Object result)
> > >         {
> > >                 if (result instanceof NativeArray) {
> > >                         // Convert JavaScript array to Collection
> > >                         NativeArray arr = (NativeArray) result;
> > >                         int len = (int) arr.getLength();
> > >                         List list = new ArrayList(len);
> > >                         for (int i = 0; i < len; i++) {
> > >                                 Object obj = arr.get(i, arr);
> > >                                 Context.toObject(obj, arr);
> > >                                 if (obj == Context.getUndefinedValue()) {
> > >                                         obj = null;
> > >                                 }
> > >                                 list.add(obj);
> > >                         }
> > >                         Object[] arr1 = list.toArray();
>
> > >                         for (int i = 0; i < arr1.length; i++) {
> > >                                 if (arr1[i] instanceof Object[]) {
> > >                                         Object[] arr2 = (Object[]) arr1[i];
> > >                                         jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
> > >                                                         .toString(), i);
> > >                                 } else {
> > >                                         jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
> > >                                                         .toString(), i);
> > >                                 }
> > >                         }
> > >                 }
> > >         }
>
> > Have you tried using Context.getElements?
>
> > --N
>
> It still has array of nativeArray objects. I am not able to to the
> actual data in it.
> Thanks

It doesn't do a deep convert; you'll have to pass each element to
Context.getElements as well. Could those elements also be arrays? If
so, you'll have to take a different approach.

What are you trying to do?

--N

0
Norris
9/18/2007 9:04:47 PM
On Sep 18, 2:04 pm, Norris Boyd <norrisb...@gmail.com> wrote:
> On Sep 18, 2:15 pm, Rhino user <anupama.jo...@gmail.com> wrote:
>
>
>
> > On Sep 18, 8:24 am, Norris Boyd <norrisb...@gmail.com> wrote:
>
> > > On Sep 18, 12:45 am, Rhino user <anupama.jo...@gmail.com> wrote:
>
> > > > Hi,
> > > >  I am getting an array as input to one of the functions in a subclass
> > > > of ScriptableObject.
> > > >  If I try to pass Object[] as an argument , I get exception.
> > > > If I give it as Object and then try to see if it is native array and
> > > > process it , somehow I cant get the actual values out pf it.
> > > >  example of javascript is --
> > > >   var arr = new Array();
> > > >     var f = this.getField("lsBox1");
> > > >     var l = this.getField("txt0");
> > > >     l.value = "Orig List:";
>
> > > >     for (var i = 0; i < f.numItems; i++){
> > > >         l.value = l.value +"\n"+f.getItemAt(i, false);
> > > >         arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
> > > >     arr.sort(compare);
> > > >     f.setItems(arr);
>
> > > > The function set Items is in the ScriptableObject subclass . Current
> > > > implementation is like this - which adds native array object to my
> > > > text.
> > > > public void jsFunction_setItems(Object result)
> > > >         {
> > > >                 if (result instanceof NativeArray) {
> > > >                         // Convert JavaScript array to Collection
> > > >                         NativeArray arr = (NativeArray) result;
> > > >                         int len = (int) arr.getLength();
> > > >                         List list = new ArrayList(len);
> > > >                         for (int i = 0; i < len; i++) {
> > > >                                 Object obj = arr.get(i, arr);
> > > >                                 Context.toObject(obj, arr);
> > > >                                 if (obj == Context.getUndefinedValue()) {
> > > >                                         obj = null;
> > > >                                 }
> > > >                                 list.add(obj);
> > > >                         }
> > > >                         Object[] arr1 = list.toArray();
>
> > > >                         for (int i = 0; i < arr1.length; i++) {
> > > >                                 if (arr1[i] instanceof Object[]) {
> > > >                                         Object[] arr2 = (Object[]) arr1[i];
> > > >                                         jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
> > > >                                                         .toString(), i);
> > > >                                 } else {
> > > >                                         jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
> > > >                                                         .toString(), i);
> > > >                                 }
> > > >                         }
> > > >                 }
> > > >         }
>
> > > Have you tried using Context.getElements?
>
> > > --N
>
> > It still has array of nativeArray objects. I am not able to to the
> > actual data in it.
> > Thanks
>
> It doesn't do a deep convert; you'll have to pass each element to
> Context.getElements as well. Could those elements also be arrays? If
> so, you'll have to take a different approach.
>
> What are you trying to do?
>
> --N

I can get an array of array from javascript. I have to get each
element at feed it to a listbox or combobox. That is why I need each
entry in the array. This array comes from an API called setItems on a
combobox and then I iterate thr' all items and insert them one by one.
Thanks

0
Rhino
9/19/2007 3:47:04 PM
Reply:

Similar Artilces:

convert javascript array to C# array
 Hiis there any way to convert array which is filled with data in javascript to C# array. plz help. Sure there is. But first it is a matter of how you communicate the javascript array to the code behind. Typically, you convert the JS array into a string with a defined delimiter, and on the server-side, you split this string (using the same delimiter). That's the theory. If you have the intention to store it in a hidden field that you post with the page, then you can use the join function to turn the array into a string: <script type="text/javascript">var ...

Converting VB.NEt array to two d Javascript array
How can this be accomplished.I have a dataset that is an array in vb.net I need to access it in Javascript client side code function.How can one be passed to the other? Hey, Look at the Page.RegisterArrayBlock (or something like that) function, which is a member of the page class, but can be called in the page or in server controls, etc. BrianBrian"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4)....

superreview granted: [Bug 310351] Cannot convert NodeList to JS Array with Array.prototype.slice any more : [Attachment 198392] Because of this
Brendan Eich <brendan@mozilla.org> has granted Blake Kaplan <mrbkap@gmail.com>'s request for superreview: Bug 310351: Cannot convert NodeList to JS Array with Array.prototype.slice any more https://bugzilla.mozilla.org/show_bug.cgi?id=310351 Attachment 198392: Because of this https://bugzilla.mozilla.org/attachment.cgi?id=198392&action=edit ------- Additional Comments from Brendan Eich <brendan@mozilla.org> Great, thanks! /be ...

convert RSA Array of Byte Arrays( modulus & Exponent) Key Material into java.security.Public
Does any one know how to convert RSA modulus & exponent ( return from a token as a pubspec[][] into a java.security.Public object. this is my best guess but it has a hole in the middle. pubKeySpec[][] = ....from JNI PKCS11 API call RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(pubSpec[0]), new BigInteger(pubSpec[1])); WHat goes here? X509EncodedKeySpec pukeySPK = new X509EncodedKeySpec( ? ); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); java.security.PublicKey publicKey = keyFactory.generatePublic(pukeySPK...

superreview requested: [Bug 310351] Cannot convert NodeList to JS Array with Array.prototype.slice any more : [Attachment 198392] Because of this
Blake Kaplan <mrbkap@gmail.com> has asked Brendan Eich <brendan@mozilla.org> for superreview: Bug 310351: Cannot convert NodeList to JS Array with Array.prototype.slice any more https://bugzilla.mozilla.org/show_bug.cgi?id=310351 Attachment 198392: Because of this https://bugzilla.mozilla.org/attachment.cgi?id=198392&action=edit ...

superreview granted: [Bug 410946] Crash in JS engine aborting applet making Java/JS calls : [Attachment 305554] Fix.
Boris Zbarsky (reviews very slow until May) <bzbarsky@mit.edu> has granted Johnny Stenback (:jst) <jst@mozilla.org>'s request for superreview: Bug 410946: Crash in JS engine aborting applet making Java/JS calls https://bugzilla.mozilla.org/show_bug.cgi?id=3D410946 Attachment 305554: Fix. https://bugzilla.mozilla.org/attachment.cgi?id=3D305554&action=3Dedit ------- Additional Comments from Boris Zbarsky (reviews very slow until May) <bzbarsky@mit.edu> Could you add documentation to nsIObjectFrame which says which of the metho= ds might destroy the frame? ...

superreview requested: [Bug 410946] Crash in JS engine aborting applet making Java/JS calls : [Attachment 305554] Fix.
Johnny Stenback (:jst) <jst@mozilla.org> has asked Boris Zbarsky (reviews very slow until May) <bzbarsky@mit.edu> for superreview: Bug 410946: Crash in JS engine aborting applet making Java/JS calls https://bugzilla.mozilla.org/show_bug.cgi?id=410946 Attachment 305554: Fix. https://bugzilla.mozilla.org/attachment.cgi?id=305554&action=edit ------- Additional Comments from Johnny Stenback (:jst) <jst@mozilla.org> See previous comment for description of what this does. ...

superreview requested: [Bug 323156] XPCDispConvert::JSArrayToCOMArray does not convert empty arrays correctly : [Attachment 208273] Patch accounting for empty JS array
Garrett Davis <gdavis@nsisoftware.com> has asked Johnny Stenback <jst@mozilla.org> for superreview: Bug 323156: XPCDispConvert::JSArrayToCOMArray does not convert empty arrays correctly https://bugzilla.mozilla.org/show_bug.cgi?id=323156 Attachment 208273: Patch accounting for empty JS array https://bugzilla.mozilla.org/attachment.cgi?id=208273&action=edit ...

superreview granted: [Bug 323156] XPCDispConvert::JSArrayToCOMArray does not convert empty arrays correctly : [Attachment 208273] Patch accounting for empty JS array
Johnny Stenback <jst@mozilla.org> has granted Garrett Davis <gdavis@nsisoftware.com>'s request for superreview: Bug 323156: XPCDispConvert::JSArrayToCOMArray does not convert empty arrays correctly https://bugzilla.mozilla.org/show_bug.cgi?id=323156 Attachment 208273: Patch accounting for empty JS array https://bugzilla.mozilla.org/attachment.cgi?id=208273&action=edit ------- Additional Comments from Johnny Stenback <jst@mozilla.org> sr=jst ...

superreview granted: [Bug 410946] Crash in JS engine aborting applet making Java/JS calls : [Attachment 306220] Updated diff, ready to land.
Johnny Stenback (:jst) <jst@mozilla.org> has granted superreview: Bug 410946: Crash in JS engine aborting applet making Java/JS calls https://bugzilla.mozilla.org/show_bug.cgi?id=410946 Attachment 306220: Updated diff, ready to land. https://bugzilla.mozilla.org/attachment.cgi?id=306220&action=edit ------- Additional Comments from Johnny Stenback (:jst) <jst@mozilla.org> Updated fix, fixes all of the above. Boris, the only real difference here is how nsPluginDestroyRunnable works, it's now a PRCList with a static list head so that when we run one of these we ca...

Convert java object to js
Hello everyone, I'm new to rhino (and to js, I fear) and have a(n) (understanding) problem converting java objects to js. Here is my code: import org.mozilla.javascript.*; import java.io.*; public class FirstTry extends ScriptableObject{ private SecondTry str; public String getClassName() { return "FirstTry"; } public void jsConstructor(int test) { this.str = new SecondTry(); } public SecondTry jsGet_SecondTry() {return str;} public FirstTry() {} } import org.mozilla.javascript.*; public class SecondTry extends ScriptableObject...

convert java object to js
Hello everyone, I'm new to rhino (and js ;-)) and have a(n) (understanding) problem converting java objects to js. Here is my code: import org.mozilla.javascript.*; import java.io.*; public class FirstTry extends ScriptableObject{ private SecondTry str; public String getClassName() { return "FirstTry"; } public void jsConstructor(int test) { this.str = new SecondTry(); } public SecondTry jsGet_SecondTry() {return str;} public FirstTry() {} } import org.mozilla.javascript.*; public class SecondTry extends ScriptableObject { p...

How to implement JavaScript-functions to the JS-engine?
Hi, Today DNS contains a lot more information than just A-Records. Since the introduction of NAPTR-Records (IETF RFC 2915) and Electronic Number Number Mapping (IETF RFC 3761) the DNS-System has even become a global addressbook and can map phone numbers to contact records an much more. But unfortunately ECMAScript does not define any functions to do DNS-Queries. So I want to add a function to the JS-engine of Firefox for doing DNS- ANY-Queries. I'm completely new to Mozilla-/Firefox-Development. If I'm wrong here, please point me to the correct forum. Otherwise please p...

Converting a Java string array to NodeSet
Hi, I am trying to convert an array (returned by an external java program call) to a node set. Here is situation.. I am calling an external java program using XPATH which returns an array object. Code: -------------------- <do-set-local-variable name="varAssignedResourcesObj" scope="policy"> <arg-string> <token-xpath expression="wsclient:getResourceAssignmentsForUser($varTargetUserDN, $varAdminUser, $varAdminPassword, $varUserappURL)"/> </arg-string> </do-set-local-variable> --------------...

Problem getting connection from DataSource: java.lang.ClassCastException: java.lang.String
When trying to get a connection from a DataSource I get the following exception: java.lang.ClassCastException: java.lang.String I just upgraded to Jaguar/PowerJ 3.61 in the hopes to resolve this problem that I was having on 3.6, but it did not. I am running Jaguar on Sun Solaris. I created an EJB SessionBean and set up the DataSource in the "J2EE Resource References" in the Deploy tab of the EJB component. This exception goes away if I go into Jaguar Manager and define/re-setup the same DataSource with the same info there, then re-start Jaguar server. However,...

Web resources about - convert org.mozilla.javascript.NativeArray to java.lang.reflect.Array - mozilla.dev.tech.js-engine

JavaFX 1.2 Top 10 and Migration Guide
... lead, since he discovered JavaFX 1.2 is downloadable from javafx.com. Enjoy the new release! Note 2: I have updated the section below on nativearray. ...

Resources last updated: 12/11/2015 3:02:08 AM