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