trim down public api
[org.ibex.core.git] / src / org / ibex / js / Interpreter.java
index 2d88042..83ff539 100644 (file)
@@ -22,18 +22,27 @@ class Interpreter implements ByteCodes, Tokens {
     final Stack stack = new Stack(); ///< the object stack
     int pc = 0;                   ///< the program counter
 
-    Interpreter(JSFunction f, boolean pauseable, JSArray args) {
+    Interpreter(JSFunction f, boolean pauseable, JSArgs args) {
         this.f = f;
         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,33 +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);
-                                    JSArray args = new JSArray();
-                                    args.addElement(tm.val);
-                                    stack.push(args);
-                                    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;
                     }
                 }
@@ -222,38 +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 put to 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));
-                    JSArray args = new JSArray();
-                    args.addElement(val);
-                    stack.push(args);
-                    f = t.f;
-                    scope = new JSScope(f.parentScope);
-                    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;
@@ -278,44 +273,48 @@ 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 write cascade in a read 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 JSArray());
-                    f = t.f;
-                    scope = new JSScope(f.parentScope);
-                    pc = -1;
+                    setupTrap(t,null,new TrapMarker(this,t,null));
+                    pc--; // we increment later
                     break;
                 } 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);
-                    if (pausecount > initialPauseCount) { pc++; return null; }   // we were paused
                     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();
@@ -325,27 +324,18 @@ class Interpreter implements ByteCodes, Tokens {
                         method = stack.pop();
                         object = stack.pop();
                     } else if (object == null) {
-                        Object name = stack.pop();
-                        stack.pop();
-                        throw new JSExn("function '"+name+"' not found");
+                        method = stack.pop();
+                        object = stack.pop();
+                        throw new JSExn("function '"+JS.debugToString(method)+"' not found in " + object.getClass().getName());
                     } else {
                         stack.pop();
                         stack.pop();
                     }
                 }
-                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();
-
 
                 if (object instanceof JSFunction) {
-                    // FIXME: use something similar to call0/call1/call2 here
-                    JSArray arguments = new JSArray();
-                    for(int i=0; i<numArgs; i++) arguments.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
                     stack.push(new CallMarker(this));
-                    stack.push(arguments);
+                    stack.push(new JSArgs(a0,a1,a2,rest,numArgs,object));
                     f = (JSFunction)object;
                     scope = new JSScope(f.parentScope);
                     pc = -1;
@@ -361,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: {
@@ -405,20 +395,6 @@ class Interpreter implements ByteCodes, Tokens {
                 break;
             }
 
-            // FIXME: This was for the old trap syntax, remove it, shouldn't be needed anymore
-            case ASSIGN_SUB: case ASSIGN_ADD: {
-                JS val = stack.pop();
-                JS key = stack.pop();
-                JS obj = stack.peek();
-                // 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(val);
-                stack.push(obj);
-                stack.push(key);
-                break;
-            }
-
             case ADD: {
                 int count = ((JSNumber)arg).toInt();
                 if(count < 2) throw new Error("this should never happen");
@@ -510,37 +486,53 @@ class Interpreter implements ByteCodes, Tokens {
             }
 
         } catch(JSExn e) {
-            while(stack.size() > 0) {
-                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 - 1;
-                        continue OUTER;
-                    } else {
-                        stack.push(new FinallyData(e));
-                        f = ((TryMarker)o).f;
-                        scope = ((TryMarker)o).scope;
-                        pc = ((TryMarker)o).finallyLoc - 1;
-                        continue OUTER;
-                    }
-                }
-            }
-            throw 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(t.isWriteTrap() ? new JSArgs(val,t.f) : new JSArgs(t.f));
+        f = t.f;
+        scope = new TrapScope(t.f.parentScope,t);
+        pc = 0;
+    }
 
 
     // Markers //////////////////////////////////////////////////////////////////////
@@ -556,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;
         }
     }
     
@@ -609,150 +603,56 @@ class Interpreter implements ByteCodes, Tokens {
         public FinallyData(JSExn exn) { this.exn = exn; this.op = -1; this.arg = null; } // Just throw this exn
     }
 
