// 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() { };
}
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;
}
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; }
}
return NaN;
}
- }
+ }*/
}