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