new js api
[org.ibex.core.git] / src / org / ibex / js / JSDate.java
index 694122c..600b59d 100644 (file)
@@ -55,19 +55,21 @@ public class JSDate extends JS {
         }
     }
 
-    public String toString() { return date_format(date, FORMATSPEC_FULL); }
+    String coerceToString() { return date_format(date, FORMATSPEC_FULL); }
 
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+    public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+        if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs);
+        String method = JS.toString(method_);
         switch(nargs) {
             case 0: {
                 //#switch(method)
-                case "toString": return date_format(date, FORMATSPEC_FULL);
-                case "toTimeString": return date_format(date, FORMATSPEC_TIME);
-                case "toDateString": return date_format(date, FORMATSPEC_DATE);
-                case "toLocaleString": return toLocaleString(date);
-                case "toLocaleTimeString": return toLocaleTimeString(date);
-                case "toLocaleDateString": return toLocaleDateString(date);
-                case "toUTCString": return toUTCString(date);
+                case "toString": return JS.S(date_format(date, FORMATSPEC_FULL));
+                case "toTimeString": return JS.S(date_format(date, FORMATSPEC_TIME));
+                case "toDateString": return JS.S(date_format(date, FORMATSPEC_DATE));
+                case "toLocaleString": return JS.S(toLocaleString(date));
+                case "toLocaleTimeString": return JS.S(toLocaleTimeString(date));
+                case "toLocaleDateString": return JS.S(toLocaleDateString(date));
+                case "toUTCString": return JS.S(toUTCString(date));
                 case "valueOf": return N(this.date);
                 case "getTime": return N(this.date);
                 case "getYear": return N(getYear(date));
@@ -89,7 +91,7 @@ public class JSDate extends JS {
                 case "getUTCMilliseconds": return N(msFromTime(date));
                 case "getTimezoneOffset": return N(getTimezoneOffset(date));
                 //#end
-                return super.callMethod(method, a0, a1, a2, rest, nargs);
+                return super.callMethod(method_, a0, a1, a2, rest, nargs);
             }
             case 1: {
                 //#switch(method)
@@ -99,7 +101,7 @@ public class JSDate extends JS {
                 // fall through
             }
             default: {
-                Object[] args = new Object[nargs];
+                JS[] args = new JS[nargs];
                 for(int i=0; i<nargs; i++) args[i] = i==0 ? a0 : i==1 ? a1 : i==2 ? a2 : rest[i-3];
                 //#switch(method)
                 case "setMilliseconds": return N(this.makeTime(args, 1, true));
@@ -119,10 +121,12 @@ public class JSDate extends JS {
                 //#end
             }
         }
-        return super.callMethod(method, a0, a1, a2, rest, nargs);
+        return super.callMethod(method_, a0, a1, a2, rest, nargs);
     }
 
-    public Object get(Object key) throws JSExn {
+    public JS get(JS key_) throws JSExn {
+        if(!JS.isString(key_)) return super.get(key_);
+        String key = JS.toString(key_);
         //#switch(key)
         case "toString": return METHOD;
         case "toTimeString": return METHOD;
@@ -168,7 +172,7 @@ public class JSDate extends JS {
         case "setFullYear": return METHOD;
         case "setUTCFullYear": return METHOD;
         //#end
-        return super.get(key);
+        return super.get(key_);
     }
 
     /* ECMA helper functions */
@@ -528,7 +532,7 @@ public class JSDate extends JS {
 
 
     private static final int MAXARGS = 7;
-    private static double jsStaticJSFunction_UTC(Object[] args) {
+    private static double jsStaticJSFunction_UTC(JS[] args) throws JSExn {
         double array[] = new double[MAXARGS];
         int loop;
         double d;
@@ -888,11 +892,11 @@ public class JSDate extends JS {
         return result.toString();
     }
 
-    private static double _toNumber(Object o) { return JS.toDouble(o); }
-    private static double _toNumber(Object[] o, int index) { return JS.toDouble(o[index]); }
+    private static double _toNumber(JS o) throws JSExn { return JS.toDouble(o); }
+    private static double _toNumber(JS[] o, int index) throws JSExn { return JS.toDouble(o[index]); }
     private static double toDouble(double d) { return d; }
 
-    public JSDate(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public JSDate(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
 
         JSDate obj = this;
         switch (nargs) {
@@ -902,16 +906,10 @@ public class JSDate extends JS {
             }
             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
+                if(isString(a0))
+                    date = date_parseString(JS.toString(a0));
+                else
                     date = _toNumber(a0);
-                } else {
-                    // it's a string; parse it.
-                    String str = (String) a0;
-                    date = date_parseString(str);
-                }
                 obj.date = TimeClip(date);
                 return;
             }
@@ -1055,7 +1053,7 @@ public class JSDate extends JS {
         return this.date;
     }
 
-    private double makeTime(Object[] args, int maxargs, boolean local) {
+    private double makeTime(JS[] args, int maxargs, boolean local) throws JSExn {
         int i;
         double conv[] = new double[4];
         double hour, min, sec, msec;
@@ -1079,7 +1077,7 @@ public class JSDate extends JS {
          * d.setMilliseconds()" returns NaN.  Blech.
          */
         if (args.length == 0)
-            args = new Object[] { null };
+            args = new JS[] { null };
 
         for (i = 0; i < args.length && i < maxargs; i++) {
             conv[i] = _toNumber(args[i]);
@@ -1131,15 +1129,15 @@ public class JSDate extends JS {
         return date;
     }
 
-    private double setHours(Object[] args) {
+    private double setHours(JS[] args) throws JSExn {
         return makeTime(args, 4, true);
     }
 
-    private double setUTCHours(Object[] args) {
+    private double setUTCHours(JS[] args) throws JSExn {
         return makeTime(args, 4, false);
     }
 
-    private double makeDate(Object[] args, int maxargs, boolean local) {
+    private double makeDate(JS[] args, int maxargs, boolean local) throws JSExn {
         int i;
         double conv[] = new double[3];
         double year, month, day;
@@ -1150,7 +1148,7 @@ public class JSDate extends JS {
 
         /* See arg padding comment in makeTime.*/
         if (args.length == 0)
-            args = new Object[] { null };
+            args = new JS[] { null };
 
         for (i = 0; i < args.length && i < maxargs; i++) {
             conv[i] = _toNumber(args[i]);