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 /** Implementation of a JavaScript Scope */
6 // HACK: JSScope doesn't really need the BT, this is just for Box.java
7 public class JSScope extends JS.BT {
9 private JSScope parentScope;
11 private static final JS NULL_PLACEHOLDER = new JS() { };
13 public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
14 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
15 public JSScope getParentScope() { return parentScope; }
17 public JS get(JS key) throws JSExn {
18 JS o = super.get(key);
19 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
20 else return parentScope == null ? null : parentScope.get(key);
23 public boolean has(JS key) throws JSExn { return super.get(key) != null; }
24 public void put(JS key, JS val) throws JSExn {
25 if (parentScope != null && !has(key)) parentScope.put(key, val);
26 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
29 public JSScope top() {
31 while(s.parentScope != null) s = s.parentScope;
35 public static class Global extends JSScope {
36 private final static JS NaN = N(Double.NaN);
37 private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
39 public Global() { super(null); }
40 public Global(JSScope p) { super(p); }
42 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
44 // HACK: "this" gets lost on the way back through the scope chain
45 // We'll have to do something better with this when Scope is rewritten
46 public JS get(JS key) throws JSExn {
48 if(ret == METHOD) return new Interpreter.Stub(this,key);
52 public JS _get(JS key) throws JSExn {
53 if(!JS.isString(key)) return super.get(key);
54 //#switch(JS.toString(key))
55 case "NaN": return NaN;
56 case "Infinity": return POSITIVE_INFINITY;
57 case "undefined": return null;
58 case "stringFromCharCode": return METHOD;
59 case "parseInt": return METHOD;
60 case "parseFloat": return METHOD;
61 case "isNaN": return METHOD;
62 case "isFinite": return METHOD;
63 case "decodeURI": return METHOD;
64 case "decodeURIComponent": return METHOD;
65 case "encodeURI": return METHOD;
66 case "encodeURIComponent": return METHOD;
67 case "escape": return METHOD;
68 case "unescape": return METHOD;
70 return super.get(key);
73 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
74 if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
75 //#switch(JS.toString(method))
76 case "parseInt": return parseInt(a0, N(0));
77 case "parseFloat": return parseFloat(a0);
78 case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
79 case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
80 case "decodeURI": throw new JSExn("unimplemented");
81 case "decodeURIComponent": throw new JSExn("unimplemented");
82 case "encodeURI": throw new JSExn("unimplemented");
83 case "encodeURIComponent": throw new JSExn("unimplemented");
84 case "escape": throw new JSExn("unimplemented");
85 case "unescape": throw new JSExn("unimplemented");
86 case "parseInt": return parseInt(a0, a1);
88 return super.callMethod(method, a0, a1, a2, rest, nargs);
91 private JS parseInt(JS arg, JS r) throws JSExn {
92 int radix = JS.toInt(r);
93 String s = JS.toString(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 JS parseFloat(JS arg) throws JSExn {
133 String s = JS.toString(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(new Double(s.substring(start,length)));
143 } catch(NumberFormatException e) { }