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