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