local scope vars indexed by number
[org.ibex.core.git] / src / org / ibex / js / JSScope.java
index f7c6355..9b9a3b1 100644 (file)
@@ -1,13 +1,51 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
 package org.ibex.js; 
 
-// FIXME: should allow parentScope to be a JS, not a JSScope
-// FIXME: Index local vars by number and lookup in an array
 /** Implementation of a JavaScript Scope */
-// HACK: JSScope doesn't really need the BT, this is just for Box.java 
-public class JSScope extends JS.BT { 
+class JSScope {
 
-    private JSScope parentScope;
+    private final int base;
+    private final JS[] vars;
+    final JSScope parent;
+
+    public static class Top extends JSScope {
+        private final JS global;
+        public Top(JS global) { super(null,0,0); this.global = global; }
+        JS get(int i) throws JSExn { throw new JSExn("scope index out of range"); }
+        void put(int i, JS o) throws JSExn { throw new JSExn("scope index out of range"); }
+        JS getGlobal() { return global; }
+    };
+        
+    // NOTE: We can't just set base to parent.base + parent.vars.length
+    // sometimes we only access part of a parent's scope
+    public JSScope(JSScope parent, int base, int size) {
+        this.parent = parent;
+        this.base = base;
+        this.vars = new JS[size];
+    }
+    
+    final JS get(JS i) throws JSExn {
+        if(i==null) throw new NullPointerException();
+        try {
+            return get(JS.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(JS.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 static final JS NULL_PLACEHOLDER = new JS() { };
 
@@ -51,8 +89,7 @@ public class JSScope extends JS.BT {
         }
         
         public JS _get(JS key) throws JSExn {
-            if(!JS.isString(key)) return super.get(key);
-            //#switch(JS.toString(key))
+            //#jswitch(key)
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
@@ -72,8 +109,7 @@ public class JSScope extends JS.BT {
         }
 
         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))
+            //#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; }
@@ -146,6 +182,6 @@ public class JSScope extends JS.BT {
             }
             return NaN;
         }
-    }
+    }*/
 }