a14bdec1fedaba22944a334f9e02db54190caa49
[org.ibex.js.git] / src / org / ibex / js / JSFunction.java
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.
4
5 package org.ibex.js;
6 import org.ibex.util.*;
7
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];
11
12     // Fields and Accessors ///////////////////////////////////////////////
13
14     int numFormalArgs = 0;         ///< the number of formal arguments
15
16     String sourceName;             ///< the source code file that this block was drawn from
17     private int firstLine = -1;    ///< the first line of this script
18
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
23
24     JSScope parentScope;           ///< the default scope to use as a parent scope when executing this
25
26
27     // Public //////////////////////////////////////////////////////////////////////////////
28
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;
35         return ret;
36     }
37     public void pause() throws NotPausableException {
38         if (runner == null) throw new NotPausableException();
39         runner.pause();
40     }
41
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
46         ret.op = this.op;
47         ret.arg = this.arg;
48         ret.line = this.line;
49         ret.size = this.size;
50         ret.numFormalArgs = this.numFormalArgs;
51         return ret;
52     }
53
54     public JS call(JS[] args) throws JSExn { return (JS)new Interpreter(this, false, args).run(null); }
55
56     JSScope getParentScope() { return parentScope; }
57
58     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
59
60     JSFunction(String sourceName, int firstLine, JSScope parentScope) {
61         this.sourceName = sourceName;
62         this.firstLine = firstLine;
63         this.parentScope = parentScope;
64     }
65
66     int get(int pos) { return op[pos]; }
67     Object getArg(int pos) { return arg[pos]; }
68     void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
69     void set(int pos, Object arg_) { arg[pos] = arg_; }
70     int pop() { size--; arg[size] = null; return op[size]; }
71     void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
72     JSFunction add(int line, int op_) { return add(line, op_, null); }
73     JSFunction add(int line, int op_, Object arg_) {
74         if (size == op.length - 1) {
75             int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
76             Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
77             int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
78         }
79         this.line[size] = line;
80         op[size] = op_;
81         arg[size] = arg_;
82         size++;
83         return this;
84     }
85     
86
87     // Debugging //////////////////////////////////////////////////////////////////////
88
89     String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
90
91     String dump() { return dump(""); }
92     private  String dump(String prefix) {
93         StringBuffer sb = new StringBuffer(1024);
94         sb.append("\n" + sourceName + ": " + firstLine + "\n");
95         for (int i=0; i < size; i++) {
96             sb.append(prefix);
97             sb.append(i).append(" (").append(line[i]).append("): ");
98             if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
99             else sb.append(codeToString[op[i]]);
100             sb.append(" ");
101             sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JSU.str((JS)arg[i]) : arg[i]);
102             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
103                 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
104             } else  if(op[i] == TRY) {
105                 int[] jmps = (int[]) arg[i];
106                 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
107                 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
108             } else if(op[i] == NEWFUNCTION) {
109                 sb.append(((JSFunction) arg[i]).dump(prefix + "     "));
110             } else if(op[i] == NEWSCOPE) {
111                 int n = ((JSNumber)arg[i]).toInt();
112                 sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
113             }
114             sb.append("\n");
115         }
116         return sb.toString();
117     } 
118
119
120 }
121