From 6b660f2812d09ab35c46dbb32aeb52c21b65b896 Mon Sep 17 00:00:00 2001 From: "arv@chromium.org" Date: Fri, 12 Sep 2014 16:17:27 +0000 Subject: [PATCH] ES6: String(symbol) should work like symbol.toString Using String as a function and passing a symbol should return the same value as if Symbol.prototype.toString was called. http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string-constructor-string-value BUG=v8:3554 LOG=Y R=rossberg@chromium.org, rossberg Review URL: https://codereview.chromium.org/564863002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23923 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/string.js | 7 ++++--- test/mjsunit/es6/symbols.js | 3 ++- test/mjsunit/harmony/private.js | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/string.js b/src/string.js index 99f5fc9..ac5cb7f 100644 --- a/src/string.js +++ b/src/string.js @@ -9,11 +9,12 @@ // ------------------------------------------------------------------- function StringConstructor(x) { - var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x); + if (%_ArgumentsLength() == 0) x = ''; if (%_IsConstructCall()) { - %_SetValueOf(this, value); + %_SetValueOf(this, TO_STRING_INLINE(x)); } else { - return value; + return IS_SYMBOL(x) ? + %_CallFunction(x, SymbolToString) : TO_STRING_INLINE(x); } } diff --git a/test/mjsunit/es6/symbols.js b/test/mjsunit/es6/symbols.js index 0b07002..60737af 100644 --- a/test/mjsunit/es6/symbols.js +++ b/test/mjsunit/es6/symbols.js @@ -112,7 +112,8 @@ TestValueOf() function TestToString() { for (var i in symbols) { - assertThrows(function() { String(symbols[i]) }, TypeError) + assertThrows(function() { new String(symbols[i]) }, TypeError) + assertEquals(symbols[i].toString(), String(symbols[i])) assertThrows(function() { symbols[i] + "" }, TypeError) assertThrows(function() { String(Object(symbols[i])) }, TypeError) assertTrue(isValidSymbolString(symbols[i].toString())) diff --git a/test/mjsunit/harmony/private.js b/test/mjsunit/harmony/private.js index 4b29fd8..218094c 100644 --- a/test/mjsunit/harmony/private.js +++ b/test/mjsunit/harmony/private.js @@ -83,7 +83,8 @@ TestConstructor() function TestToString() { for (var i in symbols) { - assertThrows(function() { String(symbols[i]) }, TypeError) + assertThrows(function() {new String(symbols[i]) }, TypeError) + assertEquals(symbols[i].toString(), String(symbols[i])) assertThrows(function() { symbols[i] + "" }, TypeError) assertTrue(isValidSymbolString(symbols[i].toString())) assertTrue(isValidSymbolString(Object(symbols[i]).toString())) -- 2.7.4