X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSFunction.java;h=0bf580e42dae07564f9352f08411f5d0257bbd0f;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=9dfc5dbc82272a5df1f270f493b06e8c4f66b51f;hpb=7c2cac7d060d75681c2043a05a4cbd7a1b415623;p=org.ibex.js.git
diff --git a/src/org/ibex/js/JSFunction.java b/src/org/ibex/js/JSFunction.java
index 9dfc5db..0bf580e 100644
--- a/src/org/ibex/js/JSFunction.java
+++ b/src/org/ibex/js/JSFunction.java
@@ -1,12 +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 */
-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 ///////////////////////////////////////////////
@@ -21,31 +22,21 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
int size = 0; ///< the number of instruction/argument pairs
JSScope parentScope; ///< the default scope to use as a parent scope when executing this
- Vec formalArgs = new Vec();
- // 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 JSArray());
- i.resume();
- }
+ // Public //////////////////////////////////////////////////////////////////////////////
- /** 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);
+ 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) {
JSFunction ret = new JSFunction(sourceName, firstLine, s);
@@ -55,23 +46,15 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task {
ret.arg = this.arg;
ret.line = this.line;
ret.size = this.size;
- ret.formalArgs = this.formalArgs;
ret.numFormalArgs = this.numFormalArgs;
return ret;
}
- /** Note: code gets run in an unpauseable 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>>16) + " size: " + (n&0xffff));
}
sb.append("\n");
}