2003/11/19 06:18:46
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:41:58 +0000 (07:41 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:41:58 +0000 (07:41 +0000)
darcs-hash:20040130074158-2ba56-e2318feb48c40457b57986559c48ba3ed0c6bf0d.gz

src/org/xwt/js/Interpreter.java
src/org/xwt/js/JS.java
src/org/xwt/js/JSArray.java
src/org/xwt/js/JSDate.java
src/org/xwt/js/JSExn.java
src/org/xwt/js/JSRegexp.java
src/org/xwt/js/Lexer.java
src/org/xwt/js/package.html

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);
                     }
             };
         }
index f227f53..0f5373c 100644 (file)
@@ -6,7 +6,6 @@ import org.xwt.*;
 import java.io.*;
 import java.util.*;
 
-// FIXME: grafts
 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
 public class JS { 
 
@@ -115,7 +114,6 @@ public class JS {
     public static final Object T = Boolean.TRUE;
     public static final Object F = Boolean.FALSE;
 
-    // FIXME: be much smarter here
     public static final Number N(int i) { return new Integer(i); }
     public static final Number N(long l) { return new Long(l); }
     public static final Number N(double d) { return new Double(d); }
index 069edc0..56d37e2 100644 (file)
@@ -5,7 +5,6 @@ import org.xwt.util.*;
 import java.io.*;
 import java.util.*;
 
-// FIXME: review, use redblacktrees
 /** A JavaScript JSArray */
 public class JSArray extends JS {
     private Vec vec = new Vec();
index 345e3d4..d41dfcc 100644 (file)
@@ -896,71 +896,62 @@ public class JSDate extends JS {
     private static double _toNumber(Object[] o, int index) { return JS.toDouble(o[index]); }
     private static double toDouble(double d) { return d; }
 
-    // FIXME: switch to new calling convention here
-    public JSDate(JSArray args_) {
-        Object[] args = new Object[args_.length()];
-        for(int i=0; i<args.length; i++) args[i] = args_.elementAt(i);
-        JSDate obj = this;
-
-        // if called as a constructor with no args,
-        // return a new Date with the current time.
-        if (args.length == 0) {
-            obj.date = Now();
-            return;
-        }
+    public JSDate(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
 
-        // if called with just one arg -
-        if (args.length == 1) {
-            double date;
-            if (args[0] instanceof JS)
-                args[0] = ((JS) args[0]).toString();
-            if (!(args[0] instanceof String)) {
-                // if it's not a string, use it as a millisecond date
-                date = _toNumber(args[0]);
-            } else {
-                // it's a string; parse it.
-                String str = (String) args[0];
-                date = date_parseString(str);
+        JSDate obj = this;
+        switch (nargs) {
+            case 0: {
+                obj.date = Now();
+                return;
             }
-            obj.date = TimeClip(date);
-            return;
-        }
-
-        // multiple arguments; year, month, day etc.
-        double array[] = new double[MAXARGS];
-        int loop;
-        double d;
-
-        for (loop = 0; loop < MAXARGS; loop++) {
-            if (loop < args.length) {
-                d = _toNumber(args[loop]);
-
-                if (d != d || Double.isInfinite(d)) {
-                    obj.date = Double.NaN;
-                    return;
+            case 1: {
+                double date;
+                if (a0 instanceof JS)
+                    a0 = ((JS) a0).toString();
+                if (!(a0 instanceof String)) {
+                    // if it's not a string, use it as a millisecond date
+                    date = _toNumber(a0);
+                } else {
+                    // it's a string; parse it.
+                    String str = (String) a0;
+                    date = date_parseString(str);
                 }
-                array[loop] = toDouble(args[loop]);
-            } else {
-                array[loop] = 0;
+                obj.date = TimeClip(date);
+                return;
+            }
+            default: {
+                // multiple arguments; year, month, day etc.
+                double array[] = new double[MAXARGS];
+                array[0] = toDouble(a0);
+                array[1] = toDouble(a1);
+                if (nargs >= 2) array[2] = toDouble(a2);
+                for(int i=0; i<nargs; i++) {
+                    double d = _toNumber(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
+                    if (d != d || Double.isInfinite(d)) {
+                        obj.date = Double.NaN;
+                        return;
+                    }
+                    array[i] = d;
+                }
+                
+                /* adjust 2-digit years into the 20th century */
+                if (array[0] >= 0 && array[0] <= 99)
+                    array[0] += 1900;
+                
+                /* if we got a 0 for 'date' (which is out of range)
+                 * pretend it's a 1 */
+                if (array[2] < 1)
+                    array[2] = 1;
+                
+                double day = MakeDay(array[0], array[1], array[2]);
+                double time = MakeTime(array[3], array[4], array[5], array[6]);
+                time = MakeDate(day, time);
+                time = internalUTC(time);
+                obj.date = TimeClip(time);
+                
+                return;
             }
         }
-
-        /* adjust 2-digit years into the 20th century */
-        if (array[0] >= 0 && array[0] <= 99)
-            array[0] += 1900;
-
-        /* if we got a 0 for 'date' (which is out of range)
-         * pretend it's a 1 */
-        if (array[2] < 1)
-            array[2] = 1;
-
-        double day = MakeDay(array[0], array[1], array[2]);
-        double time = MakeTime(array[3], array[4], array[5], array[6]);
-        time = MakeDate(day, time);
-        time = internalUTC(time);
-        obj.date = TimeClip(time);
-
-        return;
     }
 
     /* constants for toString, toUTCString */
index 8576c6a..370da2d 100644 (file)
@@ -6,7 +6,6 @@ import org.xwt.*;
 import java.io.*;
 import java.util.*;
 
-// FIXME: extend Exception, not RuntimeException
 /** An exception which can be thrown and caught by JavaScript code */
 public class JSExn extends RuntimeException { 
     private Object js = null; 
index ca22fce..37bfe6e 100644 (file)
@@ -263,9 +263,8 @@ public class JSRegexp extends JS {
     }
                     
     
-    public static Object stringSplit(Object o, String s, Object arg0) {
-        // FIXME: reintroduce args.length() < 2 ? Integer.MAX_VALUE : JS.toInt(args.elementAt(1));
-        int limit = JS.toInt(arg0);
+    public static Object stringSplit(String s, Object arg0, Object arg1, int nargs) {
+        int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
         if(limit < 0) limit = Integer.MAX_VALUE;
         if(limit == 0) return new JSArray();
         
index 5a6e02e..e0ecde9 100644 (file)
@@ -77,98 +77,64 @@ class Lexer implements Tokens {
     
     // Token Subtype Handlers /////////////////////////////////////////////////////////
 
-    // FIXME: convert to a string switch
-    private int getKeyword(String s) throws IOException {
-        char c;
-        switch (s.length()) {
-            case 2: c=s.charAt(1);
-                if (c=='f') { if (s.charAt(0)=='i') return IF; }
-                else if (c=='n') { if (s.charAt(0)=='i') return IN; }
-                else if (c=='o') { if (s.charAt(0)=='d') return DO; }
-                break;
-            case 3: switch (s.charAt(0)) {
-                case 'a': if (s.charAt(2)=='d' && s.charAt(1)=='n') return AND; break;
-                case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') return FOR; break;
-                case 'i': if (s.charAt(2)=='t' && s.charAt(1)=='n') return RESERVED;
-                case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e') return RESERVED;
-                case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') return TRY; break;
-                case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') return VAR; break;
-                } break;
-            case 4: switch (s.charAt(0)) {
-                case 'b': return s.equals("byte") ? RESERVED : -1;
-                case 'c': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') return CASE; }
-                    else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') return RESERVED; }
-                    return -1;
-                case 'e': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') return ELSE; }
-                    else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') return RESERVED; }
-                    return -1;
-                case 'g': return s.equals("goto") ? RESERVED : -1;
-                case 'l': return s.equals("long") ? RESERVED : -1;
-                case 'n': return s.equals("null") ? NULL : -1;
-                case 't': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') return TRUE; }
-                    return -1;
-                case 'w': if (s.equals("with")) return RESERVED; else return -1;
-                case 'v': if (s.equals("void")) return RESERVED; else return -1;
-                } break;
-            case 5: switch (s.charAt(2)) {
-                case 'a': return s.equals("class") ? RESERVED : -1;
-                case 'e': return s.equals("break") ? BREAK : -1;
-                case 'i': return s.equals("while") ? WHILE : -1;
-                case 'l': return s.equals("false") ? FALSE : -1;
-                case 'n': c=s.charAt(0);
-                    if (s.equals("const")) return RESERVED;
-                    else if (s.equals("final")) return RESERVED;
-                    return -1;
-                case 'o': c=s.charAt(0);
-                    if (c == 'c') return s.equals("float") ? RESERVED : -1;
-                    else if (c=='s') return s.equals("final") ? RESERVED : -1;
-                    break;
-                case 'p': return s.equals("super") ? RESERVED : -1;
-                case 'r': return s.equals("throw") ? THROW : -1;
-                case 't': return s.equals("catch") ? CATCH : -1;
-                } break;
-            case 6: switch (s.charAt(1)) {
-                case 'a': return s.equals("class") ? RESERVED : -1;
-                case 'e': c=s.charAt(0);
-                    if (s.equals("delete")) return RESERVED;
-                    else if (c=='r') return s.equals("return") ? RETURN : -1;
-                    break;
-                case 'h': return s.equals("throws") ? RESERVED : -1;
-                case 'o': return s.equals("double") ? RESERVED : -1;
-                case 's': return s.equals("assert") ? ASSERT : -1;
-                case 'u': return s.equals("public") ? RESERVED : -1;
-                case 'w': return s.equals("switch") ? SWITCH : -1;
-                case 'y': return s.equals("typeof") ? TYPEOF : -1;
-                } break;
-            case 7: switch (s.charAt(1)) {
-                case 'a': return s.equals("package") ? RESERVED : -1;
-                case 'e': return s.equals("default") ? DEFAULT : -1;
-                case 'i': return s.equals("finally") ? FINALLY : -1;
-                case 'o': return s.equals("boolean") ? RESERVED : -1;
-                case 'r': return s.equals("private") ? RESERVED : -1;
-                case 'x': return s.equals("extends") ? RESERVED : -1;
-                } break;
-            case 8: switch (s.charAt(0)) {
-                case 'a': return s.equals("abstract") ? RESERVED : -1;
-                case 'c': return s.equals("continue") ? CONTINUE : -1;
-                case 'd': return s.equals("debugger") ? RESERVED : -1;
-                case 'f': return s.equals("function") ? FUNCTION : -1;
-                case 'v': return s.equals("volatile") ? RESERVED : -1;
-                } break;
-            case 9: c=s.charAt(0);
-                if (c=='i') return s.equals("interface") ? RESERVED : -1;
-                else if (c=='p') return s.equals("protected") ? RESERVED : -1;
-                else if (c=='t') return s.equals("transient") ? RESERVED : -1;
-                break;
-            case 10: c=s.charAt(1);
-                if (c=='m') return s.equals("implements") ? RESERVED : -1;
-                else if (c=='n' && s.equals("instanceof")) return RESERVED;
-                break;
-            case 12: return s.equals("synchronized") ? RESERVED : -1;
-            }
+    private int getKeyword(String name) throws IOException {
+        //#switch(name)
+        case "if": return IF;
+        case "in": return IN;
+        case "do": return DO;
+        case "and": return AND;
+        case "for": return FOR;
+        case "int": return RESERVED;
+        case "new": return RESERVED;
+        case "try": return TRY;
+        case "var": return VAR;
+        case "byte": return RESERVED;
+        case "case": return CASE;
+        case "char": return RESERVED;
+        case "else": return ELSE;
+        case "enum": return RESERVED;
+        case "goto": return RESERVED;
+        case "long": return RESERVED;
+        case "null": return NULL;
+        case "true": return TRUE;
+        case "with": return RESERVED;
+        case "void": return RESERVED;
+        case "class": return RESERVED;
+        case "break": return BREAK;
+        case "while": return WHILE;
+        case "false": return FALSE;
+        case "const": return RESERVED;
+        case "final": return RESERVED;
+        case "super": return RESERVED;
+        case "throw": return THROW;
+        case "catch": return CATCH;
+        case "class": return RESERVED;
+        case "delete": return RESERVED;
+        case "return": return RETURN;
+        case "throws": return RESERVED;
+        case "double": return RESERVED;
+        case "assert": return ASSERT;
+        case "public": return RESERVED;
+        case "switch": return SWITCH;
+        case "typeof": return TYPEOF;
+        case "package": return RESERVED;
+        case "default": return DEFAULT;
+        case "finally": return FINALLY;
+        case "boolean": return RESERVED;
+        case "private": return RESERVED;
+        case "extends": return RESERVED;
+        case "abstract": return RESERVED;
+        case "continue": return CONTINUE;
+        case "debugger": return RESERVED;
+        case "function": return FUNCTION;
+        case "volatile": return RESERVED;
+        case "interface": return RESERVED;
+        case "protected": return RESERVED;
+        case "transient": return RESERVED;
+        case "implements": return RESERVED;
+        case "instanceof": return RESERVED;
+        case "synchronized": return RESERVED;
+        //#end
         return -1;
     }
 
index 77b3de5..118bc6d 100644 (file)
@@ -1,7 +1,7 @@
 <body>
 
 <p>
-The XWT JavaScript Engine.  See org.xwt.js.JS for the public face of the API.
+The XWT JavaScript Engine.
 </p>
 
 </body>
\ No newline at end of file