licensing update to APSL 2.0
[org.ibex.js.git] / src / org / ibex / js / JSScope.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.js; 
6
7 /** Implementation of a JavaScript Scope */
8 class JSScope extends JS.O {
9
10     private final int base;
11     private final JS[] vars;
12     final JSScope parent;
13
14     public static class Top extends JSScope {
15         private final JS global;
16         public Top(JS global) { super(null,0,0); this.global = global; }
17         JS get(int i) throws JSExn { throw new JSExn("scope index out of range"); }
18         void put(int i, JS o) throws JSExn { throw new JSExn("scope index out of range"); }
19         JS getGlobal() { return global; }
20     };
21         
22     // NOTE: We can't just set base to parent.base + parent.vars.length
23     // sometimes we only access part of a parent's scope
24     public JSScope(JSScope parent, int base, int size) {
25         this.parent = parent;
26         this.base = base;
27         this.vars = new JS[size];
28     }
29     
30     JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
31     void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
32     
33     JS getGlobal() { return parent.getGlobal(); }
34     
35     private JSScope parentScope;
36
37     private static final JS NULL_PLACEHOLDER = new JS() { };
38
39     public JSScope(JSScope parentScope) { this(parentScope, 0, 0); }
40     public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
41     public JSScope getParentScope() { return parentScope; }
42
43     public JS get(JS key) throws JSExn {
44         if (key instanceof JSNumber) try {
45             return get(JS.toInt(key));
46         } catch(ArrayIndexOutOfBoundsException e) { 
47             throw new JSExn("scope index out of range");
48         }
49         JS o = super.get(key);
50         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
51         else return parentScope == null ? null : parentScope.get(key);
52     }
53
54     public boolean has(JS key) throws JSExn { return super.get(key) != null; }
55     public void put(JS key, JS val) throws JSExn {
56         if (key instanceof JSNumber) try {
57             put(JS.toInt(key),val);
58         } catch(ArrayIndexOutOfBoundsException e) { 
59             throw new JSExn("scope index out of range");
60         }
61         if (parentScope != null && !has(key)) parentScope.put(key, val);
62         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
63     }
64     
65     public JSScope top() { 
66         JSScope s = this;
67         while(s.parentScope != null) s = s.parentScope;
68         return s;
69     }
70
71     public static class Global extends JSScope {
72         private final static JS NaN = JS.N(Double.NaN);
73         private final static JS POSITIVE_INFINITY = JS.N(Double.POSITIVE_INFINITY);
74
75         public Global() { super(null); }
76         public Global(JSScope p) { super(p); }
77         
78         public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
79         
80         // HACK: "this" gets lost on the way back through the scope chain
81         // We'll have to do something better with this when Scope is rewritten
82         public JS get(JS key) throws JSExn {
83             JS ret = _get(key);
84             if(ret == JS.METHOD) return new Interpreter.Stub(this,key);
85             return ret;
86         }
87         
88         public JS _get(JS key) throws JSExn {
89             //#switch(JS.toString(key))
90             case "JS.NaN": return JS.NaN;
91             case "Infinity": return POSITIVE_INFINITY;
92             case "undefined": return null;
93             case "stringFromCharCode": return JS.METHOD;
94             case "parseInt": return JS.METHOD;
95             case "parseFloat": return JS.METHOD;
96             case "isJS.NaN": return JS.METHOD;
97             case "isFinite": return JS.METHOD;
98             case "decodeURI": return JS.METHOD;
99             case "decodeURIComponent": return JS.METHOD;
100             case "encodeURI": return JS.METHOD;
101             case "encodeURIComponent": return JS.METHOD;
102             case "escape": return JS.METHOD;
103             case "unescape": return JS.METHOD;
104             default: return super.get(key);
105             //#end
106             return super.get(key);
107         }
108
109         public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
110             //#switch(JS.toString(method))
111             case "parseInt": return parseInt(a0, N(0));
112             case "parseFloat": return parseFloat(a0);
113             case "isJS.NaN": { double d = JS.toDouble(a0); return d == d ? JS.F : JS.T; }
114             case "isFinite": { double d = JS.toDouble(a0); return (d == d && !Double.isInfinite(d)) ? JS.T : JS.F; }
115             case "decodeURI": throw new JSExn("unimplemented");
116             case "decodeURIComponent": throw new JSExn("unimplemented");
117             case "encodeURI": throw new JSExn("unimplemented");
118             case "encodeURIComponent": throw new JSExn("unimplemented");
119             case "escape": throw new JSExn("unimplemented");
120             case "unescape": throw new JSExn("unimplemented");
121             case "parseInt": return parseInt(a0, a1);
122             default: return super.callMethod(method, a0, a1, a2, rest, nargs);
123             //#end
124             return super.callMethod(method,a0,a1,a2,rest,nargs);
125         }
126
127         private JS parseInt(JS arg, JS r) throws JSExn {
128             int radix = JS.toInt(r);
129             String s = JS.toString(arg);
130             int start = 0;
131             int length = s.length();
132             int sign = 1;
133             long n = 0;
134             if(radix != 0 && (radix < 2 || radix > 36)) return JS.NaN;
135             while(start < length && Character.isWhitespace(s.charAt(start))) start++;
136             if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
137                 sign = s.charAt(start) == '+' ? 1 : -1;
138                 start++;
139             }
140             if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
141                 start++;
142                 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
143                     start++;
144                     radix = 16;
145                 } else {
146                     radix = 8;
147                     if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
148                 }
149             }
150             if(radix == 0) radix = 10;
151             if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
152             // try the fast way first
153             try {
154                 String s2 = start == 0 ? s : s.substring(start);
155                 return JS.N(sign*Integer.parseInt(s2,radix));
156             } catch(NumberFormatException e) { }
157             // fall through to a slower but emca-compliant method
158             for(int i=start;i<length;i++) {
159                 int digit = Character.digit(s.charAt(i),radix);
160                 if(digit < 0) break;
161                 n = n*radix + digit;
162                 if(n < 0) return JS.NaN; // overflow;
163             }
164             if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
165             return JS.N((long)sign*n);
166         }
167
168         private JS parseFloat(JS arg) throws JSExn {
169             String s = JS.toString(arg);
170             int start = 0;
171             int length = s.length();
172             while(start < length && Character.isWhitespace(s.charAt(0))) start++;
173             int end = length;
174             // as long as the string has no trailing garbage,this is fast, its slow with
175             // trailing garbage
176             while(start < end) {
177                 try {
178                     return JS.N(new Double(s.substring(start,length)));
179                 } catch(NumberFormatException e) { }
180                 end--;
181             }
182             return JS.NaN;
183         }
184     }
185 }
186