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 class JSFunction extends JS implements ByteCodes, Tokens, Task {
11 // Fields and Accessors ///////////////////////////////////////////////
13 int numFormalArgs = 0; ///< the number of formal arguments
15 String sourceName; ///< the source code file that this block was drawn from
16 private int firstLine = -1; ///< the first line of this script
18 int[] line = new int[10]; ///< the line numbers
19 int[] op = new int[10]; ///< the instructions
20 Object[] arg = new Object[10]; ///< the arguments to the instructions
21 int size = 0; ///< the number of instruction/argument pairs
23 JSScope parentScope; ///< the default scope to use as a parent scope when executing this
26 // Public //////////////////////////////////////////////////////////////////////////////
28 // FEATURE: make sure that this can only be called from the Scheduler...
29 /** if you enqueue a function, it gets invoked in its own pauseable context */
30 public void perform() throws JSExn {
31 Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this));
35 /** parse and compile a function */
36 public static JSFunction _fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
37 JSFunction ret = new JSFunction(sourceName, firstLine, null);
38 if (sourceCode == null) return ret;
39 Parser p = new Parser(sourceCode, sourceName, firstLine);
42 p.parseStatement(ret, null);
43 if (s == ret.size) break;
45 ret.add(-1, LITERAL, null);
50 public JSFunction _cloneWithNewParentScope(JSScope s) {
51 JSFunction ret = new JSFunction(sourceName, firstLine, s);
52 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
53 // NOTE: Neither *this* function nor the new function should be modified after this call
58 ret.numFormalArgs = this.numFormalArgs;
62 /** Note: code gets run in an <i>unpauseable</i> context. */
63 public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
64 Interpreter cx = new Interpreter(this, false, new Interpreter.JSArgs(a0,a1,a2,rest,nargs,this));
68 public JSScope getParentScope() { return parentScope; }
70 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
72 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
73 this.sourceName = sourceName;
74 this.firstLine = firstLine;
75 this.parentScope = parentScope;
78 int get(int pos) { return op[pos]; }
79 Object getArg(int pos) { return arg[pos]; }
80 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
81 void set(int pos, Object arg_) { arg[pos] = arg_; }
82 int pop() { size--; arg[size] = null; return op[size]; }
83 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
84 JSFunction add(int line, int op_) { return add(line, op_, null); }
85 JSFunction add(int line, int op_, Object arg_) {
86 if (size == op.length - 1) {
87 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
88 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
89 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
91 this.line[size] = line;
99 // Debugging //////////////////////////////////////////////////////////////////////
101 // FIXME: Put this back in
102 public String xtoString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
104 String dump() { return dump(""); }
105 private String dump(String prefix) {
106 StringBuffer sb = new StringBuffer(1024);
107 sb.append("\n" + sourceName + ": " + firstLine + "\n");
108 for (int i=0; i < size; i++) {
110 sb.append(i).append(" (").append(line[i]).append(") :");
111 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
112 else sb.append(codeToString[op[i]]);
114 sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JS.debugToString((JS)arg[i]) : arg[i]);
115 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
116 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
117 } else if(op[i] == TRY) {
118 int[] jmps = (int[]) arg[i];
119 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
120 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
121 } else if(op[i] == NEWFUNCTION) {
122 sb.append(((JSFunction) arg[i]).dump(prefix + " "));
126 return sb.toString();