use //#jswitch more
[org.ibex.core.git] / src / org / ibex / core / Ibex.java
index 66d9189..e14a970 100644 (file)
@@ -37,28 +37,29 @@ public final class Ibex extends JS implements JS.Cloneable {
 
     /** lets us put multi-level get/put/call keys all in the same method */
     private class Sub extends JS {
-        String key;
-        Sub(String key) { this.key = key; }
-        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)); }
+        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 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(this.key + "." + JS.toString(method), a0, a1, a2, rest, nargs);
+            return Ibex.this.callMethod(JS.S(JS.toString(this.key) + "." + JS.toString(method)), a0, a1, a2, rest, nargs);
         }
     }
     private Cache subCache = new Cache(20);
-    private Sub getSub(String s) {
-        Sub ret = (Sub)subCache.get(s);
-        if (ret == null) subCache.put(s, ret = new Sub(s));
+    private Sub getSub(JS key) {
+        Sub ret = (Sub)subCache.get(key);
+        if (ret == null) subCache.put(key, ret = new Sub(key));
         return ret;
     }
 
-    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)
+    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;
+        // FEATURE: Preprocessor hack to generate specialized JS instances (avoid all this string concatenation)
+        //#jswitch(name)
         case "math": return ibexMath;
         case "string": return ibexString;
         case "date": return METHOD;
@@ -66,8 +67,8 @@ public final class Ibex extends JS implements JS.Cloneable {
         case "clone": return METHOD;
         case "bless": return METHOD;
         case "regexp": return METHOD;
-        case "ui": return getSub("ui");
-        case "ui.font": return getSub("ui.font");
+        case "ui": return getSub(name);
+        case "ui.font": return getSub(name);
         case "ui.font.wait": return METHOD;
         case "ui.font.width": return METHOD;
         case "ui.font.height": return METHOD;
@@ -75,30 +76,30 @@ public final class Ibex extends JS implements JS.Cloneable {
         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": return getSub(name);
         case "ui.mouse.button":
             if (Surface.button1 && !Surface.button2 && !Surface.button3) return N(1);
             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return N(2);
             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return N(3);
             else return ZERO;
-        case "ui.key": return getSub("ui.key");
-        case "ui.key.name": return getSub("ui.key.name");
+        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.maxdim": return N(Short.MAX_VALUE);
-        case "ui.screen": return getSub("ui.screen");
+        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("undocumented");
+        case "undocumented": return getSub(name);
         case "undocumented.initialOrigin": return JS.S(Main.origin);
         case "undocumented.initialTemplate": return JS.S(Main.initialTemplate);
-        case "thread": return getSub("thread");
+        case "thread": return getSub(name);
         case "thread.yield": return METHOD;
         case "thread.sleep": return METHOD;
-        case "stream": return getSub("stream");
+        case "stream": return getSub(name);
         case "stream.homedir": return url2res("file:" + System.getProperty("user.home"));
         case "stream.tempdir": return url2res("file:" + System.getProperty("java.io.tempdir"));
         case "stream.watch": return METHOD;
@@ -109,16 +110,16 @@ public final class Ibex extends JS implements JS.Cloneable {
         case "stream.parse.html": return METHOD;
         case "stream.parse.xml": return METHOD;
         case "stream.parse.utf8": return METHOD;
-        case "net": return getSub("net");
-        case "net.rpc": return getSub("net.rpc");
+        case "net": return getSub(name);
+        case "net.rpc": return getSub(name);
         case "net.rpc.xml": return METHOD;
         case "net.rpc.soap": return METHOD;
-        case "log": return getSub("log");
+        case "log": return getSub(name);
         case "log.debug": return METHOD;
         case "log.info": return METHOD;
         case "log.warn": return METHOD;
         case "log.error": return METHOD;
-        case "crypto": return getSub("crypto");
+        case "crypto": return getSub(name);
         case "crypto.rsa": return METHOD;
         case "crypto.md5": return METHOD;
         case "crypto.sha1": return METHOD;
@@ -127,9 +128,8 @@ public final class Ibex extends JS implements JS.Cloneable {
         return null;
     }
 
-    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)
+    public void put(JS name, JS value) throws JSExn {
+        //#jswitch(name)
         case "thread": Scheduler.add((Task)value); return;
         case "ui.clipboard": Platform.setClipBoard(JS.toString(value)); return;
         case "ui.frame": Platform.createSurface((Box)value, true, true); return;
@@ -143,12 +143,8 @@ 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 {
-        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)
+            //#jswitch(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));
                 // FIXME support object dumping
@@ -160,12 +156,12 @@ public final class Ibex extends JS implements JS.Cloneable {
  
             switch (nargs) {
                 case 0:
-                    //#switch(name)
+                    //#jswitch(name)
                     case "thread.yield": sleep(0); return null;
                     //#end
                     break;
                 case 1:
-                    //#switch(name)
+                    //#jswitch(name)
                     case "clone":
                         if(a == null) throw new JSExn("can't clone the null value");
                         return ((JS)a).jsclone();
@@ -204,12 +200,12 @@ public final class Ibex extends JS implements JS.Cloneable {
                     //#end
                     break;
                 case 2:
-                    //#switch(name)
+                    //#jswitch(name)
                     case "stream.watch": return new Stream.ProgressWatcher(a, b);
                     case "regexp": return new JSRegexp(a, b);
                     //#end
                 case 3:
-                    //#switch(name)
+                    //#jswitch(name)
                     case "ui.font.height": return N(Font.getFont(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(a, JS.toInt(b)).textwidth(JS.toString(c)));
@@ -251,7 +247,7 @@ public final class Ibex extends JS implements JS.Cloneable {
             // 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))
+                //#jswitch(key)
                 case "isNaN": return METHOD;
                 case "isFinite": return METHOD;
                 case "NaN": return METHOD;
@@ -260,7 +256,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))
+                //#jswitch(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);
@@ -273,7 +269,7 @@ public final class Ibex extends JS implements JS.Cloneable {
     public static final JS ibexString = new JS() {
             private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
             public JS get(JS key) throws JSExn {
-                //#switch(JS.toString(key))
+                //#jswitch(key)
                 case "parseInt": return METHOD;
                 case "parseFloat": return METHOD;
                 case "decodeURI": return METHOD;
@@ -287,7 +283,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))
+                //#jswitch(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);