ES6: String(symbol) should work like symbol.toString
authorarv@chromium.org <arv@chromium.org>
Fri, 12 Sep 2014 16:17:27 +0000 (16:17 +0000)
committerarv@chromium.org <arv@chromium.org>
Fri, 12 Sep 2014 16:17:27 +0000 (16:17 +0000)
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
test/mjsunit/es6/symbols.js
test/mjsunit/harmony/private.js

index 99f5fc9..ac5cb7f 100644 (file)
@@ -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);
   }
 }
 
index 0b07002..60737af 100644 (file)
@@ -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()))
index 4b29fd8..218094c 100644 (file)
@@ -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()))