move to JS interface
[org.ibex.js.git] / src / org / ibex / js / JSScope.java
index 6570840..5dee438 100644 (file)
@@ -5,7 +5,7 @@
 package org.ibex.js; 
 
 /** Implementation of a JavaScript Scope */
-class JSScope extends JS.O {
+class JSScope {
 
     private final int base;
     private final JS[] vars;
@@ -27,25 +27,36 @@ class JSScope extends JS.O {
         this.vars = new JS[size];
     }
     
+    final JS get(JS i) throws JSExn {
+        if(i==null) throw new NullPointerException();
+        try {
+            return get(Script.toInt(i));
+        } catch(ArrayIndexOutOfBoundsException e) { 
+            throw new JSExn("scope index out of range");
+        }
+    }
+    final void put(JS i, JS o) throws JSExn {
+        if(i==null) throw new NullPointerException();
+        try {
+            put(Script.toInt(i), o);
+        } catch(ArrayIndexOutOfBoundsException e) { 
+            throw new JSExn("scope index out of range");
+        }
+    }
     JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
     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 JSScope parentScope;
 
     private static final JS NULL_PLACEHOLDER = new JS() { };
 
-    public JSScope(JSScope parentScope) { this(parentScope, 0, 0); }
+    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 {
-        if (key instanceof JSNumber) try {
-            return get(JS.toInt(key));
-        } catch(ArrayIndexOutOfBoundsException e) { 
-            throw new JSExn("scope index out of range");
-        }
         JS o = super.get(key);
         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
         else return parentScope == null ? null : parentScope.get(key);
@@ -53,11 +64,6 @@ class JSScope extends JS.O {
 
     public boolean has(JS key) throws JSExn { return super.get(key) != null; }
     public void put(JS key, JS val) throws JSExn {
-       if (key instanceof JSNumber) try {
-            put(JS.toInt(key),val);
-        } catch(ArrayIndexOutOfBoundsException e) { 
-            throw new JSExn("scope index out of range");
-        }
         if (parentScope != null && !has(key)) parentScope.put(key, val);
         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
     }
@@ -69,8 +75,8 @@ class JSScope extends JS.O {
     }
 
     public static class Global extends JSScope {
-        private final static JS NaN = JS.N(Double.NaN);
-        private final static JS POSITIVE_INFINITY = JS.N(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 Global(JSScope p) { super(p); }
@@ -81,37 +87,36 @@ class JSScope extends JS.O {
         // 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 == JS.METHOD) return new Interpreter.Stub(this,key);
+            if(ret == METHOD) return new Interpreter.Stub(this,key);
             return ret;
         }
         
         public JS _get(JS key) throws JSExn {
-            //#switch(JS.toString(key))
-            case "JS.NaN": return JS.NaN;
+            #switch(JS.str(key))
+            case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
-            case "stringFromCharCode": return JS.METHOD;
-            case "parseInt": return JS.METHOD;
-            case "parseFloat": return JS.METHOD;
-            case "isJS.NaN": return JS.METHOD;
-            case "isFinite": return JS.METHOD;
-            case "decodeURI": return JS.METHOD;
-            case "decodeURIComponent": return JS.METHOD;
-            case "encodeURI": return JS.METHOD;
-            case "encodeURIComponent": return JS.METHOD;
-            case "escape": return JS.METHOD;
-            case "unescape": return JS.METHOD;
-           default: return super.get(key);
-            //#end
+            case "stringFromCharCode": return METHOD;
+            case "parseInt": return METHOD;
+            case "parseFloat": return METHOD;
+            case "isNaN": return METHOD;
+            case "isFinite": return METHOD;
+            case "decodeURI": return METHOD;
+            case "decodeURIComponent": return METHOD;
+            case "encodeURI": return METHOD;
+            case "encodeURIComponent": return METHOD;
+            case "escape": return METHOD;
+            case "unescape": 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.toString(method))
+            #switch(JS.str(method))
             case "parseInt": return parseInt(a0, N(0));
             case "parseFloat": return parseFloat(a0);
-            case "isJS.NaN": { double d = JS.toDouble(a0); return d == d ? JS.F : JS.T; }
-            case "isFinite": { double d = JS.toDouble(a0); return (d == d && !Double.isInfinite(d)) ? JS.T : JS.F; }
+            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");
@@ -119,9 +124,8 @@ class JSScope extends JS.O {
             case "escape": throw new JSExn("unimplemented");
             case "unescape": throw new JSExn("unimplemented");
             case "parseInt": return parseInt(a0, a1);
-            default: return super.callMethod(method, a0, a1, a2, rest, nargs);
-            //#end
-            return super.callMethod(method,a0,a1,a2,rest,nargs);
+            #end
+            return super.callMethod(method, a0, a1, a2, rest, nargs);
         }
 
         private JS parseInt(JS arg, JS r) throws JSExn {
@@ -131,7 +135,7 @@ class JSScope extends JS.O {
             int length = s.length();
             int sign = 1;
             long n = 0;
-            if(radix != 0 && (radix < 2 || radix > 36)) return JS.NaN;
+            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;
@@ -148,7 +152,7 @@ class JSScope extends JS.O {
                 }
             }
             if(radix == 0) radix = 10;
-            if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
+            if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
             // try the fast way first
             try {
                 String s2 = start == 0 ? s : s.substring(start);
@@ -159,7 +163,7 @@ class JSScope extends JS.O {
                 int digit = Character.digit(s.charAt(i),radix);
                 if(digit < 0) break;
                 n = n*radix + digit;
-                if(n < 0) return JS.NaN; // overflow;
+                if(n < 0) return NaN; // overflow;
             }
             if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
             return JS.N((long)sign*n);
@@ -179,8 +183,8 @@ class JSScope extends JS.O {
                 } catch(NumberFormatException e) { }
                 end--;
             }
-            return JS.NaN;
+            return NaN;
         }
-    }
+    }*/
 }