2004/01/03 04:27:27
[org.ibex.core.git] / src / org / xwt / js / JSFunction.java
index 92e3eff..8e5c92a 100644 (file)
@@ -6,24 +6,50 @@ import java.util.*;
 import java.io.*;
 
 /** A JavaScript function, compiled into bytecode */
-public class JSFunction extends JSCallable implements ByteCodes, Tokens {
+public class JSFunction extends JS implements ByteCodes, Tokens, org.xwt.Scheduler.Task {
 
 
     // Fields and Accessors ///////////////////////////////////////////////
 
     int numFormalArgs = 0;         ///< the number of formal arguments
+
     String sourceName;             ///< the source code file that this block was drawn from
-    int[] line = new int[10];      ///< the line numbers
     private int firstLine = -1;    ///< the first line of this script
+
+    int[] line = new int[10];      ///< the line numbers
     int[] op = new int[10];        ///< the instructions
     Object[] arg = new Object[10]; ///< the arguments to the instructions
     int size = 0;                  ///< the number of instruction/argument pairs
-    JSScope parentJSScope;          ///< the default scope to use as a parent scope when executing this
 
+    JSScope parentScope;           ///< the default scope to use as a parent scope when executing this
 
-    // Constructors ////////////////////////////////////////////////////////
 
-    public JSFunction cloneWithNewParentJSScope(JSScope s) {
+    // Public //////////////////////////////////////////////////////////////////////////////
+
+    // FEATURE: make sure that this can only be called from the Scheduler...
+    /** if you enqueue a function, it gets invoked in its own pauseable context */
+    public void perform() throws JSExn {
+        Interpreter i = new Interpreter(this, true, new JSArray());
+        int oldpausecount = i.pausecount;
+        i.resume();
+    }
+
+    /** parse and compile a function */
+    public static JSFunction fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
+        JSFunction ret = new JSFunction(sourceName, firstLine, null);
+        if (sourceCode == null) return ret;
+        Parser p = new Parser(sourceCode, sourceName, firstLine);
+        while(true) {
+            int s = ret.size;
+            p.parseStatement(ret, null);
+            if (s == ret.size) break;
+        }
+        ret.add(-1, LITERAL, null); 
+        ret.add(-1, RETURN);
+        return ret;
+    }
+
+    public JSFunction cloneWithNewParentScope(JSScope s) {
         JSFunction ret = new JSFunction(sourceName, firstLine, s);
         // Reuse the same op, arg, line, and size variables for the new "instance" of the function
         // NOTE: Neither *this* function nor the new function should be modified after this call
@@ -35,40 +61,26 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
         return ret;
     }
 
-    JSFunction(String sourceName, int firstLine, JSScope parentJSScope) {
-        this.sourceName = sourceName;
-        this.firstLine = firstLine;
-        this.parentJSScope = parentJSScope;
-    }
-
-    protected JSFunction(String sourceName, int firstLine, Reader sourceCode, JSScope parentJSScope) throws IOException {
-        this(sourceName, firstLine, parentJSScope);
-        if (sourceCode == null) return;
-        Parser p = new Parser(sourceCode, sourceName, firstLine);
-        while(true) {
-            int s = size;
-            p.parseStatement(this, null);
-            if (s == size) break;
-        }
-        add(-1, LITERAL, null); 
-        add(-1, RETURN);
-    }
-
     /** Note: code gets run in an <i>unpauseable</i> context. */
-    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
-        JSContext cx = new JSContext(this, false);
+    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         JSArray args = new JSArray();
         if (nargs > 0) args.addElement(a0);
         if (nargs > 1) args.addElement(a1);
         if (nargs > 2) args.addElement(a2);
         for(int i=3; i<nargs; i++) args.addElement(rest[i-3]);
-        cx.invoke(args);
-        return cx.stack.pop();
+        Interpreter cx = new Interpreter(this, false, args);
+        return cx.resume();
     }
 
 
     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
 
+    JSFunction(String sourceName, int firstLine, JSScope parentScope) {
+        this.sourceName = sourceName;
+        this.firstLine = firstLine;
+        this.parentScope = parentScope;
+    }
+
     int get(int pos) { return op[pos]; }
     Object getArg(int pos) { return arg[pos]; }
     void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
@@ -99,10 +111,8 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
         for (int i=0; i < size; i++) {
             sb.append(i);
             sb.append(": ");
-            if (op[i] < 0)
-                sb.append(bytecodeToString[-op[i]]);
-            else
-                sb.append(codeToString[op[i]]);
+            if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
+            else sb.append(codeToString[op[i]]);
             sb.append(" ");
             sb.append(arg[i] == null ? "(no arg)" : arg[i]);
             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {