1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 /** A JavaScript function, compiled into bytecode */
8 class JSFunction extends JS.Immutable implements ByteCodes, Tokens {
9 private static final JS[] emptyArgs = new JS[0];
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 // FIXME: what needs to be syncrhonized (if anything)?
29 private Interpreter runner = null;
30 public Object run(Object o) throws JSExn {
31 if (runner == null) runner = new Interpreter(this, true, emptyArgs);
32 Object ret = runner.run(o);
33 if (runner.f == null) runner = null;
36 public void pause() throws NotPausableException {
37 if (runner == null) throw new NotPausableException();
41 public JSFunction _cloneWithNewParentScope(JSScope s) {
42 JSFunction ret = new JSFunction(sourceName, firstLine, s);
43 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
44 // NOTE: Neither *this* function nor the new function should be modified after this call
49 ret.numFormalArgs = this.numFormalArgs;
53 public JS call(JS[] args) throws JSExn { return (JS)new Interpreter(this, false, args).run(null); }
55 JSScope getParentScope() { return parentScope; }
57 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
59 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
60 this.sourceName = sourceName;
61 this.firstLine = firstLine;
62 this.parentScope = parentScope;
65 int get(int pos) { return op[pos]; }
66 Object getArg(int pos) { return arg[pos]; }
67 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
68 void set(int pos, Object arg_) { arg[pos] = arg_; }
69 int pop() { size--; arg[size] = null; return op[size]; }
70 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
71 JSFunction add(int line, int op_) { return add(line, op_, null); }
72 JSFunction add(int line, int op_, Object arg_) {
73 if (size == op.length - 1) {
74 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
75 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
76 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
78 this.line[size] = line;
86 // Debugging //////////////////////////////////////////////////////////////////////
88 String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
90 String dump() { return dump(""); }
91 private String dump(String prefix) {
92 StringBuffer sb = new StringBuffer(1024);
93 sb.append("\n" + sourceName + ": " + firstLine + "\n");
94 for (int i=0; i < size; i++) {
96 sb.append(i).append(" (").append(line[i]).append("): ");
97 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
98 else sb.append(codeToString[op[i]]);
100 sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JSU.str((JS)arg[i]) : arg[i]);
101 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
102 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
103 } else if(op[i] == TRY) {
104 int[] jmps = (int[]) arg[i];
105 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
106 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
107 } else if(op[i] == NEWFUNCTION) {
108 sb.append(((JSFunction) arg[i]).dump(prefix + " "));
109 } else if(op[i] == NEWSCOPE) {
110 int n = ((JSNumber)arg[i]).toInt();
111 sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
115 return sb.toString();