renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / JSFunction.java
index d50e566..681e03e 100644 (file)
@@ -1,13 +1,12 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex.js;
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
 
-import java.io.*;
-import org.ibex.util.*;
+package org.ibex.js;
 
 /** 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 ///////////////////////////////////////////////
 
@@ -26,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) {
@@ -45,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 ///////////////////////////////////////////////////
 
@@ -96,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) {