2003/11/13 09:15:12
[org.ibex.core.git] / src / org / xwt / js / JSFunction.java
index 1d068e9..11f8031 100644 (file)
@@ -2,6 +2,7 @@
 package org.xwt.js;
 
 import org.xwt.util.*;
+import java.util.*;
 import java.io.*;
 
 /** A JavaScript function, compiled into bytecode */
@@ -9,7 +10,7 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
 
     /** Note: code gets run in an <i>unpauseable</i> context. */
     public Object call(JSArray args) {
-        Context cx = new JSContext(this, false);
+        JSContext cx = new JSContext(this, false);
         cx.invoke(args);
         return cx.stack.pop();
     }
@@ -89,7 +90,7 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
 
     /** returns false if the thread has been paused */
     static Object eval(final JSContext cx) throws JS.Exn {
-        final initialPauseCount = cx.pausecount;
+        final int initialPauseCount = cx.pausecount;
         OUTER: for(;; cx.pc++) {
         try {
             if (cx.f == null || cx.pc >= cx.f.size) return cx.stack.pop();
@@ -111,7 +112,13 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
             case JF: if (!JS.toBoolean(cx.stack.pop())) cx.pc += JS.toNumber(arg).intValue() - 1; break;
             case JMP: cx.pc += JS.toNumber(arg).intValue() - 1; break;
             case POP: cx.stack.pop(); break;
-            case SWAP: { Object o1 = cx.stack.pop(); Object o2 = cx.stack.pop(); cx.stack.push(o1); cx.stack.push(o2); break; }
+            case SWAP: {
+                int depth = (1 + (arg == null ? 1 : toInt(arg)));
+                Object save = cx.stack.elementAt(cx.stack.size() - 1);
+                for(int i=cx.stack.size() - 1; i > cx.stack.size() - depth; i--)
+                    cx.stack.setElementAt(cx.stack.elementAt(i-1), i);
+                cx.stack.setElementAt(save, cx.stack.size() - depth);
+                break; }
             case DUP: cx.stack.push(cx.stack.peek()); break;
             case NEWSCOPE: cx.scope = new JSScope(cx.scope); break;
             case OLDSCOPE: cx.scope = cx.scope.getParentJSScope(); break;
@@ -193,22 +200,22 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                         cx.pc = ((TryMarker)o).finallyLoc - 1;
                         continue OUTER;
                     } else if (o instanceof CallMarker) {
-                        if (cx.scope instanceof JSTrapScope) {
-                            JSTrapScope ts = (JSTrapScope)cx.scope;
+                        if (cx.scope instanceof JSTrap.JSTrapScope) {
+                            JSTrap.JSTrapScope ts = (JSTrap.JSTrapScope)cx.scope;
                             if (!ts.cascadeHappened) {
                                 ts.cascadeHappened = true;
                                 JSTrap t = ts.t.next;
                                 while (t != null && t.f.numFormalArgs == 0) t = t.next;
                                 if (t == null) {
-                                    ts.trappee.put(key, val);
-                                    if (cx.pausecount > initialPauseCount) return;   // we were paused
+                                    ((JS)ts.t.trapee).put(t.name, ts.val);
+                                    if (cx.pausecount > initialPauseCount) return null;   // we were paused
                                 } else {
                                     cx.stack.push(o);
                                     JSArray args = new JSArray();
                                     args.addElement(ts.val);
-                                    cx.stack.push(ta);
+                                    cx.stack.push(args);
                                     cx.f = t.f;
-                                    cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, ts.val);
+                                    cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, t, ts.val);
                                     cx.pc = -1;
                                     break;
                                 }
@@ -235,28 +242,28 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                 if (key == null)
                     throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key");
                 JSTrap t = null;
-                if (o instanceof JSTrap.JSTrappable) {
-                    t = ((JSTrap.JSTrappable)o).getTrap(v);
+                if (target instanceof JSTrap.JSTrappable) {
+                    t = ((JSTrap.JSTrappable)target).getTrap(val);
                     while (t != null && t.f.numFormalArgs == 0) t = t.next;
-                } else if (o instanceof JSTrap.JSTrapScope && key.equals("cascade")) {
-                    JSTrap.JSTrapScope ts = (JSTrap.JSTrapScope)o;
+                } else if (target instanceof JSTrap.JSTrapScope && key.equals("cascade")) {
+                    JSTrap.JSTrapScope ts = (JSTrap.JSTrapScope)target;
                     t = ts.t.next;
                     ts.cascadeHappened = true;
                     while (t != null && t.f.numFormalArgs == 0) t = t.next;
-                    if (t == null) o = ts.t.trappee;
+                    if (t == null) target = ts.t.trapee;
                 }
                 if (t != null) {
                     cx.stack.push(new CallMarker(cx));
                     JSArray args = new JSArray();
                     args.addElement(val);
-                    cx.stack.push(ta);
+                    cx.stack.push(args);
                     cx.f = t.f;
-                    cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, val);
+                    cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, t, val);
                     cx.pc = -1;
                     break;
                 }
                 ((JS)target).put(key, val);
-                if (cx.pausecount > initialPauseCount) return;   // we were paused
+                if (cx.pausecount > initialPauseCount) return null;   // we were paused
                 cx.stack.push(val);
                 break;
             }
@@ -273,8 +280,8 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                     cx.stack.push(v);
                 }
                 Object ret = null;
-                if (o == null) throw je("tried to get property \"" + v + "\" from the null value");
                 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");
                 if (o instanceof String || o instanceof Number || o instanceof Boolean) {
                     ret = Internal.getFromPrimitive(o,v);
                     cx.stack.push(ret);
@@ -284,23 +291,23 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                     if (o instanceof JSTrap.JSTrappable) {
                         t = ((JSTrap.JSTrappable)o).getTrap(v);
                         while (t != null && t.f.numFormalArgs != 0) t = t.next;
-                    } else if (o instanceof JSTrap.JSTrapScope && key.equals("cascade")) {
+                    } else if (o instanceof JSTrap.JSTrapScope && v.equals("cascade")) {
                         t = ((JSTrap.JSTrapScope)o).t.next;
                         while (t != null && t.f.numFormalArgs != 0) t = t.next;
-                        if (t == null) o = ((JSTrap.JSTrapScope)o).t.trappee;
+                        if (t == null) o = ((JSTrap.JSTrapScope)o).t.trapee;
                     }
                     if (t != null) {
                         cx.stack.push(new CallMarker(cx));
                         JSArray args = new JSArray();
-                        cx.stack.push(ta);
+                        cx.stack.push(args);
                         cx.f = t.f;
-                        cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, null);
+                        cx.scope = new JSTrap.JSTrapScope(cx.f.parentJSScope, t, null);
                         ((JSTrap.JSTrapScope)cx.scope).cascadeHappened = true;
                         cx.pc = -1;
                         break;
                     }
                     ret = ((JS)o).get(v);
-                    if (cx.pausecount > initialPauseCount) return;   // we were paused
+                    if (cx.pausecount > initialPauseCount) return null;   // we were paused
                     cx.stack.push(ret);
                     break;
                 }
