--- /dev/null
+package org.xwt.js;
+
+class GlobalScopeImpl extends JS.Scope {
+ private final static Double NaN = new Double(Double.NaN);
+ private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
+
+ public GlobalScopeImpl(JS.Scope parent) {
+ super(parent);
+ }
+ public Object get(Object key) {
+ if(key.equals("NaN")) return NaN;
+ if(key.equals("Infinity")) return POSITIVE_INFINITY;
+ if(key.equals("undefined")) return null;
+ return super.get(key);
+ }
+ public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
+ if(method.equals("parseInt")) {
+ if(checkOnly) return Boolean.TRUE;
+ return parseInt(args);
+ } else if(method.equals("parseFloat")) {
+ if(checkOnly) return Boolean.TRUE;
+ return parseFloat(args);
+ } else if(method.equals("isNaN")) {
+ if(checkOnly) return Boolean.TRUE;
+ return isNaN(args);
+ } else if(method.equals("isFinite")) {
+ if(checkOnly) return Boolean.TRUE;
+ return isFinite(args);
+ } else if(method.equals("decodeURI")) {
+ if(checkOnly) return Boolean.TRUE;
+ return decodeURI(args);
+ } else if(method.equals("decodeURIComponent")) {
+ if(checkOnly) return Boolean.TRUE;
+ return decodeURIComponent(args);
+ } else if(method.equals("encodeURI")) {
+ if(checkOnly) return Boolean.TRUE;
+ return encodeURI(args);
+ } else if(method.equals("encodeURIComponent")) {
+ if(checkOnly) return Boolean.TRUE;
+ return encodeURIComponent(args);
+ }
+ return super.callMethod(method,args,checkOnly);
+ }
+ private Object parseInt(JS.Array args) {
+ String s = args.length() > 0 ? args.elementAt(0).toString() : "";
+ int radix = args.length() > 1 ? toInt(args.elementAt(1)) : 0;
+ 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) == '-')) {
+ 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 new Integer(0);
+ }
+ }
+ if(radix == 0) radix = 10;
+ 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 new Integer(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 NaN; // overflow;
+ }
+ if(n <= Integer.MAX_VALUE) return new Integer(sign*(int)n);
+ return new Long((long)sign*n);
+ }
+ private Object parseFloat(JS.Array args) {
+ String s = args.length() > 0 ? args.elementAt(0).toString() : "";
+ 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 new Double(s.substring(start,length));
+ } catch(NumberFormatException e) { }
+ end--;
+ }
+ return NaN;
+ }
+ private Object isNaN(JS.Array args) {
+ double d = args.length() > 0 ? toDouble(args.elementAt(0)) : Double.NaN;
+ return d == d ? Boolean.FALSE : Boolean.TRUE;
+ }
+ private Object isFinite(JS.Array args) {
+ double d = args.length() > 0 ? toDouble(args.elementAt(0)) : Double.NaN;
+ return (d == d && !Double.isInfinite(d)) ? Boolean.TRUE : Boolean.FALSE;
+ }
+ private Object decodeURI(JS.Array args) {
+ throw new JS.Exn("decodeURI is unimplemented");
+ }
+ private Object decodeURIComponent(JS.Array args) {
+ throw new JS.Exn("decodeURIComponent is unimplemented");
+ }
+ private Object encodeURI(JS.Array args) {
+ throw new JS.Exn("encodeURI is unimplemented");
+ }
+ private Object encodeURIComponent(JS.Array args) {
+ throw new JS.Exn("encodeURIComponent is unimplemented");
+ }
+}