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