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