Commit cd306760 authored by dslomov@chromium.org's avatar dslomov@chromium.org

ToNumber(Symbol) should throw TypeError

https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tonumber

Based on patch from caitp <caitpotter88@gmail.com>
https://codereview.chromium.org/454233002/

BUG=v8:3499
LOG=Y
R=dslomov@chromium.org

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

Patch from Erik Arvidsson <arv@chromium.org>.

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23057 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 88f65f2c
......@@ -164,6 +164,7 @@ var kMessages = {
harmony_const_assign: ["Assignment to constant variable."],
symbol_to_string: ["Cannot convert a Symbol value to a string"],
symbol_to_primitive: ["Cannot convert a Symbol wrapper object to a primitive value"],
symbol_to_number: ["Cannot convert a Symbol value to a number"],
invalid_module_path: ["Module does not export '", "%0", "', or export is not itself a module"],
module_type_error: ["Module '", "%0", "' used improperly"],
module_export_undefined: ["Export '", "%0", "' is not defined in module"]
......
......@@ -36,6 +36,7 @@ function EQUALS(y) {
while (true) {
if (IS_NUMBER(y)) return %NumberEquals(x, y);
if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
if (IS_SYMBOL(y)) return 1; // not equal
if (!IS_SPEC_OBJECT(y)) {
// String or boolean.
return %NumberEquals(x, %ToNumber(y));
......@@ -501,7 +502,7 @@ function ToNumber(x) {
}
if (IS_BOOLEAN(x)) return x ? 1 : 0;
if (IS_UNDEFINED(x)) return NAN;
if (IS_SYMBOL(x)) return NAN;
if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
}
......@@ -512,7 +513,7 @@ function NonNumberToNumber(x) {
}
if (IS_BOOLEAN(x)) return x ? 1 : 0;
if (IS_UNDEFINED(x)) return NAN;
if (IS_SYMBOL(x)) return NAN;
if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
}
......
......@@ -925,6 +925,7 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
}
// Step 4 - Special handling for array index.
if (!IS_SYMBOL(p)) {
var index = ToUint32(p);
var emit_splice = false;
if (ToString(index) == p && index != 4294967295) {
......@@ -954,6 +955,7 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
}
return true;
}
}
// Step 5 - Fallback to default implementation.
return DefineObjectProperty(obj, p, desc, should_throw);
......
......@@ -149,8 +149,8 @@ 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)
assertThrows(function() { Number(symbols[i]) }, TypeError)
assertThrows(function() { symbols[i] + 0 }, TypeError)
}
}
TestToNumber()
......
......@@ -114,8 +114,8 @@ TestToBoolean()
function TestToNumber() {
for (var i in symbols) {
assertSame(NaN, Number(symbols[i]).valueOf())
assertSame(NaN, symbols[i] + 0)
assertThrows(function() { Number(symbols[i]); }, TypeError);
assertThrows(function() { symbols[i] + 0; }, TypeError);
}
}
TestToNumber()
......
......@@ -102,3 +102,5 @@ trace = [];
var nt = Number(ot);
assertEquals(87, nt);
assertEquals(["gvo", "gts", "ts"], trace);
assertThrows('Number(Symbol())', TypeError);
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