stream cleanup
[org.ibex.core.git] / src / org / ibex / js / JS.java
index 4928101..68fac37 100644 (file)
@@ -18,12 +18,16 @@ public abstract class JS {
     public 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() +")"); }
+    
+    public InputStream getInputStream() throws IOException {
+        throw new IOException("this object doesn't have a stream associated with it " + getClass().getName() + ")");
+    }
         
     public final boolean hasTrap(JS key) { return getTrap(key) != null; }
     
     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         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 {
@@ -87,7 +91,7 @@ public abstract class JS {
         JS _unclone() { return clonee.unclone(); }
         public JS getClonee() { return clonee; }
         public Clone(JS clonee) throws JSExn {
-            if(!(clonee instanceof Cloneable)) throw new JSExn("" + getClass().getName() + " isn't cloneable");
+            if(!(clonee instanceof Cloneable)) throw new JSExn("" + clonee.getClass().getName() + " isn't cloneable");
             this.clonee = clonee;
         }
         public boolean jsequals(JS o) { return unclone().jsequals(o.unclone()); }
@@ -100,6 +104,7 @@ public abstract class JS {
         public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
             return clonee.call(a0, a1, a2, rest, nargs);
         }
+        public InputStream getInputStream() throws IOException { return clonee.getInputStream(); }
     }
     
     // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
@@ -117,21 +122,30 @@ public abstract class JS {
     public static UnpauseCallback pause() throws NotPauseableException {
         Interpreter i = Interpreter.current();
         if (i.pausecount == -1) throw new NotPauseableException();
+        boolean get;
+        switch(i.f.op[i.pc]) {
+            case Tokens.RETURN: case ByteCodes.PUT: get = false; break;
+            case ByteCodes.GET: get = true; break;
+            default: throw new Error("should never happen");
+        }
         i.pausecount++;
-        return new JS.UnpauseCallback(i);
+        return new JS.UnpauseCallback(i,get);
     }
 
     public static class UnpauseCallback implements Task {
-        Interpreter i;
-        UnpauseCallback(Interpreter i) { this.i = i; }
-        public void perform() throws JSExn { unpause((JS)null); }
-        public void unpause(JS o) throws JSExn {
-            i.stack.push(o);
-            i.resume();
+        private Interpreter i;
+        private boolean get;
+        UnpauseCallback(Interpreter i, boolean get) { this.i = i; this.get = get; }
+        public void perform() throws JSExn { unpause(); }
+        public JS unpause() throws JSExn { return unpause((JS)null); }
+        public JS unpause(JS o) throws JSExn {
+            if (o == JS.METHOD) throw new JSExn("can't return a method to a paused context");
+            if(get) i.stack.push(o);
+            return i.resume();
         }
-        public void unpause(JSExn e) {
-            // FIXME: Throw the JSExn into the js world
-            throw new Error("Exception " + e + " thrown from background task");
+        public JS unpause(JSExn e) throws JSExn {
+            i.catchException(e);
+            return i.resume();
         }
     }
 
@@ -200,7 +214,15 @@ public abstract class JS {
     public static final JS B(boolean b) { return b ? T : F; }
     public static final JS B(int i) { return i==0 ? F : T; }
     
-    public static final JS S(String s) { return new JSString(s); }
+    private static final int CACHE_SIZE = 65536 / 4; // must be a power of two
+    private static final JSString[] stringCache = new JSString[CACHE_SIZE];
+    public static final JS S(String s)  {
+        if(s == null) return null;
+        int slot = s.hashCode()&(CACHE_SIZE-1);
+        JSString ret = stringCache[slot];
+        if(ret == null || !ret.s.equals(s)) stringCache[slot] = ret = new JSString(s);
+        return ret;
+    }
 
     public static final JS N(double d) { return new JSNumber.D(d); }
     public static final JS N(long l) { return new JSNumber.L(l); }
@@ -211,20 +233,20 @@ public abstract class JS {
         return N(n.doubleValue());
     }
 
-    private static final JSNumber.I[] smallIntCache = new JSNumber.I[65535 / 4];
-    private static final JSNumber.I[] largeIntCache = new JSNumber.I[65535 / 4];
+    private static final JSNumber.I[] smallIntCache = new JSNumber.I[CACHE_SIZE];
+    private static final JSNumber.I[] largeIntCache = new JSNumber.I[CACHE_SIZE];
     public static final JS N(int i) {
         JSNumber.I ret = null;
         int idx = i + smallIntCache.length / 2;
-        if (idx < smallIntCache.length && idx > 0) {
+        if (idx < CACHE_SIZE && idx > 0) {
             ret = smallIntCache[idx];
             if (ret != null) return ret;
         }
-        else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
+        else ret = largeIntCache[Math.abs(idx % CACHE_SIZE)];
         if (ret == null || ret.i != i) {
             ret = new JSNumber.I(i);
             if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
-            else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
+            else largeIntCache[Math.abs(idx % CACHE_SIZE)] = ret;
         }
         return ret;
     }