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