2003/11/29 03:06:09
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:19 +0000 (07:42 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:19 +0000 (07:42 +0000)
darcs-hash:20040130074219-2ba56-598c680a93b8e81b993f99d6d69db5488fac6f90.gz

src/org/xwt/Res.java
src/org/xwt/SOAP.java
src/org/xwt/Scheduler.java
src/org/xwt/Surface.java
src/org/xwt/Template.java
src/org/xwt/XMLRPC.java
src/org/xwt/XWT.java
src/org/xwt/js/Interpreter.java
src/org/xwt/js/JS.java
src/org/xwt/js/JSArray.java

index 7da19ce..f37d758 100644 (file)
@@ -101,7 +101,7 @@ public abstract class Res extends JS {
         public String toString() { return "byte[]"; }
         public String getCacheKey() throws NotCacheableException { return cacheKey; }
         public InputStream getInputStream(String path) throws IOException {
-            if (!"".equals(path)) throw new JSExn("can't get subresources of a byte[] resource");
+            if (!"".equals(path)) throw new IOException("can't get subresources of a byte[] resource");
             return new ByteArrayInputStream(bytes);
         }
     }
@@ -131,7 +131,7 @@ public abstract class Res extends JS {
             ZipInputStream zis = new ZipInputStream(pis);
             ZipEntry ze = zis.getNextEntry();
             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
-            if (ze == null) throw new JSExn("requested file (" + path + ") not found in archive");
+            if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
             return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
         }
     }
