licensing update to APSL 2.0
[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
7 import java.io.*;
8 import org.ibex.util.*;
9
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 {
13
14
15     // Fields and Accessors ///////////////////////////////////////////////
16
17     int numFormalArgs = 0;         ///< the number of formal arguments
18
19     String sourceName;             ///< the source code file that this block was drawn from
20     private int firstLine = -1;    ///< the first line of this script
21
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
26
27     JSScope parentScope;           ///< the default scope to use as a parent scope when executing this
28
29
30     // Public //////////////////////////////////////////////////////////////////////////////
31
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));
36         i.resume();
37     }
38
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
43         ret.op = this.op;
44         ret.arg = this.arg;
45         ret.line = this.line;
46         ret.size = this.size;
47         ret.numFormalArgs = this.numFormalArgs;
48         return ret;
49     }
50
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));
54         return cx.resume();
55     }
56
57     public JSScope getParentScope() { return parentScope; }
58
59     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
60
61     JSFunction(String sourceName, int firstLine, JSScope parentScope) {
62         this.sourceName = sourceName;
63         this.firstLine = firstLine;
64         this.parentScope = parentScope;
65     }
66
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;
79         }
80         this.line[size] = line;
81         op[size] = op_;
82         arg[size] = arg_;
83         size++;
84         return this;
85     }
86     
87
88     // Debugging //////////////////////////////////////////////////////////////////////
89
90     String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
91
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++) {
97             sb.append(prefix);
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]]);
101             sb.append(" ");
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));
114             }
115             sb.append("\n");
116         }
117         return sb.toString();
118     } 
119
120
121 }
122