X-Git-Url: http://git.megacz.com/?p=org.ibex.js.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FInterpreter.java;h=2b4e8887fb0f31ccef244f8cb3eaf549529ec4e9;hp=693ad9c7245886b3a6daf2c825f8d3ab28f319ff;hb=HEAD;hpb=a1e6b7e9307319c0195b0efbe5e5354c128be481 diff --git a/src/org/ibex/js/Interpreter.java b/src/org/ibex/js/Interpreter.java index 693ad9c..2b4e888 100644 --- a/src/org/ibex/js/Interpreter.java +++ b/src/org/ibex/js/Interpreter.java @@ -29,7 +29,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable { this.scope = f.parentScope; try { stack.push(new CallMarker(null)); // the "root function returned" marker -- f==null - stack.push(args); + stack.push(new JSArgs(args, f)); // FIXME: temprorary bug fix } catch(JSExn e) { throw new Error("should never happen"); } @@ -71,8 +71,8 @@ class Interpreter implements ByteCodes, Tokens, Pausable { pausecount++; switch(f.op[pc]) { case Tokens.RETURN: case ByteCodes.PUT: get = false; break; - case ByteCodes.GET: case ByteCodes.CALL: get = true; break; - default: throw new Error("should never happen"); + case ByteCodes.GET: case ByteCodes.GET_PRESERVE: case ByteCodes.CALLMETHOD: case ByteCodes.CALL: get = true; break; + default: throw new Error("paused on unexpected bytecode: " + f.op[pc]); } } @@ -123,7 +123,12 @@ class Interpreter implements ByteCodes, Tokens, Pausable { 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, (JS)stack.peek()); break; + case SCOPEPUT: { + // FIXME: HACK: share this around more and find the callee. + Object val = stack.peek(); + if (val != null && val instanceof JS[]) val = new JSArgs((JS[])val, null); + scope.put((JS)arg, (JS)val); break; + } case ASSERT: if (!JSU.toBoolean((JS)stack.pop())) throw je("ibex.assertion.failed"); break; case BITNOT: stack.push(JSU.N(~JSU.toLong((JS)stack.pop()))); break; case BANG: stack.push(JSU.B(!JSU.toBoolean((JS)stack.pop()))); break; @@ -154,7 +159,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable { case BREAK: case CONTINUE: while(!stack.empty()) { - JS o = (JS)stack.pop(); + Object 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 @@ -243,10 +248,10 @@ class Interpreter implements ByteCodes, Tokens, Pausable { 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"); + if(tm.t.isWriteTrap() != write) + throw new JSExn("tried to do a "+(write?"write":"read") + " cascade in a " + (write?"read":"write") + " trap"); JS.Trap t = write ? tm.t.nextWrite() : tm.t.nextRead(); - // FIXME: Doesn't handle multiple levels of clone's (probably can just make this a while loop) - if(t == null && target instanceof JS.Clone) { + while (t == null && target instanceof JS.Clone) { target = ((JS.Clone)target).clonee; t = target.getTrap(key); if(t != null) t = write ? t.write() : t.read(); @@ -315,7 +320,8 @@ class Interpreter implements ByteCodes, Tokens, Pausable { if (key == null) throw je("tried to get the null key from " + JSU.str(target)); if (target == null) throw je("tried to get property \"" + JSU.str(key) + "\" from the null object"); - JS.Trap t = target.getTrap(key); + JS.Trap t = null; + try { t = target.getTrap(key); } catch (JSExn e) {} if(t != null) t = t.read(); if(t == null && target instanceof JS.Clone) { @@ -338,10 +344,9 @@ class Interpreter implements ByteCodes, Tokens, Pausable { } case CALL: case CALLMETHOD: { - // FIXME: can a lot of simple cases in Parser be - // reduced so creating a new JS[] is not necessary? JS[] jsargs; if (arg instanceof JSNumber.I) { + // FIXME: we should be able to recycle JS[]'s here jsargs = new JS[((JSNumber.I)arg).toInt()]; for (int i=0; i < jsargs.length; i++) jsargs[i] = (JS)stack.pop(); } else jsargs = (JS[])arg; @@ -366,14 +371,14 @@ class Interpreter implements ByteCodes, Tokens, Pausable { if (object instanceof JSFunction) { stack.push(new CallMarker(this)); - stack.push(jsargs); f = (JSFunction)object; + stack.push(new JSArgs(jsargs, f)); scope = f.parentScope; pc = -1; break; } else { JS c = (JS)object; - ret = method == null ? c.call(jsargs) : c.call(method, jsargs); + ret = c.call(method, jsargs); } if (pausecount > initialPauseCount) { pc++; return null; } @@ -384,35 +389,12 @@ class Interpreter implements ByteCodes, Tokens, Pausable { case THROW: throw new JSExn((JS)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, Map v, JSScope scope) throws JSExn { - return r.match(s, start, v, final_scope); - } - public int matchAndWrite(String s, int start, Map 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 { - Map v = new Map(); - 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 = (JS)stack.pop(); JS key = (JS)stack.pop(); JS js = (JS)stack.peek(); // A trap addition/removal - if(!(val instanceof JSFunction)) throw new JSExn("tried to add/remove a non-function trap"); // FIXME + if(!(val instanceof JSFunction)) throw new JSExn("tried to add/remove a non-function trap"); if(op == ADD_TRAP) js.addTrap(key, val); else js.delTrap(key, val); break; @@ -486,14 +468,21 @@ class Interpreter implements ByteCodes, Tokens, Pausable { case RSH: stack.push(JSU.N(JSU.toLong(left) >> JSU.toLong(right))); break; case URSH: stack.push(JSU.N(JSU.toLong(left) >>> JSU.toLong(right))); break; - //#repeat />= LT/LE/GT/GE - case LT: { + case LT: case LE: case GT: case GE: { + int cmp = 0; if(left instanceof JSString && right instanceof JSString) - stack.push(JSU.B(JSU.toString(left).compareTo(JSU.toString(right)) < 0)); + cmp = JSU.toString(left).compareTo(JSU.toString(right)); else - stack.push(JSU.B(JSU.toDouble(left) < JSU.toDouble(right))); + cmp = (int)(100 * (JSU.toDouble(left) - JSU.toDouble(right))); + switch(op) { + case LE: stack.push(JSU.B(cmp <= 0)); break; + case LT: stack.push(JSU.B(cmp < 0)); break; + case GE: stack.push(JSU.B(cmp >= 0)); break; + case GT: stack.push(JSU.B(cmp > 0)); break; + default: throw new RuntimeException("impossible"); + } + break; } - //#end case EQ: case NE: { @@ -552,7 +541,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable { void setupTrap(JS.Trap t, JS val, CallMarker cm) throws JSExn { stack.push(cm); stack.push(new TrapArgs(t, val)); - f = (JSFunction)t.function(); // FIXME + f = (JSFunction)t.function(); scope = f.parentScope; pc = 0; } @@ -621,6 +610,25 @@ class Interpreter implements ByteCodes, Tokens, Pausable { public FinallyData(JSExn exn) { this.exn = exn; this.op = -1; this.arg = null; } // Just throw this exn } + static class JSArgs extends JS.Immutable { + private final JS[] args; + private final JS callee; + + public JSArgs(JS[] args, JS callee) { this.args = args; this.callee = callee; } + + public JS get(JS key) throws JSExn { + if(JSU.isInt(key)) { + int i = JSU.toInt(key); + return i>=args.length ? null : args[i]; + } + //#switch(JSU.toString(key)) + case "callee": return callee; + case "length": return JSU.N(args.length); + //#end + return super.get(key); + } + } + static class TrapArgs extends JS.Immutable { private Trap t; private JS val; @@ -641,7 +649,10 @@ class Interpreter implements ByteCodes, Tokens, Pausable { private JS method; JS obj; public Stub(JS obj, JS method) { this.obj = obj; this.method = method; } - public JS call(JS[] args) throws JSExn { return obj.call(method, args); } + public JS call(JS method, JS[] args) throws JSExn { + if (method==null) return obj.call(this.method, args); + return super.call(method, args); + } } static final class Stack {