better JSArray.sort() exception handling
[org.ibex.core.git] / src / org / ibex / js / JSArray.java
index d9ffad1..8aa5325 100644 (file)
@@ -95,6 +95,7 @@ public class JSArray extends JS.BT {
                 if(i > oldSize) setSize(i);
                 insertElementAt(val,i);
             }
+            return;
         }
         if(isString(key)) {
             if (JS.toString(key).equals("length")) {
@@ -105,14 +106,12 @@ public class JSArray extends JS.BT {
         super.put(key,val);
     }
 
-    // FIXME: Needs to include super's keys
-    public Enumeration keys() {
-        return new Enumeration() {
-                private int n = size();
-                public boolean hasMoreElements() { return n > 0; }
-                public Object nextElement() {
-                    if(n == 0) throw new NoSuchElementException();
-                    return new Integer(--n);
+    public Enumeration keys() throws JSExn {
+        return new Enumeration(super.keys()) {
+                private int n = 0;
+                public boolean _hasMoreElements() { return n < size(); }
+                public JS _nextElement() {
+                    return n >= size() ? null : JS.N(n++);
                 }
             };
     }
@@ -127,8 +126,8 @@ public class JSArray extends JS.BT {
     public final int length() { return size(); }
     public final JS elementAt(int i) { 
         if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i);
-        JS o = (JS) getNode(i);
-        return o == NULL ? null : o;
+        Object o = getNode(i);
+        return o == NULL ? (JS)null : (JS)o;
     }
     public final void addElement(JS o) { 
         insertNode(size(),o==null ? NULL : o);
@@ -143,8 +142,8 @@ public class JSArray extends JS.BT {
     }
     public final JS removeElementAt(int i) {
         if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i);
-        JS o = (JS) deleteNode(i);
-        return o == NULL ? null : o;
+        Object o = deleteNode(i);
+        return o == NULL ? (JS)null : (JS)o;
     }
     
     public final int size() { return treeSize(); }
@@ -186,33 +185,31 @@ public class JSArray extends JS.BT {
             a.setElementAt(elementAt(start+i),i);
         return a;
     }
-    
+        
     private static final Vec.CompareFunc defaultSort = new Vec.CompareFunc() {
         public int compare(Object a, Object b) {
             try {
                 return JS.toString((JS)a).compareTo(JS.toString((JS)b));
-            } catch(JSExn e) {
-                // FIXME: See emca about this
-                throw new RuntimeException(e.toString());
-            }
+            } catch(JSExn e) { throw new JSExn.Wrapper(e); }
         }
     };
     private JS sort(JS tmp) throws JSExn {
         Vec vec = toVec();
-        if(tmp instanceof JS) {
-            final JS jsFunc = (JS) tmp;
-            vec.sort(new Vec.CompareFunc() {
-                public int compare(Object a, Object b) {
-                    try {
-                        return JS.toInt(jsFunc.call((JS)a, (JS)b, null, null, 2));
-                    } catch (JSExn e) {
-                        // FIXME: Check ecma to see what we should do here
-                        throw new RuntimeException(e.toString());
+        try {
+            if(tmp instanceof JS) {
+                final JS jsFunc = (JS) tmp;
+                vec.sort(new Vec.CompareFunc() {
+                    public int compare(Object a, Object b) {
+                        try {
+                            return JS.toInt(jsFunc.call((JS)a, (JS)b, null, null, 2));
+                        } catch(JSExn e) { throw new JSExn.Wrapper(e); }
                     }
-                }
-            });
-        } else {
-            vec.sort(defaultSort);
+                });
+            } else {
+                vec.sort(defaultSort);
+            }
+        } catch(JSExn.Wrapper e) {
+            throw e.refill();
         }
         setFromVec(vec);
         return this;