Commit 96dd1c78 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Make ToPrimitive throw on symbol wrappers

R=mstarzinger@chromium.org
BUG=v8:3442
LOG=Y

Review URL: https://codereview.chromium.org/389263003

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22426 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 83bb0f10
...@@ -607,35 +607,37 @@ function IsPrimitive(x) { ...@@ -607,35 +607,37 @@ function IsPrimitive(x) {
// ECMA-262, section 8.6.2.6, page 28. // ECMA-262, section 8.6.2.6, page 28.
function DefaultNumber(x) { function DefaultNumber(x) {
var valueOf = x.valueOf; if (!IS_SYMBOL_WRAPPER(x)) {
if (IS_SPEC_FUNCTION(valueOf)) { var valueOf = x.valueOf;
var v = %_CallFunction(x, valueOf); if (IS_SPEC_FUNCTION(valueOf)) {
if (%IsPrimitive(v)) return v; var v = %_CallFunction(x, valueOf);
} if (%IsPrimitive(v)) return v;
}
var toString = x.toString; var toString = x.toString;
if (IS_SPEC_FUNCTION(toString)) { if (IS_SPEC_FUNCTION(toString)) {
var s = %_CallFunction(x, toString); var s = %_CallFunction(x, toString);
if (%IsPrimitive(s)) return s; if (%IsPrimitive(s)) return s;
}
} }
throw %MakeTypeError('cannot_convert_to_primitive', []); throw %MakeTypeError('cannot_convert_to_primitive', []);
} }
// ECMA-262, section 8.6.2.6, page 28. // ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) { function DefaultString(x) {
var toString = x.toString; if (!IS_SYMBOL_WRAPPER(x)) {
if (IS_SPEC_FUNCTION(toString)) { var toString = x.toString;
var s = %_CallFunction(x, toString); if (IS_SPEC_FUNCTION(toString)) {
if (%IsPrimitive(s)) return s; var s = %_CallFunction(x, toString);
} if (%IsPrimitive(s)) return s;
}
var valueOf = x.valueOf; var valueOf = x.valueOf;
if (IS_SPEC_FUNCTION(valueOf)) { if (IS_SPEC_FUNCTION(valueOf)) {
var v = %_CallFunction(x, valueOf); var v = %_CallFunction(x, valueOf);
if (%IsPrimitive(v)) return v; if (%IsPrimitive(v)) return v;
}
} }
throw %MakeTypeError('cannot_convert_to_primitive', []); throw %MakeTypeError('cannot_convert_to_primitive', []);
} }
......
...@@ -102,7 +102,9 @@ TestConstructor() ...@@ -102,7 +102,9 @@ TestConstructor()
function TestValueOf() { function TestValueOf() {
for (var i in symbols) { for (var i in symbols) {
assertTrue(symbols[i] === Object(symbols[i]).valueOf())
assertTrue(symbols[i] === symbols[i].valueOf()) assertTrue(symbols[i] === symbols[i].valueOf())
assertTrue(Symbol.prototype.valueOf.call(Object(symbols[i])) === symbols[i])
assertTrue(Symbol.prototype.valueOf.call(symbols[i]) === symbols[i]) assertTrue(Symbol.prototype.valueOf.call(symbols[i]) === symbols[i])
} }
} }
...@@ -113,7 +115,7 @@ function TestToString() { ...@@ -113,7 +115,7 @@ function TestToString() {
for (var i in symbols) { for (var i in symbols) {
assertThrows(function() { String(symbols[i]) }, TypeError) assertThrows(function() { String(symbols[i]) }, TypeError)
assertThrows(function() { symbols[i] + "" }, TypeError) assertThrows(function() { symbols[i] + "" }, TypeError)
assertTrue(isValidSymbolString(String(Object(symbols[i])))) assertThrows(function() { String(Object(symbols[i])) }, TypeError)
assertTrue(isValidSymbolString(symbols[i].toString())) assertTrue(isValidSymbolString(symbols[i].toString()))
assertTrue(isValidSymbolString(Object(symbols[i]).toString())) assertTrue(isValidSymbolString(Object(symbols[i]).toString()))
assertTrue( assertTrue(
...@@ -127,6 +129,8 @@ TestToString() ...@@ -127,6 +129,8 @@ TestToString()
function TestToBoolean() { function TestToBoolean() {
for (var i in symbols) { for (var i in symbols) {
assertTrue(Boolean(Object(symbols[i])))
assertFalse(!Object(symbols[i]))
assertTrue(Boolean(symbols[i]).valueOf()) assertTrue(Boolean(symbols[i]).valueOf())
assertFalse(!symbols[i]) assertFalse(!symbols[i])
assertTrue(!!symbols[i]) assertTrue(!!symbols[i])
...@@ -144,6 +148,8 @@ TestToBoolean() ...@@ -144,6 +148,8 @@ TestToBoolean()
function TestToNumber() { function TestToNumber() {
for (var i in symbols) { for (var i in symbols) {
assertThrows(function() { Number(Object(symbols[i])) }, TypeError)
assertThrows(function() { +Object(symbols[i]) }, TypeError)
assertSame(NaN, Number(symbols[i]).valueOf()) assertSame(NaN, Number(symbols[i]).valueOf())
assertSame(NaN, symbols[i] + 0) assertSame(NaN, symbols[i] + 0)
} }
......
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