[re]-merged in Brians stuff
[org.ibex.core.git] / src / org / ibex / core / Ibex.java
index e14a970..9427c20 100644 (file)
@@ -59,7 +59,7 @@ public final class Ibex extends JS implements JS.Cloneable {
         // 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)
+        //#switch(JS.toString(name))
         case "math": return ibexMath;
         case "string": return ibexString;
         case "date": return METHOD;
@@ -129,7 +129,7 @@ public final class Ibex extends JS implements JS.Cloneable {
     }
 
     public void put(JS name, JS value) throws JSExn {
-        //#jswitch(name)
+        //#switch(JS.toString(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;
@@ -144,7 +144,7 @@ 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 {
-            //#jswitch(name)
+            //#switch(JS.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));
                 // FIXME support object dumping
@@ -156,21 +156,21 @@ public final class Ibex extends JS implements JS.Cloneable {
  
             switch (nargs) {
                 case 0:
-                    //#jswitch(name)
+                    //#switch(JS.toString(name))
                     case "thread.yield": sleep(0); return null;
                     //#end
                     break;
                 case 1:
-                    //#jswitch(name)
+                    //#switch(JS.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 "stream.unzip": return a == null ? null : new Stream.Zip(a);
-                    case "stream.uncab": return a == null ? null : new Stream.Cab(a);
+                    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(a, "resources", true); }
+                        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);
@@ -200,12 +200,12 @@ public final class Ibex extends JS implements JS.Cloneable {
                     //#end
                     break;
                 case 2:
-                    //#jswitch(name)
-                    case "stream.watch": return new Stream.ProgressWatcher(a, b);
+                    //#switch(JS.toString(name))
+                    case "stream.watch": return new Stream.ProgressWatcher((Stream)a, b);
                     case "regexp": return new JSRegexp(a, b);
                     //#end
                 case 3:
-                    //#jswitch(name)
+                    //#switch(JS.toString(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)));
@@ -243,33 +243,33 @@ public final class Ibex extends JS implements JS.Cloneable {
         }
     }
     
-    public static final JSMath ibexMath = new JSMath() {
+    public static final JS ibexMath = new JS() {
             // 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 {
-                //#jswitch(key)
+                //#switch(JS.toString(key))
                 case "isNaN": return METHOD;
                 case "isFinite": return METHOD;
                 case "NaN": return METHOD;
                 case "Infinity": return METHOD;
                 //#end
-                return super.get(key);
+                return MATH.get(key);
             }
             public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
-                //#jswitch(name)
+                //#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);
                 case "Infinity": return gs.callMethod(name,a,b,c,rest,nargs);
                 //#end
-                return super.callMethod(name,a,b,c,rest,nargs);
+                return MATH.callMethod(name,a,b,c,rest,nargs);
             }
     };
 
     public static final JS ibexString = new JS() {
             private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
             public JS get(JS key) throws JSExn {
-                //#jswitch(key)
+                //#switch(JS.toString(key))
                 case "parseInt": return METHOD;
                 case "parseFloat": return METHOD;
                 case "decodeURI": return METHOD;
@@ -283,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 {
-                //#jswitch(name)
+                //#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);
@@ -345,16 +345,16 @@ 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); }
-    // FIXME: Does this really need to extends JS.Clone?
-    // FEATURE: Mandate that Blessings use only String keys?
-    public static class Blessing extends JS.Clone {
+    // JS:FIXME: This doesn't properly handle traps
+    public static class Blessing extends JS.O {
         private Ibex ibex;
         private Template t = null;
         public JS parentkey = null;
         public Blessing parent = null;
+        public JS clonee;
         private Hash cache = new Hash();
         public Blessing(JS clonee, Ibex ibex, Blessing parent, JS parentkey) throws JSExn {
-            super(clonee); this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; }
+            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 (cache.get(key) != null) return (JS)cache.get(key);
@@ -381,14 +381,14 @@ public final class Ibex extends JS implements JS.Cloneable {
                 } catch (IOException f) { /* DELIBERATE */ }
             return null;
         }
-        public JSScope getStatic() throws JSExn {
+        public JS getStatic() throws JSExn {
             try {
                 if (t == null) {
                     // FEATURE: Might want to handle the ".t" part better
                     JS res = parent.get(JS.S(JS.toString(parentkey) + ".t"));
                     t = Template.buildTemplate(description(), res, ibex);
                 }
-                return t != null ? t.staticScope : null;
+                return t != null ? t.staticObject : null;
             } catch (Exception e) {
                 Log.error(this, e);
                 return null;
@@ -396,7 +396,7 @@ public final class Ibex extends JS implements JS.Cloneable {
         }
         private String description() {
             String s = JS.debugToString(parentkey);
-            for(Blessing b = parent; b != null; b = b.parent) s = JS.debugToString(parentkey) + "." + s;
+            for(Blessing b = parent; b.parentkey != null; b = b.parent) s = JS.debugToString(b.parentkey) + "." + s;
             return s;
         }
         public JS call(JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
@@ -413,6 +413,8 @@ public final class Ibex extends JS implements JS.Cloneable {
             if (t == null) throw new JSExn("No such template " + JS.debugToString(parentkey));
             return t;
         }
+        // JS:FIXME: Blessing shouldn't need to roll its own JS.Clone implementation
+        public InputStream getInputStream() throws IOException { return clonee.getInputStream(); }
     }
 
 }