1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 /** Implementation of a JavaScript Scope */
8 class JSScope extends JS.O {
10 private final int base;
11 private final JS[] vars;
14 public static class Top extends JSScope {
15 private final JS global;
16 public Top(JS global) { super(null,0,0); this.global = global; }
17 JS get(int i) throws JSExn { throw new JSExn("scope index out of range"); }
18 void put(int i, JS o) throws JSExn { throw new JSExn("scope index out of range"); }
19 JS getGlobal() { return global; }
22 // NOTE: We can't just set base to parent.base + parent.vars.length
23 // sometimes we only access part of a parent's scope
24 public JSScope(JSScope parent, int base, int size) {
27 this.vars = new JS[size];
30 JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
31 void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
33 JS getGlobal() { return parent.getGlobal(); }
35 private JSScope parentScope;
37 private static final JS NULL_PLACEHOLDER = new JS() { };
39 public JSScope(JSScope parentScope) { this(parentScope, 0, 0); }
40 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
41 public JSScope getParentScope() { return parentScope; }
43 public JS get(JS key) throws JSExn {
44 if (key instanceof JSNumber) try {
45 return get(JS.toInt(key));
46 } catch(ArrayIndexOutOfBoundsException e) {
47 throw new JSExn("scope index out of range");
49 JS o = super.get(key);
50 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
51 else return parentScope == null ? null : parentScope.get(key);
54 public boolean has(JS key) throws JSExn { return super.get(key) != null; }
55 public void put(JS key, JS val) throws JSExn {
56 if (key instanceof JSNumber) try {
57 put(JS.toInt(key),val);
58 } catch(ArrayIndexOutOfBoundsException e) {
59 throw new JSExn("scope index out of range");
61 if (parentScope != null && !has(key)) parentScope.put(key, val);
62 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
65 public JSScope top() {
67 while(s.parentScope != null) s = s.parentScope;
71 public static class Global extends JSScope {
72 private final static JS NaN = JS.N(Double.NaN);
73 private final static JS POSITIVE_INFINITY = JS.N(Double.POSITIVE_INFINITY);
75 public Global() { super(null); }
76 public Global(JSScope p) { super(p); }
78 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
80 // HACK: "this" gets lost on the way back through the scope chain
81 // We'll have to do something better with this when Scope is rewritten
82 public JS get(JS key) throws JSExn {
84 if(ret == JS.METHOD) return new Interpreter.Stub(this,key);
88 public JS _get(JS key) throws JSExn {
89 //#switch(JS.toString(key))
90 case "JS.NaN": return JS.NaN;
91 case "Infinity": return POSITIVE_INFINITY;
92 case "undefined": return null;
93 case "stringFromCharCode": return JS.METHOD;
94 case "parseInt": return JS.METHOD;
95 case "parseFloat": return JS.METHOD;
96 case "isJS.NaN": return JS.METHOD;
97 case "isFinite": return JS.METHOD;
98 case "decodeURI": return JS.METHOD;
99 case "decodeURIComponent": return JS.METHOD;
100 case "encodeURI": return JS.METHOD;
101 case "encodeURIComponent": return JS.METHOD;
102 case "escape": return JS.METHOD;
103 case "unescape": return JS.METHOD;
104 default: return super.get(key);
106 return super.get(key);
109 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
110 //#switch(JS.toString(method))
111 case "parseInt": return parseInt(a0, N(0));
112 case "parseFloat": return parseFloat(a0);
113 case "isJS.NaN": { double d = JS.toDouble(a0); return d == d ? JS.F : JS.T; }
114 case "isFinite": { double d = JS.toDouble(a0); return (d == d && !Double.isInfinite(d)) ? JS.T : JS.F; }
115 case "decodeURI": throw new JSExn("unimplemented");
116 case "decodeURIComponent": throw new JSExn("unimplemented");
117 case "encodeURI": throw new JSExn("unimplemented");
118 case "encodeURIComponent": throw new JSExn("unimplemented");
119 case "escape": throw new JSExn("unimplemented");
120 case "unescape": throw new JSExn("unimplemented");
121 case "parseInt": return parseInt(a0, a1);
122 default: return super.callMethod(method, a0, a1, a2, rest, nargs);
124 return super.callMethod(method,a0,a1,a2,rest,nargs);
127 private JS parseInt(JS arg, JS r) throws JSExn {
128 int radix = JS.toInt(r);
129 String s = JS.toString(arg);
131 int length = s.length();
134 if(radix != 0 && (radix < 2 || radix > 36)) return JS.NaN;
135 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
136 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
137 sign = s.charAt(start) == '+' ? 1 : -1;
140 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
142 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
147 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
150 if(radix == 0) radix = 10;
151 if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
152 // try the fast way first
154 String s2 = start == 0 ? s : s.substring(start);
155 return JS.N(sign*Integer.parseInt(s2,radix));
156 } catch(NumberFormatException e) { }
157 // fall through to a slower but emca-compliant method
158 for(int i=start;i<length;i++) {
159 int digit = Character.digit(s.charAt(i),radix);
162 if(n < 0) return JS.NaN; // overflow;
164 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
165 return JS.N((long)sign*n);
168 private JS parseFloat(JS arg) throws JSExn {
169 String s = JS.toString(arg);
171 int length = s.length();
172 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
174 // as long as the string has no trailing garbage,this is fast, its slow with
178 return JS.N(new Double(s.substring(start,length)));
179 } catch(NumberFormatException e) { }