local scope vars indexed by number
[org.ibex.core.git] / src / org / ibex / js / JSScope.java
index 9fbd3e2..9b9a3b1 100644 (file)
@@ -1,11 +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
 /** Implementation of a JavaScript Scope */
-public class JSScope extends JS.O { 
+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() { };
 
@@ -142,6 +182,6 @@ public class JSScope extends JS.O {
             }
             return NaN;
         }
-    }
+    }*/
 }