initial checkin
[org.ibex.nanogoat.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         i.resume();
32     }
33
34     /** parse and compile a function */
35     public static JSFunction _fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
36         JSFunction ret = new JSFunction(sourceName, firstLine, null);
37         if (sourceCode == null) return ret;
38         Parser p = new Parser(sourceCode, sourceName, firstLine);
39         while(true) {
40             int s = ret.size;
41             p.parseStatement(ret, null);
42             if (s == ret.size) break;
43         }
44         ret.add(-1, LITERAL, null); 
45         ret.add(-1, RETURN);
46         return ret;
47     }
48
49     public JSFunction _cloneWithNewParentScope(JSScope s) {
50         JSFunction ret = new JSFunction(sourceName, firstLine, s);
51         // Reuse the same op, arg, line, and size variables for the new "instance" of the function
52         // NOTE: Neither *this* function nor the new function should be modified after this call
53         ret.op = this.op;
54         ret.arg = this.arg;
55         ret.line = this.line;
56         ret.size = this.size;
57         ret.numFormalArgs = this.numFormalArgs;
58         return ret;
59     }
60
61     /** Note: code gets run in an <i>unpauseable</i> context. */
62     public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
63         JSArray args = new JSArray();
64         if (nargs > 0) args.addElement(a0);
65         if (nargs > 1) args.addElement(a1);
66         if (nargs > 2) args.addElement(a2);
67         for(int i=3; i<nargs; i++) args.addElement(rest[i-3]);
68         Interpreter cx = new Interpreter(this, false, args);
69         return cx.resume();
70     }
71
72     public JSScope getParentScope() { return parentScope; }
73
74     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
75
76     JSFunction(String sourceName, int firstLine, JSScope parentScope) {
77         this.sourceName = sourceName;
78         this.firstLine = firstLine;
79         this.parentScope = parentScope;
80     }
81
82     int get(int pos) { return op[pos]; }
83     Object getArg(int pos) { return arg[pos]; }
84     void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
85     void set(int pos, Object arg_) { arg[pos] = arg_; }
86     int pop() { size--; arg[size] = null; return op[size]; }
87     void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
88     JSFunction add(int line, int op_) { return add(line, op_, null); }
89     JSFunction add(int line, int op_, Object arg_) {
90         if (size == op.length - 1) {
91             int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
92             Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
93             int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
94         }
95         this.line[size] = line;
96         op[size] = op_;
97         arg[size] = arg_;
98         size++;
99         return this;
100     }
101     
102
103     // Debugging //////////////////////////////////////////////////////////////////////
104
105     public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
106
107     public String dump() {
108         StringBuffer sb = new StringBuffer(1024);
109         sb.append("\n" + sourceName + ": " + firstLine + "\n");
110         for (int i=0; i < size; i++) {
111             sb.append(i);
112             sb.append(": ");
113             if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
114             else sb.append(codeToString[op[i]]);
115             sb.append(" ");
116             sb.append(arg[i] == null ? "(no arg)" : arg[i]);
117             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
118                 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
119             } else  if(op[i] == TRY) {
120                 int[] jmps = (int[]) arg[i];
121                 sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
122                 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
123             }
124             sb.append("\n");
125         }
126         return sb.toString();
127     } 
128
129
130 }
131