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