FIXME triage and formatting cleanups
[org.ibex.js.git] / src / org / ibex / js / JSScope.java
index 5dee438..fee3037 100644 (file)
@@ -30,7 +30,7 @@ class JSScope {
     final JS get(JS i) throws JSExn {
         if(i==null) throw new NullPointerException();
         try {
-            return get(Script.toInt(i));
+            return get(JSU.toInt(i));
         } catch(ArrayIndexOutOfBoundsException e) { 
             throw new JSExn("scope index out of range");
         }
@@ -38,7 +38,7 @@ class JSScope {
     final void put(JS i, JS o) throws JSExn {
         if(i==null) throw new NullPointerException();
         try {
-            put(Script.toInt(i), o);
+            put(JSU.toInt(i), o);
         } catch(ArrayIndexOutOfBoundsException e) { 
             throw new JSExn("scope index out of range");
         }
@@ -47,58 +47,20 @@ class JSScope {
     void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
     
     JS getGlobal() { return parent.getGlobal(); }
-    
-    /*private JSScope parentScope;
-
-    private static final JS NULL_PLACEHOLDER = new JS() { };
-
-    public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
-    public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
-    public JSScope getParentScope() { return parentScope; }
-
-    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(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);
-    }
-    
-    public JSScope top() { 
-        JSScope s = this;
-        while(s.parentScope != null) s = s.parentScope;
-        return s;
-    }
+    public static class Global extends JS.Immutable {
+        private final static JS NaN = JSU.N(Double.NaN);
+        private final static JS POSITIVE_INFINITY = JSU.N(Double.POSITIVE_INFINITY);
+        private final static JS.Method METHOD = new JS.Method();
 
-    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 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 Global() { }
         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 {
-            #switch(JS.str(key))
+            //#jsswitch(key)
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
             case "stringFromCharCode": return METHOD;
             case "parseInt": return METHOD;
-            case "parseFloat": return METHOD;
             case "isNaN": return METHOD;
             case "isFinite": return METHOD;
             case "decodeURI": return METHOD;
@@ -107,37 +69,56 @@ class JSScope {
             case "encodeURIComponent": return METHOD;
             case "escape": return METHOD;
             case "unescape": return METHOD;
-            #end
+            case "parseInt": return METHOD;
+            //#end
             return super.get(key);
         }
 
-        public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-            #switch(JS.str(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);
+        public JS call(JS method, JS[] args) throws JSExn {
+            switch(args.length) {
+                case 0: {
+                    //#jsswitch(method)
+                    case "stringFromCharCode":
+                        char buf[] = new char[args.length];
+                        for(int i=0; i<args.length; i++) buf[i] = (char)(JSU.toInt(args[i]) & 0xffff);
+                        return JSU.S(new String(buf));
+                    //#end
+                    break;
+                }
+                case 1: {
+                    //#jsswitch(method)
+                    case "parseInt": return parseInt(args[0], JSU.N(0));
+                    case "isNaN": { double d = JSU.toDouble(args[0]); return d == d ? JSU.F : JSU.T; }
+                    case "isFinite": { double d = JSU.toDouble(args[0]); return (d==d && !Double.isInfinite(d)) ? JSU.T : JSU.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: {
+                    //#jsswitch(method)
+                    case "parseInt": return parseInt(args[0], args[1]);
+                    //#end
+                    break;
+                }
+            }
+            return super.call(method, args);
         }
 
         private JS parseInt(JS arg, JS r) throws JSExn {
-            int radix = JS.toInt(r);
-            String s = JS.toString(arg);
+            int radix = JSU.toInt(r);
+            String s = JSU.toString(arg);
             int start = 0;
             int length = s.length();
             int sign = 1;
             long n = 0;
-            if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
-            while(start < length && Character.isWhitespace(s.charAt(start))) start++;
-            if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
+            if (radix != 0 && (radix < 2 || radix > 36)) return NaN;
+            while (start < length && Character.isWhitespace(s.charAt(start))) start++;
+            if ((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
                 sign = s.charAt(start) == '+' ? 1 : -1;
                 start++;
             }
@@ -148,7 +129,7 @@ class JSScope {
                     radix = 16;
                 } else {
                     radix = 8;
-                    if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
+                    if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JSU.ZERO;
                 }
             }
             if(radix == 0) radix = 10;
@@ -156,7 +137,7 @@ class JSScope {
             // try the fast way first
             try {
                 String s2 = start == 0 ? s : s.substring(start);
-                return JS.N(sign*Integer.parseInt(s2,radix));
+                return JSU.N(sign*Integer.parseInt(s2,radix));
             } catch(NumberFormatException e) { }
             // fall through to a slower but emca-compliant method
             for(int i=start;i<length;i++) {
@@ -165,12 +146,12 @@ class JSScope {
                 n = n*radix + digit;
                 if(n < 0) return NaN; // overflow;
             }
-            if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
-            return JS.N((long)sign*n);
+            if(n <= Integer.MAX_VALUE) return JSU.N(sign*(int)n);
+            return JSU.N((long)sign*n);
         }
 
         private JS parseFloat(JS arg) throws JSExn {
-            String s = JS.toString(arg);
+            String s = JSU.toString(arg);
             int start = 0;
             int length = s.length();
             while(start < length && Character.isWhitespace(s.charAt(0))) start++;
@@ -179,12 +160,11 @@ class JSScope {
             // trailing garbage
             while(start < end) {
                 try {
-                    return JS.N(new Double(s.substring(start,length)));
+                    return JSU.N(Float.parseFloat(s.substring(start,length)));
                 } catch(NumberFormatException e) { }
                 end--;
             }
             return NaN;
         }
-    }*/
+    }
 }
-