-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
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 {
-
- private JSScope parentScope;
-
- private static final Object NULL_PLACEHOLDER = new Object();
+class JSScope {
- public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
- public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
- public JSScope getParentScope() { return parentScope; }
-
- public Object get(Object key) throws JSExn {
- Object o = super.get(key);
- if (o != null) return o == NULL_PLACEHOLDER ? null : o;
- else return parentScope == null ? null : parentScope.get(key);
- }
+ private final int base;
+ private final JS[] vars;
+ final JSScope parent;
- public boolean has(Object key) throws JSExn { return super.get(key) != null; }
- public void put(Object key, Object val) throws JSExn {
- if (parentScope != null && !has(key)) parentScope.put(key, val);
- else super.put(key, val == null ? NULL_PLACEHOLDER : val);
+ 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];
}
- public JSScope top() {
- JSScope s = this;
- while(s.parentScope != null) s = s.parentScope;
- return s;
+ 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");
+ }
+ }
+ 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");
+ }
}
+ 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(); }
- 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 JS.Immutable {
+ private final static JS NaN = JSU.N(Double.NaN);
+ private final static JS POSITIVE_INFINITY = JSU.N(Double.POSITIVE_INFINITY);
+ private final static JS.Method METHOD = new JS.Method();
- public Global() { super(null); }
- public Object get(Object key) throws JSExn {
- //#switch(key)
+ public Global() { }
+ public JS get(JS key) throws JSExn {
+ //#jsswitch(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) {
+ public JS call(JS method, JS[] args) throws JSExn {
+ switch(args.length) {
case 0: {
- //#switch(method)
+ //#jsswitch(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);
+ char buf[] = new char[args.length];
+ for(int i=0; i<args.length; i++) buf[i] = (char)(JSU.toInt(args[i]) & 0xffff);
+ return JSU.S(new String(buf));
//#end
break;
}
case 1: {
- //#switch(method)
- case "parseInt": return parseInt(a0, N(0));
- 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; }
+ //#jsswitch(method)
+ case "parseInt": return parseInt(args[0], JSU.N(0));
+ case "isNaN": { double d = JSU.toDouble(args[0]); return d == d ? JSU.F : JSU.T; }
+ case "isFinite": { double d = JSU.toDouble(args[0]); return (d==d && !Double.isInfinite(d)) ? JSU.T : JSU.F; }
case "decodeURI": throw new JSExn("unimplemented");
case "decodeURIComponent": throw new JSExn("unimplemented");
case "encodeURI": throw new JSExn("unimplemented");
break;
}
case 2: {
- //#switch(method)
- case "parseInt": return parseInt(a0, a1);
+ //#jsswitch(method)
+ case "parseInt": return parseInt(args[0], args[1]);
//#end
break;
}
}
- return super.callMethod(method, a0, a1, a2, rest, nargs);
+ return super.call(method, args);
}
- private Object parseInt(Object arg, Object r) {
- int radix = JS.toInt(r);
- String s = (String)arg;
+ private JS parseInt(JS arg, JS r) throws JSExn {
+ int radix = JSU.toInt(r);
+ String s = JSU.toString(arg);
int start = 0;
int length = s.length();
int sign = 1;
long n = 0;
- if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
- while(start < length && Character.isWhitespace(s.charAt(start))) start++;
- if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
+ if (radix != 0 && (radix < 2 || radix > 36)) return 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++;
}
radix = 16;
} else {
radix = 8;
- if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
+ if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JSU.ZERO;
}
}
if(radix == 0) radix = 10;
// try the fast way first
try {
String s2 = start == 0 ? s : s.substring(start);
- return JS.N(sign*Integer.parseInt(s2,radix));
+ return JSU.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++) {
n = n*radix + digit;
if(n < 0) return NaN; // overflow;
}
- if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
- return JS.N((long)sign*n);
+ if(n <= Integer.MAX_VALUE) return JSU.N(sign*(int)n);
+ return JSU.N((long)sign*n);
}
- private Object parseFloat(Object arg) {
- String s = (String)arg;
+ private JS parseFloat(JS arg) throws JSExn {
+ String s = JSU.toString(arg);
int start = 0;
int length = s.length();
while(start < length && Character.isWhitespace(s.charAt(0))) start++;
// trailing garbage
while(start < end) {
try {
- return JS.N(s.substring(start,length));
+ return JSU.N(Float.parseFloat(s.substring(start,length)));
} catch(NumberFormatException e) { }
end--;
}
}
}
}
-