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 {
54 case "NaN": return NaN;
55 case "Infinity": return POSITIVE_INFINITY;
56 case "undefined": return null;
57 case "stringFromCharCode": return METHOD;
58 case "parseInt": return METHOD;
59 case "parseFloat": return METHOD;
60 case "isNaN": return METHOD;
61 case "isFinite": return METHOD;
62 case "decodeURI": return METHOD;
63 case "decodeURIComponent": return METHOD;
64 case "encodeURI": return METHOD;
65 case "encodeURIComponent": return METHOD;
66 case "escape": return METHOD;
67 case "unescape": return METHOD;
69 return super.get(key);
72 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
74 case "parseInt": return parseInt(a0, N(0));
75 case "parseFloat": return parseFloat(a0);
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");
84 case "parseInt": return parseInt(a0, a1);
86 return super.callMethod(method, a0, a1, a2, rest, nargs);
89 private JS parseInt(JS arg, JS r) throws JSExn {
90 int radix = JS.toInt(r);
91 String s = JS.toString(arg);
93 int length = s.length();
96 if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
97 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
98 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
99 sign = s.charAt(start) == '+' ? 1 : -1;
102 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
104 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
109 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
112 if(radix == 0) radix = 10;
113 if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
114 // try the fast way first
116 String s2 = start == 0 ? s : s.substring(start);
117 return JS.N(sign*Integer.parseInt(s2,radix));
118 } catch(NumberFormatException e) { }
119 // fall through to a slower but emca-compliant method
120 for(int i=start;i<length;i++) {
121 int digit = Character.digit(s.charAt(i),radix);
124 if(n < 0) return NaN; // overflow;
126 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
127 return JS.N((long)sign*n);
130 private JS parseFloat(JS arg) throws JSExn {
131 String s = JS.toString(arg);
133 int length = s.length();
134 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
136 // as long as the string has no trailing garbage,this is fast, its slow with
140 return JS.N(new Double(s.substring(start,length)));
141 } catch(NumberFormatException e) { }