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