better enumeration, no need for SWAP(n>1)
[org.ibex.core.git] / src / org / ibex / js / JS.java
index 5d1b58d..96ab988 100644 (file)
@@ -14,8 +14,7 @@ public abstract class JS {
     public final JS unclone() { return _unclone(); }
     public final JS jsclone() throws JSExn { return new Clone(this); }
 
-    // FEATURE: JSEnumeration
-    public Enumeration keys() throws JSExn { throw new JSExn("you can't enumerate the keys of this object (class=" + getClass().getName() +")"); }
+    public JS.Enumeration keys() throws JSExn { throw new JSExn("you can't enumerate the keys of this object (class=" + getClass().getName() +")"); }
     public JS get(JS key) throws JSExn { return null; }
     public void put(JS key, JS val) throws JSExn { throw new JSExn("" + key + " is read only (class=" + getClass().getName() +")"); }
     
@@ -48,7 +47,7 @@ public abstract class JS {
     public static class O extends JS {
         private Hash entries;
         
-        public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
+        public Enumeration keys() throws JSExn { return entries == null ? (Enumeration)EMPTY_ENUMERATION : (Enumeration)new JavaEnumeration(null,entries.keys()); }
         public JS get(JS key) throws JSExn { return entries == null ? null : (JS)entries.get(key, null); }
         public void put(JS key, JS val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }        
 
@@ -107,6 +106,39 @@ public abstract class JS {
         public InputStream getInputStream() throws IOException { return clonee.getInputStream(); }
     }
     
+    public static abstract class Enumeration extends JS {
+        final Enumeration parent;
+        boolean done;
+        public Enumeration(Enumeration parent) { this.parent = parent; }
+        protected abstract boolean _hasMoreElements();
+        protected abstract JS _nextElement() throws JSExn;
+        
+        public final boolean hasMoreElements() {
+            if(!done && !_hasMoreElements()) done = true;
+            return !done ? true : parent != null ? parent.hasMoreElements() : false;
+        }
+        public final JS nextElement() throws JSExn { return !done ? _nextElement() : parent != null ? parent.nextElement() : null; }
+        
+        public JS get(JS key) throws JSExn {
+            //#switch(JS.toString(key))
+            case "hasMoreElements": return B(hasMoreElements());
+            case "nextElement": return nextElement();
+            //#end
+            return super.get(key);
+        }
+    }
+    public static class EmptyEnumeration extends Enumeration {
+        public EmptyEnumeration(Enumeration parent) { super(parent); }
+        protected boolean _hasMoreElements() { return false; }
+        protected JS _nextElement() { return null; }
+    }
+    public static class JavaEnumeration extends Enumeration {
+        private final java.util.Enumeration e;
+        public JavaEnumeration(Enumeration parent, java.util.Enumeration e) { super(parent); this.e = e; }
+        protected boolean _hasMoreElements() { return e.hasMoreElements(); }
+        protected JS _nextElement() { return (JS) e.nextElement(); }
+    }
+    
     // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
 
     /** log a message with the current JavaScript sourceName/line */
@@ -251,10 +283,7 @@ public abstract class JS {
         return ret;
     }
     
-    private static Enumeration emptyEnumeration = new Enumeration() {
-            public boolean hasMoreElements() { return false; }
-            public Object nextElement() { throw new NoSuchElementException(); }
-        };
+    private static Enumeration EMPTY_ENUMERATION = new EmptyEnumeration(null);
     
     public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
         return JSFunction._fromReader(sourceName, firstLine, sourceCode);