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
This commit is contained in:
parent
83bb0f1001
commit
96dd1c7831
@ -607,35 +607,37 @@ function IsPrimitive(x) {
|
||||
|
||||
// ECMA-262, section 8.6.2.6, page 28.
|
||||
function DefaultNumber(x) {
|
||||
var valueOf = x.valueOf;
|
||||
if (IS_SPEC_FUNCTION(valueOf)) {
|
||||
var v = %_CallFunction(x, valueOf);
|
||||
if (%IsPrimitive(v)) return v;
|
||||
}
|
||||
if (!IS_SYMBOL_WRAPPER(x)) {
|
||||
var valueOf = x.valueOf;
|
||||
if (IS_SPEC_FUNCTION(valueOf)) {
|
||||
var v = %_CallFunction(x, valueOf);
|
||||
if (%IsPrimitive(v)) return v;
|
||||
}
|
||||
|
||||
var toString = x.toString;
|
||||
if (IS_SPEC_FUNCTION(toString)) {
|
||||
var s = %_CallFunction(x, toString);
|
||||
if (%IsPrimitive(s)) return s;
|
||||
var toString = x.toString;
|
||||
if (IS_SPEC_FUNCTION(toString)) {
|
||||
var s = %_CallFunction(x, toString);
|
||||
if (%IsPrimitive(s)) return s;
|
||||
}
|
||||
}
|
||||
|
||||
throw %MakeTypeError('cannot_convert_to_primitive', []);
|
||||
}
|
||||
|
||||
// ECMA-262, section 8.6.2.6, page 28.
|
||||
function DefaultString(x) {
|
||||
var toString = x.toString;
|
||||
if (IS_SPEC_FUNCTION(toString)) {
|
||||
var s = %_CallFunction(x, toString);
|
||||
if (%IsPrimitive(s)) return s;
|
||||
}
|
||||
if (!IS_SYMBOL_WRAPPER(x)) {
|
||||
var toString = x.toString;
|
||||
if (IS_SPEC_FUNCTION(toString)) {
|
||||
var s = %_CallFunction(x, toString);
|
||||
if (%IsPrimitive(s)) return s;
|
||||
}
|
||||
|
||||
var valueOf = x.valueOf;
|
||||
if (IS_SPEC_FUNCTION(valueOf)) {
|
||||
var v = %_CallFunction(x, valueOf);
|
||||
if (%IsPrimitive(v)) return v;
|
||||
var valueOf = x.valueOf;
|
||||
if (IS_SPEC_FUNCTION(valueOf)) {
|
||||
var v = %_CallFunction(x, valueOf);
|
||||
if (%IsPrimitive(v)) return v;
|
||||
}
|
||||
}
|
||||
|
||||
throw %MakeTypeError('cannot_convert_to_primitive', []);
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,9 @@ TestConstructor()
|
||||
|
||||
function TestValueOf() {
|
||||
for (var i in symbols) {
|
||||
assertTrue(symbols[i] === Object(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])
|
||||
}
|
||||
}
|
||||
@ -113,7 +115,7 @@ function TestToString() {
|
||||
for (var i in symbols) {
|
||||
assertThrows(function() { String(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(Object(symbols[i]).toString()))
|
||||
assertTrue(
|
||||
@ -127,6 +129,8 @@ TestToString()
|
||||
|
||||
function TestToBoolean() {
|
||||
for (var i in symbols) {
|
||||
assertTrue(Boolean(Object(symbols[i])))
|
||||
assertFalse(!Object(symbols[i]))
|
||||
assertTrue(Boolean(symbols[i]).valueOf())
|
||||
assertFalse(!symbols[i])
|
||||
assertTrue(!!symbols[i])
|
||||
@ -144,6 +148,8 @@ TestToBoolean()
|
||||
|
||||
function TestToNumber() {
|
||||
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, symbols[i] + 0)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user