1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** Implementation of a JavaScript JSScope */
9 public class JSScope extends JSCallable {
10 private JSScope parentJSScope;
11 private static final Object NULL_PLACEHOLDER = new Object();
12 public JSScope(JSScope parentJSScope) {
13 if (parentJSScope == this) throw new Error("can't make a scope its own parent!");
14 this.parentJSScope = parentJSScope;
17 public boolean isTransparent() { return false; }
18 public void declare(String s) { super.put(s, NULL_PLACEHOLDER); }
19 public JSScope getParentJSScope() { return parentJSScope; }
20 public boolean has(Object key) { return super.get(key) != null; }
22 public Object get(Object key) {
23 Object o = super.get(key);
24 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
25 else return parentJSScope == null ? null : parentJSScope.get(key);
28 public void put(Object key, Object val) {
29 if (parentJSScope != null && !has(key)) parentJSScope.put(key, val);
30 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
33 public static class Global extends JSScope {
34 private final static Double NaN = new Double(Double.NaN);
35 private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
37 public Global(JSScope parent) {
40 public Object get(Object key) {
41 if(key.equals("NaN")) return NaN;
42 if(key.equals("Infinity")) return POSITIVE_INFINITY;
43 if(key.equals("undefined")) return null;
44 return super.get(key);
47 public Object call1(Object method, Object arg0) {
49 case "parseInt": return parseInt(arg0, 0);
50 case "isNaN": { double d = toDouble(arg0); return d == d ? F : T; }
51 case "isFinite": { double d = toDouble(arg0); return (d == d && !Double.isFinite(d)) ? T : F; }
52 case "decodeURI": throw new JS.Exn("unimplemented");
53 case "decodeURIComponent": throw new JS.Exn("unimplemented");
54 case "encodeURI": throw new JS.Exn("unimplemented");
55 case "encodeURIComponent": throw new JS.Exn("unimplemented");
56 case "escape": throw new JS.Exn("unimplemented");
57 case "unescape": throw new JS.Exn("unimplemented");
58 case "stringFromCharCode": return stringFromCharCode(arg0);
63 public Object call2(Object method, Object arg1, Object arg2) {
64 if (method.equals("parseInt")) return parseInt(arg1, arg2);
68 private Object stringFromCharCode(Object arg) {
69 char buf[] = new char[args.length()];
70 for(int i=0;i<args.length();i++) buf[i] = (char)(JS.toInt(arg) & 0xffff);
71 return new String(buf);
74 private Object parseInt(Object arg, int radix) {
75 String s = (String)arg;
77 int length = s.length();
80 if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
81 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
82 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
83 sign = s.charAt(start) == '+' ? 1 : -1;
86 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
88 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
93 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return new Integer(0);
96 if(radix == 0) radix = 10;
97 if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
98 // try the fast way first
100 String s2 = start == 0 ? s : s.substring(start);
101 return new Integer(sign*Integer.parseInt(s2,radix));
102 } catch(NumberFormatException e) { }
103 // fall through to a slower but emca-compliant method
104 for(int i=start;i<length;i++) {
105 int digit = Character.digit(s.charAt(i),radix);
108 if(n < 0) return NaN; // overflow;
110 if(n <= Integer.MAX_VALUE) return new Integer(sign*(int)n);
111 return new Long((long)sign*n);
114 private Object parseFloat(Object arg) {
115 String s = (String)arg;
117 int length = s.length();
118 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
120 // as long as the string has no trailing garbage,this is fast, its slow with
124 return new Double(s.substring(start,length));
125 } catch(NumberFormatException e) { }