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 JS NULL_PLACEHOLDER = new JS() { };
14 public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
15 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
16 public JSScope getParentScope() { return parentScope; }
18 public JS get(JS key) throws JSExn {
19 JS 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(JS key) throws JSExn { return super.get(key) != null; }
25 public void put(JS key, JS 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 JS NaN = N(Double.NaN);
38 private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
40 public Global() { super(null); }
41 public Global(JSScope p) { super(p); }
43 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
45 // HACK: "this" gets lost on the way back through the scope chain
46 // We'll have to do something better with this when Scope is rewritten
47 public JS get(JS key) throws JSExn {
49 if(ret == METHOD) return new Interpreter.Stub(this,key);
53 public JS _get(JS key) throws JSExn {
54 if(!JS.isString(key)) return super.get(key);
55 //#switch(JS.toString(key))
56 case "NaN": return NaN;
57 case "Infinity": return POSITIVE_INFINITY;
58 case "undefined": return null;
59 case "stringFromCharCode": return METHOD;
60 case "parseInt": return METHOD;
61 case "parseFloat": return METHOD;
62 case "isNaN": return METHOD;
63 case "isFinite": return METHOD;
64 case "decodeURI": return METHOD;
65 case "decodeURIComponent": return METHOD;
66 case "encodeURI": return METHOD;
67 case "encodeURIComponent": return METHOD;
68 case "escape": return METHOD;
69 case "unescape": return METHOD;
71 return super.get(key);
74 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
75 if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
76 //#switch(JS.toString(method))
77 case "parseInt": return parseInt(a0, N(0));
78 case "parseFloat": return parseFloat(a0);
79 case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
80 case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
81 case "decodeURI": throw new JSExn("unimplemented");
82 case "decodeURIComponent": throw new JSExn("unimplemented");
83 case "encodeURI": throw new JSExn("unimplemented");
84 case "encodeURIComponent": throw new JSExn("unimplemented");
85 case "escape": throw new JSExn("unimplemented");
86 case "unescape": throw new JSExn("unimplemented");
87 case "parseInt": return parseInt(a0, a1);
89 return super.callMethod(method, a0, a1, a2, rest, nargs);
92 private JS parseInt(JS arg, JS r) throws JSExn {
93 int radix = JS.toInt(r);
94 String s = JS.toString(arg);
96 int length = s.length();
99 if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
100 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
101 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
102 sign = s.charAt(start) == '+' ? 1 : -1;
105 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
107 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
112 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
115 if(radix == 0) radix = 10;
116 if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
117 // try the fast way first
119 String s2 = start == 0 ? s : s.substring(start);
120 return JS.N(sign*Integer.parseInt(s2,radix));
121 } catch(NumberFormatException e) { }
122 // fall through to a slower but emca-compliant method
123 for(int i=start;i<length;i++) {
124 int digit = Character.digit(s.charAt(i),radix);
127 if(n < 0) return NaN; // overflow;
129 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
130 return JS.N((long)sign*n);
133 private JS parseFloat(JS arg) throws JSExn {
134 String s = JS.toString(arg);
136 int length = s.length();
137 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
139 // as long as the string has no trailing garbage,this is fast, its slow with
143 return JS.N(new Double(s.substring(start,length)));
144 } catch(NumberFormatException e) { }