partial update of core for new js stuff
[org.ibex.core.git] / src / org / ibex / core / Ibex.java
index 270e138..57fe96d 100644 (file)
@@ -13,10 +13,11 @@ import org.ibex.net.*;
 import org.ibex.crypto.*;
 
 /** Singleton class that provides all functionality in the ibex.* namespace */
-public final class Ibex extends JS implements JS.Cloneable {
+public final class Ibex extends JS.Obj implements JS.Cloneable {
 
     // FIXME remove this
     private final JS rr;
+    private static final JS.Method METHOD = new JS.Method();
 
     public Ibex(Stream rr) { try { this.rr = bless(rr);} catch(JSExn e) { throw new Error("should never happen: " + e); } }
 
@@ -32,23 +33,23 @@ public final class Ibex extends JS implements JS.Cloneable {
         while(str.indexOf('.') != -1) {
             String path = str.substring(0, str.indexOf('.'));
             str = str.substring(str.indexOf('.') + 1);
-            ret = ret.get(JS.S(path));
+            ret = ret.get(JSU.S(path));
         }
-        if (!"".equals(str)) ret = ret.get(JS.S(str));
+        if (!"".equals(str)) ret = ret.get(JSU.S(str));
         return ret;
     }
 
     /** lets us put multi-level get/put/call keys all in the same method */
-    private class Sub extends JS {
+    private class Sub extends JS.Obj {
         JS key;
         Sub(JS key) { this.key = key; }
-        public void put(JS key, JS val) throws JSExn { Ibex.this.put(JS.S(JS.toString(this.key) + "." + JS.toString(key)), val); }
-        public JS get(JS key) throws JSExn { return Ibex.this.get(JS.S(JS.toString(this.key) + "." + JS.toString(key))); }
+        public void put(JS key, JS val) throws JSExn { Ibex.this.put(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key)), val); }
+        public JS get(JS key) throws JSExn { return Ibex.this.get(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key))); }
         public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
             return Ibex.this.callMethod(this.key, a0, a1, a2, rest, nargs);
         }
         public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-            return Ibex.this.callMethod(JS.S(JS.toString(this.key) + "." + JS.toString(method)), a0, a1, a2, rest, nargs);
+            return Ibex.this.callMethod(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), a0, a1, a2, rest, nargs);
         }
     }
     private Cache subCache = new Cache(20);
