jswitch
[org.ibex.core.git] / src / org / ibex / js / JSScope.java
index f025b24..5036220 100644 (file)
@@ -3,24 +3,25 @@ package org.ibex.js;
 
 // FIXME: should allow parentScope to be a JS, not a JSScope
 /** Implementation of a JavaScript Scope */
-public class JSScope extends JS { 
+// HACK: JSScope doesn't really need the BT, this is just for Box.java 
+public class JSScope extends JS.BT { 
 
     private JSScope parentScope;
 
-    private static final Object NULL_PLACEHOLDER = new Object();
+    private static final JS NULL_PLACEHOLDER = new JS() { };
 
     public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
-    public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
+    public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
     public JSScope getParentScope() { return parentScope; }
 
-    public Object get(Object key) throws JSExn {
-        Object o = super.get(key);
+    public JS get(JS key) throws JSExn {
+        JS o = super.get(key);
         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
         else return parentScope == null ? null : parentScope.get(key);
     }
 
-    public boolean has(Object key) throws JSExn { return super.get(key) != null; }
-    public void put(Object key, Object val) throws JSExn {
+    public boolean has(JS key) throws JSExn { return super.get(key) != null; }
+    public void put(JS key, JS val) throws JSExn {
         if (parentScope != null && !has(key)) parentScope.put(key, val);
         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
     }
@@ -32,12 +33,24 @@ public class JSScope extends JS {
     }
 
     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);
+        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 {
+            //#jswitch(key)
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
@@ -56,43 +69,24 @@ public class JSScope extends JS {
             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 {
+            //#jswitch(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;
@@ -133,7 +127,7 @@ public class JSScope extends JS {
             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();
@@ -143,7 +137,7 @@ public class JSScope extends JS {
             // 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--;
             }