jswitch
[org.ibex.core.git] / src / org / ibex / js / JS.java
index 68fac37..65ba3f6 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() +")"); }
     
@@ -29,7 +28,6 @@ public abstract class JS {
         throw new JSExn("method not found (" + JS.debugToString(method) + ")");
     }
     
-    // FIXME: JSArgs objects, pointers into stack frame
     public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
     }
@@ -43,12 +41,13 @@ public abstract class JS {
     Trap getTrap(JS key) { return null; }
     void putTrap(JS key, Trap value) throws JSExn { throw new JSExn("traps cannot be placed on this object (class=" + this.getClass().getName() +")"); }
     String coerceToString() throws JSExn { throw new JSExn("can't coerce to a string (class=" + getClass().getName() +")"); }
+    String debugToString() { return "[class=" + getClass().getName() + "]"; }
     boolean jsequals(JS o) { return this == o; }
     
-    public static class O extends JS {
+    public static class O extends JS implements Cloneable {
         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); }        
 
@@ -75,18 +74,12 @@ public abstract class JS {
         public final void replaceNode(int index, Object o) { bt().replaceNode(index,o); }
         public final int indexNode(Object o) { return bt().indexNode(o); }
     }
-    
-    // FEATURE: JS.StringKeys
-    /* public static StringKeys extends JS {
-        public JS get(JS key) { return JS.isString(key) ? get(JS.toString(key) : null; }
-        ...
-    */
-    
+        
     JS _unclone() { return this; }
     
     public interface Cloneable { }
     
-    public static class Clone extends JS.O implements Cloneable {
+    public static class Clone extends JS.O {
         protected final JS clonee;
         JS _unclone() { return clonee.unclone(); }
         public JS getClonee() { return clonee; }
@@ -96,8 +89,8 @@ public abstract class JS {
         }
         public boolean jsequals(JS o) { return unclone().jsequals(o.unclone()); }
         public Enumeration keys() throws JSExn { return clonee.keys(); }
-        public JS get(JS key) throws JSExn { return clonee.get(key); }
-        public void put(JS key, JS val) throws JSExn { clonee.put(key, val); }
+        public JS get(JS key) throws JSExn { return clonee.getAndTriggerTraps(key); }
+        public void put(JS key, JS val) throws JSExn { clonee.putAndTriggerTraps(key, val); }
         public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
             return clonee.callMethod(method, a0, a1, a2, rest, nargs);
         }
@@ -107,6 +100,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 {
+            //#jswitch(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 */
@@ -125,7 +151,7 @@ public abstract class JS {
         boolean get;
         switch(i.f.op[i.pc]) {
             case Tokens.RETURN: case ByteCodes.PUT: get = false; break;
-            case ByteCodes.GET: get = true; break;
+            case ByteCodes.GET: case ByteCodes.CALL: get = true; break;
             default: throw new Error("should never happen");
         }
         i.pausecount++;
@@ -177,7 +203,7 @@ public abstract class JS {
     
     public static String debugToString(JS o) {
         try { return toString(o); }
-        catch(JSExn e) { return "[class=" + o.getClass().getName() + "]"; }
+        catch(JSExn e) { return o.debugToString(); }
     }
     
     public static boolean isInt(JS o) {
@@ -251,10 +277,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);
@@ -271,15 +294,21 @@ public abstract class JS {
     /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
     public void putAndTriggerTraps(JS key, JS value) throws JSExn {
         Trap t = getTrap(key);
-        if (t != null) t.invoke(value);
-        else put(key, value);
+        if(t == null || (t = t.writeTrap()) == null) put(key,value);
+        else new Interpreter(t,value,false).resume();
     }
 
     /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
     public JS getAndTriggerTraps(JS key) throws JSExn {
         Trap t = getTrap(key);
-        if (t != null) return t.invoke();
-        else return get(key);
+        if (t == null || (t = t.readTrap()) == null) return get(key);
+        else return new Interpreter(t,null,false).resume();
+    }
+    
+    public JS justTriggerTraps(JS key, JS value) throws JSExn {
+        Trap t = getTrap(key);
+        if(t == null || (t = t.writeTrap()) == null) return value;
+        else return new Interpreter(t,value,true).resume();
     }
 
     /** adds a trap, avoiding duplicates */
@@ -294,7 +323,7 @@ public abstract class JS {
     final void delTrap(JS key, JSFunction f) throws JSExn {
         Trap t = (Trap)getTrap(key);
         if (t == null) return;
-        if (t.f == f) { putTrap(t.name, t.next); return; }
+        if (t.f == f) { putTrap(t.target, t.next); return; }
         for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
     }