1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
5 import org.ibex.util.*;
7 /** A JavaScript function, compiled into bytecode */
8 // FIXME: This shouldn't be public, needed for public add/delTrap (which is needed for the Template.java hack)
9 public class JSFunction extends JS implements ByteCodes, Tokens, Task {
12 // Fields and Accessors ///////////////////////////////////////////////
14 int numFormalArgs = 0; ///< the number of formal arguments
16 String sourceName; ///< the source code file that this block was drawn from
17 private int firstLine = -1; ///< the first line of this script
19 int[] line = new int[10]; ///< the line numbers
20 int[] op = new int[10]; ///< the instructions
21 Object[] arg = new Object[10]; ///< the arguments to the instructions
22 int size = 0; ///< the number of instruction/argument pairs
24 JSScope parentScope; ///< the default scope to use as a parent scope when executing this
27 // Public //////////////////////////////////////////////////////////////////////////////
29 // FEATURE: make sure that this can only be called from the Scheduler...
30 /** if you enqueue a function, it gets invoked in its own pauseable context */
31 public void perform() throws JSExn {
32 Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this));
36 public JSFunction _cloneWithNewParentScope(JSScope s) {
37 JSFunction ret = new JSFunction(sourceName, firstLine, s);
38 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
39 // NOTE: Neither *this* function nor the new function should be modified after this call
44 ret.numFormalArgs = this.numFormalArgs;
48 /** Note: code gets run in an <i>unpauseable</i> context. */
49 public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
50 Interpreter cx = new Interpreter(this, false, new Interpreter.JSArgs(a0,a1,a2,rest,nargs,this));
54 public JSScope getParentScope() { return parentScope; }
56 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
58 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
59 this.sourceName = sourceName;
60 this.firstLine = firstLine;
61 this.parentScope = parentScope;
64 int get(int pos) { return op[pos]; }
65 Object getArg(int pos) { return arg[pos]; }
66 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
67 void set(int pos, Object arg_) { arg[pos] = arg_; }
68 int pop() { size--; arg[size] = null; return op[size]; }
69 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
70 JSFunction add(int line, int op_) { return add(line, op_, null); }
71 JSFunction add(int line, int op_, Object arg_) {
72 if (size == op.length - 1) {
73 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
74 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
75 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
77 this.line[size] = line;
85 // Debugging //////////////////////////////////////////////////////////////////////
87 String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
89 String dump() { return dump(""); }
90 private String dump(String prefix) {
91 StringBuffer sb = new StringBuffer(1024);
92 sb.append("\n" + sourceName + ": " + firstLine + "\n");
93 for (int i=0; i < size; i++) {
95 sb.append(i).append(" (").append(line[i]).append("): ");
96 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
97 else sb.append(codeToString[op[i]]);
99 sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JS.debugToString((JS)arg[i]) : arg[i]);
100 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
101 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
102 } else if(op[i] == TRY) {
103 int[] jmps = (int[]) arg[i];
104 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
105 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
106 } else if(op[i] == NEWFUNCTION) {
107 sb.append(((JSFunction) arg[i]).dump(prefix + " "));
108 } else if(op[i] == NEWSCOPE) {
109 int n = ((JSNumber)arg[i]).toInt();
110 sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
114 return sb.toString();