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.
6 import org.ibex.util.*;
8 /** A JavaScript function, compiled into bytecode */
9 class JSFunction extends JS.Immutable implements ByteCodes, Tokens, Pausable {
10 private static final JS[] emptyArgs = new JS[0];
12 // Fields and Accessors ///////////////////////////////////////////////
14 int numFormalArgs = 0; ///< the number of formal arguments
16 String sourceName; ///< the source code file that this block was drawn from
17 private int firstLine = -1; ///< the first line of this script
19 int[] line = new int[10]; ///< the line numbers
20 int[] op = new int[10]; ///< the instructions
21 Object[] arg = new Object[10]; ///< the arguments to the instructions
22 int size = 0; ///< the number of instruction/argument pairs
24 JSScope parentScope; ///< the default scope to use as a parent scope when executing this
27 // Public //////////////////////////////////////////////////////////////////////////////
29 // FIXME: what needs to be syncrhonized (if anything)?
30 private Interpreter runner = null;
31 public Object run(Object o) throws JSExn {
32 if (runner == null) runner = new Interpreter(this, true, emptyArgs);
33 Object ret = runner.run(o);
34 if (runner.f == null) runner = null;
37 public void pause() throws NotPausableException {
38 if (runner == null) throw new NotPausableException();
42 public JSFunction _cloneWithNewParentScope(JSScope s) {
43 JSFunction ret = new JSFunction(sourceName, firstLine, s);
44 // Reuse the same op, arg, line, and size variables for the new "instance" of the function
45 // NOTE: Neither *this* function nor the new function should be modified after this call
50 ret.numFormalArgs = this.numFormalArgs;
54 public JS call(JS method, JS[] args) throws JSExn {
55 if (method != null) return super.call(method, args);
56 return (JS)new Interpreter(this, false, args).run(null); }
58 JSScope getParentScope() { return parentScope; }
60 // Adding and Altering Bytecodes ///////////////////////////////////////////////////
62 JSFunction(String sourceName, int firstLine, JSScope parentScope) {
63 this.sourceName = sourceName;
64 this.firstLine = firstLine;
65 this.parentScope = parentScope;
68 int get(int pos) { return op[pos]; }
69 Object getArg(int pos) { return arg[pos]; }
70 void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
71 void set(int pos, Object arg_) { arg[pos] = arg_; }
72 int pop() { size--; arg[size] = null; return op[size]; }
73 void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
74 JSFunction add(int line, int op_) { return add(line, op_, null); }
75 JSFunction add(int line, int op_, Object arg_) {
76 if (size == op.length - 1) {
77 int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
78 Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
79 int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
81 this.line[size] = line;
89 // Debugging //////////////////////////////////////////////////////////////////////
91 String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
93 String dump() { return dump(""); }
94 private String dump(String prefix) {
95 StringBuffer sb = new StringBuffer(1024);
96 sb.append("\n" + sourceName + ": " + firstLine + "\n");
97 for (int i=0; i < size; i++) {
99 sb.append(i).append(" (").append(line[i]).append("): ");
100 if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
101 else sb.append(codeToString[op[i]]);
103 sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JSU.str((JS)arg[i]) : arg[i]);
104 if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
105 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
106 } else if(op[i] == TRY) {
107 int[] jmps = (int[]) arg[i];
108 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
109 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
110 } else if(op[i] == NEWFUNCTION) {
111 sb.append(((JSFunction) arg[i]).dump(prefix + " "));
112 } else if(op[i] == NEWSCOPE) {
113 int n = ((JSNumber)arg[i]).toInt();
114 sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
118 return sb.toString();