preliminary core conversion
[org.ibex.core.git] / src / org / ibex / core / Ibex.java
index b44ba5a..6492ed2 100644 (file)
@@ -29,9 +29,9 @@ 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 = (JS)ret.get(path);
+            ret = ret.get(JS.S(path));
         }
-        if (!"".equals(str)) ret = (JS)ret.get(str);
+        if (!"".equals(str)) ret = ret.get(JS.S(str));
         return ret;
     }
 
@@ -39,13 +39,13 @@ public final class Ibex extends JS implements JS.Cloneable {
     private class Sub extends JS {
         String key;
         Sub(String key) { this.key = key; }
-        public void put(Object key, Object val) throws JSExn { Ibex.this.put(this.key + "." + key, val); }
-        public Object get(Object key) throws JSExn { return Ibex.this.get(this.key + "." + key); }
-        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+        public void put(JS key, JS val) throws JSExn { Ibex.this.put(this.key + "." + JS.toString(key), val); }
+        public JS get(JS key) throws JSExn { return Ibex.this.get(this.key + "." + JS.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 Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-            return Ibex.this.callMethod(this.key + "." + method, 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(this.key + "." + JS.toString(method), a0, a1, a2, rest, nargs);
         }
     }
     private Cache subCache = new Cache(20);
@@ -55,7 +55,8 @@ public final class Ibex extends JS implements JS.Cloneable {
         return ret;
     }
 
-    public Object get(Object name) throws JSExn {
+    public JS get(JS key) throws JSExn { return JS.isString(key) ? get(JS.toString(key)) : null; }
+    JS get(String name) throws JSExn {
         if (name instanceof String && ((String)name).length() == 0) return rr;
         //#switch(name)
         case "math": return ibexMath;
@@ -70,9 +71,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("fonts/vera/Vera.ttf");
-        case "ui.font.monospace": return Main.builtin.get("fonts/vera/VeraMono.ttf");
-        case "ui.font.serif": return Main.builtin.get("fonts/vera/VeraSe.ttf");
+        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.browser": return METHOD;
         case "ui.mouse": return getSub("ui.mouse");
         case "ui.mouse.button":
@@ -82,18 +83,18 @@ public final class Ibex extends JS implements JS.Cloneable {
             else return ZERO;
         case "ui.key": return getSub("ui.key");
         case "ui.key.name": return getSub("ui.key.name");
-        case "ui.key.name.alt": return Platform.altKeyName();
+        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 Platform.getClipBoard();
+        case "ui.clipboard": return JS.S((String)Platform.getClipBoard());
         case "ui.maxdim": return N(Short.MAX_VALUE);
         case "ui.screen": return getSub("ui.screen");
         case "ui.screen.width": return N(Platform.getScreenWidth());
         case "ui.screen.height": return N(Platform.getScreenHeight());
         case "undocumented": return getSub("undocumented");
-        case "undocumented.initialOrigin": return Main.origin;
-        case "undocumented.initialTemplate": return Main.initialTemplate;
+        case "undocumented.initialOrigin": return JS.S(Main.origin);
+        case "undocumented.initialTemplate": return JS.S(Main.initialTemplate);
         case "thread": return getSub("thread");
         case "thread.yield": return METHOD;
         case "thread.sleep": return METHOD;
@@ -123,13 +124,14 @@ public final class Ibex extends JS implements JS.Cloneable {
         case "crypto.sha1": return METHOD;
         case "crypto.rc4": return METHOD;
         //#end
-        return super.get(name);
+        return null;
     }
 
-    public void put(Object name, final Object value) throws JSExn {
+    public void put(JS key, JS value) throws JSExn { if(JS.isString(key)) put(JS.toString(key),value); else super.put(key,value); }
+    void put(Object name, final JS value) throws JSExn {
         //#switch(name)
         case "thread": Scheduler.add((Task)value); return;
-        case "ui.clipboard": Platform.setClipBoard((String)value); return;
+        case "ui.clipboard": Platform.setClipBoard(JS.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":
@@ -140,11 +142,15 @@ public final class Ibex extends JS implements JS.Cloneable {
         throw new JSExn("attempted to put unknown property: ibex."+name);
     }
 
-    public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+    public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
+        if(JS.isString(name)) return callMethod(JS.toString(name),a,b,c,rest,nargs);
+        return super.callMethod(name,a,b,c,rest,nargs);
+    }
+    JS callMethod(String name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
         try {
             //#switch(name)
             case "date": return new JSDate(a, b, c, rest, nargs);
-            case "net.rpc.soap": return new SOAP((String)a, "", (String)b, (String)c);
+            case "net.rpc.soap": return new SOAP(JS.toString(a), "", JS.toString(b), JS.toString(c));
                 // FIXME support object dumping
             case "log.debug":    JS.debug(a== null ? "**null**" : a.toString()); return null;
             case "log.info":     JS.info(a== null ? "**null**" : a.toString()); return null;
@@ -164,14 +170,14 @@ public final class Ibex extends JS implements JS.Cloneable {
                         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((String)a); return null;
+                    case "ui.browser": Platform.newBrowserWindow(JS.toString(a)); return null;
                     case "stream.unzip": return new Stream.Zip((Stream)a);
                     case "stream.uncab": return new Stream.Cab((Stream)a);
                     case "stream.cache":
                         try { return new Stream.CachedStream((Stream)a, "resources", true); }
                         catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
                     case "stream.url": {
-                        String url = (String)a;
+                        String url = JS.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);
@@ -184,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((String)a, "");
+                    case "net.rpc.xml": return new XMLRPC(JS.toString(a), "");
                     case "crypto.rsa": /* FEATURE */ return null;
                     case "crypto.md5": /* FEATURE */ return null;
                     case "crypto.sha1": /* FEATURE */ return null;
@@ -192,7 +198,7 @@ public final class Ibex extends JS implements JS.Cloneable {
                     case "stream.parse.html": throw new JSExn("not implemented yet"); //return null;
                     case "stream.parse.xml": new XMLHelper((JS)b).doParse((JS)a); return null;
                         // FIXME backgrounding
-                    case "stream.parse.utf8": try { return new String(InputStreamToByteArray.convert(Stream.getInputStream(a))); }
+                    case "stream.parse.utf8": try { return JS.S(new String(InputStreamToByteArray.convert(Stream.getInputStream(a)))); }
                                               catch (Exception e) { Log.warn(this, e); }
                     //#end
                     break;
@@ -203,9 +209,9 @@ public final class Ibex extends JS implements JS.Cloneable {
                     //#end
                 case 3:
                     //#switch(name)
-                    case "ui.font.height": return N(Font.getFont((Stream)a, JS.toInt(b)).textheight((String)c));
+                    case "ui.font.height": return N(Font.getFont((Stream)a, JS.toInt(b)).textheight(JS.toString(c)));
                     case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented");
-                    case "ui.font.width": return N(Font.getFont((Stream)a, JS.toInt(b)).textwidth((String)c));
+                    case "ui.font.width": return N(Font.getFont((Stream)a, JS.toInt(b)).textwidth(JS.toString(c)));
                     //#end
                     break;
             }
@@ -242,9 +248,9 @@ public final class Ibex extends JS implements JS.Cloneable {
     
     public static final JSMath ibexMath = new JSMath() {
             // FEATURE: find a cleaner way to do this
-            private JS gs = new JSScope.Global();
-            public Object get(Object key) throws JSExn {
-                //#switch(key)
+            private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
+            public JS get(JS key) throws JSExn {
+                //#switch(JS.toString(key))
                 case "isNaN": return METHOD;
                 case "isFinite": return METHOD;
                 case "NaN": return METHOD;
@@ -252,8 +258,8 @@ public final class Ibex extends JS implements JS.Cloneable {
                 //#end
                 return super.get(key);
             }
-            public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
-                //#switch(name)
+            public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
+                //#switch(JS.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);
@@ -264,9 +270,9 @@ public final class Ibex extends JS implements JS.Cloneable {
     };
 
     public static final JS ibexString = new JS() {
-            private JS gs = new JSScope.Global();
-            public Object get(Object key) throws JSExn {
-                //#switch(key)
+            private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
+            public JS get(JS key) throws JSExn {
+                //#switch(JS.toString(key))
                 case "parseInt": return METHOD;
                 case "parseFloat": return METHOD;
                 case "decodeURI": return METHOD;
@@ -279,8 +285,8 @@ public final class Ibex extends JS implements JS.Cloneable {
                 //#end
                 return super.get(key);
             }
-            public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
-                //#switch(name)
+            public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
+                //#switch(JS.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);
@@ -300,29 +306,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 = (JS)b.getAndTriggerTraps("startElement");
-            endElement   = (JS)b.getAndTriggerTraps("endElement");
-            characters   = (JS)b.getAndTriggerTraps("characters");
-            whitespace   = (JS)b.getAndTriggerTraps("whitespace");
+            startElement = b.getAndTriggerTraps(JS.S("startElement"));
+            endElement   = b.getAndTriggerTraps(JS.S("endElement"));
+            characters   = b.getAndTriggerTraps(JS.S("characters"));
+            whitespace   = b.getAndTriggerTraps(JS.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(c.getAttrKey(i), c.getAttrVal(i));
-                startElement.call(c.getLocalName(), attrs, c.getUri(), null, 3);
+                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);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void endElement(XML.Element c) throws XML.Exn { try {
-                endElement.call(c.getLocalName(), c.getUri(), null, null, 2);
+                endElement.call(JS.S(c.getLocalName()), JS.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(new String(ch, start, length), null, null, null, 1);
+                characters.call(JS.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(new String(ch, start, length), null, null, null, 1);
+                whitespace.call(JS.S(new String(ch, start, length)), null, null, null, 1);
         } catch (JSExn jse) { throw new Wrapper(jse); } }
 
         public void doParse(JS s) throws JSExn {
@@ -346,21 +352,19 @@ public final class Ibex extends JS implements JS.Cloneable {
     public static class Blessing extends JS.Clone {
         private Ibex ibex;
         private Template t = null;
-        public Object parentkey = null;
+        public JS parentkey = null;
         public Blessing parent = null;
         private Hash cache = new Hash();
-        public Blessing(JS clonee, Ibex ibex, Blessing parent, Object parentkey) throws JSExn {
+        public Blessing(JS clonee, Ibex ibex, Blessing parent, JS parentkey) throws JSExn {
             super(clonee); this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; }
-        public Object get(Object key) throws JSExn {
-            if (key.equals("")) return ((Object)getStatic());
-            if (cache.get(key) != null) return cache.get(key);
-            Object ret = new Blessing((JS)clonee.get(key), ibex, this, key);
+        public JS get(JS key) throws JSExn {
+            if (JS.isString(key) && JS.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);
             return ret;
         }
-        public static Blessing getBlessing(Object o) {
-            if (!(o instanceof JS)) return null;
-            JS js = (JS)o;
+        public static Blessing getBlessing(JS js) {
             while (js instanceof JS.Clone && !(js instanceof Blessing)) js = ((JS.Clone)js).getClonee();
             if (!(js instanceof Blessing)) return null;
             return (Blessing)js;
@@ -373,7 +377,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 = Stream.getInputStream(parent.get(parentkey + exts[i]));
+                    InputStream in = Stream.getInputStream(parent.get(JS.S(JS.toString(parentkey) + exts[i])));
                     if (in != null) return in;
                 } catch (IOException f) { /* DELIBERATE */ }
             return null;
@@ -381,7 +385,7 @@ public final class Ibex extends JS implements JS.Cloneable {
         public JSScope getStatic() {
             try {
                 if (t == null) {
-                    JS res = (JS) parent.get(parentkey + ".t");
+                    JS res = (JS) parent.get(JS.S(JS.toString(parentkey) + ".t"));
                     t = Template.buildTemplate(res.unclone().toString(), res, ibex);
                 }
                 return t.staticScope;
@@ -390,12 +394,13 @@ public final class Ibex extends JS implements JS.Cloneable {
                 return null;
             }
         }
-        public Object call(Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+        public JS call(JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
             // GROSS hack
             if (nargs != 1 && nargs != 9999) throw new JSExn("FIXME can only call with one arg");
             getStatic();
             if (t == null) throw new JSExn("No such template " + parentkey);
-            if (nargs == 9999) return t;
+            if (nargs == 9999) /*return t;*/ throw new Error("FIXME"); // FIXME: 9999 stuff
+            if(!(a instanceof Box)) throw new JSExn("can only apply templates to boxes");
             t.apply((Box)a);
             return a;
         }