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