1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 /** Implementation of a JavaScript Scope */
7 private final int base;
8 private final JS[] vars;
11 public static class Top extends JSScope {
12 private final JS global;
13 public Top(JS global) { super(null,0,0); this.global = global; }
14 JS get(int i) throws JSExn { throw new JSExn("scope index out of range"); }
15 void put(int i, JS o) throws JSExn { throw new JSExn("scope index out of range"); }
16 JS getGlobal() { return global; }
19 // NOTE: We can't just set base to parent.base + parent.vars.length
20 // sometimes we only access part of a parent's scope
21 public JSScope(JSScope parent, int base, int size) {
24 this.vars = new JS[size];
27 final JS get(JS i) throws JSExn {
28 if(i==null) throw new NullPointerException();
30 return get(JS.toInt(i));
31 } catch(ArrayIndexOutOfBoundsException e) {
32 throw new JSExn("scope index out of range");
35 final void put(JS i, JS o) throws JSExn {
36 if(i==null) throw new NullPointerException();
39 } catch(ArrayIndexOutOfBoundsException e) {
40 throw new JSExn("scope index out of range");
43 JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
44 void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
46 JS getGlobal() { return parent.getGlobal(); }
48 /*private JSScope parentScope;
50 private static final JS NULL_PLACEHOLDER = new JS() { };
52 public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
53 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
54 public JSScope getParentScope() { return parentScope; }
56 public JS get(JS key) throws JSExn {
57 JS o = super.get(key);
58 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
59 else return parentScope == null ? null : parentScope.get(key);
62 public boolean has(JS key) throws JSExn { return super.get(key) != null; }
63 public void put(JS key, JS val) throws JSExn {
64 if (parentScope != null && !has(key)) parentScope.put(key, val);
65 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
68 public JSScope top() {
70 while(s.parentScope != null) s = s.parentScope;
74 public static class Global extends JSScope {
75 private final static JS NaN = N(Double.NaN);
76 private final static JS POSITIVE_INFINITY = N(Double.POSITIVE_INFINITY);
78 public Global() { super(null); }
79 public Global(JSScope p) { super(p); }
81 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
83 // HACK: "this" gets lost on the way back through the scope chain
84 // We'll have to do something better with this when Scope is rewritten
85 public JS get(JS key) throws JSExn {
87 if(ret == METHOD) return new Interpreter.Stub(this,key);
91 public JS _get(JS key) throws JSExn {
93 case "NaN": return NaN;
94 case "Infinity": return POSITIVE_INFINITY;
95 case "undefined": return null;
96 case "stringFromCharCode": return METHOD;
97 case "parseInt": return METHOD;
98 case "parseFloat": return METHOD;
99 case "isNaN": return METHOD;
100 case "isFinite": return METHOD;
101 case "decodeURI": return METHOD;
102 case "decodeURIComponent": return METHOD;
103 case "encodeURI": return METHOD;
104 case "encodeURIComponent": return METHOD;
105 case "escape": return METHOD;
106 case "unescape": return METHOD;
108 return super.get(key);
111 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
113 case "parseInt": return parseInt(a0, N(0));
114 case "parseFloat": return parseFloat(a0);
115 case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
116 case "isFinite": { double d = toDouble(a0); return (d == d && !Double.isInfinite(d)) ? T : F; }
117 case "decodeURI": throw new JSExn("unimplemented");
118 case "decodeURIComponent": throw new JSExn("unimplemented");
119 case "encodeURI": throw new JSExn("unimplemented");
120 case "encodeURIComponent": throw new JSExn("unimplemented");
121 case "escape": throw new JSExn("unimplemented");
122 case "unescape": throw new JSExn("unimplemented");
123 case "parseInt": return parseInt(a0, a1);
125 return super.callMethod(method, a0, a1, a2, rest, nargs);
128 private JS parseInt(JS arg, JS r) throws JSExn {
129 int radix = JS.toInt(r);
130 String s = JS.toString(arg);
132 int length = s.length();
135 if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
136 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
137 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
138 sign = s.charAt(start) == '+' ? 1 : -1;
141 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
143 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
148 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
151 if(radix == 0) radix = 10;
152 if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
153 // try the fast way first
155 String s2 = start == 0 ? s : s.substring(start);
156 return JS.N(sign*Integer.parseInt(s2,radix));
157 } catch(NumberFormatException e) { }
158 // fall through to a slower but emca-compliant method
159 for(int i=start;i<length;i++) {
160 int digit = Character.digit(s.charAt(i),radix);
163 if(n < 0) return NaN; // overflow;
165 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
166 return JS.N((long)sign*n);
169 private JS parseFloat(JS arg) throws JSExn {
170 String s = JS.toString(arg);
172 int length = s.length();
173 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
175 // as long as the string has no trailing garbage,this is fast, its slow with
179 return JS.N(new Double(s.substring(start,length)));
180 } catch(NumberFormatException e) { }