X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSFunction.java;h=0bf580e42dae07564f9352f08411f5d0257bbd0f;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=d50e5661ac61812bd4405982c1ac39fc7c220a3e;hpb=19d66e161db458135518efd3539048f44e1e5622;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSFunction.java b/src/org/ibex/js/JSFunction.java index d50e566..0bf580e 100644 --- a/src/org/ibex/js/JSFunction.java +++ b/src/org/ibex/js/JSFunction.java @@ -1,13 +1,13 @@ -// 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.*; +package org.ibex.js; 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, Pausable { + private static final JS[] emptyArgs = new JS[0]; // Fields and Accessors /////////////////////////////////////////////// @@ -26,11 +26,16 @@ 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(); + 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,11 @@ 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 method, JS[] args) throws JSExn { + if (method != null) return super.call(method, args); + return (JS)new Interpreter(this, false, args).run(null); } - public JSScope getParentScope() { return parentScope; } + JSScope getParentScope() { return parentScope; } // Adding and Altering Bytecodes /////////////////////////////////////////////////// @@ -96,7 +99,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) {