Bug 534377 - Object literal duplicate-property checking doesn't work for same property as number and as string. r=jimb
authorJeff Walden <jwalden@mit.edu>
Thu, 24 Dec 2009 16:10:18 -0500
changeset 40856 7bdf4c4951896a47152974fdc37203c28f07aff5
parent 40855 e47d2506e0ad6e314fe8014d33f784c339cdcf99
child 40857 121debb9ff3d7362f3bd8be330144420f7f86d53
push id12825
push userrsayre@mozilla.com
push dateThu, 15 Apr 2010 16:08:37 +0000
treeherdermozilla-central@1138f6156b1b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimb
bugs534377
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 534377 - Object literal duplicate-property checking doesn't work for same property as number and as string. r=jimb
js/src/jsparse.cpp
js/src/tests/ecma_5/strict/11.1.5.js
--- a/js/src/jsparse.cpp
+++ b/js/src/jsparse.cpp
@@ -8054,16 +8054,29 @@ JSCompiler::primaryExpr(TokenKind tt, JS
                     attributesMask = JSPROP_GETTER;
                 } else if (op == JSOP_SETTER) {
                     attributesMask = JSPROP_SETTER;
                 } else {
                     JS_NOT_REACHED("bad opcode in object initializer");
                     attributesMask = 0;
                 }
 
+                /*
+                 * Use only string-valued atoms for detecting duplicate
+                 * properties so that 1 and "1" properly collide.
+                 */
+                if (ATOM_IS_DOUBLE(atom)) {
+                    JSString *str = js_NumberToString(context, pn3->pn_dval);
+                    if (!str)
+                        return JS_FALSE;
+                    atom = js_AtomizeString(context, str, 0);
+                    if (!atom)
+                        return JS_FALSE;
+                }
+
                 JSAtomListElement *ale = seen.lookup(atom);
                 if (ale) {
                     if (ALE_INDEX(ale) & attributesMask) {
                         const char *name = js_AtomToPrintableString(context, atom);
                         if (!name ||
                             !ReportStrictModeError(context, &tokenStream, tc, NULL,
                                                    JSMSG_DUPLICATE_PROPERTY, name)) {
                             return NULL;
--- a/js/src/tests/ecma_5/strict/11.1.5.js
+++ b/js/src/tests/ecma_5/strict/11.1.5.js
@@ -53,16 +53,36 @@ assertEq(testLenientAndStrict('({6.02214
                               parseRaisesException(SyntaxError)),
          true);
 
 assertEq(testLenientAndStrict('({6.02214179e23:1, 3.1415926535:1})',
                               parsesSuccessfully,
                               parsesSuccessfully),
          true);
 
+assertEq(testLenientAndStrict('({ 1: 1, "1": 2 })',
+                              parsesSuccessfully,
+                              parseRaisesException(SyntaxError)),
+         true);
+
+assertEq(testLenientAndStrict('({ "1": 1, 1: 2 })',
+                              parsesSuccessfully,
+                              parseRaisesException(SyntaxError)),
+         true);
+
+assertEq(testLenientAndStrict('({ 2.5: 1, "2.5": 2 })',
+                              parsesSuccessfully,
+                              parseRaisesException(SyntaxError)),
+         true);
+
+assertEq(testLenientAndStrict('({ "2.5": 1, 2.5: 2 })',
+                              parsesSuccessfully,
+                              parseRaisesException(SyntaxError)),
+         true);
+
 /* Many properties, to exercise JSAtomList's hash-table variant. */
 assertEq(testLenientAndStrict('({a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1, i:1, j:1, k:1, l:1, m:1, n:1, o:1, p:1, q:1, r:1, s:1, t:1, u:1, v:1, w:1, x:1, y:1, z:1})',
                               parsesSuccessfully,
                               parsesSuccessfully),
          true);
 
 assertEq(testLenientAndStrict('({a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1, i:1, j:1, k:1, l:1, m:1, n:1, o:1, p:1, q:1, r:1, s:1, t:1, u:1, v:1, w:1, x:1, y:1, a:1})',
                               parsesSuccessfully,