@@ -60,9 +61,9 @@ public final class Ibex extends JS implements JS.Cloneable {
 
     public JS get(JS name) throws JSExn {
         // FIXME: SHouldn't need this (just trap [""])
-        if (JS.isString(name) && JS.toString(name).length() == 0) return rr;
+        if (JS.isString(name) && JSU.toString(name).length() == 0) return rr;
         // FEATURE: Preprocessor hack to generate specialized JS instances (avoid all this string concatenation)
-        //#switch(JS.toString(name))
+        //#switch(JSU.toString(name))
         case "math": return ibexMath;
         case "string": return ibexString;
         case "date": return METHOD;
@@ -75,9 +76,9 @@ public final class Ibex extends JS implements JS.Cloneable {
         case "ui.font.wait": return METHOD;
         case "ui.font.width": return METHOD;
         case "ui.font.height": return METHOD;
-        case "ui.font.sansserif": return Main.builtin.get(JS.S("fonts/vera/Vera.ttf"));
-        case "ui.font.monospace": return Main.builtin.get(JS.S("fonts/vera/VeraMono.ttf"));
-        case "ui.font.serif": return Main.builtin.get(JS.S("fonts/vera/VeraSe.ttf"));
+        case "ui.font.sansserif": return Main.builtin.get(JSU.S("fonts/vera/Vera.ttf"));
+        case "ui.font.monospace": return Main.builtin.get(JSU.S("fonts/vera/VeraMono.ttf"));
+        case "ui.font.serif": return Main.builtin.get(JSU.S("fonts/vera/VeraSe.ttf"));
         case "ui.browser": return METHOD;
         case "ui.mouse": return getSub(name);
         case "ui.mouse.button":
@@ -87,18 +88,18 @@ public final class Ibex extends JS implements JS.Cloneable {
             else return ZERO;
         case "ui.key": return getSub(name);
         case "ui.key.name": return getSub(name);
-        case "ui.key.name.alt": return JS.S(Platform.altKeyName());
-        case "ui.key.alt": return Surface.alt ? T : F;
-        case "ui.key.control": return Surface.control ? T : F;
-        case "ui.key.shift": return Surface.shift ? T : F;
-        case "ui.clipboard": return JS.S((String)Platform.getClipBoard());
+        case "ui.key.name.alt": return JSU.S(Platform.altKeyName());
+        case "ui.key.alt": return Surface.alt ? JSU.T : JSU.F;
+        case "ui.key.control": return Surface.control ? JSU.T : JSU.F;
+        case "ui.key.shift": return Surface.shift ? JSU.T : JSU.F;
+        case "ui.clipboard": return JSU.S((String)Platform.getClipBoard());
         case "ui.maxdim": return N(Short.MAX_VALUE);
         case "ui.screen": return getSub(name);
         case "ui.screen.width": return N(Platform.getScreenWidth());
         case "ui.screen.height": return N(Platform.getScreenHeight());
         case "undocumented": return getSub(name);
-        case "undocumented.initialOrigin": return JS.S(Main.origin);
-        case "undocumented.initialTemplate": return JS.S(Main.initialTemplate);
+        case "undocumented.initialOrigin": return JSU.S(Main.origin);
+        case "undocumented.initialTemplate": return JSU.S(Main.initialTemplate);
         case "thread": return getSub(name);
         case "thread.yield": return METHOD;
         case "thread.sleep": return METHOD;
@@ -132,9 +133,9 @@ public final class Ibex extends JS implements JS.Cloneable {
     }
 
     public void put(JS name, JS value) throws JSExn {
-        //#switch(JS.toString(name))
+        //#switch(JSU.toString(name))
         case "thread": Scheduler.add((Task)value); return;
-        case "ui.clipboard": Platform.setClipBoard(JS.toString(value)); return;
+        case "ui.clipboard": Platform.setClipBoard(JSU.toString(value)); return;
         case "ui.frame": Platform.createSurface((Box)value, true, true); return;
         case "ui.window": Platform.createSurface((Box)value, false, true); return;
         case "undocumented.proxyAuthorization":
@@ -147,9 +148,9 @@ public final class Ibex extends JS implements JS.Cloneable {
 
     public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
         try {
-            //#switch(JS.toString(name))
+            //#switch(JSU.toString(name))
             case "date": return new JSDate(a, b, c, rest, nargs);
-            case "net.rpc.soap": return new SOAP(JS.toString(a), "", JS.toString(b), JS.toString(c));
+            case "net.rpc.soap": return new SOAP(JSU.toString(a), "", JSU.toString(b), JSU.toString(c));
                 // FIXME support object dumping
             case "log.debug":    JS.debug(a== null ? "**null**" : JS.debugToString(a)); return null;
             case "log.info":     JS.info(a== null ? "**null**" : JS.debugToString(a)); return null;
@@ -159,24 +160,24 @@ public final class Ibex extends JS implements JS.Cloneable {
  
             switch (nargs) {
                 case 0:
-                    //#switch(JS.toString(name))
+                    //#switch(JSU.toString(name))
                     case "thread.yield": sleep(0); return null;
                     //#end
                     break;
                 case 1:
-                    //#switch(JS.toString(name))
+                    //#switch(JSU.toString(name))
                     case "clone":
                         if(a == null) throw new JSExn("can't clone the null value");
                         return ((JS)a).jsclone();
                     case "bless": return bless((JS)a);
-                    case "ui.browser": Platform.newBrowserWindow(JS.toString(a)); return null;
+                    case "ui.browser": Platform.newBrowserWindow(JSU.toString(a)); return null;
                     case "stream.unzip": return a == null ? null : new Stream.Zip((Stream)a);
                        //case "stream.uncab": return a == null ? null : new Stream.Cab(a);
                     case "stream.cache":
                         try { return a == null ? null : new Stream.CachedStream((Stream)a, "resources", true); }
                         catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
                     case "stream.url": {
-                        String url = JS.toString(a);
+                        String url = JSU.toString(a);
                         if (url.startsWith("http://")) return new Stream.HTTP(url);
                         else if (url.startsWith("https://")) return new Stream.HTTP(url);
                         else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
@@ -189,7 +190,7 @@ public final class Ibex extends JS implements JS.Cloneable {
                     }
                     case "thread.sleep": sleep(JS.toInt(a)); return null;
                     case "regexp": return new JSRegexp(a, null);
-                    case "net.rpc.xml": return new XMLRPC(JS.toString(a), "");
+                    case "net.rpc.xml": return new XMLRPC(JSU.toString(a), "");
                     case "crypto.rsa": /* FEATURE */ return null;
                     case "crypto.md5": /* FEATURE */ return null;
                     case "crypto.sha1": /* FEATURE */ return null;
@@ -198,20 +199,20 @@ public final class Ibex extends JS implements JS.Cloneable {
                     case "stream.parse.xml": if(a == null) return null; new XMLHelper(b).doParse(a); return null;
                         // FIXME backgrounding
                     case "stream.parse.utf8": if(a == null) return null;
-                                              try { return JS.S(new String(InputStreamToByteArray.convert(a.getInputStream()))); }
+                                              try { return JSU.S(new String(InputStreamToByteArray.convert(a.getInputStream()))); }
                                               catch (Exception e) { Log.warn(this, e); }
                     //#end
                     break;
                 case 2:
-                    //#switch(JS.toString(name))
+                    //#switch(JSU.toString(name))
                     case "stream.watch": return new Stream.ProgressWatcher((Stream)a, b);
                     case "regexp": return new JSRegexp(a, b);
                     //#end
                 case 3:
-                    //#switch(JS.toString(name))
-                    case "ui.font.height": return N(Font.getFont(a, JS.toInt(b)).textheight(JS.toString(c)));
+                    //#switch(JSU.toString(name))
+                    case "ui.font.height": return N(Font.getFont(a, JS.toInt(b)).textheight(JSU.toString(c)));
                     case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented");
-                    case "ui.font.width": return N(Font.getFont(a, JS.toInt(b)).textwidth(JS.toString(c)));
+                    case "ui.font.width": return N(Font.getFont(a, JS.toInt(b)).textwidth(JSU.toString(c)));
                     //#end
                     break;
             }
@@ -241,16 +242,16 @@ public final class Ibex extends JS implements JS.Cloneable {
                 try { Thread.sleep(i); } catch (InterruptedException e) { }
                 Scheduler.add(callback);
             } }.start();
-        } catch (JS.NotPauseableException npe) {
+        } catch (JSU.NotPauseableException npe) {
             throw new JSExn("you cannot sleep or yield in the foreground thread");
         }
     }
     
-    public static final JS ibexMath = new JS() {
+    public static final JS ibexMath = new JS.Obj() {
             // FEATURE: find a cleaner way to do this
             private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
             public JS get(JS key) throws JSExn {
-                //#switch(JS.toString(key))
+                //#switch(JSU.toString(key))
                 case "isNaN": return METHOD;
                 case "isFinite": return METHOD;
                 case "NaN": return METHOD;
@@ -259,7 +260,7 @@ public final class Ibex extends JS implements JS.Cloneable {
                 return MATH.get(key);
             }
             public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
-                //#switch(JS.toString(name))
+                //#switch(JSU.toString(name))
                 case "isNaN": return gs.callMethod(name,a,b,c,rest,nargs);
                 case "isFinite": return gs.callMethod(name,a,b,c,rest,nargs);
                 case "NaN": return gs.callMethod(name,a,b,c,rest,nargs);
@@ -269,10 +270,10 @@ public final class Ibex extends JS implements JS.Cloneable {
             }
     };
 
-    public static final JS ibexString = new JS() {
+    public static final JS ibexString = new JS.Obj() {
             private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
             public JS get(JS key) throws JSExn {
-                //#switch(JS.toString(key))
+                //#switch(JSU.toString(key))
                 case "parseInt": return METHOD;
                 case "parseFloat": return METHOD;
                 case "decodeURI": return METHOD;
@@ -286,7 +287,7 @@ public final class Ibex extends JS implements JS.Cloneable {
                 return super.get(key);
             }
             public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
-                //#switch(JS.toString(name))
+                //#switch(JSU.toString(name))
                 case "parseInt": return gs.callMethod(name,a,b,c,rest,nargs);
                 case "parseFloat": return gs.callMethod(name,a,b,c,rest,nargs);
                 case "decodeURI": return gs.callMethod(name,a,b,c,rest,nargs);
@@ -306,29 +307,29 @@ public final class Ibex extends JS implements JS.Cloneable {
         private JS characters, whitespace, endElement, startElement;
         public XMLHelper(JS b) throws JSExn {
             super(BUFFER_SIZE);
-            startElement = b.getAndTriggerTraps(JS.S("startElement"));
-            endElement   = b.getAndTriggerTraps(JS.S("endElement"));
-            characters   = b.getAndTriggerTraps(JS.S("characters"));
-            whitespace   = b.getAndTriggerTraps(JS.S("whitespace"));
+            startElement = b.getAndTriggerTraps(JSU.S("startElement"));
+            endElement   = b.getAndTriggerTraps(JSU.S("endElement"));
+            characters   = b.getAndTriggerTraps(JSU.S("characters"));
+            whitespace   = b.getAndTriggerTraps(JSU.S("whitespace"));
         }
 
         public void startElement(XML.Element c) throws XML.Exn { try {
                 JS attrs = new JS.O();
                 // FIXME attribute URIs? add an additional hash?
-                for(int i=0; i<c.getAttrLen(); i++) attrs.put(JS.S(c.getAttrKey(i)), JS.S(c.getAttrVal(i)));
-                startElement.call(JS.S(c.getLocalName()), attrs, JS.S(c.getUri()), null, 3);
+                for(int i=0; i<c.getAttrLen(); i++) attrs.put(JSU.S(c.getAttrKey(i)), JSU.S(c.getAttrVal(i)));
+                startElement.call(JSU.S(c.getLocalName()), attrs, JSU.S(c.getUri()), null, 3);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void endElement(XML.Element c) throws XML.Exn { try {
-                endElement.call(JS.S(c.getLocalName()), JS.S(c.getUri()), null, null, 2);
+                endElement.call(JSU.S(c.getLocalName()), JSU.S(c.getUri()), null, null, 2);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void characters(char[] ch, int start, int length) throws XML.Exn { try {
-                characters.call(JS.S(new String(ch, start, length)), null, null, null, 1);
+                characters.call(JSU.S(new String(ch, start, length)), null, null, null, 1);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void whitespace(char[] ch, int start, int length) throws XML.Exn { try {
-                whitespace.call(JS.S(new String(ch, start, length)), null, null, null, 1);
+                whitespace.call(JSU.S(new String(ch, start, length)), null, null, null, 1);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void doParse(JS s) throws JSExn {
@@ -349,7 +350,7 @@ public final class Ibex extends JS implements JS.Cloneable {
     // FEATURE: move this into builtin.xwar
     public Blessing bless(JS b) throws JSExn { return new Ibex.Blessing(b, this, null, null); }
     // JS:FIXME: This doesn't properly handle traps
-    public static class Blessing extends JS.O {
+    public static class Blessing extends JS.Obj {
         private Ibex ibex;
         private Template t = null;
         public JS parentkey = null;
@@ -359,7 +360,7 @@ public final class Ibex extends JS implements JS.Cloneable {
         public Blessing(JS clonee, Ibex ibex, Blessing parent, JS parentkey) throws JSExn {
             this.clonee = clonee; this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; }
         public JS get(JS key) throws JSExn {
-            if (JS.isString(key) && JS.toString(key).equals("")) return getStatic();
+            if (JS.isString(key) && JSU.toString(key).equals("")) return getStatic();
             if (cache.get(key) != null) return (JS)cache.get(key);
             JS ret = new Blessing(clonee.get(key), ibex, this, key);
             cache.put(key, ret);
@@ -379,7 +380,7 @@ public final class Ibex extends JS implements JS.Cloneable {
             String[] exts = new String[] { ".png", ".jpeg", ".gif" };
             for (int i=0; i < exts.length; i++)
                 try {
-                    InputStream in = parent.get(JS.S(JS.toString(parentkey) + exts[i])).getInputStream();
+                    InputStream in = parent.get(JSU.S(JSU.toString(parentkey) + exts[i])).getInputStream();
                     if (in != null) return in;
                 } catch (IOException f) { /* DELIBERATE */ }
             return null;
@@ -388,7 +389,7 @@ public final class Ibex extends JS implements JS.Cloneable {
             try {
                 if (t == null) {
                     // FEATURE: Might want to handle the ".t" part better
-                    JS res = parent.get(JS.S(JS.toString(parentkey) + ".t"));
+                    JS res = parent.get(JSU.S(JSU.toString(parentkey) + ".t"));
                     t = Template.buildTemplate(description(), res, ibex);
                 }
                 return t != null ? t.staticObject : null;