1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 /** Implementation of a JavaScript Scope */
5 class JSScope extends JS.O {
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 JS get(int i) throws JSExn { return i < base ? parent.get(i) : vars[i-base]; }
28 void put(int i, JS o) throws JSExn { if(i < base) parent.put(i,o); else vars[i-base] = o; }
30 JS getGlobal() { return parent.getGlobal(); }
32 private JSScope parentScope;
34 private static final JS NULL_PLACEHOLDER = new JS() { };
36 public JSScope(JSScope parentScope) { this(parentScope, 0, 0); }
37 public void declare(JS s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
38 public JSScope getParentScope() { return parentScope; }
40 public JS get(JS key) throws JSExn {
41 if (key instanceof JSNumber) try {
42 return get(JS.toInt(key));
43 } catch(ArrayIndexOutOfBoundsException e) {
44 throw new JSExn("scope index out of range");
46 JS o = super.get(key);
47 if (o != null) return o == NULL_PLACEHOLDER ? null : o;
48 else return parentScope == null ? null : parentScope.get(key);
51 public boolean has(JS key) throws JSExn { return super.get(key) != null; }
52 public void put(JS key, JS val) throws JSExn {
53 if (key instanceof JSNumber) try {
54 put(JS.toInt(key),val);
55 } catch(ArrayIndexOutOfBoundsException e) {
56 throw new JSExn("scope index out of range");
58 if (parentScope != null && !has(key)) parentScope.put(key, val);
59 else super.put(key, val == null ? NULL_PLACEHOLDER : val);
62 public JSScope top() {
64 while(s.parentScope != null) s = s.parentScope;
68 public static class Global extends JSScope {
69 private final static JS NaN = JS.N(Double.NaN);
70 private final static JS POSITIVE_INFINITY = JS.N(Double.POSITIVE_INFINITY);
72 public Global() { super(null); }
73 public Global(JSScope p) { super(p); }
75 public void declare(JS k) throws JSExn { throw new JSExn("can't declare variables in the global scope"); }
77 // HACK: "this" gets lost on the way back through the scope chain
78 // We'll have to do something better with this when Scope is rewritten
79 public JS get(JS key) throws JSExn {
81 if(ret == JS.METHOD) return new Interpreter.Stub(this,key);
85 public JS _get(JS key) throws JSExn {
86 //#switch(JS.toString(key))
87 case "JS.NaN": return JS.NaN;
88 case "Infinity": return POSITIVE_INFINITY;
89 case "undefined": return null;
90 case "stringFromCharCode": return JS.METHOD;
91 case "parseInt": return JS.METHOD;
92 case "parseFloat": return JS.METHOD;
93 case "isJS.NaN": return JS.METHOD;
94 case "isFinite": return JS.METHOD;
95 case "decodeURI": return JS.METHOD;
96 case "decodeURIComponent": return JS.METHOD;
97 case "encodeURI": return JS.METHOD;
98 case "encodeURIComponent": return JS.METHOD;
99 case "escape": return JS.METHOD;
100 case "unescape": return JS.METHOD;
101 default: return super.get(key);
103 return super.get(key);
106 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
107 //#switch(JS.toString(method))
108 case "parseInt": return parseInt(a0, N(0));
109 case "parseFloat": return parseFloat(a0);
110 case "isJS.NaN": { double d = JS.toDouble(a0); return d == d ? JS.F : JS.T; }
111 case "isFinite": { double d = JS.toDouble(a0); return (d == d && !Double.isInfinite(d)) ? JS.T : JS.F; }
112 case "decodeURI": throw new JSExn("unimplemented");
113 case "decodeURIComponent": throw new JSExn("unimplemented");
114 case "encodeURI": throw new JSExn("unimplemented");
115 case "encodeURIComponent": throw new JSExn("unimplemented");
116 case "escape": throw new JSExn("unimplemented");
117 case "unescape": throw new JSExn("unimplemented");
118 case "parseInt": return parseInt(a0, a1);
119 default: return super.callMethod(method, a0, a1, a2, rest, nargs);
121 return super.callMethod(method,a0,a1,a2,rest,nargs);
124 private JS parseInt(JS arg, JS r) throws JSExn {
125 int radix = JS.toInt(r);
126 String s = JS.toString(arg);
128 int length = s.length();
131 if(radix != 0 && (radix < 2 || radix > 36)) return JS.NaN;
132 while(start < length && Character.isWhitespace(s.charAt(start))) start++;
133 if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
134 sign = s.charAt(start) == '+' ? 1 : -1;
137 if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
139 if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
144 if(length == start || Character.digit(s.charAt(start+1),8)==-1) return JS.ZERO;
147 if(radix == 0) radix = 10;
148 if(length == start || Character.digit(s.charAt(start),radix) == -1) return JS.NaN;
149 // try the fast way first
151 String s2 = start == 0 ? s : s.substring(start);
152 return JS.N(sign*Integer.parseInt(s2,radix));
153 } catch(NumberFormatException e) { }
154 // fall through to a slower but emca-compliant method
155 for(int i=start;i<length;i++) {
156 int digit = Character.digit(s.charAt(i),radix);
159 if(n < 0) return JS.NaN; // overflow;
161 if(n <= Integer.MAX_VALUE) return JS.N(sign*(int)n);
162 return JS.N((long)sign*n);
165 private JS parseFloat(JS arg) throws JSExn {
166 String s = JS.toString(arg);
168 int length = s.length();
169 while(start < length && Character.isWhitespace(s.charAt(0))) start++;
171 // as long as the string has no trailing garbage,this is fast, its slow with
175 return JS.N(new Double(s.substring(start,length)));
176 } catch(NumberFormatException e) { }