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