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