Commit 569574b7 authored by lrn@chromium.org's avatar lrn@chromium.org

Fix implementation of == to correctly convert Date objects to primitives.

Fix issue 1356

BUG=v8:1356
TEST=mjsunit/double-equals

Review URL: http://codereview.chromium.org/6912021

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7761 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent eda2a0d4
...@@ -49,41 +49,47 @@ const $Function = global.Function; ...@@ -49,41 +49,47 @@ const $Function = global.Function;
const $Boolean = global.Boolean; const $Boolean = global.Boolean;
const $NaN = 0/0; const $NaN = 0/0;
// ECMA-262 Section 11.9.3.
// ECMA-262, section 11.9.1, page 55.
function EQUALS(y) { function EQUALS(y) {
if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
var x = this; var x = this;
// NOTE: We use iteration instead of recursion, because it is
// difficult to call EQUALS with the correct setting of 'this' in
// an efficient way.
while (true) { while (true) {
if (IS_NUMBER(x)) { if (IS_NUMBER(x)) {
if (y == null) return 1; // not equal while (true) {
return %NumberEquals(x, %ToNumber(y)); if (IS_NUMBER(y)) return %NumberEquals(x, y);
if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
if (!IS_SPEC_OBJECT(y)) {
// String or boolean.
return %NumberEquals(x, %ToNumber(y));
}
y = %ToPrimitive(y, NO_HINT);
}
} else if (IS_STRING(x)) { } else if (IS_STRING(x)) {
if (IS_STRING(y)) return %StringEquals(x, y); while (true) {
if (IS_STRING(y)) return %StringEquals(x, y);
if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
y = %ToPrimitive(y, NO_HINT);
}
} else if (IS_BOOLEAN(x)) {
if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
if (IS_NULL_OR_UNDEFINED(y)) return 1;
if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
if (y == null) return 1; // not equal // y is object.
x = %ToNumber(x);
y = %ToPrimitive(y, NO_HINT); y = %ToPrimitive(y, NO_HINT);
} else if (IS_BOOLEAN(x)) { } else if (IS_NULL_OR_UNDEFINED(x)) {
if (IS_BOOLEAN(y)) { return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
return %_ObjectEquals(x, y) ? 0 : 1;
}
if (y == null) return 1; // not equal
return %NumberEquals(%ToNumber(x), %ToNumber(y));
} else if (x == null) {
// NOTE: This checks for both null and undefined.
return (y == null) ? 0 : 1;
} else { } else {
// x is not a number, boolean, null or undefined. // x is an object.
if (y == null) return 1; // not equal
if (IS_SPEC_OBJECT(y)) { if (IS_SPEC_OBJECT(y)) {
return %_ObjectEquals(x, y) ? 0 : 1; return %_ObjectEquals(x, y) ? 0 : 1;
} }
if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
if (IS_BOOLEAN(y)) y = %ToNumber(y);
x = %ToPrimitive(x, NO_HINT); x = %ToPrimitive(x, NO_HINT);
} }
} }
......
...@@ -31,84 +31,206 @@ ...@@ -31,84 +31,206 @@
* implementation of assertEquals. * implementation of assertEquals.
*/ */
assertTrue (void 0 == void 0, "void 0 == void 0"); function testEqual(a, b) {
assertTrue (null == null, "null == null"); assertTrue(a == b);
assertFalse(NaN == NaN, "NaN == NaN"); assertTrue(b == a);
assertFalse(NaN == 0, "NaN == 0"); assertFalse(a != b);
assertFalse(0 == NaN, "0 == NaN"); assertFalse(b != a);
assertFalse(NaN == Infinity, "NaN == Inf"); }
assertFalse(Infinity == NaN, "Inf == NaN");
function testNotEqual(a, b) {
assertTrue(Number.MAX_VALUE == Number.MAX_VALUE, "MAX == MAX"); assertFalse(a == b);
assertTrue(Number.MIN_VALUE == Number.MIN_VALUE, "MIN == MIN"); assertFalse(b == a);
assertTrue(Infinity == Infinity, "Inf == Inf"); assertTrue(a != b);
assertTrue(-Infinity == -Infinity, "-Inf == -Inf"); assertTrue(b != a);
}
assertTrue(0 == 0, "0 == 0");
assertTrue(0 == -0, "0 == -0"); // Object where ToPrimitive returns value.
assertTrue(-0 == 0, "-0 == 0"); function Wrapper(value) {
assertTrue(-0 == -0, "-0 == -0"); this.value = value;
this.valueOf = function () { return this.value; };
assertFalse(0.9 == 1, "0.9 == 1"); }
assertFalse(0.999999 == 1, "0.999999 == 1");
assertFalse(0.9999999999 == 1, "0.9999999999 == 1"); // Object where ToPrimitive returns value by failover to toString when
assertFalse(0.9999999999999 == 1, "0.9999999999999 == 1"); // valueOf isn't a function.
function Wrapper2(value) {
assertTrue('hello' == 'hello', "'hello' == 'hello'"); this.value = value;
this.valueOf = null;
assertTrue (true == true, "true == true"); this.toString = function () { return this.value; };
assertTrue (false == false, "false == false"); }
assertFalse(true == false, "true == false");
assertFalse(false == true, "false == true");
// Compare values of same type.
assertFalse(new Wrapper(null) == new Wrapper(null), "new Wrapper(null) == new Wrapper(null)");
assertFalse(new Boolean(true) == new Boolean(true), "new Boolean(true) == new Boolean(true)"); // Numbers are equal if same, unless NaN, which isn't equal to anything, and
assertFalse(new Boolean(false) == new Boolean(false), "new Boolean(false) == new Boolean(false)"); // +/-0 being equal.
testNotEqual(NaN, NaN);
testNotEqual(NaN, 0);
testNotEqual(NaN, Infinity);
testEqual(Number.MAX_VALUE, Number.MAX_VALUE);
testEqual(Number.MIN_VALUE, Number.MIN_VALUE);
testEqual(Infinity, Infinity);
testEqual(-Infinity, -Infinity);
testEqual(0, 0);
testEqual(0, -0);
testEqual(-0, -0);
testNotEqual(0.9, 1);
testNotEqual(0.999999, 1);
testNotEqual(0.9999999999, 1);
testNotEqual(0.9999999999999, 1);
// Strings are equal if containing the same code points.
testEqual('hello', 'hello');
testEqual('hello', 'hel' + 'lo');
testEqual('', '');
testEqual('\u0020\x20', ' '); // Escapes are not part of the value.
// Booleans are equal if they are the same.
testEqual(true, true);
testEqual(false, false);
testNotEqual(true, false);
// Null and undefined are equal to themselves.
testEqual(null, null);
testEqual(undefined, undefined);
// Objects are equal if they are the same object only.
testEqual(Math, Math);
testEqual(Object.prototype, Object.prototype);
(function () { (function () {
var x = new Wrapper(null); var x = new Wrapper(null);
var y = x, z = x; var y = x, z = x;
assertTrue(y == x); testEqual(y, x);
})(); })();
(function () { (function () {
var x = new Boolean(true); var x = new Boolean(true);
var y = x, z = x; var y = x, z = x;
assertTrue(y == x); testEqual(y, x);
})(); })();
(function () { (function () {
var x = new Boolean(false); var x = new Boolean(false);
var y = x, z = x; var y = x, z = x;
assertTrue(y == x); testEqual(y, x);
})(); })();
assertTrue(null == void 0, "null == void 0"); // Test comparing values of different types.
assertTrue(void 0 == null, "void 0 == null");
assertFalse(new Wrapper(null) == null, "new Wrapper(null) == null"); // Null and undefined are equal to each-other, and to nothing else.
assertFalse(null == new Wrapper(null), "null == new Wrapper(null)"); testEqual(null, undefined);
testEqual(undefined, null);
assertTrue(1 == '1', "1 == '1");
assertTrue(255 == '0xff', "255 == '0xff'"); testNotEqual(null, new Wrapper(null));
assertTrue(0 == '\r', "0 == '\\r'"); testNotEqual(null, 0);
assertTrue(1e19 == '1e19', "1e19 == '1e19'"); testNotEqual(null, false);
testNotEqual(null, "");
assertTrue(new Boolean(true) == true, "new Boolean(true) == true"); testNotEqual(null, new Object());
assertTrue(new Boolean(false) == false, "new Boolean(false) == false"); testNotEqual(undefined, new Wrapper(undefined));
assertTrue(true == new Boolean(true), "true == new Boolean(true)"); testNotEqual(undefined, 0);
assertTrue(false == new Boolean(false), "false == new Boolean(false)"); testNotEqual(undefined, false);
testNotEqual(undefined, "");
assertTrue(Boolean(true) == true, "Boolean(true) == true"); testNotEqual(undefined, new Object());
assertTrue(Boolean(false) == false, "Boolean(false) == false");
assertTrue(true == Boolean(true), "true == Boolean(true)"); // Numbers compared to Strings will convert the string to a number using
assertTrue(false == Boolean(false), "false == Boolean(false)"); // the internal ToNumber conversion.
assertTrue(new Wrapper(true) == true, "new Wrapper(true) == true"); testEqual(1, '1');
assertTrue(new Wrapper(false) == false, "new Wrapper(false) == false"); testEqual(255, '0xff');
assertTrue(true == new Wrapper(true), "true = new Wrapper(true)"); testEqual(0, '\r'); // ToNumber ignores tailing and trailing whitespace.
assertTrue(false == new Wrapper(false), "false = new Wrapper(false)"); testEqual(1e19, '1e19');
testEqual(Infinity, "Infinity");
function Wrapper(value) {
this.value = value; // Booleans compared to anything else will be converted to numbers.
this.valueOf = function () { return this.value; }; testEqual(false, 0);
testEqual(true, 1);
testEqual(false, "0"); // String also converted to number.
testEqual(true, "1");
// Objects compared to Number or String (or Boolean, since that's converted
// to Number too) is converted to primitive using ToPrimitive with NO HINT.
// Having no hint means Date gets a string hint, and everything else gets
// a number hint.
testEqual(new Boolean(true), true);
testEqual(new Boolean(true), 1); // First to primtive boolean, then to number.
testEqual(new Boolean(false), false);
testEqual(new Boolean(false), 0);
testEqual(new Wrapper(true), true);
testEqual(new Wrapper(true), 1);
testEqual(new Wrapper(false), false);
testEqual(new Wrapper(false), 0);
testEqual(new Wrapper2(true), true);
testEqual(new Wrapper2(true), 1);
testEqual(new Wrapper2(false), false);
testEqual(new Wrapper2(false), 0);
testEqual(new Number(1), true);
testEqual(new Number(1), 1);
testEqual(new Number(0), false);
testEqual(new Number(0), 0);
// Date objects convert to string, not number (and the string does not
// convert to the number).
testEqual(new Date(42), String(new Date(42)));
testNotEqual(new Date(42), Number(new Date(42)));
var dnow = new Date();
testEqual(dnow, dnow);
testEqual(dnow, String(dnow));
testNotEqual(dnow, Number(dnow));
// Doesn't just call toString, but uses ToPrimitive which tries toString first
// and valueOf second.
dnow.toString = null;
testEqual(dnow, Number(dnow));
dnow.valueOf = function () { return "42"; };
testEqual(dnow, 42);
dnow.toString = function () { return "1"; };
testEqual(dnow, true);
// Objects compared to other objects, or to null and undefined, are not
// converted to primitive.
testNotEqual(new Wrapper(null), new Wrapper(null));
testNotEqual(new Boolean(true), new Boolean(true));
testNotEqual(new Boolean(false), new Boolean(false));
testNotEqual(new String("a"), new String("a"));
testNotEqual(new Number(42), new Number(42));
testNotEqual(new Date(42), new Date(42));
testNotEqual(new Array(42), new Array(42));
testNotEqual(new Object(), new Object());
// Object that can't be converted to primitive.
var badObject = {
valueOf: null,
toString: function() {
return this; // Not primitive.
}
};
testEqual(badObject, badObject);
testNotEqual(badObject, {});
testNotEqual(badObject, null);
testNotEqual(badObject, undefined);
// Forcing conversion will throw.
function testBadConversion(value) {
assertThrows(function() { return badObject == value; });
assertThrows(function() { return badObject != value; });
assertThrows(function() { return value == badObject; });
assertThrows(function() { return value != badObject; });
} }
testBadConversion(0);
testBadConversion("string");
testBadConversion(true);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment