3e797f3d4e3808923c0969c964b517e5d52f0819
[org.ibex.core.git] / src / org / xwt / js / JSScope.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.xwt.js; 
3
4 import org.xwt.util.*; 
5 import java.io.*;
6 import java.util.*;
7
8 /** Implementation of a JavaScript Scope */
9 public class JSScope extends JS { 
10
11     private JSScope parentScope;
12
13     private static final Object NULL_PLACEHOLDER = new Object();
14
15     public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
16     public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
17     public JSScope getParentScope() { return parentScope; }
18
19     public Object get(Object key) throws JSExn {
20         Object o = super.get(key);
21         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
22         else return parentScope == null ? null : parentScope.get(key);
23     }
24
25     public boolean has(Object key) throws JSExn { return super.get(key) != null; }
26     public void put(Object key, Object val) throws JSExn {
27         if (parentScope != null && !has(key)) parentScope.put(key, val);
28         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
29     }
30
31     public static class Global extends JSScope {
32         private final static Double NaN = new Double(Double.NaN);
33         private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
34
35         public Global() { super(null); }
36         public Object get(Object key) throws JSExn {
37             //#switch(key)
38             case "NaN": return NaN;
39             case "Infinity": return POSITIVE_INFINITY;
40             case "undefined": return null;
41             case "stringFromCharCode": return METHOD;
42             case "parseInt": return METHOD;
43             case "isNaN": return METHOD;
44             case "isFinite": return METHOD;
45             case "decodeURI": return METHOD;
46             case "decodeURIComponent": return METHOD;
47             case "encodeURI": return METHOD;
48             case "encodeURIComponent": return METHOD;
49             case "escape": return METHOD;
50             case "unescape": return METHOD;
51             case "parseInt": return METHOD;
52             //#end
53             return super.get(key);
54         }
55
56         public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
57             switch(nargs) {
58                 case 0: {
59                     //#switch(method)
60                     case "stringFromCharCode":
61                         char buf[] = new char[nargs];
62                         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);
63                         return new String(buf);
64                     //#end
65                     break;
66                 }
67                 case 1: {
68                     //#switch(method)
69                     case "parseInt": return parseInt(a0, N(0));
70                     case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
71                     case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
72                     case "decodeURI": throw new JSExn("unimplemented");
73                     case "decodeURIComponent": throw new JSExn("unimplemented");
74                     case "encodeURI": throw new JSExn("unimplemented");
75                     case "encodeURIComponent": throw new JSExn("unimplemented");
76                     case "escape": throw new JSExn("unimplemented");
77                     case "unescape": throw new JSExn("unimplemented");
78                     //#end
79                     break;
80                 }
81                 case 2: {
82                     //#switch(method)
83                     case "parseInt": return parseInt(a0, a1);
84                     //#end
85                     break;
86                 }
87             }
88             return super.callMethod(method, a0, a1, a2, rest, nargs);
89         }
90
91         private Object parseInt(Object arg, Object r) {
92             int radix = JS.toInt(r);
93             String s = (String)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 Object parseFloat(Object arg) {
133             String s = (String)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(s.substring(start,length));
143                 } catch(NumberFormatException e) { }
144                 end--;
145             }
146             return NaN;
147         }
148     }
149 }
150