1cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Copyright 2012 the V8 project authors. All rights reserved.
2cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Redistribution and use in source and binary forms, with or without
3cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// modification, are permitted provided that the following conditions are
4cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// met:
5cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//
6cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//     * Redistributions of source code must retain the above copyright
7cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       notice, this list of conditions and the following disclaimer.
8cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//     * Redistributions in binary form must reproduce the above
9cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       copyright notice, this list of conditions and the following
10cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       disclaimer in the documentation and/or other materials provided
11cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       with the distribution.
12cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//     * Neither the name of Google Inc. nor the names of its
13cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       contributors may be used to endorse or promote products derived
14cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//       from this software without specific prior written permission.
15cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org//
16cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
28cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Number.prototype methods on non-Numbers.
29cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
30cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toExponential.call({}) },
31cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
32cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
33cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toPrecision.call({}) },
34cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
35cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
36cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toFixed.call({}) },
37cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
38cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
39cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toString.call({}) },
40cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
41cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
42cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toLocaleString.call({}) },
43cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
44cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
45cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.ValueOf.call({}) },
46cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             TypeError);
47cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
48cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
49cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Call on Number objects with custom valueOf method.
50cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
51cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgvar x_obj = new Number(1);
52cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgx_obj.valueOf = function() { assertUnreachable(); };
53cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
54cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.00e+0",
55cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toExponential.call(x_obj, 2));
56cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
57cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.0",
58cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toPrecision.call(x_obj, 2));
59cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
60cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.00",
61cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toFixed.call(x_obj, 2));
62cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
63cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Call on primitive numbers.
64cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.00e+0",
65cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toExponential.call(1, 2));
66cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
67cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.0",
68cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toPrecision.call(1, 2));
69cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
70cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("1.00",
71cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toFixed.call(1, 2));
72cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
73cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
74cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// toExponential and toPrecision does following steps in order
75cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// 1) convert the argument using ToInteger
76cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// 2) check for non-finite receiver, on which it returns,
77cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// 3) check argument range and throw exception if out of range.
78cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Note that the the last two steps are reversed for toFixed.
79cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// Luckily, the receiver is expected to be a number or number
80cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// wrapper, so that getting its value is not observable.
81cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
82cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgvar f_flag = false;
83cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgvar f_obj = { valueOf: function() { f_flag = true; return 1000; } };
84cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
85cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("NaN",
86cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toExponential.call(NaN, f_obj));
87cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
88cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
89cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
90cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("Infinity",
91cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toExponential.call(1/0, f_obj));
92cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
93cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
94cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
95cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("-Infinity",
96cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toExponential.call(-1/0, f_obj));
97cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
98cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
99cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
100cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("NaN",
101cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toPrecision.call(NaN, f_obj));
102cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
103cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
104cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
105cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("Infinity",
106cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toPrecision.call(1/0, f_obj));
107cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
108cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
109cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
110cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertEquals("-Infinity",
111cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             Number.prototype.toPrecision.call(-1/0, f_obj));
112cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
113cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
114cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org// The odd man out: toFixed.
115cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
116cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
117cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toFixed.call(NaN, f_obj) },
118cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             RangeError);
119cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
120cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
121cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
122cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toFixed.call(1/0, f_obj) },
123cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             RangeError);
124cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
125cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org
126cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgf_flag = false;
127cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertThrows(function() { Number.prototype.toFixed.call(-1/0, f_obj) },
128cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org             RangeError);
129cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.orgassertTrue(f_flag);
130