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.
8 import org.ibex.util.*;
10 /** A JavaScript function, compiled into bytecode */
11 // FIXME: This shouldn't be public, needed for public add/delTrap (which is needed for the Template.java hack)
12 public class JSFunction extends JS implements ByteCodes, Tokens, Task {
15 // Fields and Accessors ///////////////////////////////////////////////
17 int numFormalArgs = 0; ///< the number of formal arguments
19 String sourceName; ///< the source code file that this block was drawn from
20 private int firstLine = -1; ///< the first line of this script
22 int[] line = new int[10]; ///< the line numbers
23 int[] op = new int[10]; ///< the instructions
24 Object[] arg = new Object[10]; ///< the arguments to the instructions
25 int size = 0; ///< the number of instruction/argument pairs
27 JSScope parentScope; ///< the default scope to use as a parent scope when executing this
30 // Public //////////////////////////////////////////////////////////////////////////////
32 // FEATURE: make sure that this can only be called from the Scheduler...
33 /** if you enqueue a function, it gets invoked in its own pauseable context */
34 public void perform() throws JSExn {
35 Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this));
39 public JSFunction _cloneWithNewParentScope(JSScope s) {
40 JSFunction ret = new JSFunction(sourceName, firstLine, s);
41 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
42 // NOTE: Neither *this* function nor the new function should be modified after this call
47 ret.numFormalArgs = this.numFormalArgs;
51 /** Note: code gets run in an <i>unpauseable</i> context. */
52 public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
53 Interpreter cx = new Interpreter(this, false, new Interpreter.JSArgs(a0,a1,a2,rest,nargs,this));
57 public JSScope getParentScope() { return parentScope; }
59 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
61 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
62 this.sourceName = sourceName;
63 this.firstLine = firstLine;
64 this.parentScope = parentScope;
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 String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
92 String dump() { return dump(""); }
93 private String dump(String prefix) {
94 StringBuffer sb = new StringBuffer(1024);
95 sb.append("\n" + sourceName + ": " + firstLine + "\n");
96 for (int i=0; i < size; i++) {
98 sb.append(i).append(" (").append(line[i]).append("): ");
99 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
100 else sb.append(codeToString[op[i]]);
102 sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JS.debugToString((JS)arg[i]) : 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]));
109 } else if(op[i] == NEWFUNCTION) {
110 sb.append(((JSFunction) arg[i]).dump(prefix + " "));
111 } else if(op[i] == NEWSCOPE) {
112 int n = ((JSNumber)arg[i]).toInt();
113 sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
117 return sb.toString();