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 */
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 final JS get(JS i) throws JSExn {
31 if(i==null) throw new NullPointerException();
33 return get(Script.toInt(i));
34 } catch(ArrayIndexOutOfBoundsException e) {
35 throw new JSExn("scope index out of range");
38 final void put(JS i, JS o) throws JSExn {
39 if(i==null) throw new NullPointerException();
41 put(Script.toInt(i), o);
42 } catch(ArrayIndexOutOfBoundsException e) {
43 throw new JSExn("scope index out of range");
46 JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
47 void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
49 JS getGlobal() { return parent.getGlobal(); }
51 /*private JSScope parentScope;
53 private static final JS NULL_PLACEHOLDER = new JS() { };
55 public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
56 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
57 public JSScope getParentScope() { return parentScope; }
59 public JS get(JS key) throws JSExn {
60 JS o = super.get(key);
61 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
62 else return parentScope == null ? null : parentScope.get(key);
65 public boolean has(JS key) throws JSExn { return super.get(key) != null; }
66 public void put(JS key, JS val) throws JSExn {
67 if (parentScope != null && !has(key)) parentScope.put(key, val);
68 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
71 public JSScope top() {
73 while(s.parentScope != null) s = s.parentScope;
77 public static class Global extends JSScope {
78 private final static JS NaN = N(Double.NaN);
79 private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
81 public Global() { super(null); }
82 public Global(JSScope p) { super(p); }
84 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
86 // HACK: "this" gets lost on the way back through the scope chain
87 // We'll have to do something better with this when Scope is rewritten
88 public JS get(JS key) throws JSExn {
90 if(ret == METHOD) return new Interpreter.Stub(this,key);
94 public JS _get(JS key) throws JSExn {
96 case "NaN": return NaN;
97 case "Infinity": return POSITIVE_INFINITY;
98 case "undefined": return null;
99 case "stringFromCharCode": return METHOD;
100 case "parseInt": return METHOD;
101 case "parseFloat": return METHOD;
102 case "isNaN": return METHOD;
103 case "isFinite": return METHOD;
104 case "decodeURI": return METHOD;
105 case "decodeURIComponent": return METHOD;
106 case "encodeURI": return METHOD;
107 case "encodeURIComponent": return METHOD;
108 case "escape": return METHOD;
109 case "unescape": return METHOD;
111 return super.get(key);
114 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
115 #switch(JS.str(method))
116 case "parseInt": return parseInt(a0, N(0));
117 case "parseFloat": return parseFloat(a0);
118 case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
119 case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
120 case "decodeURI": throw new JSExn("unimplemented");
121 case "decodeURIComponent": throw new JSExn("unimplemented");
122 case "encodeURI": throw new JSExn("unimplemented");
123 case "encodeURIComponent": throw new JSExn("unimplemented");
124 case "escape": throw new JSExn("unimplemented");
125 case "unescape": throw new JSExn("unimplemented");
126 case "parseInt": return parseInt(a0, a1);
128 return super.callMethod(method, a0, a1, a2, rest, nargs);
131 private JS parseInt(JS arg, JS r) throws JSExn {
132 int radix = JS.toInt(r);
133 String s = JS.toString(arg);
135 int length = s.length();
138 if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
139 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
140 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
141 sign = s.charAt(start) == '+' ? 1 : -1;
144 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
146 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
151 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
154 if(radix == 0) radix = 10;
155 if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
156 // try the fast way first
158 String s2 = start == 0 ? s : s.substring(start);
159 return JS.N(sign*Integer.parseInt(s2,radix));
160 } catch(NumberFormatException e) { }
161 // fall through to a slower but emca-compliant method
162 for(int i=start;i<length;i++) {
163 int digit = Character.digit(s.charAt(i),radix);
166 if(n < 0) return NaN; // overflow;
168 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
169 return JS.N((long)sign*n);
172 private JS parseFloat(JS arg) throws JSExn {
173 String s = JS.toString(arg);
175 int length = s.length();
176 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
178 // as long as the string has no trailing garbage,this is fast, its slow with
182 return JS.N(new Double(s.substring(start,length)));
183 } catch(NumberFormatException e) { }