@@ -308,7 +315,10 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
             }
             
             case CALL: case CALLMETHOD: {
-                int numArgs = JS.toNumber(arg).intValue();
+                int numArgs = JS.toInt(arg);
+                JSArray arguments = null;
+                Object arg0 = null;
+                Object arg1 = null;
                 Object o = cx.stack.pop();
                 if(o == null) throw je("attempted to call null");
                 Object ret;
@@ -317,14 +327,19 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                     method = o;
                     if (method == null) throw new JS.Exn("cannot call the null method");
                     o = cx.stack.pop();
+                    System.out.println("o == " + o);
+                    System.out.println("method == " + method);
                     if(o instanceof String || o instanceof Number || o instanceof Boolean) {
-                        JSArray arguments = new JSArray();
+                        arguments = new JSArray();
                         for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(cx.stack.pop(), j);
-                        ret = Internal.callMethodOnPrimitive(o,method,arguments);
+                        System.out.println(cx.f.dump());
+                        System.out.println(cx.pc);
+                        ret = Internal.callMethodOnPrimitive(o, method, arguments);
                         cx.stack.push(ret);
                         cx.pc += 2;  // skip the GET and CALL
                         break;
                     } else if (o instanceof JSCallable) {
+                        cx.pc += 2;  // skip the GET and CALL
                         // fall through
                     } else {
                         // put the args back on the stack and let the GET followed by CALL happen
@@ -335,7 +350,7 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
 
                 } else if (o instanceof JSFunction) {
                     // FEATURE: use something similar to call0/call1/call2 here
-                    JSArray arguments = new JSArray();
+                    arguments = new JSArray();
                     for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(cx.stack.pop(), j);
                     cx.stack.push(new CallMarker(cx));
                     cx.stack.push(arguments);
@@ -345,21 +360,21 @@ public class JSFunction extends JSCallable implements ByteCodes, Tokens {
                     break;
                 }
 
+                if (!(o instanceof JSCallable)) throw new JS.Exn("can't call " + o + " @ " + cx.pc + "\n" + cx.f.dump());
                 JSCallable c = ((JSCallable)o);
                 switch(numArgs) {
                     case 0: ret = c.call0(method); break;
                     case 1: ret = c.call1(method, cx.stack.pop()); break;
-                    case 2: ret = c.call2(method, cx.stack.pop(), cx.stack.pop()); break;
+                    case 2: { Object first = cx.stack.pop(); ret = c.call2(method, cx.stack.pop(), first); break; }
                     default: {
-                        JSArray arguments = new JSArray();
+                        arguments = new JSArray();
                         for(int j=numArgs - 1; j >= 0; j--) arguments.setElementAt(cx.stack.pop(), j);
                         ret = c.call(method, arguments);
-                        if (cx.pausecount > initialPauseCount) return;   // we were paused
+                        if (cx.pausecount > initialPauseCount) return null;   // we were paused
                         break;
                     }
                 }
                 cx.stack.push(ret);
-                if (method != null) cx.pc += 2;  // skip the GET and CALL if this was a GETCALL
 
                 break;
             }