updated Makefile.common
[org.ibex.core.git] / src / org / ibex / js / JSFunction.java
index 40a550d..d50e566 100644 (file)
@@ -5,7 +5,8 @@ import java.io.*;
 import org.ibex.util.*;
 
 /** A JavaScript function, compiled into bytecode */
-class JSFunction extends JS implements ByteCodes, Tokens, Task {
+// FIXME: This shouldn't be public, needed for public add/delTrap (which is needed for the Template.java hack)
+public class JSFunction extends JS implements ByteCodes, Tokens, Task {
 
 
     // Fields and Accessors ///////////////////////////////////////////////
@@ -28,25 +29,10 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
     // 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());
+        Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this));
         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
@@ -60,13 +46,8 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
     }
 
     /** Note: code gets run in an <i>unpauseable</i> context. */
-    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]);
-        Interpreter cx = new Interpreter(this, false, args);
+    public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+        Interpreter cx = new Interpreter(this, false, new Interpreter.JSArgs(a0,a1,a2,rest,nargs,this));
         return cx.resume();
     }
 
@@ -103,7 +84,7 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
 
     // Debugging //////////////////////////////////////////////////////////////////////
 
-    public String toString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; }
+    String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
 
     String dump() { return dump(""); }
     private  String dump(String prefix) {
@@ -111,11 +92,11 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
         sb.append("\n" + sourceName + ": " + firstLine + "\n");
         for (int i=0; i < size; i++) {
             sb.append(prefix);
-            sb.append(i).append(" (").append(line[i]).append(") :");
+            sb.append(i).append(" (").append(line[i]).append("): ");
             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]);
+            sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JS.debugToString((JS)arg[i]) : arg[i]);
             if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
                 sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
             } else  if(op[i] == TRY) {
@@ -124,6 +105,9 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
                 sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
             } else if(op[i] == NEWFUNCTION) {
                 sb.append(((JSFunction) arg[i]).dump(prefix + "     "));
+            } else if(op[i] == NEWSCOPE) {
+                int n = ((JSNumber)arg[i]).toInt();
+                sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
             }
             sb.append("\n");
         }