X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FInterpreter.java;h=b5becd03287aaae0e251f84adfed5ae96930c688;hb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;hp=66931fdaee4c26e2c27cbfeee35d88e613deccd8;hpb=a060cc5025b58e8d3e319aefa6ae44fe7c6182ad;p=org.ibex.core.git diff --git a/src/org/xwt/js/Interpreter.java b/src/org/xwt/js/Interpreter.java index 66931fd..b5becd0 100644 --- a/src/org/xwt/js/Interpreter.java +++ b/src/org/xwt/js/Interpreter.java @@ -1,4 +1,4 @@ -// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.js; import org.xwt.util.*; @@ -44,7 +44,17 @@ class Interpreter implements ByteCodes, Tokens { } } - private static JSExn je(String s) { return new JSExn(JS.getSourceName() + ":" + JS.getLine() + " " + s); } + 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); } // FIXME: double check the trap logic private Object run() throws JSExn { @@ -60,6 +70,7 @@ class Interpreter implements ByteCodes, Tokens { 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; } @@ -83,20 +94,20 @@ class Interpreter implements ByteCodes, Tokens { case DUP: stack.push(stack.peek()); break; case NEWSCOPE: scope = new JSScope(scope); break; case OLDSCOPE: scope = scope.getParentScope(); break; - case ASSERT: if (!JS.toBoolean(stack.pop())) throw je("assertion failed"); break; + case ASSERT: if (!JS.toBoolean(stack.pop())) throw je("xwt.assertion.failed" /*FEATURE: line number*/); 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 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 JS) stack.push(((JS)o).typeName()); + else if (o instanceof JS) stack.push("object"); else if (o instanceof String) stack.push("string"); else if (o instanceof Number) stack.push("number"); else if (o instanceof Boolean) stack.push("boolean"); - else stack.push("unknown"); + else throw new Error("this should not happen"); break; } @@ -138,13 +149,13 @@ class Interpreter implements ByteCodes, Tokens { } } throw new Error("CONTINUE/BREAK invoked but couldn't find LoopMarker at " + - JS.getSourceName() + ":" + JS.getLine()); + 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], scope)); + stack.push(new TryMarker(jmps[0] < 0 ? -1 : pc + jmps[0], jmps[1] < 0 ? -1 : pc + jmps[1], this)); break; } @@ -160,8 +171,10 @@ class Interpreter implements ByteCodes, Tokens { pc = ((TryMarker)o).finallyLoc - 1; continue OUTER; } else if (o instanceof CallMarker) { - if (scope instanceof Trap.TrapScope) { + if (scope instanceof Trap.TrapScope) { // handles return component of a read trap Trap.TrapScope ts = (Trap.TrapScope)scope; + if (retval != null && retval instanceof Boolean && ((Boolean)retval).booleanValue()) + ts.cascadeHappened = true; if (!ts.cascadeHappened) { ts.cascadeHappened = true; Trap t = ts.t.next; @@ -177,7 +190,7 @@ class Interpreter implements ByteCodes, Tokens { f = t.f; scope = new Trap.TrapScope(f.parentScope, t, ts.val); pc = -1; - break; + continue OUTER; } } } @@ -201,22 +214,36 @@ class Interpreter implements ByteCodes, Tokens { throw je("tried to put a value to the " + key + " property on a " + target.getClass().getName()); if (key == null) throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key"); + Trap t = null; - if (target instanceof JS) { - t = ((JS)target).getTrap(key); - while (t != null && t.f.numFormalArgs == 0) t = t.next; - } else if (target instanceof Trap.TrapScope && key.equals("cascade")) { + if (target instanceof Trap.TrapScope && key.equals("cascade")) { Trap.TrapScope ts = (Trap.TrapScope)target; t = ts.t.next; ts.cascadeHappened = true; while (t != null && t.f.numFormalArgs == 0) t = t.next; - if (t == null) target = ts.t.trapee; + if (t == null) { target = ts.t.trapee; key = ts.t.name; } + + } else if (target instanceof Trap.TrapScope && key.equals(((Trap.TrapScope)target).t.name)) { + throw je("tried to put to " + key + " inside a trap it owns; use cascade instead"); + + } else if (target instanceof JS) { + if (target instanceof JSScope) { + JSScope p = (JSScope)target; // search the scope-path for the trap + t = p.getTrap(key); + while (t == null && p.getParentScope() != null) { p = p.getParentScope(); t = p.getTrap(key); } + } else { + t = ((JS)target).getTrap(key); + } + + while (t != null && t.f.numFormalArgs == 0) t = t.next; // find the first write trap + if (t != null) { + stack.push(new CallMarker(this)); + JSArray args = new JSArray(); + args.addElement(val); + stack.push(args); + } } if (t != null) { - stack.push(new CallMarker(this)); - JSArray args = new JSArray(); - args.addElement(val); - stack.push(args); f = t.f; scope = new Trap.TrapScope(f.parentScope, t, val); pc = -1; @@ -248,18 +275,28 @@ class Interpreter implements ByteCodes, Tokens { break; } else if (o instanceof JS) { Trap t = null; - if (o instanceof JS) { - t = ((JS)o).getTrap(v); - while (t != null && t.f.numFormalArgs != 0) t = t.next; - } else if (o instanceof Trap.TrapScope && v.equals("cascade")) { + if (o instanceof Trap.TrapScope && v.equals("cascade")) { t = ((Trap.TrapScope)o).t.next; while (t != null && t.f.numFormalArgs != 0) t = t.next; - if (t == null) o = ((Trap.TrapScope)o).t.trapee; + if (t == null) { v = ((Trap.TrapScope)o).t.name; o = ((Trap.TrapScope)o).t.trapee; } + + } else if (o instanceof JS) { + if (o instanceof JSScope) { + JSScope p = (JSScope)o; // search the scope-path for the trap + t = p.getTrap(v); + while (t == null && p.getParentScope() != null) { p = p.getParentScope(); t = p.getTrap(v); } + } else { + t = ((JS)o).getTrap(v); + } + + while (t != null && t.f.numFormalArgs != 0) t = t.next; // get first read trap + if (t != null) { + stack.push(new CallMarker(this)); + JSArray args = new JSArray(); + stack.push(args); + } } if (t != null) { - stack.push(new CallMarker(this)); - JSArray args = new JSArray(); - stack.push(args); f = t.f; scope = new Trap.TrapScope(f.parentScope, t, null); ((Trap.TrapScope)scope).cascadeHappened = true; @@ -285,6 +322,10 @@ class Interpreter implements ByteCodes, Tokens { if (object == JS.METHOD) { method = stack.pop(); object = stack.pop(); + } else if (object == null) { + Object name = stack.pop(); + stack.pop(); + throw new JSExn("function '"+name+"' not found"); } else { stack.pop(); stack.pop(); @@ -323,33 +364,52 @@ class Interpreter implements ByteCodes, Tokens { break; } - case THROW: { - Object o = stack.pop(); - if(o instanceof JSExn) throw (JSExn)o; - throw new JSExn(o); + case THROW: + throw new JSExn(stack.pop()); + + 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: { + Object val = stack.pop(); + Object key = stack.pop(); + Object obj = stack.peek(); + // A trap addition/removal + JS js = obj instanceof JSScope ? ((JSScope)obj).top() : (JS) obj; + if(op == ADD_TRAP) js.addTrap(key, (JSFunction)val); + else js.delTrap(key, (JSFunction)val); + break; } case ASSIGN_SUB: case ASSIGN_ADD: { Object val = stack.pop(); - Object old = stack.pop(); Object key = stack.pop(); Object obj = stack.peek(); - if (val instanceof JSFunction && obj instanceof JSScope) { - JSScope parent = (JSScope)obj; - while(parent.getParentScope() != null) parent = parent.getParentScope(); - if (parent instanceof JS) { - JS b = (JS)parent; - if (op == ASSIGN_ADD) b.addTrap(key, (JSFunction)val); - else b.delTrap(key, (JSFunction)val); - // skip over the "normal" implementation of +=/-= - pc += ((Integer)arg).intValue() - 1; - break; - } - } - // use the "normal" implementation + // The following setup is VERY important. The generated bytecode depends on the stack + // being setup like this (top to bottom) KEY, OBJ, VAL, KEY, OBJ stack.push(key); - stack.push(old); stack.push(val); + stack.push(obj); + stack.push(key); break; } @@ -447,6 +507,7 @@ class Interpreter implements ByteCodes, Tokens { } } catch(JSExn e) { + if(f.op[pc] != FINALLY_DONE) e.addBacktrace(f.sourceName,f.line[pc]); while(stack.size() > 0) { Object o = stack.pop(); if (o instanceof CatchMarker || o instanceof TryMarker) { @@ -460,19 +521,24 @@ class Interpreter implements ByteCodes, Tokens { stack.push(o); stack.push(catchMarker); stack.push(e.getObject()); + f = ((TryMarker)o).f; scope = ((TryMarker)o).scope; pc = ((TryMarker)o).catchLoc - 1; continue OUTER; } else { - stack.push(e); - stack.push(new FinallyData(THROW)); + stack.push(new FinallyData(e)); + f = ((TryMarker)o).f; scope = ((TryMarker)o).scope; pc = ((TryMarker)o).finallyLoc - 1; continue OUTER; } + } else if(o instanceof CallMarker) { + CallMarker cm = (CallMarker) o; + if(cm.f == null) + e.addBacktrace("",0); // This might not even be worth mentioning + else + e.addBacktrace(cm.f.sourceName,cm.f.line[cm.pc-1]); } - // no handler found within this func - if(o instanceof CallMarker) throw e; } throw e; } // end try/catch @@ -507,23 +573,30 @@ class Interpreter implements ByteCodes, Tokens { public int catchLoc; public int finallyLoc; public JSScope scope; - public TryMarker(int catchLoc, int finallyLoc, JSScope scope) { + public JSFunction f; + public TryMarker(int catchLoc, int finallyLoc, Interpreter cx) { this.catchLoc = catchLoc; this.finallyLoc = finallyLoc; - this.scope = scope; + this.scope = cx.scope; + this.f = cx.f; } } public static class FinallyData { public int op; public Object arg; - public FinallyData(int op, Object arg) { this.op = op; this.arg = arg; } + public JSExn exn; public FinallyData(int op) { this(op,null); } + public FinallyData(int op, Object arg) { this.op = op; this.arg = arg; } + public FinallyData(JSExn exn) { this.exn = exn; } // Just throw this exn } // Operations on Primitives ////////////////////////////////////////////////////////////////////// static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) throws JSExn { + if (method == null || !(method instanceof String) || "".equals(method)) + throw new JSExn("attempt to call a non-existant method on a primitive"); + if (o instanceof Number) { //#switch(method) case "toFixed": throw new JSExn("toFixed() not implemented"); @@ -590,7 +663,7 @@ class Interpreter implements ByteCodes, Tokens { return JS.N(s.lastIndexOf(search,start)); } case "match": return JSRegexp.stringMatch(s,arg0); - case "replace": return JSRegexp.stringReplace(s,(String)arg0,arg1); + case "replace": return JSRegexp.stringReplace(s,arg0,arg1); case "search": return JSRegexp.stringSearch(s,arg0); case "split": return JSRegexp.stringSplit(s,arg0,arg1,alength); case "toLowerCase": return s.toLowerCase();