+++ /dev/null
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex.js;
-
-import org.ibex.util.*;
-import java.util.*;
-
-/** Encapsulates a single JS interpreter (ie call stack) */
-class Interpreter implements ByteCodes, Tokens {
- // Thread-Interpreter Mapping /////////////////////////////////////////////////////////////////////////
-
- static Interpreter current() { return (Interpreter)threadToInterpreter.get(Thread.currentThread()); }
- private static Hashtable threadToInterpreter = new Hashtable();
-
-
- // Instance members and methods //////////////////////////////////////////////////////////////////////
-
- int pausecount; ///< the number of times pause() has been invoked; -1 indicates unpauseable
- JSFunction f = null; ///< the currently-executing JSFunction
- JSScope scope; ///< the current top-level scope (LIFO stack via NEWSCOPE/OLDSCOPE)
- final Stack stack = new Stack(); ///< the object stack
- int pc = 0; ///< the program counter
-
- Interpreter(JSFunction f, boolean pauseable, JSArgs args) {
- this.f = f;
- this.pausecount = pauseable ? 0 : -1;
- this.scope = f.parentScope;
- try {
- stack.push(new CallMarker(null)); // the "root function returned" marker -- f==null
- stack.push(args);
- } catch(JSExn e) {
- throw new Error("should never happen");
- }
- }
-
- Interpreter(Trap t, JS val, boolean pauseOnPut) {
- this.pausecount = -1;
- try {
- setupTrap(t,val,new TrapMarker(null,t,val,pauseOnPut));
- } catch(JSExn e) {
- throw new Error("should never happen");
- }
- }
-
- /** this is the only synchronization point we need in order to be threadsafe */
- synchronized JS resume() throws JSExn {
- if(f == null) throw new RuntimeException("function already finished");
- if(scope == null) throw new RuntimeException("scope is null");
- Thread t = Thread.currentThread();
- Interpreter old = (Interpreter)threadToInterpreter.get(t);
- threadToInterpreter.put(t, this);
- try {
- return run();
- } finally {
- if (old == null) threadToInterpreter.remove(t);
- else threadToInterpreter.put(t, old);
- }
- }
-
- static int getLine() {
- Interpreter c = Interpreter.current();
- return c == null || c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
- }
-
- static String getSourceName() {
- Interpreter c = Interpreter.current();
- return c == null || c.f == null ? null : c.f.sourceName;
- }
-
- private static JSExn je(String s) { return new JSExn(getSourceName() + ":" + getLine() + " " + s); }
-
- private JS run() throws JSExn {
-
- // if pausecount changes after a get/put/call, we know we've been paused
- final int initialPauseCount = pausecount;
-
- OUTER: for(;; pc++) {
- try {
- int op = f.op[pc];
- Object arg = f.arg[pc];
- if(op == FINALLY_DONE) {
- FinallyData fd = (FinallyData) stack.pop();
- if(fd == null) continue OUTER; // NOP
- if(fd.exn != null) throw fd.exn;
- op = fd.op;
- arg = fd.arg;
- }
- switch(op) {
- case LITERAL: stack.push((JS)arg); break;
- case OBJECT: stack.push(new JS.O()); break;
- case ARRAY: stack.push(new JSArray(JS.toInt((JS)arg))); break;
- //case DECLARE: scope.declare((JS)(arg==null ? stack.peek() : arg)); if(arg != null) stack.push((JS)arg); break;
- case JT: if (JS.toBoolean(stack.pop())) pc += JS.toInt((JS)arg) - 1; break;
- case JF: if (!JS.toBoolean(stack.pop())) pc += JS.toInt((JS)arg) - 1; break;
- case JMP: pc += JS.toInt((JS)arg) - 1; break;
- case POP: stack.pop(); break;
- case SWAP: stack.swap(); break;
- case DUP: stack.push(stack.peek()); break;
- case NEWSCOPE: {
- int n = JS.toInt((JS)arg);
- scope = new JSScope(scope,(n>>>16)&0xffff,(n>>>0)&0xffff);
- break;
- }
- case OLDSCOPE: scope = scope.parent; break;
- case GLOBALSCOPE: stack.push(scope.getGlobal()); break;
- case SCOPEGET: stack.push(scope.get((JS)arg)); break;
- case SCOPEPUT: scope.put((JS)arg,stack.peek()); break;
- case ASSERT: if (!JS.toBoolean(stack.pop())) throw je("ibex.assertion.failed"); break;
- case BITNOT: stack.push(JS.N(~JS.toLong(stack.pop()))); break;
- case BANG: stack.push(JS.B(!JS.toBoolean(stack.pop()))); break;
- case NEWFUNCTION: stack.push(((JSFunction)arg)._cloneWithNewParentScope(scope)); break;
- case LABEL: break;
-
- case TYPEOF: {
- Object o = stack.pop();
- if (o == null) stack.push(null);
- else if (o instanceof JSString) stack.push(JS.S("string"));
- else if (o instanceof JSNumber.B) stack.push(JS.S("boolean"));
- else if (o instanceof JSNumber) stack.push(JS.S("number"));
- else stack.push(JS.S("object"));
- break;
- }
-
- case PUSHKEYS: {
- JS o = stack.peek();
- stack.push(o == null ? null : o.keys());
- break;
- }
-
- case LOOP:
- stack.push(new LoopMarker(pc, pc > 0 && f.op[pc - 1] == LABEL ? (String)f.arg[pc - 1] : (String)null, scope));
- stack.push(JS.T);
- break;
-
- case BREAK:
- case CONTINUE:
- while(!stack.empty()) {
- JS o = stack.pop();
- if (o instanceof CallMarker) je("break or continue not within a loop");
- if (o instanceof TryMarker) {
- if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
- stack.push(new FinallyData(op, arg));
- scope = ((TryMarker)o).scope;
- pc = ((TryMarker)o).finallyLoc - 1;
- continue OUTER;
- }
- if (o instanceof LoopMarker) {
- if (arg == null || arg.equals(((LoopMarker)o).label)) {
- int loopInstructionLocation = ((LoopMarker)o).location;
- int endOfLoop = JS.toInt((JS)f.arg[loopInstructionLocation]) + loopInstructionLocation;
- scope = ((LoopMarker)o).scope;
- if (op == CONTINUE) { stack.push(o); stack.push(JS.F); }
- pc = op == BREAK ? endOfLoop - 1 : loopInstructionLocation;
- continue OUTER;
- }
- }
- }
- throw new Error("CONTINUE/BREAK invoked but couldn't find LoopMarker at " +
- getSourceName() + ":" + getLine());
-
- case TRY: {
- 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
- stack.push(new TryMarker(jmps[0] < 0 ? -1 : pc + jmps[0], jmps[1] < 0 ? -1 : pc + jmps[1], this));
- break;
- }
-
- case RETURN: {
- JS retval = stack.pop();
- while(!stack.empty()) {
- Object o = stack.pop();
- if (o instanceof TryMarker) {
- if(((TryMarker)o).finallyLoc < 0) continue;
- stack.push(retval);
- stack.push(new FinallyData(RETURN));
- scope = ((TryMarker)o).scope;
- pc = ((TryMarker)o).finallyLoc - 1;
- continue OUTER;
- } else if (o instanceof CallMarker) {
- boolean didTrapPut = false;
- if (o instanceof TrapMarker) { // handles return component of a write trap
- TrapMarker tm = (TrapMarker) o;
- boolean cascade = tm.t.isWriteTrap() && !tm.cascadeHappened && !JS.toBoolean(retval);
- if(cascade) {
- Trap t = tm.t.nextWriteTrap();
- if(t == null && tm.t.target instanceof JS.Clone) {
- t = ((JS.Clone)tm.t.target).clonee.getTrap(tm.t.key);
- if(t != null) t = t.writeTrap();
- }
- if(t != null) {
- tm.t = t; // we reuse the old trap marker
- setupTrap(t,tm.val,tm);
- pc--; // we increment it on the next iter
- continue OUTER;
- } else {
- didTrapPut = true;
- if(!tm.pauseOnPut) tm.t.target.put(tm.t.key,tm.val);
- }
- }
- }
- CallMarker cm = (CallMarker) o;
- scope = cm.scope;
- pc = cm.pc - 1;
- f = cm.f;
- if (didTrapPut) {
- if (((TrapMarker)cm).pauseOnPut) { pc++; return ((TrapMarker)cm).val; }
- if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
- } else {
- stack.push(retval);
- }
- if (f == null) return retval;
- continue OUTER;
- }
- }
- throw new Error("error: RETURN invoked but couldn't find a CallMarker!");
- }
-
- case CASCADE: {
- boolean write = JS.toBoolean((JS)arg);
- JS val = write ? stack.pop() : null;
- CallMarker o = stack.findCall();
- if(!(o instanceof TrapMarker)) throw new JSExn("tried to CASCADE while not in a trap");
- TrapMarker tm = (TrapMarker) o;
- JS key = tm.t.key;
- JS target = tm.t.target;
- if(tm.t.isWriteTrap() != write) throw new JSExn("tried to do a " + (write?"write":"read") + " cascade in a " + (write?"read":"write") + " trap");
- Trap t = write ? tm.t.nextWriteTrap() : tm.t.nextReadTrap();
- // FIXME: Doesn't handle multiple levels of clone's (probably can just make this a while loop)
- if(t == null && target instanceof JS.Clone) {
- target = ((JS.Clone)target).clonee;
- t = target.getTrap(key);
- if(t != null) t = write ? t.writeTrap() : t.readTrap();
- }
- if(write) {
- tm.cascadeHappened = true;
- stack.push(val);
- }
- if(t != null) {
- setupTrap(t,val,new TrapMarker(this,t,val,tm.pauseOnPut));
- pc--; // we increment later
- } else {
- if(write) {
- if (tm.pauseOnPut) { pc++; return val; }
- target.put(key,val);
- } else {
- JS ret = target.get(key);
- if (ret == JS.METHOD) ret = new Stub(target, key);
- stack.push(ret);
- }
- if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
- }
- break;
- }
-
- case PUT: {
- JS val = stack.pop();
- JS key = stack.pop();
- JS target = stack.peek();
- if (target == null) throw je("tried to put " + JS.debugToString(val) + " to the " + JS.debugToString(key) + " property on the null value");
- if (key == null) throw je("tried to assign \"" + JS.debugToString(val) + "\" to the null key");
-
- Trap t = target.getTrap(key);
- if(t != null) t = t.writeTrap();
-
- if(t == null && target instanceof JS.Clone) {
- target = ((JS.Clone)target).clonee;
- t = target.getTrap(key);
- if(t != null) t = t.writeTrap();
- }
-
- stack.push(val);
-
- if(t != null) {
- setupTrap(t,val,new TrapMarker(this,t,val));
- pc--; // we increment later
- } else {
- target.put(key,val);
- if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
- }
- break;
- }
-
- case GET:
- case GET_PRESERVE: {
- JS target, key;
- if (op == GET) {
- key = arg == null ? stack.pop() : (JS)arg;
- target = stack.pop();
- } else {
- key = stack.pop();
- target = stack.peek();
- stack.push(key);
- }
- JS ret = null;
- if (key == null) throw je("tried to get the null key from " + JS.debugToString(target));
- if (target == null) throw je("tried to get property \"" + JS.debugToString(key) + "\" from the null object");
-
- Trap t = target.getTrap(key);
- if(t != null) t = t.readTrap();
-
- if(t == null && target instanceof JS.Clone) {
- target = ((JS.Clone)target).clonee;
- t = target.getTrap(key);
- if(t != null) t = t.readTrap();
- }
-
- if(t != null) {
- setupTrap(t,null,new TrapMarker(this,t,null));
- pc--; // we increment later
- } else {
- ret = target.get(key);
- if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
- if (ret == JS.METHOD) ret = new Stub(target, key);
- stack.push(ret);
- }
- break;
- }
-
- case CALL: case CALLMETHOD: {
- int numArgs = JS.toInt((JS)arg);
-
- JS[] rest = numArgs > 3 ? new JS[numArgs - 3] : null;
- for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop();
- JS a2 = numArgs <= 2 ? null : stack.pop();
- JS a1 = numArgs <= 1 ? null : stack.pop();
- JS a0 = numArgs <= 0 ? null : stack.pop();
-
- JS method = null;
- JS ret = null;
- JS object = stack.pop();
-
- if (op == CALLMETHOD) {
- if (object == JS.METHOD) {
- method = stack.pop();
- object = stack.pop();
- } else if (object == null) {
- method = stack.pop();
- object = stack.pop();
- throw new JSExn("function '"+JS.debugToString(method)+"' not found in " + object.getClass().getName());
- } else {
- stack.pop();
- stack.pop();
- }
- }
-
- if (object instanceof JSFunction) {
- stack.push(new CallMarker(this));
- stack.push(new JSArgs(a0,a1,a2,rest,numArgs,object));
- f = (JSFunction)object;
- scope = f.parentScope;
- pc = -1;
- break;
- } else {
- JS c = (JS)object;
- ret = method == null ? c.call(a0, a1, a2, rest, numArgs) : c.callMethod(method, a0, a1, a2, rest, numArgs);
- }
-
- if (pausecount > initialPauseCount) { pc++; return null; }
- stack.push(ret);
- break;
- }
-
- case THROW:
- throw new JSExn(stack.pop(), this);
-
- /* FIXME GRAMMAR
- case MAKE_GRAMMAR: {
- final Grammar r = (Grammar)arg;
- final JSScope final_scope = scope;
- Grammar r2 = new Grammar() {
- public int match(String s, int start, Hash v, JSScope scope) throws JSExn {
- return r.match(s, start, v, final_scope);
- }
- public int matchAndWrite(String s, int start, Hash v, JSScope scope, String key) throws JSExn {
- return r.matchAndWrite(s, start, v, final_scope, key);
- }
- public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
- Hash v = new Hash();
- r.matchAndWrite((String)a0, 0, v, final_scope, "foo");
- return v.get("foo");
- }
- };
- Object obj = stack.pop();
- if (obj != null && obj instanceof Grammar) r2 = new Grammar.Alternative((Grammar)obj, r2);
- stack.push(r2);
- break;
- }
- */
- case ADD_TRAP: case DEL_TRAP: {
- JS val = stack.pop();
- JS key = stack.pop();
- JS js = stack.peek();
- // A trap addition/removal
- if(!(val instanceof JSFunction)) throw new JSExn("tried to add/remove a non-function trap");
- if(op == ADD_TRAP) js.addTrap(key, (JSFunction)val);
- else js.delTrap(key, (JSFunction)val);
- break;
- }
-
- case ADD: {
- int count = ((JSNumber)arg).toInt();
- if(count < 2) throw new Error("this should never happen");
- if(count == 2) {
- // common case
- JS right = stack.pop();
- JS left = stack.pop();
- JS ret;
- if(left instanceof JSString || right instanceof JSString)
- ret = JS.S(JS.toString(left).concat(JS.toString(right)));
- else if(left instanceof JSNumber.D || right instanceof JSNumber.D)
- ret = JS.N(JS.toDouble(left) + JS.toDouble(right));
- else {
- long l = JS.toLong(left) + JS.toLong(right);
- if(l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) ret = JS.N(l);
- ret = JS.N((int)l);
- }
- stack.push(ret);
- } else {
- JS[] args = new JS[count];
- while(--count >= 0) args[count] = stack.pop();
- if(args[0] instanceof JSString) {
- StringBuffer sb = new StringBuffer(64);
- for(int i=0;i<args.length;i++) sb.append(JS.toString(args[i]));
- stack.push(JS.S(sb.toString()));
- } else {
- int numStrings = 0;
- for(int i=0;i<args.length;i++) if(args[i] instanceof JSString) numStrings++;
- if(numStrings == 0) {
- double d = 0.0;
- for(int i=0;i<args.length;i++) d += JS.toDouble(args[i]);
- stack.push(JS.N(d));
- } else {
- int i=0;
- StringBuffer sb = new StringBuffer(64);
- if(!(args[0] instanceof JSString || args[1] instanceof JSString)) {
- double d=0.0;
- do {
- d += JS.toDouble(args[i++]);
- } while(!(args[i] instanceof JSString));
- sb.append(JS.toString(JS.N(d)));
- }
- while(i < args.length) sb.append(JS.toString(args[i++]));
- stack.push(JS.S(sb.toString()));
- }
- }
- }
- break;
- }
-
- default: {
- JS right = stack.pop();
- JS left = stack.pop();
- switch(op) {
-
- case BITOR: stack.push(JS.N(JS.toLong(left) | JS.toLong(right))); break;
- case BITXOR: stack.push(JS.N(JS.toLong(left) ^ JS.toLong(right))); break;
- case BITAND: stack.push(JS.N(JS.toLong(left) & JS.toLong(right))); break;
-
- case SUB: stack.push(JS.N(JS.toDouble(left) - JS.toDouble(right))); break;
- case MUL: stack.push(JS.N(JS.toDouble(left) * JS.toDouble(right))); break;
- case DIV: stack.push(JS.N(JS.toDouble(left) / JS.toDouble(right))); break;
- case MOD: stack.push(JS.N(JS.toDouble(left) % JS.toDouble(right))); break;
-
- case LSH: stack.push(JS.N(JS.toLong(left) << JS.toLong(right))); break;
- case RSH: stack.push(JS.N(JS.toLong(left) >> JS.toLong(right))); break;
- case URSH: stack.push(JS.N(JS.toLong(left) >>> JS.toLong(right))); break;
-
- //#repeat </<=/>/>= LT/LE/GT/GE
- case LT: {
- if(left instanceof JSString && right instanceof JSString)
- stack.push(JS.B(JS.toString(left).compareTo(JS.toString(right)) < 0));
- else
- stack.push(JS.B(JS.toDouble(left) < JS.toDouble(right)));
- }
- //#end
-
- case EQ:
- case NE: {
- boolean ret;
- if(left == null && right == null) ret = true;
- else if(left == null || right == null) ret = false;
- else ret = left.jsequals(right);
- stack.push(JS.B(op == EQ ? ret : !ret)); break;
- }
-
- default: throw new Error("unknown opcode " + op);
- } }
- }
-
- } catch(JSExn e) {
- catchException(e);
- pc--; // it'll get incremented on the next iteration
- } // end try/catch
- } // end for
- }
-
- /** tries to find a handler withing the call chain for this exception
- if a handler is found the interpreter is setup to call the exception handler
- if a handler is not found the exception is thrown
- */
- void catchException(JSExn e) throws JSExn {
- while(!stack.empty()) {
- JS o = stack.pop();
- if (o instanceof CatchMarker || o instanceof TryMarker) {
- boolean inCatch = o instanceof CatchMarker;
- if(inCatch) {
- o = stack.pop();
- if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
- }
- if(!inCatch && ((TryMarker)o).catchLoc >= 0) {
- // run the catch block, this will implicitly run the finally block, if it exists
- stack.push(o);
- stack.push(catchMarker);
- stack.push(e.getObject());
- f = ((TryMarker)o).f;
- scope = ((TryMarker)o).scope;
- pc = ((TryMarker)o).catchLoc;
- return;
- } else {
- stack.push(new FinallyData(e));
- f = ((TryMarker)o).f;
- scope = ((TryMarker)o).scope;
- pc = ((TryMarker)o).finallyLoc;
- return;
- }
- }
- }
- throw e;
- }
-
- void setupTrap(Trap t, JS val, CallMarker cm) throws JSExn {
- stack.push(cm);
- stack.push(new TrapArgs(t,val));
- f = t.f;
- scope = f.parentScope;
- pc = 0;
- }
-
-
- // Markers //////////////////////////////////////////////////////////////////////
-
- static class Marker extends JS {
- public JS get(JS key) throws JSExn { throw new Error("this should not be accessible from a script"); }
- public void put(JS key, JS val) throws JSExn { throw new Error("this should not be accessible from a script"); }
- public String coerceToString() { throw new Error("this should not be accessible from a script"); }
- public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { throw new Error("this should not be accessible from a script"); }
- }
-
- static class CallMarker extends Marker {
- final int pc;
- final JSScope scope;
- final JSFunction f;
- public CallMarker(Interpreter cx) {
- pc = cx == null ? -1 : cx.pc + 1;
- scope = cx == null ? null : cx.scope;
- f = cx == null ? null : cx.f;
- }
- }
-
- static class TrapMarker extends CallMarker {
- Trap t;
- JS val;
- boolean cascadeHappened;
- final boolean pauseOnPut;
- public TrapMarker(Interpreter cx, Trap t, JS val) { this(cx,t,val,false); }
- public TrapMarker(Interpreter cx, Trap t, JS val, boolean pauseOnPut) {
- super(cx);
- this.t = t;
- this.val = val;
- this.pauseOnPut = pauseOnPut;
- }
- }
-
- static class CatchMarker extends Marker { }
- private static CatchMarker catchMarker = new CatchMarker();
-
- static class LoopMarker extends Marker {
- final public int location;
- final public String label;
- final public JSScope scope;
- public LoopMarker(int location, String label, JSScope scope) {
- this.location = location;
- this.label = label;
- this.scope = scope;
- }
- }
- static class TryMarker extends Marker {
- final public int catchLoc;
- final public int finallyLoc;
- final public JSScope scope;
- final public JSFunction f;
- public TryMarker(int catchLoc, int finallyLoc, Interpreter cx) {
- this.catchLoc = catchLoc;
- this.finallyLoc = finallyLoc;
- this.scope = cx.scope;
- this.f = cx.f;
- }
- }
- static class FinallyData extends Marker {
- final public int op;
- final public Object arg;
- final public JSExn exn;
- public FinallyData(int op) { this(op,null); }
- public FinallyData(int op, Object arg) { this.op = op; this.arg = arg; this.exn = null; }
- public FinallyData(JSExn exn) { this.exn = exn; this.op = -1; this.arg = null; } // Just throw this exn
- }
-
- static class TrapArgs extends JS {
- private Trap t;
- private JS val;
- public TrapArgs(Trap t, JS val) { this.t = t; this.val = val; }
- public JS get(JS key) throws JSExn {
- if(JS.isInt(key) && JS.toInt(key) == 0) return val;
- //#jswitch(key)
- case "trapee": return t.target;
- case "callee": return t.f;
- case "trapname": return t.key;
- case "length": return t.isWriteTrap() ? ONE : ZERO;
- //#end
- return super.get(key);
- }
- }
-
- static class JSArgs extends JS {
- private final JS a0;
- private final JS a1;
- private final JS a2;
- private final JS[] rest;
- private final int nargs;
- private final JS callee;
-
- public JSArgs(JS callee) { this(null,null,null,null,0,callee); }
- public JSArgs(JS a0, JS callee) { this(a0,null,null,null,1,callee); }
- public JSArgs(JS a0, JS a1, JS a2, JS[] rest, int nargs, JS callee) {
- this.a0 = a0; this.a1 = a1; this.a2 = a2;
- this.rest = rest; this.nargs = nargs;
- this.callee = callee;
- }
-
- public JS get(JS key) throws JSExn {
- if(JS.isInt(key)) {
- int n = JS.toInt(key);
- switch(n) {
- case 0: return a0;
- case 1: return a1;
- case 2: return a2;
- default: return n>= 0 && n < nargs ? rest[n-3] : null;
- }
- }
- //#jswitch(key)
- case "callee": return callee;
- case "length": return JS.N(nargs);
- //#end
- return super.get(key);
- }
- }
-
- static class Stub extends JS {
- private JS method;
- JS obj;
- public Stub(JS obj, JS method) { this.obj = obj; this.method = method; }
- public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
- return ((JS)obj).callMethod(method, a0, a1, a2, rest, nargs);
- }
- }
-
- static class Stack {
- private static final int MAX_STACK_SIZE = 512;
- private JS[] stack = new JS[64];
- private int sp = 0;
-
- boolean empty() { return sp == 0; }
- void push(JS o) throws JSExn { if(sp == stack.length) grow(); stack[sp++] = o; }
- JS peek() { if(sp == 0) throw new RuntimeException("Stack underflow"); return stack[sp-1]; }
- final JS pop() { if(sp == 0) throw new RuntimeException("Stack underflow"); return stack[--sp]; }
- void swap() throws JSExn {
- if(sp < 2) throw new JSExn("stack overflow");
- JS tmp = stack[sp-2];
- stack[sp-2] = stack[sp-1];
- stack[sp-1] = tmp;
- }
- CallMarker findCall() {
- for(int i=sp-1;i>=0;i--) if(stack[i] instanceof CallMarker) return (CallMarker) stack[i];
- return null;
- }
- void grow() throws JSExn {
- if(stack.length >= MAX_STACK_SIZE) throw new JSExn("Stack overflow");
- JS[] stack2 = new JS[stack.length * 2];
- System.arraycopy(stack,0,stack2,0,stack.length);
- }
-
- void backtrace(JSExn e) {
- for(int i=sp-1;i>=0;i--) {
- if (stack[i] instanceof CallMarker) {
- CallMarker cm = (CallMarker)stack[i];
- if(cm.f == null) break;
- String s = cm.f.sourceName + ":" + cm.f.line[cm.pc-1];
- if(cm instanceof Interpreter.TrapMarker)
- s += " (trap on " + JS.debugToString(((Interpreter.TrapMarker)cm).t.key) + ")";
- e.addBacktrace(s);
- }
- }
- }
- }
-}