-// 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;
/** 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;
- }
+ JS getGlobal() { return parent.getGlobal(); }
- 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 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 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 Global() { }
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;
+ //#jsswitch(key)
+ case "NaN": return 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);
+ case "stringFromCharCode": return METHOD;
+ case "parseInt": return METHOD;
+ case "isNaN": return METHOD;
+ case "isFinite": return METHOD;
+ case "decodeURI": return METHOD;
+ case "decodeURIComponent": return METHOD;
+ case "encodeURI": return METHOD;
+ case "encodeURIComponent": return METHOD;
+ case "escape": return METHOD;
+ case "unescape": return METHOD;
+ case "parseInt": return METHOD;
//#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);
+ public JS call(JS method, JS[] args) throws JSExn {
+ switch(args.length) {
+ case 0: {
+ //#jsswitch(method)
+ case "stringFromCharCode":
+ 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: {
+ //#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");
+ case "encodeURIComponent": throw new JSExn("unimplemented");
+ case "escape": throw new JSExn("unimplemented");
+ case "unescape": throw new JSExn("unimplemented");
+ //#end
+ break;
+ }
+ case 2: {
+ //#jsswitch(method)
+ case "parseInt": return parseInt(args[0], args[1]);
+ //#end
+ break;
+ }
+ }
+ return super.call(method, args);
}
private JS parseInt(JS arg, JS r) throws JSExn {
- int radix = JS.toInt(r);
- String s = JS.toString(arg);
+ 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 JS.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;
- if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
+ if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
// 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++) {
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 < 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 JS parseFloat(JS arg) throws JSExn {
- String s = JS.toString(arg);
+ 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(new Double(s.substring(start,length)));
+ return JSU.N(Float.parseFloat(s.substring(start,length)));
} catch(NumberFormatException e) { }
end--;
}
- return JS.NaN;
+ return NaN;
}
}
}
-