2003/11/19 06:18:46
[org.ibex.core.git] / src / org / xwt / js / Interpreter.java
index f65a585..c049088 100644 (file)
@@ -291,16 +291,19 @@ class Interpreter implements ByteCodes, Tokens {
                     }
                 }
 
+                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 +313,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 +524,7 @@ 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) {
         if (o instanceof Number) {
             //#switch(method)
             case "toFixed": throw new JSExn("toFixed() not implemented");
@@ -576,12 +574,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 +593,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;
@@ -658,7 +653,7 @@ class Interpreter implements ByteCodes, Tokens {
             return new JS() {
                     public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
                         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);
                     }
             };
         }