f7d3d6de93514cdb30a162c285cf6a4bc0724e0e
[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             if(!JS.isString(key)) return super.get(key);
54             //#switch(JS.toString(key))
55             case "NaN": return NaN;
56             case "Infinity": return POSITIVE_INFINITY;
57             case "undefined": return null;
58             case "stringFromCharCode": return METHOD;
59             case "parseInt": return METHOD;
60             case "parseFloat": return METHOD;
61             case "isNaN": return METHOD;
62             case "isFinite": return METHOD;
63             case "decodeURI": return METHOD;
64             case "decodeURIComponent": return METHOD;
65             case "encodeURI": return METHOD;
66             case "encodeURIComponent": return METHOD;
67             case "escape": return METHOD;
68             case "unescape": return METHOD;
69             //#end
70             return super.get(key);
71         }
72
73         public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
74             if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
75             //#switch(JS.toString(method))
76             case "parseInt": return parseInt(a0, N(0));
77             case "parseFloat": return parseFloat(a0);
78             case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
79             case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
80             case "decodeURI": throw new JSExn("unimplemented");
81             case "decodeURIComponent": throw new JSExn("unimplemented");
82             case "encodeURI": throw new JSExn("unimplemented");
83             case "encodeURIComponent": throw new JSExn("unimplemented");
84             case "escape": throw new JSExn("unimplemented");
85             case "unescape": throw new JSExn("unimplemented");
86             case "parseInt": return parseInt(a0, a1);
87             //#end
88             return super.callMethod(method, a0, a1, a2, rest, nargs);
89         }
90
91         private JS parseInt(JS arg, JS r) throws JSExn {
92             int radix = JS.toInt(r);
93             String s = JS.toString(arg);
94             int start = 0;
95             int length = s.length();
96             int sign = 1;
97             long n = 0;
98             if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
99             while(start < length && Character.isWhitespace(s.charAt(start))) start++;
100             if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
101                 sign = s.charAt(start) == '+' ? 1 : -1;
102                 start++;
103             }
104             if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
105                 start++;
106                 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
107                     start++;
108                     radix = 16;
109                 } else {
110                     radix = 8;
111                     if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
112                 }
113             }
114             if(radix == 0) radix = 10;
115             if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
116             // try the fast way first
117             try {
118                 String s2 = start == 0 ? s : s.substring(start);
119                 return JS.N(sign*Integer.parseInt(s2,radix));
120             } catch(NumberFormatException e) { }
121             // fall through to a slower but emca-compliant method
122             for(int i=start;i<length;i++) {
123                 int digit = Character.digit(s.charAt(i),radix);
124                 if(digit < 0) break;
125                 n = n*radix + digit;
126                 if(n < 0) return NaN; // overflow;
127             }
128             if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
129             return JS.N((long)sign*n);
130         }
131
132         private JS parseFloat(JS arg) throws JSExn {
133             String s = JS.toString(arg);
134             int start = 0;
135             int length = s.length();
136             while(start < length && Character.isWhitespace(s.charAt(0))) start++;
137             int end = length;
138             // as long as the string has no trailing garbage,this is fast, its slow with
139             // trailing garbage
140             while(start < end) {
141                 try {
142                     return JS.N(new Double(s.substring(start,length)));
143                 } catch(NumberFormatException e) { }
144                 end--;
145             }
146             return NaN;
147         }
148     }
149 }
150