2003/12/29 04:04:35
[org.ibex.core.git] / src / org / xwt / js / Interpreter.java
index f65a585..a471412 100644 (file)
@@ -32,7 +32,7 @@ class Interpreter implements ByteCodes, Tokens {
     }
     
     /** this is the only synchronization point we need in order to be threadsafe */
-    synchronized Object resume() {
+    synchronized Object resume() throws JSExn {
         Thread t = Thread.currentThread();
         Interpreter old = (Interpreter)threadToInterpreter.get(t);
         threadToInterpreter.put(t, this);
@@ -83,7 +83,7 @@ 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;
@@ -160,14 +160,15 @@ 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) ts.cascadeHappened = true;
                             if (!ts.cascadeHappened) {
                                 ts.cascadeHappened = true;
                                 Trap t = ts.t.next;
                                 while (t != null && t.f.numFormalArgs == 0) t = t.next;
                                 if (t == null) {
-                                    ((JS)ts.t.trapee).put(t.name, ts.val);
+                                    ((JS)ts.t.trapee).put(ts.t.name, ts.val);
                                     if (pausecount > initialPauseCount) { pc++; return null; }   // we were paused
                                 } else {
                                     stack.push(o);
@@ -177,12 +178,12 @@ class Interpreter implements ByteCodes, Tokens {
                                     f = t.f;
                                     scope = new Trap.TrapScope(f.parentScope, t, ts.val);
                                     pc = -1;
-                                    break;
+                                    continue OUTER;
                                 }
                             }
                         }
                         scope = ((CallMarker)o).scope;
-                        pc = ((CallMarker)o).pc;
+                        pc = ((CallMarker)o).pc - 1;
                         f = (JSFunction)((CallMarker)o).f;
                         stack.push(retval);
                         continue OUTER;
@@ -201,22 +202,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(val);
-                    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;
@@ -241,25 +256,35 @@ class Interpreter implements ByteCodes, Tokens {
                 }
                 Object ret = null;
                 if (v == null) throw je("tried to get the null key from " + o);
-                if (o == null) throw je("tried to get property \"" + v + "\" from the null value @" + pc + "\n" + f.dump());
+                if (o == null) throw je("tried to get property \"" + v + "\" from the null object");
                 if (o instanceof String || o instanceof Number || o instanceof Boolean) {
                     ret = getFromPrimitive(o,v);
                     stack.push(ret);
                     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,22 +310,28 @@ 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();
                     }
                 }
+                Object[] rest = numArgs > 3 ? new Object[numArgs - 3] : null;
+                for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop();
+                Object a2 = numArgs <= 2 ? null : stack.pop();
+                Object a1 = numArgs <= 1 ? null : stack.pop();
+                Object a0 = numArgs <= 0 ? null : stack.pop();
 
                 if (object instanceof String || object instanceof Number || object instanceof Boolean) {
-                    if (numArgs > 2) throw new JSExn("too many arguments to primitive method");
-                    Object arg1 = numArgs >= 2 ? stack.pop() : null;
-                    Object arg0 = numArgs >= 1 ? stack.pop() : null;
-                    ret = callMethodOnPrimitive(object, method, arg0, arg1, numArgs);
+                    ret = callMethodOnPrimitive(object, method, a0, a1, a2, null, numArgs);
 
                 } else if (object instanceof JSFunction) {
-                    // FEATURE: use something similar to call0/call1/call2 here
+                    // FIXME: use something similar to call0/call1/call2 here
                     JSArray arguments = new JSArray();
-                    for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(stack.pop(), j);
+                    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);
                     f = (JSFunction)object;
@@ -310,11 +341,6 @@ class Interpreter implements ByteCodes, Tokens {
 
                 } else if (object instanceof JS) {
                     JS c = (JS)object;
-                    Object[] rest = numArgs > 3 ? new Object[numArgs - 3] : null;
-                    for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop();
-                    Object a2 = numArgs <= 2 ? null : stack.pop();
-                    Object a1 = numArgs <= 1 ? null : stack.pop();
-                    Object a0 = numArgs <= 0 ? null : stack.pop();
                     ret = method == null ? c.call(a0, a1, a2, rest, numArgs) : c.callMethod(method, a0, a1, a2, rest, numArgs);
 
                 } else {
@@ -526,7 +552,10 @@ class Interpreter implements ByteCodes, Tokens {
 
     // Operations on Primitives //////////////////////////////////////////////////////////////////////
 
-    static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, int alength) {
+    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");
@@ -576,12 +605,9 @@ class Interpreter implements ByteCodes, Tokens {
             return JS.N(s.charAt(p));
         }
         case "concat": {
-            // FIXME takes variable number of arguments...
-            /*
             StringBuffer sb = new StringBuffer(slength*2).append(s);
-            for(int i=0;i<alength;i++) sb.append(args.elementAt(i));
+            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 ? arg0.toString() : "null";
@@ -598,7 +624,7 @@ class Interpreter implements ByteCodes, Tokens {
         case "match": return JSRegexp.stringMatch(s,arg0);
         case "replace": return JSRegexp.stringReplace(s,(String)arg0,arg1);
         case "search": return JSRegexp.stringSearch(s,arg0);
-        case "split": return JSRegexp.stringSplit(s,(String)arg0,arg1);
+        case "split": return JSRegexp.stringSplit(s,arg0,arg1,alength);
         case "toLowerCase": return s.toLowerCase();
         case "toUpperCase": return s.toUpperCase();
         case "toString": return s;
@@ -618,7 +644,7 @@ class Interpreter implements ByteCodes, Tokens {
         throw new JSExn("Attempted to call non-existent method: " + method);
     }
     
-    static Object getFromPrimitive(Object o, Object key) {
+    static Object getFromPrimitive(Object o, Object key) throws JSExn {
         boolean returnJS = false;
         if (o instanceof Boolean) {
             throw new JSExn("cannot call methods on Booleans");
@@ -649,16 +675,16 @@ class Interpreter implements ByteCodes, Tokens {
             case "toLowerCase": returnJS = true; break; 
             case "toUpperCase": returnJS = true; break; 
             case "toString": returnJS = true; break; 
-            case "substr": returnJS = true; break; 
-            //#end
+            case "substr": returnJS = true; break;  
+           //#end
         }
         if (returnJS) {
             final Object target = o;
             final String method = key.toString();
             return new JS() {
-                    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+                    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, nargs);
+                        return callMethodOnPrimitive(target, method, a0, a1, a2, rest, nargs);
                     }
             };
         }
@@ -669,7 +695,7 @@ class Interpreter implements ByteCodes, Tokens {
         private Object method;
         JS obj;
         public Stub(JS obj, Object method) { this.obj = obj; this.method = method; }
-        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return ((JS)obj).callMethod(method, a0, a1, a2, rest, nargs);
         }
     }