renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / JSFunction.java
index 5d8838e..681e03e 100644 (file)
@@ -4,13 +4,9 @@
 
 package org.ibex.js;
 
-import java.io.*;
-import org.ibex.util.*;
-
 /** A JavaScript function, compiled into bytecode */
-// 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 {
-
+class JSFunction extends JS.Immutable implements ByteCodes, Tokens {
+    private static final JS[] emptyArgs = new JS[0];
 
     // Fields and Accessors ///////////////////////////////////////////////
 
@@ -29,11 +25,17 @@ public class JSFunction extends JS implements ByteCodes, Tokens, Task {
 
     // 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 Interpreter.JSArgs(this));
-        i.resume();
+    // FIXME: what needs to be syncrhonized (if anything)?
+    private Interpreter runner = null;
+    public Object run(Object o) throws JSExn {
+        if (runner == null) runner = new Interpreter(this, true, emptyArgs);
+        Object ret = runner.run(o);
+        if (runner.f == null) runner = null;
+        return ret;
+    }
+    public void pause() throws NotPausableException {
+        if (runner == null) throw new NotPausableException();
+        runner.pause();
     }
 
     public JSFunction _cloneWithNewParentScope(JSScope s) {
@@ -48,13 +50,9 @@ public class JSFunction extends JS implements ByteCodes, Tokens, Task {
         return ret;
     }
 
-    /** Note: code gets run in an <i>unpauseable</i> context. */
-    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();
-    }
+    public JS call(JS[] args) throws JSExn { return (JS)new Interpreter(this, false, args).run(null); }
 
-    public JSScope getParentScope() { return parentScope; }
+    JSScope getParentScope() { return parentScope; }
 
     // Adding and Altering Bytecodes ///////////////////////////////////////////////////
 
@@ -99,7 +97,7 @@ public class JSFunction extends JS implements ByteCodes, Tokens, Task {
             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] instanceof JS ? JS.debugToString((JS)arg[i]) : arg[i]);
+            sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JSU.str((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) {