1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** A JavaScript function, compiled into bytecode */
9 public class JSFunction extends JS implements ByteCodes, Tokens {
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 /** parse and compile a function */
30 public static JSFunction fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
31 JSFunction ret = new JSFunction(sourceName, firstLine, null);
32 if (sourceCode == null) return ret;
33 Parser p = new Parser(sourceCode, sourceName, firstLine);
36 p.parseStatement(ret, null);
37 if (s == ret.size) break;
39 ret.add(-1, LITERAL, null);
44 public JSFunction cloneWithNewParentScope(JSScope s) {
45 JSFunction ret = new JSFunction(sourceName, firstLine, s);
46 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
47 // NOTE: Neither *this* function nor the new function should be modified after this call
52 ret.numFormalArgs = this.numFormalArgs;
56 /** Note: code gets run in an <i>unpauseable</i> context. */
57 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
58 JSArray args = new JSArray();
59 if (nargs > 0) args.addElement(a0);
60 if (nargs > 1) args.addElement(a1);
61 if (nargs > 2) args.addElement(a2);
62 for(int i=3; i<nargs; i++) args.addElement(rest[i-3]);
63 Interpreter cx = new Interpreter(this, false, args);
68 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
70 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
71 this.sourceName = sourceName;
72 this.firstLine = firstLine;
73 this.parentScope = parentScope;
76 int get(int pos) { return op[pos]; }
77 Object getArg(int pos) { return arg[pos]; }
78 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
79 void set(int pos, Object arg_) { arg[pos] = arg_; }
80 int pop() { size--; arg[size] = null; return op[size]; }
81 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
82 JSFunction add(int line, int op_) { return add(line, op_, null); }
83 JSFunction add(int line, int op_, Object arg_) {
84 if (size == op.length - 1) {
85 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
86 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
87 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
89 this.line[size] = line;
97 // Debugging //////////////////////////////////////////////////////////////////////
99 public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
100 public String dump() {
101 StringBuffer sb = new StringBuffer(1024);
102 sb.append("\n" + sourceName + ": " + firstLine + "\n");
103 for (int i=0; i < size; i++) {
106 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
107 else sb.append(codeToString[op[i]]);
109 sb.append(arg[i] == null ? "(no arg)" : arg[i]);
110 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
111 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
112 } else if(op[i] == TRY) {
113 int[] jmps = (int[]) arg[i];
114 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
115 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
119 return sb.toString();