diff --git a/js/rhino/src/org/mozilla/javascript/NativeDate.java b/js/rhino/src/org/mozilla/javascript/NativeDate.java index 60a8bab7bf4..dae8152f9ae 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeDate.java +++ b/js/rhino/src/org/mozilla/javascript/NativeDate.java @@ -79,6 +79,10 @@ final class NativeDate extends IdScriptable { return super.getDefaultValue(typeHint); } + double getJSTimeValue() { + return date; + } + protected void fillConstructorProperties (Context cx, IdFunction ctor, boolean sealed) { diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java index e8a4bf92475..3afaa729fe0 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java @@ -43,6 +43,7 @@ import java.io.*; import java.lang.reflect.*; import java.util.Hashtable; import java.util.Enumeration; +import java.util.Date; /** * This class reflects non-Array Java objects into the JavaScript environment. It @@ -372,11 +373,13 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} case JSTYPE_OBJECT: // Other objects takes #1-#3 spots - if (fromObj instanceof NativeArray && to.isArray()) { - // This is a native array conversion to a java array - // Array conversions are all equal, and preferable to object - // and string conversion, per LC3. - result = 1; + if (to.isArray()) { + if (fromObj instanceof NativeArray) { + // This is a native array conversion to a java array + // Array conversions are all equal, and preferable to object + // and string conversion, per LC3. + result = 1; + } } else if (to == ScriptRuntime.ObjectClass) { result = 2; @@ -384,6 +387,12 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} else if (to == ScriptRuntime.StringClass) { result = 3; } + else if (to == ScriptRuntime.DateClass) { + if (fromObj instanceof NativeDate) { + // This is a native date to java date conversion + result = 1; + } + } else if (to.isPrimitive() || to != Boolean.TYPE) { result = 3 + NativeJavaObject.getSizeRank(to); } @@ -624,6 +633,13 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)} else if (type.isInstance(value)) { return value; } + else if (type == ScriptRuntime.DateClass + && value instanceof NativeDate) + { + double time = ((NativeDate)value).getJSTimeValue(); + // XXX: This will replace NaN by 0 + return new Date((long)time); + } else if (type.isArray() && value instanceof NativeArray) { // Make a new java array, and coerce the JS array components // to the target (component) type. diff --git a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index 5f784a8af2d..6e5d889fe5c 100644 --- a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -82,6 +82,7 @@ public class ScriptRuntime { public final static Class FunctionClass = Function.class; public final static Class ClassClass = Class.class; public final static Class SerializableClass = java.io.Serializable.class; + public final static Class DateClass = java.util.Date.class; // Can not use .class as Comparable is only since JDK 1.2 public final static Class