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 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 method, JS[] args) throws JSExn {
54 if (method != null) return super.call(method, args);
55 return (JS)new Interpreter(this, false, args).run(null); }
57 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 ? JSU.str((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();