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(JSArray args) {
59 JSContext cx = new JSContext(this, false);
61 return cx.stack.pop();
65 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
67 int get(int pos) { return op[pos]; }
68 Object getArg(int pos) { return arg[pos]; }
69 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
70 void set(int pos, Object arg_) { arg[pos] = arg_; }
71 int pop() { size--; arg[size] = null; return op[size]; }
72 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
73 JSFunction add(int line, int op_) { return add(line, op_, null); }
74 JSFunction add(int line, int op_, Object arg_) {
75 if (size == op.length - 1) {
76 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
77 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
78 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
80 this.line[size] = line;
88 // Debugging //////////////////////////////////////////////////////////////////////
90 public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
91 public String dump() {
92 StringBuffer sb = new StringBuffer(1024);
93 sb.append("\n" + sourceName + ": " + firstLine + "\n");
94 for (int i=0; i < size; i++) {
98 sb.append(bytecodeToString[-op[i]]);
100 sb.append(codeToString[op[i]]);
102 sb.append(arg[i] == null ? "(no arg)" : arg[i]);
103 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
104 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
105 } else if(op[i] == TRY) {
106 int[] jmps = (int[]) arg[i];
107 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
108 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
112 return sb.toString();