X-Git-Url: http://git.megacz.com/?p=org.ibex.js.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSFunction.java;h=681e03e938625aa1b93f2a43348a924dc1eedaba;hp=5d8838e5ba1a7017981ac5e36177964ab9df91a2;hb=a1e6b7e9307319c0195b0efbe5e5354c128be481;hpb=73131826a18c93af4fb04672bc3ec820e1197ad1 diff --git a/src/org/ibex/js/JSFunction.java b/src/org/ibex/js/JSFunction.java index 5d8838e..681e03e 100644 --- a/src/org/ibex/js/JSFunction.java +++ b/src/org/ibex/js/JSFunction.java @@ -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 unpauseable 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) {