1    package org.xwt.js;
2    
3    class GlobalScopeImpl extends JS.Scope {
4        private final static Double NaN = new Double(Double.NaN);
5        private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
6        
7        public GlobalScopeImpl(JS.Scope parent) {
8            super(parent);
9        }
10       public Object get(Object key) {
11           if(key.equals("NaN")) return NaN;
12           if(key.equals("Infinity")) return POSITIVE_INFINITY;
13           if(key.equals("undefined")) return null;
14           return super.get(key);
15       }
16       public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
17           if(method.equals("parseInt")) {
18               if(checkOnly) return Boolean.TRUE;
19               return parseInt(args);
20           } else if(method.equals("parseFloat")) {
21               if(checkOnly) return Boolean.TRUE;
22               return parseFloat(args);
23           } else if(method.equals("isNaN")) {
24               if(checkOnly) return Boolean.TRUE;
25               return isNaN(args);
26           } else if(method.equals("isFinite")) {
27               if(checkOnly) return Boolean.TRUE;
28               return isFinite(args);
29           } else if(method.equals("decodeURI")) {
30               if(checkOnly) return Boolean.TRUE;
31               return decodeURI(args);
32           } else if(method.equals("decodeURIComponent")) {
33               if(checkOnly) return Boolean.TRUE;
34               return decodeURIComponent(args);
35           } else if(method.equals("encodeURI")) {
36               if(checkOnly) return Boolean.TRUE;
37               return encodeURI(args);
38           } else if(method.equals("encodeURIComponent")) {
39               if(checkOnly) return Boolean.TRUE;
40               return encodeURIComponent(args);
41           } else if(method.equals("escape")) {
42               if(checkOnly) return Boolean.TRUE;
43               return escape(args);
44           } else if(method.equals("unescape")) {
45               if(checkOnly) return Boolean.TRUE;
46               return unescape(args);
47           } else if(method.equals("stringFromCharCode")) {
48               if(checkOnly) return Boolean.TRUE;
49               return stringFromCharCode(args);
50           }
51           return super.callMethod(method,args,checkOnly);
52       }
53       private Object stringFromCharCode(JS.Array args) {
54           char buf[] = new char[args.length()];
55           for(int i=0;i<args.length();i++)
56               buf[i] = (char)(JS.toInt(args.elementAt(i)) & 0xffff);
57           return new String(buf);
58       }
59       private Object parseInt(JS.Array args) {
60           String s = args.length() > 0 ? args.elementAt(0).toString() : "";
61           int radix = args.length() > 1 ? toInt(args.elementAt(1)) : 0;
62           int start = 0;
63           int length = s.length();
64           int sign = 1;
65           long n = 0;
66           if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
67           while(start < length && Character.isWhitespace(s.charAt(start))) start++;
68           if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
69               sign = s.charAt(start) == '+' ? 1 : -1;
70               start++;
71           }
72           if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
73               start++;
74               if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
75                   start++;
76                   radix = 16;
77               } else {
78                   radix = 8;
79                   if(length == start || Character.digit(s.charAt(start+1),8)==-1) return new Integer(0);
80               }
81           }
82           if(radix == 0) radix = 10;
83           if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
84           // try the fast way first
85           try {
86               String s2 = start == 0 ? s : s.substring(start);
87               return new Integer(sign*Integer.parseInt(s2,radix));
88           } catch(NumberFormatException e) { }
89           // fall through to a slower but emca-compliant method
90           for(int i=start;i<length;i++) {
91               int digit = Character.digit(s.charAt(i),radix);
92               if(digit < 0) break;
93               n = n*radix + digit;
94               if(n < 0) return NaN; // overflow;
95           }
96           if(n <= Integer.MAX_VALUE) return new Integer(sign*(int)n);
97           return new Long((long)sign*n);
98       }
99       private Object parseFloat(JS.Array args) {
100          String s = args.length() > 0 ? args.elementAt(0).toString() : "";
101          int start = 0;
102          int length = s.length();
103          while(start < length && Character.isWhitespace(s.charAt(0))) start++;
104          int end = length;
105          // as long as the string has no trailing garbage,this is fast, its slow with
106          // trailing garbage
107          while(start < end) {
108              try {
109                  return new Double(s.substring(start,length));
110              } catch(NumberFormatException e) { }
111              end--;
112          }
113          return NaN;
114      }
115      private Object isNaN(JS.Array args) {
116          double d = args.length() > 0 ? toDouble(args.elementAt(0)) : Double.NaN;
117          return d == d ? Boolean.FALSE : Boolean.TRUE;
118      }
119      private Object isFinite(JS.Array args) {
120          double d = args.length() > 0 ? toDouble(args.elementAt(0)) : Double.NaN;
121          return (d == d && !Double.isInfinite(d)) ? Boolean.TRUE : Boolean.FALSE;
122      }
123      private Object decodeURI(JS.Array args) {
124          throw new JS.Exn("decodeURI is unimplemented");
125      }
126      private Object decodeURIComponent(JS.Array args) {
127          throw new JS.Exn("decodeURIComponent is unimplemented");
128      }
129      private Object encodeURI(JS.Array args) {
130          throw new JS.Exn("encodeURI is unimplemented");
131      }
132      private Object encodeURIComponent(JS.Array args) {
133          throw new JS.Exn("encodeURIComponent is unimplemented");
134      }
135      private Object escape(JS.Array args) {
136          throw new JS.Exn("escape is unimplemented");
137      }
138      private Object unescape(JS.Array args) {
139          throw new JS.Exn("unescape is unimplemented");
140      }
141  }
142