trim down public api
[org.ibex.core.git] / src / org / ibex / js / Interpreter.java
index c17fcc3..83ff539 100644 (file)
@@ -27,13 +27,22 @@ class Interpreter implements ByteCodes, Tokens {
         this.pausecount = pauseable ? 0 : -1;
         this.scope = new JSScope(f.parentScope);
         try {
-            stack.push(new CallMarker());    // the "root function returned" marker -- f==null
+            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");
@@ -86,23 +95,11 @@ class Interpreter implements ByteCodes, Tokens {
             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: {
-                int depth = (arg == null ? 1 : JS.toInt((JS)arg));
-                JS save = stack.elementAt(stack.size() - 1);
-                for(int i=stack.size() - 1; i > stack.size() - 1 - depth; i--)
-                    stack.setElementAt(stack.elementAt(i-1), i);
-                stack.setElementAt(save, stack.size() - depth - 1);
-                break;
-            }
+            case SWAP: stack.swap(); break;
             case DUP: stack.push(stack.peek()); break;
             case NEWSCOPE: scope = new JSScope(scope); break;
             case OLDSCOPE: scope = scope.getParentScope(); break;
-            case ASSERT: {
-                JS o = stack.pop();
-                if (JS.checkAssertions && !JS.toBoolean(o))
-                    throw je("ibex.assertion.failed");
-                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;
@@ -120,11 +117,7 @@ class Interpreter implements ByteCodes, Tokens {
 
             case PUSHKEYS: {
                 JS o = stack.peek();
-                Enumeration e = o.keys();
-                JSArray a = new JSArray();
-                // FEATURE: Take advantage of the Enumeration, don't create a JSArray
-                while(e.hasMoreElements()) a.addElement((JS)e.nextElement());
-                stack.push(a);
+                stack.push(o == null ? null : o.keys());
                 break;
             }
 
@@ -135,7 +128,7 @@ class Interpreter implements ByteCodes, Tokens {
 
             case BREAK:
             case CONTINUE:
-                while(stack.size() > 0) {
+                while(!stack.empty()) {
                     JS o = stack.pop();
                     if (o instanceof CallMarker) je("break or continue not within a loop");
                     if (o instanceof TryMarker) {
@@ -169,7 +162,7 @@ class Interpreter implements ByteCodes, Tokens {
 
             case RETURN: {
                 JS retval = stack.pop();
-                while(stack.size() > 0) {
+                while(!stack.empty()) {
                     Object o = stack.pop();
                     if (o instanceof TryMarker) {
                         if(((TryMarker)o).finallyLoc < 0) continue;
@@ -179,31 +172,38 @@ class Interpreter implements ByteCodes, Tokens {
                         pc = ((TryMarker)o).finallyLoc - 1;
                         continue OUTER;
                     } else if (o instanceof CallMarker) {
+                        boolean didTrapPut = false;
                         if (o instanceof TrapMarker) { // handles return component of a read trap
                             TrapMarker tm = (TrapMarker) o;
-                            boolean cascade = tm.t.writeTrap() && !tm.cascadeHappened && !JS.toBoolean(retval);
+                            boolean cascade = tm.t.isWriteTrap() && !tm.cascadeHappened && !JS.toBoolean(retval);
                             if(cascade) {
-                                Trap t = tm.t.next;
-                                while(t != null && t.readTrap()) t = t.next;
+                                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;
-                                    stack.push(tm);
-                                    stack.push(new JSArgs(tm.val,t.f));
-                                    f = t.f;
-                                    scope = new JSScope(f.parentScope);
-                                    pc = -1;
+                                    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 {
-                                    tm.trapee.put(tm.key,tm.val);
+                                    didTrapPut = true;
+                                    if(!tm.pauseOnPut) tm.t.target.put(tm.t.key,tm.val);
                                 }
                             }
                         }
-                        scope = ((CallMarker)o).scope;
-                        pc = ((CallMarker)o).pc - 1;
-                        f = (JSFunction)((CallMarker)o).f;
-                        stack.push(retval);
-                        if (pausecount > initialPauseCount) { pc++; return null; }   // we were paused
-                        if(f == null) return retval;
+                        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;
                     }
                 }
@@ -220,36 +220,35 @@ class Interpreter implements ByteCodes, Tokens {
                 Trap t = null;
                 TrapMarker tm = null;
                 if(target instanceof JSScope && key.jsequals(CASCADE)) {
-                    Object o=null;
-                    int i;
-                    for(i=stack.size()-1;i>=0;i--) if((o = stack.elementAt(i)) instanceof CallMarker) break;
-                    if(i==0) throw new Error("didn't find a call marker while doing cascade");
+                    CallMarker o = stack.findCall();
                     if(o instanceof TrapMarker) {
                         tm = (TrapMarker) o;
-                        target = tm.trapee;
-                        key = tm.key;
+                        target = tm.t.target;
+                        key = tm.t.key;
                         tm.cascadeHappened = true;
                         t = tm.t;
-                        if(t.readTrap()) throw new JSExn("can't do a write cascade in a read trap");
-                        t = t.next;
-                        while(t != null && t.readTrap()) t = t.next;
+                        if(t.isReadTrap()) throw new JSExn("can't do a write cascade in a read trap");
+                        t = t.nextWriteTrap();
                     }
                 }
-                if(tm == null) { // didn't find a trap marker, try to find a trap
-                    t = target instanceof JSScope ? t = ((JSScope)target).top().getTrap(key) : ((JS)target).getTrap(key);
-                    while(t != null && t.readTrap()) t = t.next;
+                if(tm == null) { // not cascading
+                    t = target instanceof JSScope ? t = ((JSScope)target).top().getTrap(key) : 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) {
-                    stack.push(new TrapMarker(this,t,target,key,val));
-                    stack.push(new JSArgs(t.f));
-                    f = t.f;
-                    scope = new TrapScope(f.parentScope,target,f,key);
-                    pc = -1;
+                    setupTrap(t,val,new TrapMarker(this,t,val, tm != null && tm.pauseOnPut));
+                    pc--; // we increment later
                     break;
                 } else {
+                    if (tm != null && tm.pauseOnPut) { pc++; return val; }
                     target.put(key,val);
                     if (pausecount > initialPauseCount) { pc++; return null; }   // we were paused
                     break;
@@ -274,32 +273,29 @@ class Interpreter implements ByteCodes, Tokens {
                 Trap t = null;
                 TrapMarker tm = null;
                 if(target instanceof JSScope && key.jsequals(CASCADE)) {
-                    JS o=null;
-                    int i;
-                    for(i=stack.size()-1;i>=0;i--) if((o = stack.elementAt(i)) instanceof CallMarker) break;
-                    if(i==0) throw new Error("didn't find a call marker while doing cascade");
+                    CallMarker o = stack.findCall();
                     if(o instanceof TrapMarker) {
                         tm = (TrapMarker) o;
-                        target = tm.trapee;
-                        key = tm.key;
+                        target = tm.t.target;
+                        key = tm.t.key;
                         t = tm.t;
-                        if(t.writeTrap()) throw new JSExn("can't do a read cascade in a write trap");
-                        t = t.next;
-                        while(t != null && t.writeTrap()) t = t.next;
-                        if(t != null) tm.cascadeHappened = true;
+                        if(t.isWriteTrap()) throw new JSExn("can't do a read cascade in a write trap");
+                        t = t.nextReadTrap();
                     }
                 }
-                if(tm == null) { // didn't find a trap marker, try to find a trap
+                if(tm == null) { // not cascading
                     t = target instanceof JSScope ? t = ((JSScope)target).top().getTrap(key) : ((JS)target).getTrap(key);
-                    while(t != null && t.writeTrap()) t = t.next;
+                    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) {
-                    stack.push(new TrapMarker(this,t,(JS)target,key,null));
-                    stack.push(new JSArgs(t.f));
-                    f = t.f;
-                    scope = new TrapScope(f.parentScope,target,f,key);
-                    pc = -1;
+                    setupTrap(t,null,new TrapMarker(this,t,null));
+                    pc--; // we increment later
                     break;
                 } else {
                     ret = target.get(key);
@@ -355,7 +351,7 @@ class Interpreter implements ByteCodes, Tokens {
             }
 
             case THROW:
-                throw new JSExn(stack.pop(), stack, f, pc, scope);
+                throw new JSExn(stack.pop(), this);
 
                 /* FIXME GRAMMAR
             case MAKE_GRAMMAR: {
@@ -501,7 +497,7 @@ class Interpreter implements ByteCodes, Tokens {
         if a handler is not found the exception is thrown
     */
     void catchException(JSExn e) throws JSExn {
-        while(stack.size() > 0) {
+        while(!stack.empty()) {
             JS o = stack.pop();
             if (o instanceof CatchMarker || o instanceof TryMarker) {
                 boolean inCatch = o instanceof CatchMarker;
@@ -530,6 +526,13 @@ class Interpreter implements ByteCodes, Tokens {
         throw e;
     }
 
+    void setupTrap(Trap t, JS val, CallMarker cm) throws JSExn {
+        stack.push(cm);
+        stack.push(t.isWriteTrap() ? new JSArgs(val,t.f) : new JSArgs(t.f));
+        f = t.f;
+        scope = new TrapScope(t.f.parentScope,t);
+        pc = 0;
+    }
 
 
     // Markers //////////////////////////////////////////////////////////////////////
@@ -545,22 +548,24 @@ class Interpreter implements ByteCodes, Tokens {
         final int pc;
         final JSScope scope;
         final JSFunction f;
-        public CallMarker(Interpreter cx) { pc = cx.pc + 1; scope = cx.scope; f = cx.f; }
-        public CallMarker() { pc = -1; scope = null; f = null; }
+        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;
-        final JS trapee;
-        final JS key;
-        final JS val;
+        JS val;
         boolean cascadeHappened;
-        public TrapMarker(Interpreter cx, Trap t, JS trapee, JS key, JS val) {
+        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.trapee = trapee;
-            this.key = key;
             this.val = val;
+            this.pauseOnPut = pauseOnPut;
         }
     }
     
@@ -599,20 +604,16 @@ class Interpreter implements ByteCodes, Tokens {
     }
 
     static class TrapScope extends JSScope {
-        JS trapee;
-        JS callee;
-        JS trapname;
-        public TrapScope(JSScope parent, JS trapee, JS callee, JS trapname) {
-            super(parent); this.trapee = trapee; this.callee = callee; this.trapname = trapname;
+        private Trap t;
+        public TrapScope(JSScope parent, Trap t) {
+            super(parent); this.t = t;
         }
         public JS get(JS key) throws JSExn {
-            if(JS.isString(key)) {
-                //#switch(JS.toString(key))
-                case "trapee": return trapee;
-                case "callee": return callee;
-                case "trapname": return trapname;
-                //#end
-            }
+            //#jswitch(key)
+            case "trapee": return t.target;
+            case "callee": return t.f;
+            case "trapname": return t.key;
+            //#end
             return super.get(key);
         }
     }
@@ -643,7 +644,7 @@ class Interpreter implements ByteCodes, Tokens {
                     default: return n>= 0 && n < nargs ? rest[n-3] : null;
                 }
             }
-            //#switch(JS.toString(key))
+            //#jswitch(key)
             case "callee": return callee;
             case "length": return JS.N(nargs);
             //#end
@@ -664,27 +665,38 @@ class Interpreter implements ByteCodes, Tokens {
         private static final int MAX_STACK_SIZE = 512;
         private JS[] stack = new JS[64];
         private int sp = 0;
-        public final void push(JS o) throws JSExn {
-            if(sp == stack.length) grow();
-            stack[sp++] = o;
-        }
-        public final JS peek() {
-            if(sp == 0) throw new RuntimeException("Stack underflow");
-            return stack[sp-1];
+        
+        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;
         }
-        public final JS pop() {
-            if(sp == 0) throw new RuntimeException("Stack underflow");
-            return stack[--sp];
+        CallMarker findCall() {
+            for(int i=sp-1;i>=0;i--) if(stack[i] instanceof CallMarker) return (CallMarker) stack[i];
+            return null;
         }
-        private void grow() throws JSExn {
+        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);
-        }
-        public int size() { return sp; }
-        public JS elementAt(int i) { return stack[i]; }
+        }       
         
-        // FIXME: Eliminate all uses of SWAP n>1 so we don't need this
-        public void setElementAt(JS o, int i) { stack[i] = o; }
+        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);
+                }
+            }
+        }
     }
 }