X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FInterpreter.java;h=0d2c21905ef06b8b2bf7615d6ff9f75386f0ff98;hb=29d34750ad40b2addfbc5f20cdab3d4accac157e;hp=ce83f696fc22df44cdfcbdf20768327ac173b3b0;hpb=b1a58aeb4adfade0697650583ec5801fa6e88d5e;p=org.ibex.core.git diff --git a/src/org/ibex/js/Interpreter.java b/src/org/ibex/js/Interpreter.java index ce83f69..0d2c219 100644 --- a/src/org/ibex/js/Interpreter.java +++ b/src/org/ibex/js/Interpreter.java @@ -6,7 +6,7 @@ import java.util.*; /** Encapsulates a single JS interpreter (ie call stack) */ class Interpreter implements ByteCodes, Tokens { - + private static final int MAX_STACK_SIZE = 512; // Thread-Interpreter Mapping ///////////////////////////////////////////////////////////////////////// @@ -217,17 +217,25 @@ class Interpreter implements ByteCodes, Tokens { throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key"); Trap t = null; - 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; 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 && key.equals("cascade")) { + Trap.TrapScope ts = null; + JSScope p = (JSScope)target; // search the scope-path for the trap + if (target instanceof Trap.TrapScope) { + ts = (Trap.TrapScope)target; + } else { + while (ts == null && p.getParentScope() != null) { + p = p.getParentScope(); + if (p instanceof Trap.TrapScope) ts = (Trap.TrapScope)p; + } + } + if(ts != null) { + t = ts.t.next; + ts.cascadeHappened = true; + while (t != null && t.f.numFormalArgs == 0) t = t.next; + if (t == null) { target = ts.t.trapee; key = ts.t.name; } + } + } + if(t == null) { if (target instanceof JSScope) { JSScope p = (JSScope)target; // search the scope-path for the trap t = p.getTrap(key); @@ -239,6 +247,7 @@ class Interpreter implements ByteCodes, Tokens { } if (t != null) { stack.push(new CallMarker(this)); + if(stack.size() > MAX_STACK_SIZE) throw new JSExn("stack overflow"); JSArray args = new JSArray(); args.addElement(val); stack.push(args); @@ -290,6 +299,7 @@ class Interpreter implements ByteCodes, Tokens { } if (t != null) { stack.push(new CallMarker(this)); + if(stack.size() > MAX_STACK_SIZE) throw new JSExn("stack overflow"); JSArray args = new JSArray(); stack.push(args); f = t.f; @@ -340,6 +350,7 @@ class Interpreter implements ByteCodes, Tokens { JSArray arguments = new JSArray(); for(int i=0; i MAX_STACK_SIZE) throw new JSExn("stack overflow"); stack.push(arguments); f = (JSFunction)object; scope = new JSScope(f.parentScope); @@ -390,7 +401,17 @@ class Interpreter implements ByteCodes, Tokens { Object key = stack.pop(); Object obj = stack.peek(); // A trap addition/removal - JS js = obj instanceof JSScope ? ((JSScope)obj).top() : (JS) obj; + JS js = (JS) obj; + if(js instanceof JSScope) { + JSScope s = (JSScope) js; + while(s.getParentScope() != null) { + if(s.has(key)) throw new JSExn("cannot trap a variable that isn't at the top level scope"); + s = s.getParentScope(); + } + js = s; + } + // might want this? + // if(!js.has(key)) throw new JSExn("tried to add/remove a trap from an uninitialized variable"); if(op == ADD_TRAP) js.addTrap(key, (JSFunction)val); else js.delTrap(key, (JSFunction)val); break; @@ -474,7 +495,7 @@ class Interpreter implements ByteCodes, Tokens { if (right == null) right = JS.N(0); int result = 0; if (left instanceof String || right instanceof String) { - result = left.toString().compareTo(right.toString()); + result = JS.toString(left).compareTo(JS.toString(right)); } else { result = (int)java.lang.Math.ceil(JS.toDouble(left) - JS.toDouble(right)); } @@ -485,6 +506,7 @@ class Interpreter implements ByteCodes, Tokens { case EQ: case NE: { + // FIXME: This is not correct, see ECMA-262 11.9.3 Object l = left; Object r = right; boolean ret; @@ -493,7 +515,7 @@ class Interpreter implements ByteCodes, Tokens { else if (r == null) ret = false; // l != null, so its false else if (l instanceof Boolean) ret = JS.B(JS.toBoolean(r)).equals(l); 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 if (l instanceof String) ret = r != null && l.equals(JS.toString(r)); else ret = l.equals(r); stack.push(JS.B(op == EQ ? ret : !ret)); break; } @@ -640,13 +662,13 @@ class Interpreter implements ByteCodes, Tokens { return sb.toString(); } case "indexOf": { - String search = alength >= 1 ? arg0.toString() : "null"; + String search = alength >= 1 ? JS.toString(arg0) : "null"; int start = alength >= 2 ? JS.toInt(arg1) : 0; // Java's indexOf handles an out of bounds start index, it'll return -1 return JS.N(s.indexOf(search,start)); } case "lastIndexOf": { - String search = alength >= 1 ? arg0.toString() : "null"; + String search = alength >= 1 ? JS.toString(arg0) : "null"; int start = alength >= 2 ? JS.toInt(arg1) : 0; // Java's indexOf handles an out of bounds start index, it'll return -1 return JS.N(s.lastIndexOf(search,start)); @@ -684,7 +706,7 @@ class Interpreter implements ByteCodes, Tokens { } if (!returnJS) { // the string stuff applies to everything - String s = o.toString(); + String s = JS.toString(o); // this is sort of ugly, but this list should never change // These should provide a complete (enough) implementation of the ECMA-262 String object @@ -699,7 +721,7 @@ class Interpreter implements ByteCodes, Tokens { case "lastIndexOf": returnJS = true; break; case "match": returnJS = true; break; case "replace": returnJS = true; break; - case "seatch": returnJS = true; break; + case "search": returnJS = true; break; case "slice": returnJS = true; break; case "split": returnJS = true; break; case "toLowerCase": returnJS = true; break; @@ -710,7 +732,7 @@ class Interpreter implements ByteCodes, Tokens { } if (returnJS) { final Object target = o; - final String method = key.toString(); + final String method = JS.toString(o); return new JS() { public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { if (nargs > 2) throw new JSExn("cannot call that method with that many arguments");