renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / JSDate.java
index 694122c..19e4fc6 100644 (file)
@@ -44,7 +44,8 @@ import java.text.DateFormat;
  * @author Mike McCabe
  * @author Adam Megacz (many modifications
  */
-public class JSDate extends JS {
+public class JSDate extends JS.Immutable {
+    private static final JS.Method METHOD = new JS.Method();
 
     public JSDate() {
         if (thisTimeZone == null) {
@@ -55,75 +56,73 @@ public class JSDate extends JS {
         }
     }
 
-    public String toString() { return date_format(date, FORMATSPEC_FULL); }
+    public 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 {
-        switch(nargs) {
+    public JS call(JS method, JS[] args) throws JSExn {
+        switch(args.length) {
             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 "valueOf": return N(this.date);
-                case "getTime": return N(this.date);
-                case "getYear": return N(getYear(date));
-                case "getFullYear": return N(YearFromTime(LocalTime(date)));
-                case "getUTCFullYear": return N(YearFromTime(date));
-                case "getMonth": return N(MonthFromTime(LocalTime(date)));
-                case "getUTCMonth": return N(MonthFromTime(date));
-                case "getDate": return N(DateFromTime(LocalTime(date)));
-                case "getUTCDate": return N(DateFromTime(date));
-                case "getDay": return N(WeekDay(LocalTime(date)));
-                case "getUTCDay": return N(WeekDay(date));
-                case "getHours": return N(HourFromTime(LocalTime(date)));
-                case "getUTCHours": return N(HourFromTime(date));
-                case "getMinutes": return N(MinFromTime(LocalTime(date)));
-                case "getUTCMinutes": return N(MinFromTime(date));
-                case "getSeconds": return N(SecFromTime(LocalTime(date)));
-                case "getUTCSeconds": return N(SecFromTime(date));
-                case "getMilliseconds": return N(msFromTime(LocalTime(date)));
-                case "getUTCMilliseconds": return N(msFromTime(date));
-                case "getTimezoneOffset": return N(getTimezoneOffset(date));
+                //#switch(JSU.toString(method))
+                case "toString": return JSU.S(date_format(date, FORMATSPEC_FULL));
+                case "toTimeString": return JSU.S(date_format(date, FORMATSPEC_TIME));
+                case "toDateString": return JSU.S(date_format(date, FORMATSPEC_DATE));
+                case "toLocaleString": return JSU.S(toLocaleString(date));
+                case "toLocaleTimeString": return JSU.S(toLocaleTimeString(date));
+                case "toLocaleDateString": return JSU.S(toLocaleDateString(date));
+                case "toUTCString": return JSU.S(toUTCString(date));
+                case "valueOf": return JSU.N(this.date);
+                case "getTime": return JSU.N(this.date);
+                case "getYear": return JSU.N(getYear(date));
+                case "getFullYear": return JSU.N(YearFromTime(LocalTime(date)));
+                case "getUTCFullYear": return JSU.N(YearFromTime(date));
+                case "getMonth": return JSU.N(MonthFromTime(LocalTime(date)));
+                case "getUTCMonth": return JSU.N(MonthFromTime(date));
+                case "getDate": return JSU.N(DateFromTime(LocalTime(date)));
+                case "getUTCDate": return JSU.N(DateFromTime(date));
+                case "getDay": return JSU.N(WeekDay(LocalTime(date)));
+                case "getUTCDay": return JSU.N(WeekDay(date));
+                case "getHours": return JSU.N(HourFromTime(LocalTime(date)));
+                case "getUTCHours": return JSU.N(HourFromTime(date));
+                case "getMinutes": return JSU.N(MinFromTime(LocalTime(date)));
+                case "getUTCMinutes": return JSU.N(MinFromTime(date));
+                case "getSeconds": return JSU.N(SecFromTime(LocalTime(date)));
+                case "getUTCSeconds": return JSU.N(SecFromTime(date));
+                case "getMilliseconds": return JSU.N(msFromTime(LocalTime(date)));
+                case "getUTCMilliseconds": return JSU.N(msFromTime(date));
+                case "getTimezoneOffset": return JSU.N(getTimezoneOffset(date));
                 //#end
-                return super.callMethod(method, a0, a1, a2, rest, nargs);
+                return super.call(method, args);
             }
             case 1: {
-                //#switch(method)
-                case "setTime": return N(this.setTime(toDouble(a0)));
-                case "setYear": return N(this.setYear(toDouble(a0)));
+                //#switch(JSU.toString(method))
+                case "setTime": return JSU.N(this.setTime(JSU.toDouble(args[0])));
+                case "setYear": return JSU.N(this.setYear(JSU.toDouble(args[0])));
                 //#end
                 // fall through
             }
             default: {
-                Object[] args = new Object[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));
-                case "setUTCMilliseconds": return N(this.makeTime(args, 1, false));
-                case "setSeconds": return N(this.makeTime(args, 2, true));
-                case "setUTCSeconds": return N(this.makeTime(args, 2, false));
-                case "setMinutes": return N(this.makeTime(args, 3, true));
-                case "setUTCMinutes": return N(this.makeTime(args, 3, false));
-                case "setHours": return N(this.makeTime(args, 4, true));
-                case "setUTCHours": return N(this.makeTime(args, 4, false));
-                case "setDate": return N(this.makeDate(args, 1, true));
-                case "setUTCDate": return N(this.makeDate(args, 1, false));
-                case "setMonth": return N(this.makeDate(args, 2, true));
-                case "setUTCMonth": return N(this.makeDate(args, 2, false));
-                case "setFullYear": return N(this.makeDate(args, 3, true));
-                case "setUTCFullYear": return N(this.makeDate(args, 3, false));
+                //#switch(JSU.toString(method))
+                case "setMilliseconds": return JSU.N(this.makeTime(args, 1, true));
+                case "setUTCMilliseconds": return JSU.N(this.makeTime(args, 1, false));
+                case "setSeconds": return JSU.N(this.makeTime(args, 2, true));
+                case "setUTCSeconds": return JSU.N(this.makeTime(args, 2, false));
+                case "setMinutes": return JSU.N(this.makeTime(args, 3, true));
+                case "setUTCMinutes": return JSU.N(this.makeTime(args, 3, false));
+                case "setHours": return JSU.N(this.makeTime(args, 4, true));
+                case "setUTCHours": return JSU.N(this.makeTime(args, 4, false));
+                case "setDate": return JSU.N(this.makeDate(args, 1, true));
+                case "setUTCDate": return JSU.N(this.makeDate(args, 1, false));
+                case "setMonth": return JSU.N(this.makeDate(args, 2, true));
+                case "setUTCMonth": return JSU.N(this.makeDate(args, 2, false));
+                case "setFullYear": return JSU.N(this.makeDate(args, 3, true));
+                case "setUTCFullYear": return JSU.N(this.makeDate(args, 3, false));
                 //#end
             }
         }
-        return super.callMethod(method, a0, a1, a2, rest, nargs);
+        return super.call(method, args);
     }
 
-    public Object get(Object key) throws JSExn {
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#switch(JSU.toString(key))
         case "toString": return METHOD;
         case "toTimeString": return METHOD;
         case "toDateString": return METHOD;
@@ -528,7 +527,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;
@@ -539,7 +538,7 @@ public class JSDate extends JS {
                 if (d != d || Double.isInfinite(d)) {
                     return Double.NaN;
                 }
-                array[loop] = toDouble(args[loop]);
+                array[loop] = JSU.toDouble(args[loop]);
             } else {
                 array[loop] = 0;
             }
@@ -558,7 +557,7 @@ public class JSDate extends JS {
                               array[3], array[4], array[5], array[6]);
         d = TimeClip(d);
         return d;
-        //        return N(d);
+        //        return JSU.N(d);
     }
 
     /*
@@ -888,41 +887,34 @@ 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 toDouble(double d) { return d; }
+    private static double _toNumber(JS o) throws JSExn { return JSU.toDouble(o); }
+    private static double _toNumber(JS[] o, int index) throws JSExn { return JSU.toDouble(o[index]); }
 
-    public JSDate(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public JSDate(JS[] args) throws JSExn {
 
         JSDate obj = this;
-        switch (nargs) {
+        switch (args.length) {
             case 0: {
                 obj.date = Now();
                 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);
-                }
+                if(JSU.isString(args[0]))
+                    date = date_parseString(JSU.toString(args[0]));
+                else
+                    date = _toNumber(args[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]);
+                array[0] = JSU.toDouble(args[0]);
+                array[1] = JSU.toDouble(args[1]);
+                if (args.length >= 2) array[2] = JSU.toDouble(args[2]);
+                for (int i=0; i < args.length; i++) {
+                    double d = _toNumber(args[i]);
                     if (d != d || Double.isInfinite(d)) {
                         obj.date = Double.NaN;
                         return;
@@ -1055,7 +1047,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 +1071,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]);
@@ -1089,7 +1081,6 @@ public class JSDate extends JS {
                 this.date = Double.NaN;
                 return this.date;
             }
-            conv[i] = toDouble(conv[i]);
         }
 
         if (local)
@@ -1131,15 +1122,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 +1141,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]);
@@ -1160,7 +1151,6 @@ public class JSDate extends JS {
                 this.date = Double.NaN;
                 return this.date;
             }
-            conv[i] = toDouble(conv[i]);
         }
 
         /* return NaN if date is NaN and we're not setting the year,