1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** Implementation of a JavaScript Scope */
9 public class JSScope extends JS {
11 private JSScope parentScope;
13 private static final Object NULL_PLACEHOLDER = new Object();
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; }
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);
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);
31 public JSScope top() {
33 while(s.parentScope != null) s = s.parentScope;
37 public static class Global extends JSScope {
38 private final static Double NaN = new Double(Double.NaN);
39 private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
41 public Global() { super(null); }
42 public Object get(Object key) throws JSExn {
44 case "NaN": return NaN;
45 case "Infinity": return POSITIVE_INFINITY;
46 case "undefined": return null;
47 case "stringFromCharCode": return METHOD;
48 case "parseInt": 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 case "parseInt": return METHOD;
59 return super.get(key);
62 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
66 case "stringFromCharCode":
67 char buf[] = new char[nargs];
68 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);
69 return new String(buf);
75 case "parseInt": return parseInt(a0, N(0));
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) {
98 int radix = JS.toInt(r);
99 String s = (String)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) {
139 String s = (String)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) { }