-
-    // Operations on Primitives //////////////////////////////////////////////////////////////////////
-
-    // FIXME: Move these into JSString and JSNumber
-    /*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");
-            case "toExponential": throw new JSExn("toExponential() not implemented");
-            case "toPrecision": throw new JSExn("toPrecision() not implemented");
-            case "toString": {
-                int radix = alength >= 1 ? JS.toInt(arg0) : 10;
-                return Long.toString(((Number)o).longValue(),radix);
-            }
-            //#end
-        } else if (o instanceof Boolean) {
-            // No methods for Booleans
-            throw new JSExn("attempt to call a method on a Boolean");
-        }
-
-        String s = JS.toString(o);
-        int slength = s.length();
-        //#switch(method)
-        case "substring": {
-            int a = alength >= 1 ? JS.toInt(arg0) : 0;
-            int b = alength >= 2 ? JS.toInt(arg1) : slength;
-            if (a > slength) a = slength;
-            if (b > slength) b = slength;
-            if (a < 0) a = 0;
-            if (b < 0) b = 0;
-            if (a > b) { int tmp = a; a = b; b = tmp; }
-            return s.substring(a,b);
-        }
-        case "substr": {
-            int start = alength >= 1 ? JS.toInt(arg0) : 0;
-            int len = alength >= 2 ? JS.toInt(arg1) : Integer.MAX_VALUE;
-            if (start < 0) start = slength + start;
-            if (start < 0) start = 0;
-            if (len < 0) len = 0;
-            if (len > slength - start) len = slength - start;
-            if (len <= 0) return "";
-            return s.substring(start,start+len);
-        }
-        case "charAt": {
-            int p = alength >= 1 ? JS.toInt(arg0) : 0;
-            if (p < 0 || p >= slength) return "";
-            return s.substring(p,p+1);
-        }
-        case "charCodeAt": {
-            int p = alength >= 1 ? JS.toInt(arg0) : 0;
-            if (p < 0 || p >= slength) return JS.N(Double.NaN);
-            return JS.N(s.charAt(p));
-        }
-        case "concat": {
-            StringBuffer sb = new StringBuffer(slength*2).append(s);
-            for(int i=0;i<alength;i++) sb.append(i==0?arg0:i==1?arg1:i==2?arg2:rest[i-3]);
-            return sb.toString();
-        }
-        case "indexOf": {
-            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 ? 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));            
+    static class TrapScope extends JSScope {
+        private Trap t;
+        public TrapScope(JSScope parent, Trap t) {
+            super(parent); this.t = t;
         }
-        case "match": return JSRegexp.stringMatch(s,arg0);
-        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();
-        case "toUpperCase": return s.toUpperCase();
-        case "toString": return s;
-        case "slice": {
-            int a = alength >= 1 ? JS.toInt(arg0) : 0;
-            int b = alength >= 2 ? JS.toInt(arg1) : slength;
-            if (a < 0) a = slength + a;
-            if (b < 0) b = slength + b;
-            if (a < 0) a = 0;
-            if (b < 0) b = 0;
-            if (a > slength) a = slength;
-            if (b > slength) b = slength;
-            if (a > b) return "";
-            return s.substring(a,b);
+        public JS get(JS key) throws JSExn {
+            //#jswitch(key)
+            case "trapee": return t.target;
+            case "callee": return t.f;
+            case "trapname": return t.key;
+            //#end
+            return super.get(key);
         }
-        //#end
-        throw new JSExn("Attempted to call non-existent method: " + method);
     }
     
-    static Object getFromPrimitive(Object o, Object key) throws JSExn {
-        boolean returnJS = false;
-        if (o instanceof Boolean) {
-            throw new JSExn("Booleans do not have properties");
-        } else if (o instanceof Number) {
-            if (key.equals("toPrecision") || key.equals("toExponential") || key.equals("toFixed"))
-                returnJS = true;
-        }
-        if (!returnJS) {
-            // the string stuff applies to everything
-            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
-
-            //#switch(key)
-            case "length": return JS.N(s.length());
-            case "substring": returnJS = true; break; 
-            case "charAt": returnJS = true; break; 
-            case "charCodeAt": returnJS = true; break; 
-            case "concat": returnJS = true; break; 
-            case "indexOf": returnJS = true; break; 
-            case "lastIndexOf": returnJS = true; break; 
-            case "match": returnJS = true; break; 
-            case "replace": returnJS = true; break; 
-            case "search": returnJS = true; break; 
-            case "slice": returnJS = true; break; 
-            case "split": returnJS = true; break; 
-            case "toLowerCase": returnJS = true; break; 
-            case "toUpperCase": returnJS = true; break; 
-            case "toString": returnJS = true; break; 
-            case "substr": returnJS = true; break;  
-           //#end
+    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;
         }
-        if (returnJS) {
-            final Object target = o;
-            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");
-                        return callMethodOnPrimitive(target, method, a0, a1, a2, rest, nargs);
-                    }
-            };
+        
+        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);
         }
-        return null;
-    }*/
+    }
 
-    private static class Stub extends JS {
+    static class Stub extends JS {
         private JS method;
         JS obj;
         public Stub(JS obj, JS method) { this.obj = obj; this.method = method; }
@@ -765,26 +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);
+        }       
+        
+        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);
+                }
+            }
         }
-        // FIXME: Eliminate all uses of SWAP n>1 so we don't need this
-        public int size() { return sp; }
-        public void setElementAt(JS o, int i) { stack[i] = o; }
-        public JS elementAt(int i) { return stack[i]; }
     }
 }