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