// 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() { };
return s;
}
- // FIXME: Update this for new API, it also has some other problems
- /*public static class Global extends JSScope {
- private final static Double NaN = new Double(Double.NaN);
- private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
+ public static class Global extends JSScope {
+ private final static JS NaN = N(Double.NaN);
+ private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
public Global() { super(null); }
- public Object get(Object key) throws JSExn {
- //#switch(key)
+ public Global(JSScope p) { super(p); }
+
+ public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
+
+ // HACK: "this" gets lost on the way back through the scope chain
+ // 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 == METHOD) return new Interpreter.Stub(this,key);
+ return ret;
+ }
+
+ public JS _get(JS key) throws JSExn {
+ //#jswitch(key)
case "NaN": return NaN;
case "Infinity": return POSITIVE_INFINITY;
case "undefined": return null;
return super.get(key);
}
- public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
- switch(nargs) {
- case 0: {
- //#switch(method)
- case "stringFromCharCode":
- char buf[] = new char[nargs];
- for(int i=0; i<nargs; i++) buf[i] = (char)(JS.toInt(i==0?a0:i==1?a1:i==2?a2:rest[i-3]) & 0xffff);
- return new String(buf);
- //#end
- break;
- }
- case 1: {
- //#switch(method)
- case "parseInt": return parseInt(a0, N(0));
- case "parseFloat": return parseFloat(a0);
- 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");
- case "encodeURIComponent": throw new JSExn("unimplemented");
- case "escape": throw new JSExn("unimplemented");
- case "unescape": throw new JSExn("unimplemented");
- //#end
- break;
- }
- case 2: {
- //#switch(method)
- case "parseInt": return parseInt(a0, a1);
- //#end
- break;
- }
- }
+ public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+ //#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; }
+ 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");
+ case "encodeURIComponent": throw new JSExn("unimplemented");
+ case "escape": throw new JSExn("unimplemented");
+ case "unescape": throw new JSExn("unimplemented");
+ case "parseInt": return parseInt(a0, a1);
+ //#end
return super.callMethod(method, a0, a1, a2, rest, nargs);
}
- private Object parseInt(Object arg, Object r) throws JSExn {
+ private JS parseInt(JS arg, JS r) throws JSExn {
int radix = JS.toInt(r);
String s = JS.toString(arg);
int start = 0;
return JS.N((long)sign*n);
}
- private Object parseFloat(Object arg) throws JSExn {
+ private JS parseFloat(JS arg) throws JSExn {
String s = JS.toString(arg);
int start = 0;
int length = s.length();
// trailing garbage
while(start < end) {
try {
- return JS.N(s.substring(start,length));
+ return JS.N(new Double(s.substring(start,length)));
} catch(NumberFormatException e) { }
end--;
}