2003/11/22 06:44:38
[org.ibex.core.git] / src / org / xwt / XWT.java
index fec4565..d3c53fd 100644 (file)
@@ -11,13 +11,13 @@ import org.xwt.translators.*;
 import org.bouncycastle.util.encoders.Base64;
 
 /** Singleton class that provides all functionality in the xwt.* namespace */
-public final class XWT extends JSCallable {
+public final class XWT extends JS {
 
     public final Res rr;
     public XWT(Res rr) { this.rr = rr; }
 
     /** lets us put multi-level get/put/call keys all in the same method */
-    private class Sub extends JSCallable {
+    private class Sub extends JS {
         String key;
         Sub(String key) { this.key = key; }
         public String toString() { return "XWTSUB " + key; }
@@ -64,7 +64,7 @@ public final class XWT extends JSCallable {
             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(2);
             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(3);
-            else return new Integer(0);
+            else return ZERO;
         case "undocumented": return new Sub("undocumented");
         case "undocumented.internal": return new Sub("undocumented.internal");
         case "thread.yield": return METHOD;
@@ -98,7 +98,15 @@ public final class XWT extends JSCallable {
     public void put(Object name, final Object value) {
         //#switch(name)
         case "thread":
-            Scheduler.add(new Scheduler.Task() { public void perform() { JSContext.invokePauseable((JSFunction)value); } });
+            Scheduler.add(new Scheduler.Task() {
+                    public void perform() {
+                        try {
+                            JS.invokePauseable((JSFunction)value);
+                        } catch (JS.PausedException pe) {
+                            // okay; wait for ourselves to be re-enqueued
+                        }
+                    }
+                });
         case "ui.clipboard": Platform.setClipBoard((String)value);
         case "ui.frame": Platform.createSurface((Box)value, true, true);
         case "ui.window": Platform.createSurface((Box)value, false, true);
@@ -108,12 +116,8 @@ public final class XWT extends JSCallable {
         //#end
     }
 
-    public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JS.Exn {
-        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);
-        }
+    public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+        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)
@@ -124,15 +128,9 @@ public final class XWT extends JSCallable {
                 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);
-            } else if (name.equals("graft")) {
-                if (a instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
-                if (a instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
-                if (a instanceof String) throw new JS.Exn("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;
@@ -140,14 +138,14 @@ public final class XWT extends JSCallable {
             case "res.unzip": return new Res.Zip((Res)a);
             case "res.uncab": return new Res.Cab((Res)a);
             case "res.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
-                              catch (Res.NotCacheableException e) { throw new JS.Exn("this resource cannot be cached"); }
+                              catch (Res.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
             case "res.url":
                 String url = (String)a;
                 if (url.startsWith("http://")) return new Res.HTTP(url);
                 else if (url.startsWith("https://")) return new Res.HTTP(url);
                 else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
                 else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
-                throw new JS.Exn("invalid resource specifier " + url);
+                throw new JSExn("invalid resource specifier " + url);
             case "thread.sleep": sleep(JS.toInt(a)); return null;
             case "log.println": Log.logJS(this, a== null ? "**null**" : a.toString()); return null;
             case "log.dump": Log.recursiveLog("","",a); return null;
@@ -167,18 +165,30 @@ public final class XWT extends JSCallable {
     }
 
     public static void sleep(final int i) {
-        final Callback callback = JSContext.pause();
-        final long currentTime = System.currentTimeMillis();
-        new Thread() {
-            public void run() {
-                try { Thread.sleep(i); } catch (InterruptedException e) { }
-                Scheduler.add(new Scheduler.Task() { public void perform() { callback.call(null); } });
-            }
-        }.start();
+        try {
+            final JS.UnpauseCallback callback = JS.pause();
+            final long currentTime = System.currentTimeMillis();
+            new Thread() {
+                public void run() {
+                    try { Thread.sleep(i); } catch (InterruptedException e) { }
+                    Scheduler.add(new Scheduler.Task() {
+                            public void perform() {
+                                try {
+                                    callback.unpause(null);
+                                } catch (JS.PausedException pe) {
+                                    // okay
+                                }
+                            }
+                        });
+                }
+            }.start();
+        } catch (JS.NotPauseableException npe) {
+            throw new JSExn("you cannot sleep or yield in the foreground thread");
+        }
     }
     
     public static final JSMath xwtMath = new JSMath() {
-            private JS gs = new JSScope.Global(null);
+            private JS gs = new JSScope.Global();
             public String toString() { return "XWTMATH"; }
             public Object get(Object key) {
                 //#switch(key)
@@ -192,7 +202,7 @@ public final class XWT extends JSCallable {
         };
 
     public static final JS xwtString = new JS() {
-            private JS gs = new JSScope.Global(null);
+            private JS gs = new JSScope.Global();
             public void put(Object key, Object val) { }
             public Object get(Object key) {
                 //#switch(key)
@@ -242,16 +252,16 @@ public final class XWT extends JSCallable {
             }
         }
         public void whitespace(char[] ch, int start, int length) {}
-        public JS doParse(InputStream is) throws JS.Exn {
+        public JS doParse(InputStream is) throws JSExn {
             try { 
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 parse(r);
             } catch (XML.XMLException e) {
-                throw new JS.Exn("error parsing XML: " + e.toString());
+                throw new JSExn("error parsing XML: " + e.toString());
             } catch (IOException e) {
                 if (Log.on) Log.log(this, "IO Exception while reading from file");
                 if (Log.on) Log.log(this, e);
-                throw new JS.Exn("error reading from Resource");
+                throw new JSExn("error reading from Resource");
             }
             return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
         }