@@ -190,7 +190,7 @@ public abstract class Res extends JS {
                     public int read(byte[] b, int off, int len) throws IOException {
                         int ret = super.read(b, off, len);
                         if (ret != 1) bytesDownloaded += ret;
-                        Scheduler.add(new Scheduler.Task() { public void perform() {
+                        Scheduler.add(new Scheduler.Task() { public void perform() throws Exception {
                             callback.call(N(bytesDownloaded),
                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
                         } });
index 7ad91ef..4d5a642 100644 (file)
@@ -153,7 +153,11 @@ class SOAP extends XMLRPC {
 
         } else if (parent != null && parent instanceof JS) {
             objects.removeElementAt(objects.size() - 1);
-            ((JS)parent).put(name, me);
+            try {
+                ((JS)parent).put(name, me);
+            } catch (JSExn e) {
+                throw new Error("this should never happen");
+            }
 
         }
 
index cecdee8..1f57fe7 100644 (file)
@@ -11,7 +11,7 @@ public class Scheduler {
     // Public API Exposed to org.xwt /////////////////////////////////////////////////
 
     private static Scheduler singleton;
-    public static abstract class Task { public abstract void perform(); }
+    public static abstract class Task { public abstract void perform() throws Exception; }
     public static void add(Task t) { singleton.runnable.append(t); }
     public static void init() {
         if (singleton != null) return;
index ac2a837..97041f1 100644 (file)
@@ -2,6 +2,7 @@
 package org.xwt;
 
 import org.bouncycastle.util.encoders.Base64;
+import org.xwt.js.*;
 import org.xwt.util.*;
 import java.io.*;
 import java.util.*;
@@ -298,7 +299,11 @@ public abstract class Surface extends PixelBuffer {
         this.root = root;
         Surface old = fromBox(root);
         if (old != null) old.dispose(false);
-        else root.put("thisbox", null);
+        else try {
+            root.put("thisbox", null);
+        } catch (JSExn e) {
+            throw new Error("this should never happen");
+        }
 
         // make sure the root is properly sized
         do { abort = false; root.reflow(root.width, root.height); } while(abort);
index 79de777..4cbf5e4 100644 (file)
@@ -70,7 +70,11 @@ public class Template {
     public static Res resolveStringToResource(String str, XWT xwt, boolean permitAbsolute) {
         // URL
         if (str.indexOf("://") != -1) {
-            if (permitAbsolute) return (Res)xwt.callMethod("res.url", str, null, null, null, 1);
+            try {
+                if (permitAbsolute) return (Res)xwt.callMethod("res.url", str, null, null, null, 1);
+            } catch (JSExn jse) {
+                throw new Error("this should never happen");
+            }
             Log.log(Template.class, "absolute URL " + str + " not permitted here");
             return null;
         }
@@ -102,7 +106,11 @@ public class Template {
         if (staticscript == null) return staticJSScope;
         JSFunction temp = staticscript;
         staticscript = null;
-        temp.cloneWithNewParentScope(staticJSScope).call(null, null, null, null, 0);
+        try {
+            temp.cloneWithNewParentScope(staticJSScope).call(null, null, null, null, 0);
+        } catch (JSExn jse) {
+            throw new Error("this should never happen");
+        }
         return staticJSScope;
     }
     
@@ -110,8 +118,8 @@ public class Template {
      *  @param pboxes a vector of all box parents on which to put $-references
      *  @param ptemplates a vector of the fileNames to recieve private references on the pboxes
      */
-    void apply(Box b, XWT xwt) { apply(b, xwt, null); }
-    void apply(Box b, XWT xwt, PerInstantiationJSScope parentPis) {
+    void apply(Box b, XWT xwt) throws JSExn { apply(b, xwt, null); }
+    void apply(Box b, XWT xwt, PerInstantiationJSScope parentPis) throws JSExn {
 
         getStatic();
 
@@ -357,7 +365,7 @@ public class Template {
         XWT xwt = null;
         PerInstantiationJSScope parentBoxPis = null;
         JSScope myStatic = null;
-        void putDollar(String key, Box target) {
+        void putDollar(String key, Box target) throws JSExn {
             if (parentBoxPis != null) parentBoxPis.putDollar(key, target);
             declare("$" + key);
             put("$" + key, target);
@@ -368,13 +376,13 @@ public class Template {
             this.xwt = xwt;
             this.myStatic = myStatic;
         }
-        public Object get(Object key) {
+        public Object get(Object key) throws JSExn {
             if (super.has(key)) return super.get(key);
             if (key.equals("xwt")) return xwt;
             if (key.equals("static")) return myStatic;
             return super.get(key);
         }
-        public void put(Object key, Object val) {
+        public void put(Object key, Object val) throws JSExn {
             if (super.has(key)) super.put(key, val);
             else super.put(key, val);
         }
index 39fc30d..4d541fd 100644 (file)
@@ -130,13 +130,21 @@ class XMLRPC extends JS {
                 Object memberValue = objects.elementAt(objects.size() - 1);
                 String memberName = (String)objects.elementAt(objects.size() - 2);
                 JS struct = (JS)objects.elementAt(objects.size() - 3);
-                struct.put(memberName, memberValue);
+                try {
+                    struct.put(memberName, memberValue);
+                } catch (JSExn e) {
+                    throw new Error("this should never happen");
+                }
                 objects.setSize(objects.size() - 2);
             case "data":
                 int i;
                 for(i=objects.size() - 1; objects.elementAt(i) != null; i--);
                 JSArray arr = new JSArray();
-                for(int j = i + 1; j<objects.size(); j++) arr.put(new Integer(j - i - 1), objects.elementAt(j));
+                try {
+                    for(int j = i + 1; j<objects.size(); j++) arr.put(new Integer(j - i - 1), objects.elementAt(j));
+                } catch (JSExn e) {
+                    throw new Error("this should never happen");
+                }
                 objects.setElementAt(arr, i);
                 objects.setSize(i + 1);
             //#end            
@@ -354,19 +362,27 @@ class XMLRPC extends JS {
             new java.lang.Thread() {
                 public void run() {
                     try {
-                        final Object ret = call_(args);
-                        Scheduler.add(new Scheduler.Task() {
-                                public void perform() {
-                                    try {
-                                        callback.unpause(null);
-                                    } catch (JS.PausedException pe) {
-                                        // okay
+                        try {
+                            final Object ret = call_(args);
+                            Scheduler.add(new Scheduler.Task() {
+                                    public void perform() {
+                                        try {
+                                            callback.unpause(null);
+                                        } catch (JS.PausedException pe) {
+                                            // okay
+                                        } catch (JSExn e) {
+                                            // FIXME
+                                            throw new Error("FIXME");
+                                        }
                                     }
-                                }
-                            });
-                    } catch (IOException se) {
-                        if (Log.on) Log.log(this, se);
-                        throw new JSExn("socket exception: " + se);
+                                });
+                        } catch (IOException se) {
+                            if (Log.on) Log.log(this, se);
+                            throw new JSExn("socket exception: " + se);
+                        }
+                    } catch (JSExn e) {
+                        // FIXME
+                        throw new Error("FIXME");
                     }
                 } }.start();
             return null;
index d3c53fd..a987d9e 100644 (file)
@@ -23,10 +23,10 @@ public final class XWT extends JS {
         public String toString() { return "XWTSUB " + key; }
         public void put(Object key, Object val) { XWT.this.put(this.key + "." + key, val); }
         public Object get(Object key) { return XWT.this.get(this.key + "." + key); }
-        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
         }
-        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return XWT.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
         }
     }
@@ -99,7 +99,7 @@ public final class XWT extends JS {
         //#switch(name)
         case "thread":
             Scheduler.add(new Scheduler.Task() {
-                    public void perform() {
+                    public void perform() throws JSExn {
                         try {
                             JS.invokePauseable((JSFunction)value);
                         } catch (JS.PausedException pe) {
@@ -164,7 +164,7 @@ public final class XWT extends JS {
         return null;
     }
 
-    public static void sleep(final int i) {
+    public static void sleep(final int i) throws JSExn {
         try {
             final JS.UnpauseCallback callback = JS.pause();
             final long currentTime = System.currentTimeMillis();
@@ -172,7 +172,7 @@ public final class XWT extends JS {
                 public void run() {
                     try { Thread.sleep(i); } catch (InterruptedException e) { }
                     Scheduler.add(new Scheduler.Task() {
-                            public void perform() {
+                            public void perform() throws JSExn {
                                 try {
                                     callback.unpause(null);
                                 } catch (JS.PausedException pe) {
@@ -190,7 +190,7 @@ public final class XWT extends JS {
     public static final JSMath xwtMath = new JSMath() {
             private JS gs = new JSScope.Global();
             public String toString() { return "XWTMATH"; }
-            public Object get(Object key) {
+            public Object get(Object key) throws JSExn {
                 //#switch(key)
                 case "isNaN": return gs.get("isNaN");
                 case "isFinite": return gs.get("isFinite");
@@ -204,7 +204,7 @@ public final class XWT extends JS {
     public static final JS xwtString = new JS() {
             private JS gs = new JSScope.Global();
             public void put(Object key, Object val) { }
-            public Object get(Object key) {
+            public Object get(Object key) throws JSExn {
                 //#switch(key)
                 case "parseInt": return gs.get("parseInt");
                 case "parseFloat": return gs.get("parseFloat");
@@ -224,31 +224,43 @@ public final class XWT extends JS {
         Vector obStack = new Vector();
         public XMLHelper() { super(BUFFER_SIZE); }
         public void startElement(XML.Element c) throws XML.SchemaException {
-            JS o = new JS();
-            o.put("$name", c.localName);
-            for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
-            o.put("$numchildren", new Integer(0));
-            obStack.addElement(o);
+            try {
+                JS o = new JS();
+                o.put("$name", c.localName);
+                for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
+                o.put("$numchildren", new Integer(0));
+                obStack.addElement(o);
+            } catch (JSExn jse) {
+                throw new Error("this should never happen");
+            }
         }
         public void endElement(XML.Element c) throws XML.SchemaException {
-            if (obStack.size() == 1) return;
-            JS me = (JS)obStack.lastElement();
-            obStack.setSize(obStack.size() - 1);
-            JS parent = (JS)obStack.lastElement();
-            int numchildren = ((Integer)parent.get("$numchildren")).intValue();
-            parent.put("$numchildren", new Integer(numchildren + 1));
-            parent.put(new Integer(numchildren), me);
+            try {
+                if (obStack.size() == 1) return;
+                JS me = (JS)obStack.lastElement();
+                obStack.setSize(obStack.size() - 1);
+                JS parent = (JS)obStack.lastElement();
+                int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+                parent.put("$numchildren", new Integer(numchildren + 1));
+                parent.put(new Integer(numchildren), me);
+            } catch (JSExn jse) {
+                throw new Error("this should never happen");
+            }
         }
         public void characters(char[] ch, int start, int length) throws XML.SchemaException {
-            String s = new String(ch, start, length);
-            JS parent = (JS)obStack.lastElement();
-            int numchildren = ((Integer)parent.get("$numchildren")).intValue();
-            Object lastChild = parent.get(new Integer(numchildren - 1));
-            if (lastChild instanceof String) {
-                parent.put(new Integer(numchildren - 1), lastChild + s);
-            } else {
-                parent.put("$numchildren", new Integer(numchildren + 1));
-                parent.put(new Integer(numchildren), s);
+            try {
+                String s = new String(ch, start, length);
+                JS parent = (JS)obStack.lastElement();
+                int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+                Object lastChild = parent.get(new Integer(numchildren - 1));
+                if (lastChild instanceof String) {
+                    parent.put(new Integer(numchildren - 1), lastChild + s);
+                } else {
+                    parent.put("$numchildren", new Integer(numchildren + 1));
+                    parent.put(new Integer(numchildren), s);
+                }
+            } catch (JSExn jse) {
+                throw new Error("this should never happen");
             }
         }
         public void whitespace(char[] ch, int start, int length) {}
index 0a0b27b..66931fd 100644 (file)
@@ -32,7 +32,7 @@ class Interpreter implements ByteCodes, Tokens {
     }
     
     /** this is the only synchronization point we need in order to be threadsafe */
-    synchronized Object resume() {
+    synchronized Object resume() throws JSExn {
         Thread t = Thread.currentThread();
         Interpreter old = (Interpreter)threadToInterpreter.get(t);
         threadToInterpreter.put(t, this);
@@ -523,7 +523,7 @@ class Interpreter implements ByteCodes, Tokens {
 
     // Operations on Primitives //////////////////////////////////////////////////////////////////////
 
-    static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) {
+    static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) throws JSExn {
         if (o instanceof Number) {
             //#switch(method)
             case "toFixed": throw new JSExn("toFixed() not implemented");
@@ -612,7 +612,7 @@ class Interpreter implements ByteCodes, Tokens {
         throw new JSExn("Attempted to call non-existent method: " + method);
     }
     
-    static Object getFromPrimitive(Object o, Object key) {
+    static Object getFromPrimitive(Object o, Object key) throws JSExn {
         boolean returnJS = false;
         if (o instanceof Boolean) {
             throw new JSExn("cannot call methods on Booleans");
@@ -643,14 +643,14 @@ class Interpreter implements ByteCodes, Tokens {
             case "toLowerCase": returnJS = true; break; 
             case "toUpperCase": returnJS = true; break; 
             case "toString": returnJS = true; break; 
-            case "substr": returnJS = true; break; 
-            //#end
+            case "substr": returnJS = true; break;  
+           //#end
         }
         if (returnJS) {
             final Object target = o;
             final String method = key.toString();
             return new JS() {
-                    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+                    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
                         if (nargs > 2) throw new JSExn("cannot call that method with that many arguments");
                         return callMethodOnPrimitive(target, method, a0, a1, a2, rest, nargs);
                     }
@@ -663,7 +663,7 @@ class Interpreter implements ByteCodes, Tokens {
         private Object method;
         JS obj;
         public Stub(JS obj, Object method) { this.obj = obj; this.method = method; }
-        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return ((JS)obj).callMethod(method, a0, a1, a2, rest, nargs);
         }
     }
index 28622a3..5da9e76 100644 (file)
@@ -23,7 +23,7 @@ public class JS extends org.xwt.util.BalancedTree {
 
     public static class PausedException extends Exception { PausedException() { } }
 
-    public static void invokePauseable(JSFunction function) throws JS.PausedException {
+    public static void invokePauseable(JSFunction function) throws JS.PausedException, JSExn {
         Interpreter i = new Interpreter(function, true, new JSArray());
         int oldpausecount = i.pausecount;
         i.resume();
@@ -43,7 +43,7 @@ public class JS extends org.xwt.util.BalancedTree {
     public static class UnpauseCallback {
         Interpreter i;
         UnpauseCallback(Interpreter i) { this.i = i; }
-        public void unpause(Object o) throws PausedException {
+        public void unpause(Object o) throws PausedException, JSExn {
             i.stack.push(o);
             i.resume();
         }
@@ -140,9 +140,14 @@ public class JS extends org.xwt.util.BalancedTree {
         };
     
     private Hash entries = null;
-    public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
-    public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
-    public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
+    public Enumeration keys() throws JSExn {
+        return entries == null ? emptyEnumeration : entries.keys();
+    }
+    public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
+    public void put(Object key, Object val) throws JSExn {
+        if (entries == null) entries = new Hash();
+        entries.put(key, null, val);
+    }
 
 
     // Trap support //////////////////////////////////////////////////////////////////////////////
@@ -151,14 +156,14 @@ public class JS extends org.xwt.util.BalancedTree {
     protected boolean isTrappable() { return false; }
 
     /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
-    public final void putAndTriggerTraps(Object key, Object value) {
+    public void putAndTriggerTraps(Object key, Object value) throws JSExn {
         Trap t = getTrap(key);
         if (t != null) t.invoke(key, value);
         else put(key, value);
     }
 
     /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
-    public final Object getAndTriggerTraps(Object key) {
+    public Object getAndTriggerTraps(Object key) throws JSExn {
         Trap t = getTrap(key);
         if (t != null) return t.invoke(key);
         else return get(key);
@@ -195,19 +200,19 @@ public class JS extends org.xwt.util.BalancedTree {
 
     // return this from get() if the key was actually a method.
     public static final Object METHOD = new Object();
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         throw new JSExn("attempted to call the null value (method "+method+")");
     }    
-    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
     }
 
 
     // Typing Support //////////////////////////////////////////////////////////////////////////////
 
-    public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); }
-    public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); }
-    public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); }
+    public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); }
+    public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); }
+    public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); }
 
     public String typeName() { return "object"; }
 
index 56d37e2..2f5f04d 100644 (file)
@@ -22,7 +22,7 @@ public class JSArray extends JS {
         return Integer.parseInt(s);
     }
     
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         //#switch(method)
         case "pop": return vec.pop();
         case "reverse": return reverse();
@@ -80,7 +80,7 @@ public class JSArray extends JS {
         return super.get(key);
     }
 
-    public void put(Object key, Object val) {
+    public void put(Object key, Object val) throws JSExn {
         if (key.equals("length")) vec.setSize(toNumber(val).intValue());
         int i = intVal(key);
         if (i == Integer.MIN_VALUE) super.put(key, val);
@@ -151,15 +151,20 @@ public class JSArray extends JS {
             return JS.toString(a).compareTo(JS.toString(b));
         }
     };
-    private Object sort(Object tmp) {
+    private Object sort(Object tmp) throws JSExn {
         if(tmp instanceof JS) {
             final JSArray funcArgs = new JSArray(2);
             final JS jsFunc = (JS) tmp;
             vec.sort(new Vec.CompareFunc() {
                 public int compare(Object a, Object b) {
-                    funcArgs.setElementAt(a,0);
-                    funcArgs.setElementAt(b,1);
-                    return JS.toInt(jsFunc.call(a, b, null, null, 2));
+                    try {
+                        funcArgs.setElementAt(a,0);
+                        funcArgs.setElementAt(b,1);
+                        return JS.toInt(jsFunc.call(a, b, null, null, 2));
+                    } catch (Exception e) {
+                        // FIXME
+                        throw new JSRuntimeExn(e.toString());
+                    }
                 }
             });
         } else {