2003/12/03 06:18:13
[org.ibex.core.git] / src / org / xwt / XWT.java
index 4e3b3e1..00d15d1 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);
         }
     }
@@ -70,15 +70,15 @@ public final class XWT extends JS {
         case "thread.yield": return METHOD;
         case "thread.sleep": return METHOD;
         case "res.watch": return METHOD;
+        case "res.unzip": return METHOD;
+        case "res.uncab": return METHOD;
+        case "res.cache": return METHOD;
+        case "res.url": return METHOD;
         case "soap": return METHOD;
         case "apply": return METHOD;
         case "graft": return METHOD;
         case "ui.browser": return METHOD;
         case "clone": return METHOD;
-        case "res.unzip": return METHOD;
-        case "res.uncab": return METHOD;
-        case "res.cache": return METHOD;
-        case "res.url": return METHOD;
         case "log.println": return METHOD;
         case "log.dump": return METHOD;
         case "regexp": return METHOD;
@@ -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) {
@@ -117,11 +117,7 @@ public final class XWT extends JS {
     }
 
     public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
-        if (name.equals("date")) {
-            JSArray args = new JSArray();
-            for(int i=0; i<nargs; i++) args.addElement(i==0?a:i==1?b:i==2?c:rest[i-3]);
-            return new org.xwt.js.JSDate(args);
-        }
+        if (name.equals("date")) return new JSDate(a, b, c, rest, nargs);
         else if (nargs == 0 && name.equals("thread.yield")) { sleep(0); return null; }
         else if (nargs == 2) {
             //#switch(name)
@@ -132,17 +128,9 @@ public final class XWT extends JS {
                 return a;
             //#end
         } else if (nargs == 3 && name.equals("soap")) {
-            if (name.equals("soap")) {
+            if (name.equals("soap"))
                 return new SOAP((String)a, "", (String)b, (String)c);
-                /* FIXME
-            } else if (name.equals("graft")) {
-                if (a instanceof Box) throw new JSExn("can't graft onto Boxes (yet)");
-                if (a instanceof Number) throw new JSExn("can't graft onto Numbers (yet)");
-                if (a instanceof String) throw new JSExn("can't graft onto Strings (yet)");
-                if (a instanceof Res) return new Res.Graft((Res)a, b, c);
-                return new JS.Graft((JS)a, b, c);
-                */
-            }
+
         } else if (nargs == 1) {
             //#switch(name)
             case "ui.browser": Platform.newBrowserWindow((String)a); return null;
@@ -176,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();
@@ -184,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) {
@@ -202,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");
@@ -216,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");
@@ -236,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) {}