1 // Copyright 2003 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) { super.put(s, NULL_PLACEHOLDER); }
17 public JSScope getParentScope() { return parentScope; }
19 public Object get(Object key) {
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) { return super.get(key) != null; }
26 public void put(Object key, Object val) {
27 if (parentScope != null && !has(key)) parentScope.put(key, val);
28 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
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);
35 public Global() { super(null); }
36 public Object get(Object 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;
53 return super.get(key);
56 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
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);
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");
83 case "parseInt": return parseInt(a0, a1);
88 return super.callMethod(method, a0, a1, a2, rest, nargs);
91 private Object parseInt(Object arg, Object r) {
92 int radix = JS.toInt(r);
93 String s = (String)arg;
95 int length = s.length();
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;
104 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
106 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
111 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
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
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);
126 if(n < 0) return NaN; // overflow;
128 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
129 return JS.N((long)sign*n);
132 private Object parseFloat(Object arg) {
133 String s = (String)arg;
135 int length = s.length();
136 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
138 // as long as the string has no trailing garbage,this is fast, its slow with
142 return JS.N(s.substring(start,length));
143 } catch(NumberFormatException e) { }