1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 /** Implementation of a JavaScript Scope */
10 private final int base;
11 private final JS[] vars;
14 public static class Top extends JSScope {
15 private final JS global;
16 public Top(JS global) { super(null,0,0); this.global = global; }
17 JS get(int i) throws JSExn { throw new JSExn("scope index out of range"); }
18 void put(int i, JS o) throws JSExn { throw new JSExn("scope index out of range"); }
19 JS getGlobal() { return global; }
22 // NOTE: We can't just set base to parent.base + parent.vars.length
23 // sometimes we only access part of a parent's scope
24 public JSScope(JSScope parent, int base, int size) {
27 this.vars = new JS[size];
30 final JS get(JS i) throws JSExn {
31 if(i==null) throw new NullPointerException();
33 return get(JSU.toInt(i));
34 } catch(ArrayIndexOutOfBoundsException e) {
35 throw new JSExn("scope index out of range");
38 final void put(JS i, JS o) throws JSExn {
39 if(i==null) throw new NullPointerException();
42 } catch(ArrayIndexOutOfBoundsException e) {
43 throw new JSExn("scope index out of range");
46 JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
47 void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
49 JS getGlobal() { return parent.getGlobal(); }