2004/01/07 20:37:33
[org.ibex.core.git] / src / org / xwt / js / JSScope.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.xwt.js; 
3
4 import org.xwt.util.*; 
5 import java.io.*;
6 import java.util.*;
7
8 /** Implementation of a JavaScript Scope */
9 public class JSScope extends JS { 
10
11     private JSScope parentScope;
12
13     private static final Object NULL_PLACEHOLDER = new Object();
14
15     public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
16     public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
17     public JSScope getParentScope() { return parentScope; }
18
19     public Object get(Object key) throws JSExn {
20         Object o = super.get(key);
21         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
22         else return parentScope == null ? null : parentScope.get(key);
23     }
24
25     public boolean has(Object key) throws JSExn { return super.get(key) != null; }
26     public void put(Object key, Object val) throws JSExn {
27         if (parentScope != null && !has(key)) parentScope.put(key, val);
28         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
29     }
30     
31     public JSScope top() { 
32         JSScope s = this;
33         while(s.parentScope != null) s = s.parentScope;
34         return s;
35     }
36
37     public static class Global extends JSScope {
38         private final static Double NaN = new Double(Double.NaN);
39         private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
40
41         public Global() { super(null); }
42         public Object get(Object key) throws JSExn {
43             //#switch(key)
44             case "NaN": return NaN;
45             case "Infinity": return POSITIVE_INFINITY;
46             case "undefined": return null;
47             case "stringFromCharCode": return METHOD;
48             case "parseInt": return METHOD;
49             case "isNaN": return METHOD;
50             case "isFinite": return METHOD;
51             case "decodeURI": return METHOD;
52             case "decodeURIComponent": return METHOD;
53             case "encodeURI": return METHOD;
54             case "encodeURIComponent": return METHOD;
55             case "escape": return METHOD;
56             case "unescape": return METHOD;
57             case "parseInt": return METHOD;
58             //#end
59             return super.get(key);
60         }
61
62         public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
63             switch(nargs) {
64                 case 0: {
65                     //#switch(method)
66                     case "stringFromCharCode":
67                         char buf[] = new char[nargs];
68                         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);
69                         return new String(buf);
70                     //#end
71                     break;
72                 }
73                 case 1: {
74                     //#switch(method)
75                     case "parseInt": return parseInt(a0, N(0));
76                     case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
77                     case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
78                     case "decodeURI": throw new JSExn("unimplemented");
79                     case "decodeURIComponent": throw new JSExn("unimplemented");
80                     case "encodeURI": throw new JSExn("unimplemented");
81                     case "encodeURIComponent": throw new JSExn("unimplemented");
82                     case "escape": throw new JSExn("unimplemented");
83                     case "unescape": throw new JSExn("unimplemented");
84                     //#end
85                     break;
86                 }
87                 case 2: {
88                     //#switch(method)
89                     case "parseInt": return parseInt(a0, a1);
90                     //#end
91                     break;
92                 }
93             }
94             return super.callMethod(method, a0, a1, a2, rest, nargs);
95         }
96
97         private Object parseInt(Object arg, Object r) {
98             int radix = JS.toInt(r);
99             String s = (String)arg;
100             int start = 0;
101             int length = s.length();
102             int sign = 1;
103             long n = 0;
104             if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
105             while(start < length && Character.isWhitespace(s.charAt(start))) start++;
106             if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
107                 sign = s.charAt(start) == '+' ? 1 : -1;
108                 start++;
109             }
110             if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
111                 start++;
112                 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
113                     start++;
114                     radix = 16;
115                 } else {
116                     radix = 8;
117                     if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
118                 }
119             }
120             if(radix == 0) radix = 10;
121             if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
122             // try the fast way first
123             try {
124                 String s2 = start == 0 ? s : s.substring(start);
125                 return JS.N(sign*Integer.parseInt(s2,radix));
126             } catch(NumberFormatException e) { }
127             // fall through to a slower but emca-compliant method
128             for(int i=start;i<length;i++) {
129                 int digit = Character.digit(s.charAt(i),radix);
130                 if(digit < 0) break;
131                 n = n*radix + digit;
132                 if(n < 0) return NaN; // overflow;
133             }
134             if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
135             return JS.N((long)sign*n);
136         }
137
138         private Object parseFloat(Object arg) {
139             String s = (String)arg;
140             int start = 0;
141             int length = s.length();
142             while(start < length && Character.isWhitespace(s.charAt(0))) start++;
143             int end = length;
144             // as long as the string has no trailing garbage,this is fast, its slow with
145             // trailing garbage
146             while(start < end) {
147                 try {
148                     return JS.N(s.substring(start,length));
149                 } catch(NumberFormatException e) { }
150                 end--;
151             }
152             return NaN;
153         }
154     }
155 }
156