more new js api fixes and cleanup
[org.ibex.core.git] / src / org / ibex / js / JSScope.java
index 51cc33a..f7c6355 100644 (file)
@@ -33,14 +33,26 @@ public class JSScope extends JS.BT {
         return s;
     }
 
-    // FIXME: Update this for new API, it also has some other problems
-    /*public static class Global extends JSScope {
-        private final static Double NaN = new Double(Double.NaN);
-        private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
+    public static class Global extends JSScope {
+        private final static JS NaN = N(Double.NaN);
+        private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
 
         public Global() { super(null); }
-        public Object get(Object key) throws JSExn {
-            //#switch(key)
+        public Global(JSScope p) { super(p); }
+        
+        public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
+        
+        // HACK: "this" gets lost on the way back through the scope chain
+        // We'll have to do something better with this when Scope is rewritten
+        public JS get(JS key) throws JSExn {
+            JS ret = _get(key);
+            if(ret == METHOD) return new Interpreter.Stub(this,key);
+            return ret;
+        }
+        
+        public JS _get(JS key) throws JSExn {
+            if(!JS.isString(key)) return super.get(key);
+            //#switch(JS.toString(key))
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
@@ -59,43 +71,25 @@ public class JSScope extends JS.BT {
             return super.get(key);
         }
 
-        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-            switch(nargs) {
-                case 0: {
-                    //#switch(method)
-                    case "stringFromCharCode":
-                        char buf[] = new char[nargs];
-                        for(int i=0; i<nargs; i++) buf[i] = (char)(JS.toInt(i==0?a0:i==1?a1:i==2?a2:rest[i-3]) & 0xffff);
-                        return new String(buf);
-                    //#end
-                    break;
-                }
-                case 1: {
-                    //#switch(method)
-                    case "parseInt": return parseInt(a0, N(0));
-                    case "parseFloat": return parseFloat(a0);
-                    case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
-                    case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
-                    case "decodeURI": throw new JSExn("unimplemented");
-                    case "decodeURIComponent": throw new JSExn("unimplemented");
-                    case "encodeURI": throw new JSExn("unimplemented");
-                    case "encodeURIComponent": throw new JSExn("unimplemented");
-                    case "escape": throw new JSExn("unimplemented");
-                    case "unescape": throw new JSExn("unimplemented");
-                    //#end
-                    break;
-                }
-                case 2: {
-                    //#switch(method)
-                    case "parseInt": return parseInt(a0, a1);
-                    //#end
-                    break;
-                }
-            }
+        public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+            if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
+            //#switch(JS.toString(method))
+            case "parseInt": return parseInt(a0, N(0));
+            case "parseFloat": return parseFloat(a0);
+            case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
+            case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
+            case "decodeURI": throw new JSExn("unimplemented");
+            case "decodeURIComponent": throw new JSExn("unimplemented");
+            case "encodeURI": throw new JSExn("unimplemented");
+            case "encodeURIComponent": throw new JSExn("unimplemented");
+            case "escape": throw new JSExn("unimplemented");
+            case "unescape": throw new JSExn("unimplemented");
+            case "parseInt": return parseInt(a0, a1);
+            //#end
             return super.callMethod(method, a0, a1, a2, rest, nargs);
         }
 
-        private Object parseInt(Object arg, Object r) throws JSExn {
+        private JS parseInt(JS arg, JS r) throws JSExn {
             int radix = JS.toInt(r);
             String s = JS.toString(arg);
             int start = 0;
@@ -136,7 +130,7 @@ public class JSScope extends JS.BT {
             return JS.N((long)sign*n);
         }
 
-        private Object parseFloat(Object arg) throws JSExn {
+        private JS parseFloat(JS arg) throws JSExn {
             String s = JS.toString(arg);
             int start = 0;
             int length = s.length();
@@ -146,12 +140,12 @@ public class JSScope extends JS.BT {
             // trailing garbage
             while(start < end) {
                 try {
-                    return JS.N(s.substring(start,length));
+                    return JS.N(new Double(s.substring(start,length)));
                 } catch(NumberFormatException e) { }
                 end--;
             }
             return NaN;
         }
-    }*/
+    }
 }