2003/11/03 00:08:27
[org.ibex.core.git] / src / org / xwt / js / CompiledFunctionImpl.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.js;
3
4 import org.xwt.util.*;
5 import java.io.*;
6
7 /** a JavaScript function, compiled into bytecode */
8 class CompiledFunctionImpl extends JS.Obj implements ByteCodes, Tokens {
9
10     // Fields and Accessors ///////////////////////////////////////////////
11
12     /** the number of formal arguments */
13     int numFormalArgs = 20;
14
15     /** the source code file that this block was drawn from */
16      String sourceName;
17     public String getSourceName() throws JS.Exn { return sourceName; }
18     
19     /** the line numbers */
20      int[] line = new int[10];
21
22     /** the first line of this script */
23     private int firstLine = -1;
24
25     /** the instructions */
26      int[] op = new int[10];
27
28     /** the arguments to the instructions */
29      Object[] arg = new Object[10];
30
31     /** the number of instruction/argument pairs */
32      int size = 0;
33     int size() { return size; }
34
35     /** the scope in which this function was declared; by default this function is called in a fresh subscope of the parentScope */
36     JS.Scope parentScope;
37
38     // Constructors ////////////////////////////////////////////////////////
39
40     private CompiledFunctionImpl cloneWithNewParentScope(JS.Scope s) throws IOException {
41         CompiledFunctionImpl ret = new JS.CompiledFunction(sourceName, firstLine, null, s);
42         // Reuse the same op, arg, line, and size variables for the new "instance" of the function
43         // NOTE: Neither *this* function nor the new function should be modified after this call
44         ret.op = this.op;
45         ret.arg = this.arg;
46         ret.line = this.line;
47         ret.size = this.size;
48         ret.numFormalArgs = this.numFormalArgs;
49         return ret;
50     }
51
52     protected CompiledFunctionImpl(String sourceName, int firstLine, Reader sourceCode, JS.Scope parentScope) throws IOException {
53         this.sourceName = sourceName;
54         this.firstLine = firstLine;
55         this.parentScope = parentScope;
56         if (sourceCode == null) return;
57         Parser p = new Parser(sourceCode, sourceName, firstLine);
58         while(true) {
59             int s = size();
60             p.parseStatement(this, null);
61             if (s == size()) break;
62         }
63         add(-1, LITERAL, null); 
64         add(-1, RETURN);
65     }
66     
67
68
69     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
70
71     int get(int pos) { return op[pos]; }
72     Object getArg(int pos) { return arg[pos]; }
73     void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
74     void set(int pos, Object arg_) { arg[pos] = arg_; }
75     int pop() { size--; arg[size] = null; return op[size]; }
76     void paste(CompiledFunctionImpl other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
77     CompiledFunctionImpl add(int line, int op_) { return add(line, op_, null); }
78     CompiledFunctionImpl add(int line, int op_, Object arg_) {
79         if (size == op.length - 1) {
80             int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
81             Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
82             int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
83         }
84         this.line[size] = line;
85         op[size] = op_;
86         arg[size] = arg_;
87         size++;
88         return this;
89     }
90     
91     public static int getLine(JS.Thread cx) {
92         if(cx.pc < 0 || cx.pc >= ((CompiledFunctionImpl)cx.currentCompiledFunction).size) return -1;
93         return ((CompiledFunctionImpl)cx.currentCompiledFunction).line[cx.pc];
94     }
95
96
97     // Invoking the Bytecode ///////////////////////////////////////////////////////
98
99     /** returns false if the thread has been paused */
100     static Object eval(final JS.Thread cx) throws JS.Exn {
101         OUTER: for(;; cx.pc++) {
102         try {
103             if (cx.paused) return null;
104             cx.bind();
105             if (cx.currentCompiledFunction == null) return cx.stack.pop();
106             if (cx.pc >= ((CompiledFunctionImpl)cx.currentCompiledFunction).size) return cx.stack.pop();
107             String label = null;
108             int curOP = cx.currentCompiledFunction.op[cx.pc];
109             Object curArg = cx.currentCompiledFunction.arg[cx.pc];
110             if(curOP == FINALLY_DONE) {
111                 FinallyData fd = (FinallyData) cx.stack.pop();
112                 if(fd == null) continue OUTER; // NOP
113                 curOP = fd.op;
114                 curArg = fd.arg;
115             }
116             switch(curOP) {
117             case LITERAL: cx.stack.push(curArg); break;
118             case OBJECT: cx.stack.push(new JS.Obj()); break;
119             case ARRAY: cx.stack.push(new JS.Array(JS.toNumber(curArg).intValue())); break;
120             case DECLARE: cx.scope.declare((String)(curArg==null ? cx.stack.peek() : curArg)); if(curArg != null) cx.stack.push(curArg); break;
121             case TOPSCOPE: cx.stack.push(cx.scope); break;
122             case JT: if (JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(curArg).intValue() - 1; break;
123             case JF: if (!JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(curArg).intValue() - 1; break;
124             case JMP: cx.pc += JS.toNumber(curArg).intValue() - 1; break;
125             case POP: cx.stack.pop(); break;
126             case SWAP: { Object o1 = cx.stack.pop(); Object o2 = cx.stack.pop(); cx.stack.push(o1); cx.stack.push(o2); break; }
127             case DUP: cx.stack.push(cx.stack.peek()); break;
128             case NEWSCOPE: cx.scope = new JS.Scope(cx.scope); break;
129             case OLDSCOPE: cx.scope = cx.scope.getParentScope(); break;
130             case ASSERT: if (!JS.toBoolean(cx.stack.pop())) throw je("assertion failed"); break;
131             case BITNOT: cx.stack.push(new Long(~JS.toLong(cx.stack.pop()))); break;
132             case BANG: cx.stack.push(new Boolean(!JS.toBoolean(cx.stack.pop()))); break;
133
134             case TYPEOF: {
135                 Object o = cx.stack.pop();
136                 if (o == null) cx.stack.push(null);
137                 else if (o instanceof JS) cx.stack.push(((JS)o).typeName());
138                 else if (o instanceof String) cx.stack.push("string");
139                 else if (o instanceof Number) cx.stack.push("number");
140                 else if (o instanceof Boolean) cx.stack.push("boolean");
141                 else cx.stack.push("unknown");
142                 break;
143             }
144
145             case NEWFUNCTION: {
146                 try {
147                     cx.stack.push(((CompiledFunctionImpl)curArg).cloneWithNewParentScope(cx.scope));
148                 } catch (IOException e) {
149                     throw new Error("this should never happen");
150                 }
151                 break;
152             }
153
154             case PUSHKEYS: {
155                 Object o = cx.stack.peek();
156                 Object[] keys = ((JS)o).keys();
157                 JS.Array a = new JS.Array();
158                 a.setSize(keys.length);
159                 for(int j=0; j<keys.length; j++) a.setElementAt(keys[j], j);
160                 cx.stack.push(a);
161                 break;
162             }
163
164             case LABEL: break;
165             case LOOP: {
166                 cx.stack.push(new LoopMarker(cx.pc, cx.pc > 0 && cx.currentCompiledFunction.op[cx.pc - 1] == LABEL ? (String)cx.currentCompiledFunction.arg[cx.pc - 1] : (String)null, cx.scope));
167                 cx.stack.push(Boolean.TRUE);
168                 break;
169             }
170
171             case BREAK:
172             case CONTINUE:
173                 while(cx.stack.size() > 0) {
174                     Object o = cx.stack.pop();
175                     if (o instanceof CallMarker) ee("break or continue not within a loop");
176                     if (o instanceof TryMarker) {
177                         if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
178                         cx.stack.push(new FinallyData(curOP, curArg));
179                         cx.scope = ((TryMarker)o).scope;
180                         cx.pc = ((TryMarker)o).finallyLoc - 1;
181                         continue OUTER;
182                     }
183                     if (o instanceof LoopMarker) {
184                         if (curArg == null || curArg.equals(((LoopMarker)o).label)) {
185                             int loopInstructionLocation = ((LoopMarker)o).location;
186                             int endOfLoop = ((Integer)cx.currentCompiledFunction.arg[loopInstructionLocation]).intValue() + loopInstructionLocation;
187                             cx.scope = ((LoopMarker)o).scope;
188                             if (curOP == CONTINUE) { cx.stack.push(o); cx.stack.push(Boolean.FALSE); }
189                             cx.pc = curOP == BREAK ? endOfLoop - 1 : loopInstructionLocation;
190                             continue OUTER;
191                         }
192                     }
193                 }
194                 throw new Error("CONTINUE/BREAK invoked but couldn't find a LoopMarker at " + ((CompiledFunctionImpl)cx.currentCompiledFunction).sourceName + ":" + getLine(cx));
195
196             case TRY: {
197                 int[] jmps = (int[]) curArg;
198                 // jmps[0] is how far away the catch block is, jmps[1] is how far away the finally block is
199                 // each can be < 0 if the specified block does not exist
200                 cx.stack.push(new TryMarker(jmps[0] < 0 ? -1 : cx.pc + jmps[0], jmps[1] < 0 ? -1 : cx.pc + jmps[1], cx.scope));
201                 break;
202             }
203
204             case RETURN: {
205                 Object retval = cx.stack.pop();
206                 while(cx.stack.size() > 0) {
207                     Object o = cx.stack.pop();
208                     if (o instanceof TryMarker) {
209                         if(((TryMarker)o).finallyLoc < 0) continue;
210                         cx.stack.push(retval); 
211                         cx.stack.push(new FinallyData(RETURN));
212                         cx.scope = ((TryMarker)o).scope;
213                         cx.pc = ((TryMarker)o).finallyLoc - 1;
214                         continue OUTER;
215                     }
216                     if (o instanceof CallMarker) {
217                         cx.scope = ((CallMarker)o).scope;
218                         cx.pc = ((CallMarker)o).pc;
219                         cx.currentCompiledFunction = (CompiledFunction)((CallMarker)o).currentCompiledFunction;
220                         cx.stack.push(retval);
221                         continue OUTER;
222                     }
223                 }
224                 throw new Error("error: RETURN invoked but couldn't find a CallMarker!");
225             }
226
227             case PUT: {
228                 Object val = cx.stack.pop();
229                 Object key = cx.stack.pop();
230                 Object target = cx.stack.peek();
231                 if (target == null)
232                     throw je("tried to put a value to the " + key + " property on the null value");
233                 if (!(target instanceof JS))
234                     throw je("tried to put a value to the " + key + " property on a " + target.getClass().getName());
235                 if (key == null)
236                     throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key");
237                 // FIXME too many allocations here
238                 TailCall tail = new TailCall();
239                 ((JS)target).put(key, val, tail);
240                 cx.stack.push(val);
241                 if (tail.func != null) {
242                     cx.stack.push(new CallMarker(cx));
243                     cx.stack.push(tail.args);
244                     cx.currentCompiledFunction = tail.func;
245                     cx.scope = new CompiledFunctionImpl.FunctionScope("unknown", tail.func.parentScope);
246                     cx.pc = -1;
247                     break;
248                 }
249                 break;
250             }
251
252             case GET:
253             case GET_PRESERVE: {
254                 Object o, v;
255                 if (curOP == GET) {
256                     v = curArg == null ? cx.stack.pop() : curArg;
257                     o = cx.stack.pop();
258                 } else {
259                     v = cx.stack.pop();
260                     o = cx.stack.peek();
261                     cx.stack.push(v);
262                 }
263                 Object ret = null;
264                 if (o == null) throw je("tried to get property \"" + v + "\" from the null value");
265                 if (v == null) throw je("tried to get the null key from " + o);
266                 if (o instanceof String || o instanceof Number || o instanceof Boolean)
267                     ret = Internal.getFromPrimitive(o,v);
268                 else if (o instanceof JS) {
269                     ret = ((JS)o).get(v);
270                     if (ret instanceof JS.TailCall) {
271                         cx.stack.push(new CallMarker(cx));
272                         cx.stack.push(((JS.TailCall)ret).args);
273                         cx.currentCompiledFunction = ((JS.TailCall)ret).func;
274                         cx.scope = new CompiledFunctionImpl.FunctionScope("unknown", ((JS.TailCall)ret).func.parentScope);
275                         cx.pc = -1;
276                         break;
277                     }
278                 } else 
279                     throw je("tried to get property " + v + " from a " + o.getClass().getName());
280                 cx.stack.push(ret);
281                 break;
282             }
283             
284             case CALLMETHOD:
285             case CALL:
286             {
287                 JS.Array arguments = new JS.Array();
288                 int numArgs = JS.toNumber(curArg).intValue();
289                 arguments.setSize(numArgs);
290                 for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(cx.stack.pop(), j);
291                 Object o = cx.stack.pop();
292                 if(o == null) throw je("attempted to call null");
293                 Object ret;
294                 if(curOP == CALLMETHOD) {
295                     Object method = o;
296                     o = cx.stack.pop();
297                     if(o instanceof String || o instanceof Number || o instanceof Boolean) {
298                         ret = Internal.callMethodOnPrimitive(o,method,arguments);
299                         cx.stack.push(ret);
300                         break;
301                     } else if (o instanceof JS) {
302                         if (((JS)o).callMethod(method, arguments, true) == Boolean.TRUE) {
303                             ret = ((JS)o).callMethod(method,arguments,false);
304                             cx.stack.push(ret);
305                             break;
306                         } else {
307                             o = ((JS)o).get(method);
308                             if (o instanceof JS.TailCall) {
309                                 cx.stack.push(new CallMarker(cx));
310                                 cx.stack.push(((JS.TailCall)o).args);
311                                 cx.currentCompiledFunction = ((JS.TailCall)o).func;
312                                 cx.scope = new CompiledFunctionImpl.FunctionScope("unknown", ((JS.TailCall)o).func.parentScope);
313                                 cx.pc = -1;
314                                 break;
315                             }
316                         }
317                     } else {
318                         throw new JS.Exn("Tried to call a method on an object that isn't a JS object: " + o);
319                     }
320                 }
321
322                 if (o instanceof CompiledFunctionImpl) {
323                     cx.stack.push(new CallMarker(cx));
324                     cx.stack.push(arguments);
325                     cx.currentCompiledFunction = (CompiledFunction)o;
326                     cx.scope = new FunctionScope("unknown", cx.currentCompiledFunction.parentScope);
327                     cx.pc = -1;
328                     break;
329                     
330                 } else {
331                     ret = ((JS.Callable)o).call(arguments);
332                     if (ret instanceof JS.TailCall) {
333                         cx.stack.push(new CallMarker(cx));
334                         cx.stack.push(((JS.TailCall)ret).args);
335                         cx.currentCompiledFunction = ((JS.TailCall)ret).func;
336                         cx.scope = new CompiledFunctionImpl.FunctionScope("unknown", ((JS.TailCall)ret).func.parentScope);
337                         cx.pc = -1;
338                         break;
339                     }
340                 }
341                 cx.stack.push(ret);
342                 break;
343             }
344             // fall through if exception was thrown
345             case THROW: {
346                 Object o = cx.stack.pop();
347                 if(o instanceof JS.Exn) throw (JS.Exn)o;
348                 throw new JS.Exn(o);
349             }
350
351             case INC: case DEC: {
352                 boolean isPrefix = JS.toBoolean(curArg);
353                 Object key = cx.stack.pop();
354                 JS obj = (JS)cx.stack.pop();
355                 Number num = JS.toNumber(obj.get(key));
356                 Number val = new Double(curOP == INC ? num.doubleValue() + 1.0 : num.doubleValue() - 1.0);
357                 obj.put(key, val);
358                 cx.stack.push(isPrefix ? val : num);
359                 break;
360             }
361             
362             case ASSIGN_SUB: case ASSIGN_ADD: {
363                 Object val = cx.stack.pop();
364                 Object old = cx.stack.pop();
365                 Object key = cx.stack.pop();
366                 Object obj = cx.stack.peek();
367                 if (val instanceof CompiledFunction) {
368                     if (obj instanceof JS.Scope) {
369                         JS.Scope parent = (JS.Scope)obj;
370                         while(parent.getParentScope() != null) parent = parent.getParentScope();
371                         if (parent instanceof org.xwt.Box) {
372                             if (curOP == ASSIGN_ADD) {
373                                 ((org.xwt.Box)parent).addTrap(key, val);
374                             } else {
375                                 ((org.xwt.Box)parent).delTrap(key, val);
376                             }
377                             // skip over the "normal" implementation of +=/-=
378                             cx.pc += ((Integer)curArg).intValue() - 1;
379                             break;
380                         }
381                     }
382                 }
383                 // use the "normal" implementation
384                 cx.stack.push(key);
385                 cx.stack.push(old);
386                 cx.stack.push(val);
387                 break;
388             }
389
390             case ADD: {
391                 int count = ((Number)curArg).intValue();
392                 if(count < 2) throw new Error("this should never happen");
393                 if(count == 2) {
394                     // common case
395                     Object right = cx.stack.pop();
396                     Object left = cx.stack.pop();
397                     if(left instanceof String || right instanceof String) cx.stack.push(JS.toString(left).concat(JS.toString(right)));
398                     else cx.stack.push(new Double(JS.toDouble(left) + JS.toDouble(right)));
399                 } else {
400                     Object[] args = new Object[count];
401                     while(--count >= 0) args[count] = cx.stack.pop();
402                     if(args[0] instanceof String) {
403                         StringBuffer sb = new StringBuffer(64);
404                         for(int i=0;i<args.length;i++) sb.append(JS.toString(args[i]));
405                         cx.stack.push(sb.toString());
406                     } else {
407                         int numStrings = 0;
408                         for(int i=0;i<args.length;i++) if(args[i] instanceof String) numStrings++;
409                         if(numStrings == 0) {
410                             double d = 0.0;
411                             for(int i=0;i<args.length;i++) d += JS.toDouble(args[i]);
412                             cx.stack.push(new Double(d));
413                         } else {
414                             int i=0;
415                             StringBuffer sb = new StringBuffer(64);
416                             if(!(args[0] instanceof String || args[1] instanceof String)) {
417                                 double d=0.0;
418                                 do {
419                                     d += JS.toDouble(args[i++]);
420                                 } while(!(args[i] instanceof String));
421                                 sb.append(JS.toString(new Double(d)));
422                             }
423                             while(i < args.length) sb.append(JS.toString(args[i++]));
424                             cx.stack.push(sb.toString());
425                         }
426                     }
427                 }
428                 break;
429             }
430
431             default: {
432                 Object right = cx.stack.pop();
433                 Object left = cx.stack.pop();
434                 switch(curOP) {
435                         
436                 case BITOR: cx.stack.push(new Long(JS.toLong(left) | JS.toLong(right))); break;
437                 case BITXOR: cx.stack.push(new Long(JS.toLong(left) ^ JS.toLong(right))); break;
438                 case BITAND: cx.stack.push(new Long(JS.toLong(left) & JS.toLong(right))); break;
439
440                 case SUB: cx.stack.push(new Double(JS.toDouble(left) - JS.toDouble(right))); break;
441                 case MUL: cx.stack.push(new Double(JS.toDouble(left) * JS.toDouble(right))); break;
442                 case DIV: cx.stack.push(new Double(JS.toDouble(left) / JS.toDouble(right))); break;
443                 case MOD: cx.stack.push(new Double(JS.toDouble(left) % JS.toDouble(right))); break;
444                         
445                 case LSH: cx.stack.push(new Long(JS.toLong(left) << JS.toLong(right))); break;
446                 case RSH: cx.stack.push(new Long(JS.toLong(left) >> JS.toLong(right))); break;
447                 case URSH: cx.stack.push(new Long(JS.toLong(left) >>> JS.toLong(right))); break;
448                         
449                 case LT: case LE: case GT: case GE: {
450                     if (left == null) left = new Integer(0);
451                     if (right == null) right = new Integer(0);
452                     int result = 0;
453                     if (left instanceof String || right instanceof String) {
454                         result = left.toString().compareTo(right.toString());
455                     } else {
456                         result = (int)java.lang.Math.ceil(JS.toDouble(left) - JS.toDouble(right));
457                     }
458                     cx.stack.push(new Boolean((curOP == LT && result < 0) || (curOP == LE && result <= 0) ||
459                                        (curOP == GT && result > 0) || (curOP == GE && result >= 0)));
460                     break;
461                 }
462                     
463                 case EQ:
464                 case NE: {
465                     Object l = left;
466                     Object r = right;
467                     boolean ret;
468                     if (l == null) { Object tmp = r; r = l; l = tmp; }
469                     if (l == null && r == null) ret = true;
470                     else if (r == null) ret = false; // l != null, so its false
471                     else if (l instanceof Boolean) ret = new Boolean(JS.toBoolean(r)).equals(l);
472                     else if (l instanceof Number) ret = JS.toNumber(r).doubleValue() == JS.toNumber(l).doubleValue();
473                     else if (l instanceof String) ret = r != null && l.equals(r.toString());
474                     else ret = l.equals(r);
475                     cx.stack.push(new Boolean(curOP == EQ ? ret : !ret)); break;
476                 }
477
478                 default: throw new Error("unknown opcode " + curOP);
479                 } }
480             }
481         } catch(JS.Exn e) {
482             while(cx.stack.size() > 0) {
483                 Object o = cx.stack.pop();
484                 if (o instanceof CatchMarker || o instanceof TryMarker) {
485                     boolean inCatch = o instanceof CatchMarker;
486                     if(inCatch) {
487                         o = cx.stack.pop();
488                         if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
489                     }
490                     if(!inCatch && ((TryMarker)o).catchLoc >= 0) {
491                         // run the catch block, this will implicitly run the finally block, if it exists
492                         cx.stack.push(o);
493                         cx.stack.push(catchMarker);
494                         cx.stack.push(e.getObject());
495                         cx.scope = ((TryMarker)o).scope;
496                         cx.pc = ((TryMarker)o).catchLoc - 1;
497                         continue OUTER;
498                     } else {
499                         cx.stack.push(e);
500                         cx.stack.push(new FinallyData(THROW));
501                         cx.scope = ((TryMarker)o).scope;
502                         cx.pc = ((TryMarker)o).finallyLoc - 1;
503                         continue OUTER;
504                     }
505                 }
506                 // no handler found within this func
507                 if(o instanceof CallMarker) throw e;
508             }
509             throw e;
510         } // end try/catch
511         } // end for
512     }
513
514     // Debugging //////////////////////////////////////////////////////////////////////
515
516     public String toString() {
517         StringBuffer sb = new StringBuffer(1024);
518         sb.append("\n" + sourceName + ": " + firstLine + "\n");
519         for (int i=0; i < size; i++) {
520             sb.append(i);
521             sb.append(": ");
522             if (op[i] < 0)
523                 sb.append(bytecodeToString[-op[i]]);
524             else
525                 sb.append(codeToString[op[i]]);
526             sb.append(" ");
527             sb.append(arg[i] == null ? "(no arg)" : arg[i]);
528             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
529                 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
530             } else  if(op[i] == TRY) {
531                 int[] jmps = (int[]) arg[i];
532                 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
533                 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
534             }
535             sb.append("\n");
536         }
537         return sb.toString();
538     } 
539
540     // Exception Stuff ////////////////////////////////////////////////////////////////
541
542     static class EvaluatorException extends RuntimeException { public EvaluatorException(String s) { super(s); } }
543     static EvaluatorException ee(String s) { throw new EvaluatorException(((CompiledFunctionImpl)(JS.Thread.current()).currentCompiledFunction).sourceName + ":" + JS.Thread.current().getLine() + " " + s); }
544     static JS.Exn je(String s) { throw new JS.Exn(((CompiledFunctionImpl)(JS.Thread.current()).currentCompiledFunction).sourceName + ":" + JS.Thread.current().getLine() + " " + s); }
545
546
547     // FunctionScope /////////////////////////////////////////////////////////////////
548
549     static class FunctionScope extends JS.Scope {
550         String sourceName;
551         public FunctionScope(String sourceName, Scope parentScope) { super(parentScope); this.sourceName = sourceName; }
552         public String getSourceName() { return sourceName; }
553     }
554
555
556     // Markers //////////////////////////////////////////////////////////////////////
557
558     public static class CallMarker {
559         int pc;
560         Scope scope;
561         CompiledFunctionImpl currentCompiledFunction;
562         public CallMarker(JS.Thread cx) { pc = cx.pc + 1; scope = cx.scope; currentCompiledFunction = cx.currentCompiledFunction; }
563     }
564     
565     public static class CatchMarker { public CatchMarker() { } }
566     private static CatchMarker catchMarker = new CatchMarker();
567     
568     public static class LoopMarker {
569         public int location;
570         public String label;
571         public JS.Scope scope;
572         public LoopMarker(int location, String label, JS.Scope scope) {
573             this.location = location;
574             this.label = label;
575             this.scope = scope;
576         }
577     }
578     public static class TryMarker {
579         public int catchLoc;
580         public int finallyLoc;
581         public JS.Scope scope;
582         public TryMarker(int catchLoc, int finallyLoc, JS.Scope scope) {
583             this.catchLoc = catchLoc;
584             this.finallyLoc = finallyLoc;
585             this.scope = scope;
586         }
587     }
588     public static class FinallyData {
589         public int op;
590         public Object arg;
591         public FinallyData(int op, Object arg) { this.op = op; this.arg = arg; }
592         public FinallyData(int op) { this(op,null); }
593     }
594 }
595
596 /** this class exists solely to work around a GCJ bug */
597 abstract class JSCallable extends JS.Callable {
598         public abstract Object call(JS.Array args) throws JS.Exn;
599 }