1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
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 {
10 private JSScope parentScope;
12 private static final Object NULL_PLACEHOLDER = new Object();
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; }
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);
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);
30 public JSScope top() {
32 while(s.parentScope != null) s = s.parentScope;
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);
40 public Global() { super(null); }
41 public Object get(Object key) throws JSExn {
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;
58 return super.get(key);
61 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
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);
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");
89 case "parseInt": return parseInt(a0, a1);
94 return super.callMethod(method, a0, a1, a2, rest, nargs);
97 private Object parseInt(Object arg, Object r) throws JSExn {
98 int radix = JS.toInt(r);
99 String s = JS.toString(arg);
101 int length = s.length();
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;
110 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
112 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
117 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
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
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);
132 if(n < 0) return NaN; // overflow;
134 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
135 return JS.N((long)sign*n);
138 private Object parseFloat(Object arg) throws JSExn {
139 String s = JS.toString(arg);
141 int length = s.length();
142 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
144 // as long as the string has no trailing garbage,this is fast, its slow with
148 return JS.N(s.substring(start,length));
149 } catch(NumberFormatException e) { }