2003/11/16 08:28:10
[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(JSArray args) {
59         JSContext cx = new JSContext(this, false);
60         cx.invoke(args);
61         return cx.stack.pop();
62     }
63
64
65     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
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     public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
91     public String dump() {
92         StringBuffer sb = new StringBuffer(1024);
93         sb.append("\n" + sourceName + ": " + firstLine + "\n");
94         for (int i=0; i < size; i++) {
95             sb.append(i);
96             sb.append(": ");
97             if (op[i] < 0)
98                 sb.append(bytecodeToString[-op[i]]);
99             else
100                 sb.append(codeToString[op[i]]);
101             sb.append(" ");
102             sb.append(arg[i] == null ? "(no arg)" : 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             }
110             sb.append("\n");
111         }
112         return sb.toString();
113     } 
114
115
116 }
117