Bug 1187233 - Date constructor should create a copy when called with a Date object. Original patch by Giovanni Sferro. r=jwalden

This commit is contained in:
Jan de Mooij 2015-07-29 22:47:51 -07:00
parent 6c3b08a91f
commit 52f99ae0b7
2 changed files with 56 additions and 1 deletions

View File

@ -3030,11 +3030,26 @@ DateOneArgument(JSContext* cx, const CallArgs& args)
MOZ_ASSERT(args.length() == 1);
if (args.isConstructing()) {
ClippedTime t;
if (args[0].isObject()) {
RootedObject obj(cx, &args[0].toObject());
ESClassValue cls;
if (!GetBuiltinClass(cx, obj, &cls))
return false;
if (cls == ESClass_Date) {
RootedValue unboxed(cx);
if (!Unbox(cx, obj, &unboxed))
return false;
return NewDateObject(cx, args, TimeClip(unboxed.toNumber()));
}
}
if (!ToPrimitive(cx, args[0]))
return false;
ClippedTime t;
if (args[0].isString()) {
JSLinearString* linearStr = args[0].toString()->ensureLinear(cx);
if (!linearStr)

View File

@ -0,0 +1,40 @@
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommonn.org/licenses/publicdomain/
*/
var BUGNUMBER = 1187233;
var summary =
"Passing a Date object to |new Date()| should copy it, not convert it to " +
"a primitive and create it from that.";
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
Date.prototype.toString = Date.prototype.valueOf = null;
var d = new Date(new Date(8675309));
assertEq(d.getTime(), 8675309);
Date.prototype.valueOf = () => 42;
d = new Date(new Date(8675309));
assertEq(d.getTime(), 8675309);
var D = newGlobal().Date;
D.prototype.toString = D.prototype.valueOf = null;
var d = new Date(new D(3141592654));
assertEq(d.getTime(), 3141592654);
D.prototype.valueOf = () => 525600;
d = new Date(new D(3141592654));
assertEq(d.getTime(), 3141592654);
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");