Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Side by Side Diff: src/runtime.js

Issue 6912021: Fix implementation of == to correctly convert Date objects to primitives. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added more case for Date Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/double-equals.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // The following const declarations are shared with other native JS files. 42 // The following const declarations are shared with other native JS files.
43 // They are all declared at this one spot to avoid const redeclaration errors. 43 // They are all declared at this one spot to avoid const redeclaration errors.
44 const $Object = global.Object; 44 const $Object = global.Object;
45 const $Array = global.Array; 45 const $Array = global.Array;
46 const $String = global.String; 46 const $String = global.String;
47 const $Number = global.Number; 47 const $Number = global.Number;
48 const $Function = global.Function; 48 const $Function = global.Function;
49 const $Boolean = global.Boolean; 49 const $Boolean = global.Boolean;
50 const $NaN = 0/0; 50 const $NaN = 0/0;
51 51
52 52 // ECMA-262 Section 11.9.3.
53 // ECMA-262, section 11.9.1, page 55.
54 function EQUALS(y) { 53 function EQUALS(y) {
55 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); 54 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
56 var x = this; 55 var x = this;
57 56
58 // NOTE: We use iteration instead of recursion, because it is
59 // difficult to call EQUALS with the correct setting of 'this' in
60 // an efficient way.
61 while (true) { 57 while (true) {
62 if (IS_NUMBER(x)) { 58 if (IS_NUMBER(x)) {
63 if (y == null) return 1; // not equal 59 while (true) {
64 return %NumberEquals(x, %ToNumber(y)); 60 if (IS_NUMBER(y)) return %NumberEquals(x, y);
61 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
62 if (!IS_SPEC_OBJECT(y)) {
63 // String or boolean.
64 return %NumberEquals(x, %ToNumber(y));
65 }
66 y = %ToPrimitive(y, NO_HINT);
67 }
65 } else if (IS_STRING(x)) { 68 } else if (IS_STRING(x)) {
66 if (IS_STRING(y)) return %StringEquals(x, y); 69 while (true) {
70 if (IS_STRING(y)) return %StringEquals(x, y);
71 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
72 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
73 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
74 y = %ToPrimitive(y, NO_HINT);
75 }
76 } else if (IS_BOOLEAN(x)) {
77 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
78 if (IS_NULL_OR_UNDEFINED(y)) return 1;
67 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); 79 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
68 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); 80 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
69 if (y == null) return 1; // not equal 81 // y is object.
82 x = %ToNumber(x);
70 y = %ToPrimitive(y, NO_HINT); 83 y = %ToPrimitive(y, NO_HINT);
71 } else if (IS_BOOLEAN(x)) { 84 } else if (IS_NULL_OR_UNDEFINED(x)) {
72 if (IS_BOOLEAN(y)) { 85 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
73 return %_ObjectEquals(x, y) ? 0 : 1;
74 }
75 if (y == null) return 1; // not equal
76 return %NumberEquals(%ToNumber(x), %ToNumber(y));
77 } else if (x == null) {
78 // NOTE: This checks for both null and undefined.
79 return (y == null) ? 0 : 1;
80 } else { 86 } else {
81 // x is not a number, boolean, null or undefined. 87 // x is an object.
82 if (y == null) return 1; // not equal
83 if (IS_SPEC_OBJECT(y)) { 88 if (IS_SPEC_OBJECT(y)) {
84 return %_ObjectEquals(x, y) ? 0 : 1; 89 return %_ObjectEquals(x, y) ? 0 : 1;
85 } 90 }
86 91 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
92 if (IS_BOOLEAN(y)) y = %ToNumber(y);
87 x = %ToPrimitive(x, NO_HINT); 93 x = %ToPrimitive(x, NO_HINT);
88 } 94 }
89 } 95 }
90 } 96 }
91 97
92 // ECMA-262, section 11.9.4, page 56. 98 // ECMA-262, section 11.9.4, page 56.
93 function STRICT_EQUALS(x) { 99 function STRICT_EQUALS(x) {
94 if (IS_STRING(this)) { 100 if (IS_STRING(this)) {
95 if (!IS_STRING(x)) return 1; // not equal 101 if (!IS_STRING(x)) return 1; // not equal
96 return %StringEquals(this, x); 102 return %StringEquals(this, x);
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 throw %MakeTypeError('cannot_convert_to_primitive', []); 640 throw %MakeTypeError('cannot_convert_to_primitive', []);
635 } 641 }
636 642
637 643
638 // NOTE: Setting the prototype for Array must take place as early as 644 // NOTE: Setting the prototype for Array must take place as early as
639 // possible due to code generation for array literals. When 645 // possible due to code generation for array literals. When
640 // generating code for a array literal a boilerplate array is created 646 // generating code for a array literal a boilerplate array is created
641 // that is cloned when running the code. It is essiential that the 647 // that is cloned when running the code. It is essiential that the
642 // boilerplate gets the right prototype. 648 // boilerplate gets the right prototype.
643 %FunctionSetPrototype($Array, new $Array(0)); 649 %FunctionSetPrototype($Array, new $Array(0));
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/double-equals.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698