2003/11/17 01:53:25
[org.ibex.core.git] / src / org / xwt / js / JSFunction.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.js;
3
4 import org.xwt.util.*;
5 import java.util.*;
6 import java.io.*;
7
8 /** A JavaScript function, compiled into bytecode */
9 public class JSFunction extends JSCallable implements ByteCodes, Tokens {
10
11
12     // Fields and Accessors ///////////////////////////////////////////////
13
14     int numFormalArgs = 0;         ///< the number of formal arguments
15     String sourceName;             ///< the source code file that this block was drawn from
16     int[] line = new int[10];      ///< the line numbers
17     private int firstLine = -1;    ///< the first line of this script
18     int[] op = new int[10];        ///< the instructions
19     Object[] arg = new Object[10]; ///< the arguments to the instructions
20     int size = 0;                  ///< the number of instruction/argument pairs
21     JSScope parentJSScope;          ///< the default scope to use as a parent scope when executing this
22
23
24     // Constructors ////////////////////////////////////////////////////////
25
26     public JSFunction cloneWithNewParentJSScope(JSScope s) {
27         JSFunction ret = new JSFunction(sourceName, firstLine, s);
28         // Reuse the same op, arg, line, and size variables for the new "instance" of the function
29         // NOTE: Neither *this* function nor the new function should be modified after this call
30         ret.op = this.op;
31         ret.arg = this.arg;
32         ret.line = this.line;
33         ret.size = this.size;
34         ret.numFormalArgs = this.numFormalArgs;
35         return ret;
36     }
37
38     JSFunction(String sourceName, int firstLine, JSScope parentJSScope) {
39         this.sourceName = sourceName;
40         this.firstLine = firstLine;
41         this.parentJSScope = parentJSScope;
42     }
43
44     protected JSFunction(String sourceName, int firstLine, Reader sourceCode, JSScope parentJSScope) throws IOException {
45         this(sourceName, firstLine, parentJSScope);
46         if (sourceCode == null) return;
47         Parser p = new Parser(sourceCode, sourceName, firstLine);
48         while(true) {
49             int s = size;
50             p.parseStatement(this, null);
51             if (s == size) break;
52         }
53         add(-1, LITERAL, null); 
54         add(-1, RETURN);
55     }
56
57     /** Note: code gets run in an <i>unpauseable</i> context. */
58     public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
59         JSContext cx = new JSContext(this, false);
60         JSArray args = new JSArray();
61         if (nargs > 0) args.addElement(a0);
62         if (nargs > 1) args.addElement(a1);
63         if (nargs > 2) args.addElement(a2);
64         for(int i=3; i<nargs; i++) args.addElement(rest[i-3]);
65         cx.invoke(args);
66         return cx.stack.pop();
67     }
68
69
70     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
71
72     int get(int pos) { return op[pos]; }
73     Object getArg(int pos) { return arg[pos]; }
74     void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
75     void set(int pos, Object arg_) { arg[pos] = arg_; }
76     int pop() { size--; arg[size] = null; return op[size]; }
77     void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
78     JSFunction add(int line, int op_) { return add(line, op_, null); }
79     JSFunction add(int line, int op_, Object arg_) {
80         if (size == op.length - 1) {
81             int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
82             Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
83             int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
84         }
85         this.line[size] = line;
86         op[size] = op_;
87         arg[size] = arg_;
88         size++;
89         return this;
90     }
91     
92
93     // Debugging //////////////////////////////////////////////////////////////////////
94
95     public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
96     public String dump() {
97         StringBuffer sb = new StringBuffer(1024);
98         sb.append("\n" + sourceName + ": " + firstLine + "\n");
99         for (int i=0; i < size; i++) {
100             sb.append(i);
101             sb.append(": ");
102             if (op[i] < 0)
103                 sb.append(bytecodeToString[-op[i]]);
104             else
105                 sb.append(codeToString[op[i]]);
106             sb.append(" ");
107             sb.append(arg[i] == null ? "(no arg)" : arg[i]);
108             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
109                 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
110             } else  if(op[i] == TRY) {
111                 int[] jmps = (int[]) arg[i];
112                 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
113                 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
114             }
115             sb.append("\n");
116         }
117         return sb.toString();
118     } 
119
120
121 }
122