package org.ibex.js;
/** Implementation of a JavaScript Scope */
-class JSScope extends JS.O {
+class JSScope {
private final int base;
private final JS[] vars;
this.vars = new JS[size];
}
- 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 JSScope(JSScope parentScope) { this(parentScope, 0, 0); }
- public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
- public JSScope getParentScope() { return parentScope; }
-
- public JS get(JS key) throws JSExn {
- if (key instanceof JSNumber) try {
- return get(JS.toInt(key));
+ final JS get(JS i) throws JSExn {
+ if(i==null) throw new NullPointerException();
+ try {
+ return get(JSU.toInt(i));
} catch(ArrayIndexOutOfBoundsException e) {
throw new JSExn("scope index out of range");
}
- JS o = super.get(key);
- if (o != null) return o == NULL_PLACEHOLDER ? null : o;
- else return parentScope == null ? null : parentScope.get(key);
}
-
- public boolean has(JS key) throws JSExn { return super.get(key) != null; }
- public void put(JS key, JS val) throws JSExn {
- if (key instanceof JSNumber) try {
- put(JS.toInt(key),val);
+ final void put(JS i, JS o) throws JSExn {
+ if(i==null) throw new NullPointerException();
+ try {
+ put(JSU.toInt(i), o);
} catch(ArrayIndexOutOfBoundsException e) {
throw new JSExn("scope index out of range");
}
- if (parentScope != null && !has(key)) parentScope.put(key, val);
- else super.put(key, val == null ? NULL_PLACEHOLDER : val);
}
+ 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; }
- public JSScope top() {
- JSScope s = this;
- while(s.parentScope != null) s = s.parentScope;
- return s;
- }
-
- public static class Global extends JSScope {
- private final static JS NaN = JS.N(Double.NaN);
- private final static JS POSITIVE_INFINITY = JS.N(Double.POSITIVE_INFINITY);
-
- public Global() { super(null); }
- 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 == JS.METHOD) return new Interpreter.Stub(this,key);
- return ret;
- }
-
- public JS _get(JS key) throws JSExn {
- //#switch(JS.toString(key))
- case "JS.NaN": return JS.NaN;
- case "Infinity": return POSITIVE_INFINITY;
- case "undefined": return null;
- case "stringFromCharCode": return JS.METHOD;
- case "parseInt": return JS.METHOD;
- case "parseFloat": return JS.METHOD;
- case "isJS.NaN": return JS.METHOD;
- case "isFinite": return JS.METHOD;
- case "decodeURI": return JS.METHOD;
- case "decodeURIComponent": return JS.METHOD;
- case "encodeURI": return JS.METHOD;
- case "encodeURIComponent": return JS.METHOD;
- case "escape": return JS.METHOD;
- case "unescape": return JS.METHOD;
- default: return super.get(key);
- //#end
- return super.get(key);
- }
-
- public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
- //#switch(JS.toString(method))
- case "parseInt": return parseInt(a0, N(0));
- case "parseFloat": return parseFloat(a0);
- case "isJS.NaN": { double d = JS.toDouble(a0); return d == d ? JS.F : JS.T; }
- case "isFinite": { double d = JS.toDouble(a0); return (d == d && !Double.isInfinite(d)) ? JS.T : JS.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);
- default: return super.callMethod(method, a0, a1, a2, rest, nargs);
- //#end
- return super.callMethod(method,a0,a1,a2,rest,nargs);
- }
-
- private JS parseInt(JS arg, JS r) throws JSExn {
- int radix = JS.toInt(r);
- String s = JS.toString(arg);
- int start = 0;
- int length = s.length();
- int sign = 1;
- long n = 0;
- if(radix != 0 && (radix < 2 || radix > 36)) return JS.NaN;
- while(start < length && Character.isWhitespace(s.charAt(start))) start++;
- if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
- sign = s.charAt(start) == '+' ? 1 : -1;
- start++;
- }
- if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
- start++;
- if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
- start++;
- radix = 16;
- } else {
- radix = 8;
- if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
- }
- }
- if(radix == 0) radix = 10;
- if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
- // try the fast way first
- try {
- String s2 = start == 0 ? s : s.substring(start);
- return JS.N(sign*Integer.parseInt(s2,radix));
- } catch(NumberFormatException e) { }
- // fall through to a slower but emca-compliant method
- for(int i=start;i<length;i++) {
- int digit = Character.digit(s.charAt(i),radix);
- if(digit < 0) break;
- n = n*radix + digit;
- if(n < 0) return JS.NaN; // overflow;
- }
- if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
- return JS.N((long)sign*n);
- }
-
- private JS parseFloat(JS arg) throws JSExn {
- String s = JS.toString(arg);
- int start = 0;
- int length = s.length();
- while(start < length && Character.isWhitespace(s.charAt(0))) start++;
- int end = length;
- // as long as the string has no trailing garbage,this is fast, its slow with
- // trailing garbage
- while(start < end) {
- try {
- return JS.N(new Double(s.substring(start,length)));
- } catch(NumberFormatException e) { }
- end--;
- }
- return JS.NaN;
- }
- }
+ JS getGlobal() { return parent.getGlobal(); }
}
-