import java.io.*;
/** a JavaScript function, compiled into bytecode */
-class CompiledFunctionImpl extends JS.Obj implements ByteCodes, Tokens {
+public class Function extends JS.Obj implements ByteCodes, Tokens {
- // Fields and Accessors ///////////////////////////////////////////////
-
- /** the number of formal arguments */
- int numFormalArgs = 20;
-
- /** the source code file that this block was drawn from */
- String sourceName;
- public String getSourceName() throws JS.Exn { return sourceName; }
-
- /** the line numbers */
- int[] line = new int[10];
-
- /** the first line of this script */
- private int firstLine = -1;
+ public int getNumFormalArgs() { return numFormalArgs; }
- /** the instructions */
- int[] op = new int[10];
- /** the arguments to the instructions */
- Object[] arg = new Object[10];
+ // Fields and Accessors ///////////////////////////////////////////////
- /** the number of instruction/argument pairs */
- int size = 0;
- int size() { return size; }
+ int numFormalArgs = 0; ///< the number of formal arguments
+ String sourceName; ///< the source code file that this block was drawn from
+ int[] line = new int[10]; ///< the line numbers
+ private int firstLine = -1; ///< the first line of this script
+ int[] op = new int[10]; ///< the instructions
+ Object[] arg = new Object[10]; ///< the arguments to the instructions
+ int size = 0; ///< the number of instruction/argument pairs
+ JS.Scope parentScope; ///< the default scope to use as a parent scope when executing this
- /** the scope in which this function was declared; by default this function is called in a fresh subscope of the parentScope */
- JS.Scope parentScope;
// Constructors ////////////////////////////////////////////////////////
- private CompiledFunctionImpl cloneWithNewParentScope(JS.Scope s) throws IOException {
- CompiledFunctionImpl ret = new JS.CompiledFunction(sourceName, firstLine, null, s);
+ private Function cloneWithNewParentScope(JS.Scope s) {
+ Function ret = new Function(sourceName, firstLine, s);
// Reuse the same op, arg, line, and size variables for the new "instance" of the function
// NOTE: Neither *this* function nor the new function should be modified after this call
ret.op = this.op;
return ret;
}
- protected CompiledFunctionImpl(String sourceName, int firstLine, Reader sourceCode, JS.Scope parentScope) throws IOException {
+ private Function(String sourceName, int firstLine, JS.Scope parentScope) {
this.sourceName = sourceName;
this.firstLine = firstLine;
this.parentScope = parentScope;
+ }
+
+ protected Function(String sourceName, int firstLine, Reader sourceCode, JS.Scope parentScope) throws IOException {
+ this(sourceName, firstLine, parentScope);
if (sourceCode == null) return;
Parser p = new Parser(sourceCode, sourceName, firstLine);
while(true) {
- int s = size();
+ int s = size;
p.parseStatement(this, null);
- if (s == size()) break;
+ if (s == size) break;
}
add(-1, LITERAL, null);
add(-1, RETURN);
void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
void set(int pos, Object arg_) { arg[pos] = arg_; }
int pop() { size--; arg[size] = null; return op[size]; }
- void paste(CompiledFunctionImpl other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
- CompiledFunctionImpl add(int line, int op_) { return add(line, op_, null); }
- CompiledFunctionImpl add(int line, int op_, Object arg_) {
+ void paste(Function other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
+ Function add(int line, int op_) { return add(line, op_, null); }
+ Function add(int line, int op_, Object arg_) {
if (size == op.length - 1) {
int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
return this;
}
- public static int getLine(Context cx) {
- if(cx.pc < 0 || cx.pc >= ((CompiledFunctionImpl)cx.f).size) return -1;
- return ((CompiledFunctionImpl)cx.f).line[cx.pc];
- }
-
// Invoking the Bytecode ///////////////////////////////////////////////////////
/** returns false if the thread has been paused */
- static Object eval(final Context cx, Object pushFirst) throws JS.Exn {
- cx.stack.push(pushFirst);
+ static Object eval(final Context cx) throws JS.Exn {
OUTER: for(;; cx.pc++) {
try {
- if (cx.f == null) return cx.stack.pop();
- if (cx.pc >= ((CompiledFunctionImpl)cx.f).size) return cx.stack.pop();
- String label = null;
- int curOP = cx.f.op[cx.pc];
- Object curArg = cx.f.arg[cx.pc];
+ if (cx.f == null || cx.pc >= cx.f.size) return cx.stack.pop();
+ int op = cx.f.op[cx.pc];
+ Object arg = cx.f.arg[cx.pc];
Object returnedFromJava = null;
boolean checkReturnedFromJava = false;
- if(curOP == FINALLY_DONE) {
+ if(op == FINALLY_DONE) {
FinallyData fd = (FinallyData) cx.stack.pop();
if(fd == null) continue OUTER; // NOP
- curOP = fd.op;
- curArg = fd.arg;
+ op = fd.op;
+ arg = fd.arg;
}
- switch(curOP) {
- case LITERAL: cx.stack.push(curArg); break;
+ switch(op) {
+ case LITERAL: cx.stack.push(arg); break;
case OBJECT: cx.stack.push(new JS.Obj()); break;
- case ARRAY: cx.stack.push(new JS.Array(JS.toNumber(curArg).intValue())); break;
- case DECLARE: cx.scope.declare((String)(curArg==null ? cx.stack.peek() : curArg)); if(curArg != null) cx.stack.push(curArg); break;
+ case ARRAY: cx.stack.push(new JS.Array(JS.toNumber(arg).intValue())); break;
+ case DECLARE: cx.scope.declare((String)(arg==null ? cx.stack.peek() : arg)); if(arg != null) cx.stack.push(arg); break;
case TOPSCOPE: cx.stack.push(cx.scope); break;
- case JT: if (JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(curArg).intValue() - 1; break;
- case JF: if (!JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(curArg).intValue() - 1; break;
- case JMP: cx.pc += JS.toNumber(curArg).intValue() - 1; break;
+ case JT: if (JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(arg).intValue() - 1; break;
+ case JF: if (!JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(arg).intValue() - 1; break;
+ case JMP: cx.pc += JS.toNumber(arg).intValue() - 1; break;
case POP: cx.stack.pop(); break;
case SWAP: { Object o1 = cx.stack.pop(); Object o2 = cx.stack.pop(); cx.stack.push(o1); cx.stack.push(o2); break; }
case DUP: cx.stack.push(cx.stack.peek()); break;
case ASSERT: if (!JS.toBoolean(cx.stack.pop())) throw je("assertion failed"); break;
case BITNOT: cx.stack.push(new Long(~JS.toLong(cx.stack.pop()))); break;
case BANG: cx.stack.push(new Boolean(!JS.toBoolean(cx.stack.pop()))); break;
+ case NEWFUNCTION: cx.stack.push(((Function)arg).cloneWithNewParentScope(cx.scope)); break;
+ case LABEL: break;
case TYPEOF: {
Object o = cx.stack.pop();
break;
}
- case NEWFUNCTION: {
- try {
- cx.stack.push(((CompiledFunctionImpl)curArg).cloneWithNewParentScope(cx.scope));
- } catch (IOException e) {
- throw new Error("this should never happen");
- }
- break;
- }
-
case PUSHKEYS: {
Object o = cx.stack.peek();
Object[] keys = ((JS)o).keys();
break;
}
- case LABEL: break;
- case LOOP: {
- cx.stack.push(new LoopMarker(cx.pc, cx.pc > 0 && cx.f.op[cx.pc - 1] == LABEL ? (String)cx.f.arg[cx.pc - 1] : (String)null, cx.scope));
+ case LOOP:
+ cx.stack.push(new LoopMarker(cx.pc, cx.pc > 0 && cx.f.op[cx.pc - 1] == LABEL ?
+ (String)cx.f.arg[cx.pc - 1] : (String)null, cx.scope));
cx.stack.push(Boolean.TRUE);
break;
- }
case BREAK:
case CONTINUE:
if (o instanceof CallMarker) ee("break or continue not within a loop");
if (o instanceof TryMarker) {
if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
- cx.stack.push(new FinallyData(curOP, curArg));
+ cx.stack.push(new FinallyData(op, arg));
cx.scope = ((TryMarker)o).scope;
cx.pc = ((TryMarker)o).finallyLoc - 1;
continue OUTER;
}
if (o instanceof LoopMarker) {
- if (curArg == null || curArg.equals(((LoopMarker)o).label)) {
+ if (arg == null || arg.equals(((LoopMarker)o).label)) {
int loopInstructionLocation = ((LoopMarker)o).location;
int endOfLoop = ((Integer)cx.f.arg[loopInstructionLocation]).intValue() + loopInstructionLocation;
cx.scope = ((LoopMarker)o).scope;
- if (curOP == CONTINUE) { cx.stack.push(o); cx.stack.push(Boolean.FALSE); }
- cx.pc = curOP == BREAK ? endOfLoop - 1 : loopInstructionLocation;
+ if (op == CONTINUE) { cx.stack.push(o); cx.stack.push(Boolean.FALSE); }
+ cx.pc = op == BREAK ? endOfLoop - 1 : loopInstructionLocation;
continue OUTER;
}
}
}
- throw new Error("CONTINUE/BREAK invoked but couldn't find a LoopMarker at " + ((CompiledFunctionImpl)cx.f).sourceName + ":" + getLine(cx));
+ throw new Error("CONTINUE/BREAK invoked but couldn't find LoopMarker at " +
+ cx.getSourceName() + ":" + cx.getLine());
case TRY: {
- int[] jmps = (int[]) curArg;
+ int[] jmps = (int[]) arg;
// jmps[0] is how far away the catch block is, jmps[1] is how far away the finally block is
// each can be < 0 if the specified block does not exist
cx.stack.push(new TryMarker(jmps[0] < 0 ? -1 : cx.pc + jmps[0], jmps[1] < 0 ? -1 : cx.pc + jmps[1], cx.scope));
cx.scope = ((TryMarker)o).scope;
cx.pc = ((TryMarker)o).finallyLoc - 1;
continue OUTER;
- }
- if (o instanceof CallMarker) {
+ } else if (o instanceof CallMarker) {
cx.scope = ((CallMarker)o).scope;
cx.pc = ((CallMarker)o).pc;
- cx.f = (CompiledFunction)((CallMarker)o).f;
+ cx.f = (Function)((CallMarker)o).f;
cx.stack.push(retval);
continue OUTER;
}
if (key == null)
throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key");
returnedFromJava = ((JS)target).put(key, val);
- if (returnedFromJava != null) {
- checkReturnedFromJava = true;
- } else {
- cx.stack.push(val);
- }
+ if (returnedFromJava != null) checkReturnedFromJava = true;
+ else cx.stack.push(val);
break;
}
case GET:
case GET_PRESERVE: {
Object o, v;
- if (curOP == GET) {
- v = curArg == null ? cx.stack.pop() : curArg;
+ if (op == GET) {
+ v = arg == null ? cx.stack.pop() : arg;
o = cx.stack.pop();
} else {
v = cx.stack.pop();
Object ret = null;
if (o == null) throw je("tried to get property \"" + v + "\" from the null value");
if (v == null) throw je("tried to get the null key from " + o);
- if (o instanceof String || o instanceof Number || o instanceof Boolean)
+ if (o instanceof String || o instanceof Number || o instanceof Boolean) {
ret = Internal.getFromPrimitive(o,v);
- else if (o instanceof JS) {
+ cx.stack.push(ret);
+ break;
+ } else if (o instanceof JS) {
returnedFromJava = ((JS)o).get(v);
checkReturnedFromJava = true;
break;
- } else
- throw je("tried to get property " + v + " from a " + o.getClass().getName());
- cx.stack.push(ret);
- break;
+ }
+ throw je("tried to get property " + v + " from a " + o.getClass().getName());
}
- case CALLMETHOD:
- case CALL:
- case CALL_REVERSED:
- {
+ case CALL: case CALLMETHOD: case CALL_REVERSED: {
JS.Array arguments = new JS.Array();
- int numArgs = JS.toNumber(curArg).intValue();
+ int numArgs = JS.toNumber(arg).intValue();
arguments.setSize(numArgs);
Object o = null;
- if (curOP == CALL_REVERSED) o = cx.stack.pop();
+ if (op == CALL_REVERSED) o = cx.stack.pop();
for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(cx.stack.pop(), j);
- if (curOP != CALL_REVERSED) o = cx.stack.pop();
+ if (op != CALL_REVERSED) o = cx.stack.pop();
if(o == null) throw je("attempted to call null");
Object ret;
- if(curOP == CALLMETHOD) {
+ if(op == CALLMETHOD) {
Object method = o;
o = cx.stack.pop();
if(o instanceof String || o instanceof Number || o instanceof Boolean) {
ret = Internal.callMethodOnPrimitive(o,method,arguments);
cx.stack.push(ret);
cx.pc += 2; // skip the GET and CALL
- break;
} else if (o instanceof JS && ((JS)o).callMethod(method, arguments, true) == Boolean.TRUE) {
returnedFromJava = ((JS)o).callMethod(method, arguments, false);
checkReturnedFromJava = true;
cx.pc += 2; // skip the GET and CALL
- break;
} else {
// put the args back on the stack and let the GET followed by CALL happen
for(int j=0; j<numArgs; j++) cx.stack.push(arguments.elementAt(j));
cx.stack.push(o);
cx.stack.push(method);
- break;
}
- }
- if (o instanceof CompiledFunctionImpl) {
+ } else if (o instanceof Function) {
cx.stack.push(new CallMarker(cx));
cx.stack.push(arguments);
- cx.f = (CompiledFunction)o;
- cx.scope = new FunctionScope("unknown", cx.f.parentScope);
+ cx.f = (Function)o;
+ cx.scope = new Scope(cx.f.parentScope);
cx.pc = -1;
- break;
} else {
returnedFromJava = ((JS.Callable)o).call(arguments);
checkReturnedFromJava = true;
- break;
}
+ break;
}
case THROW: {
}
case INC: case DEC: {
- boolean isPrefix = JS.toBoolean(curArg);
+ boolean isPrefix = JS.toBoolean(arg);
Object key = cx.stack.pop();
JS obj = (JS)cx.stack.pop();
Number num = JS.toNumber(obj.get(key));
- Number val = new Double(curOP == INC ? num.doubleValue() + 1.0 : num.doubleValue() - 1.0);
+ Number val = new Double(op == INC ? num.doubleValue() + 1.0 : num.doubleValue() - 1.0);
obj.put(key, val);
cx.stack.push(isPrefix ? val : num);
break;
Object old = cx.stack.pop();
Object key = cx.stack.pop();
Object obj = cx.stack.peek();
- if (val instanceof CompiledFunction) {
- if (obj instanceof JS.Scope) {
- JS.Scope parent = (JS.Scope)obj;
- while(parent.getParentScope() != null) parent = parent.getParentScope();
- if (parent instanceof org.xwt.Box) {
- if (curOP == ASSIGN_ADD) {
- ((org.xwt.Box)parent).addTrap(key, val);
- } else {
- ((org.xwt.Box)parent).delTrap(key, val);
- }
- // skip over the "normal" implementation of +=/-=
- cx.pc += ((Integer)curArg).intValue() - 1;
- break;
- }
+ if (val instanceof Function && obj instanceof JS.Scope) {
+ JS.Scope parent = (JS.Scope)obj;
+ while(parent.getParentScope() != null) parent = parent.getParentScope();
+ if (parent instanceof org.xwt.Box) {
+ org.xwt.Box b = (org.xwt.Box)parent;
+ if (op == ASSIGN_ADD) b.addTrap(key, val); else b.delTrap(key, val);
+ // skip over the "normal" implementation of +=/-=
+ cx.pc += ((Integer)arg).intValue() - 1;
+ break;
}
}
// use the "normal" implementation
}
case ADD: {
- int count = ((Number)curArg).intValue();
+ int count = ((Number)arg).intValue();
if(count < 2) throw new Error("this should never happen");
if(count == 2) {
// common case
Object right = cx.stack.pop();
Object left = cx.stack.pop();
- if(left instanceof String || right instanceof String) cx.stack.push(JS.toString(left).concat(JS.toString(right)));
+ if(left instanceof String || right instanceof String)
+ cx.stack.push(JS.toString(left).concat(JS.toString(right)));
else cx.stack.push(new Double(JS.toDouble(left) + JS.toDouble(right)));
} else {
Object[] args = new Object[count];
default: {
Object right = cx.stack.pop();
Object left = cx.stack.pop();
- switch(curOP) {
+ switch(op) {
case BITOR: cx.stack.push(new Long(JS.toLong(left) | JS.toLong(right))); break;
case BITXOR: cx.stack.push(new Long(JS.toLong(left) ^ JS.toLong(right))); break;
} else {
result = (int)java.lang.Math.ceil(JS.toDouble(left) - JS.toDouble(right));
}
- cx.stack.push(new Boolean((curOP == LT && result < 0) || (curOP == LE && result <= 0) ||
- (curOP == GT && result > 0) || (curOP == GE && result >= 0)));
+ cx.stack.push(new Boolean((op == LT && result < 0) || (op == LE && result <= 0) ||
+ (op == GT && result > 0) || (op == GE && result >= 0)));
break;
}
else if (l instanceof Number) ret = JS.toNumber(r).doubleValue() == JS.toNumber(l).doubleValue();
else if (l instanceof String) ret = r != null && l.equals(r.toString());
else ret = l.equals(r);
- cx.stack.push(new Boolean(curOP == EQ ? ret : !ret)); break;
+ cx.stack.push(new Boolean(op == EQ ? ret : !ret)); break;
}
- default: throw new Error("unknown opcode " + curOP);
+ default: throw new Error("unknown opcode " + op);
} }
}
cx.stack.push(new CallMarker(cx));
cx.stack.push(((JS.TailCall)returnedFromJava).args);
cx.f = ((JS.TailCall)returnedFromJava).func;
- cx.scope = new CompiledFunctionImpl.FunctionScope("unknown", cx.f.parentScope);
+ cx.scope = new Scope(cx.f.parentScope);
cx.pc = -1;
} else {
cx.stack.push(returnedFromJava);
} // end for
}
+
// Debugging //////////////////////////////////////////////////////////////////////
- public String toString() {
+ public String toString() { return "Function [" + sourceName + ":" + firstLine + "]"; }
+ public String dump() {
StringBuffer sb = new StringBuffer(1024);
sb.append("\n" + sourceName + ": " + firstLine + "\n");
for (int i=0; i < size; i++) {
return sb.toString();
}
+
// Exception Stuff ////////////////////////////////////////////////////////////////
static class EvaluatorException extends RuntimeException { public EvaluatorException(String s) { super(s); } }
static EvaluatorException ee(String s) {
- throw new EvaluatorException(JS.Context.getSourceName() + ":" + JS.Context.getLine() + " " + s);
+ throw new EvaluatorException(Context.getSourceName() + ":" + Context.getLine() + " " + s);
}
static JS.Exn je(String s) {
- throw new JS.Exn(JS.Context.getSourceName() + ":" + JS.Context.getLine() + " " + s);
- }
-
- // FunctionScope /////////////////////////////////////////////////////////////////
-
- static class FunctionScope extends JS.Scope {
- String sourceName;
- public FunctionScope(String sourceName, Scope parentScope) { super(parentScope); this.sourceName = sourceName; }
- public String getSourceName() { return sourceName; }
+ throw new JS.Exn(Context.getSourceName() + ":" + Context.getLine() + " " + s);
}
public static class CallMarker {
int pc;
Scope scope;
- CompiledFunctionImpl f;
+ Function f;
public CallMarker(Context cx) { pc = cx.pc + 1; scope = cx.scope; f = cx.f; }
}
}
}
-/** this class exists solely to work around a GCJ bug */
-abstract class JSCallable extends JS.Callable {
- public abstract Object call(JS.Array args) throws JS.Exn;
-}