1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** A JavaScript function, compiled into bytecode */
9 public class JSFunction extends JSCallable implements ByteCodes, Tokens {
12 // Fields and Accessors ///////////////////////////////////////////////
14 int numFormalArgs = 0; ///< the number of formal arguments
15 String sourceName; ///< the source code file that this block was drawn from
16 int[] line = new int[10]; ///< the line numbers
17 private int firstLine = -1; ///< the first line of this script
18 int[] op = new int[10]; ///< the instructions
19 Object[] arg = new Object[10]; ///< the arguments to the instructions
20 int size = 0; ///< the number of instruction/argument pairs
21 JSScope parentJSScope; ///< the default scope to use as a parent scope when executing this
24 // Constructors ////////////////////////////////////////////////////////
26 public JSFunction cloneWithNewParentJSScope(JSScope s) {
27 JSFunction ret = new JSFunction(sourceName, firstLine, s);
28 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
29 // NOTE: Neither *this* function nor the new function should be modified after this call
34 ret.numFormalArgs = this.numFormalArgs;
38 JSFunction(String sourceName, int firstLine, JSScope parentJSScope) {
39 this.sourceName = sourceName;
40 this.firstLine = firstLine;
41 this.parentJSScope = parentJSScope;
44 protected JSFunction(String sourceName, int firstLine, Reader sourceCode, JSScope parentJSScope) throws IOException {
45 this(sourceName, firstLine, parentJSScope);
46 if (sourceCode == null) return;
47 Parser p = new Parser(sourceCode, sourceName, firstLine);
50 p.parseStatement(this, null);
53 add(-1, LITERAL, null);
57 /** Note: code gets run in an <i>unpauseable</i> context. */
58 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
59 JSContext cx = new JSContext(this, false);
60 JSArray args = new JSArray();
61 if (nargs > 0) args.addElement(a0);
62 if (nargs > 1) args.addElement(a1);
63 if (nargs > 2) args.addElement(a2);
64 for(int i=3; i<nargs; i++) args.addElement(rest[i-3]);
66 return cx.stack.pop();
70 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
72 int get(int pos) { return op[pos]; }
73 Object getArg(int pos) { return arg[pos]; }
74 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
75 void set(int pos, Object arg_) { arg[pos] = arg_; }
76 int pop() { size--; arg[size] = null; return op[size]; }
77 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
78 JSFunction add(int line, int op_) { return add(line, op_, null); }
79 JSFunction add(int line, int op_, Object arg_) {
80 if (size == op.length - 1) {
81 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
82 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
83 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
85 this.line[size] = line;
93 // Debugging //////////////////////////////////////////////////////////////////////
95 public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
96 public String dump() {
97 StringBuffer sb = new StringBuffer(1024);
98 sb.append("\n" + sourceName + ": " + firstLine + "\n");
99 for (int i=0; i < size; i++) {
103 sb.append(bytecodeToString[-op[i]]);
105 sb.append(codeToString[op[i]]);
107 sb.append(arg[i] == null ? "(no arg)" : arg[i]);
108 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
109 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
110 } else if(op[i] == TRY) {
111 int[] jmps = (int[]) arg[i];
112 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
113 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
117 return sb.toString();