OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
8 #include "src/conversions.h" | 8 #include "src/conversions.h" |
9 #include "src/counters.h" | 9 #include "src/counters.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 Handle<Object> fraction_digits = args.atOrUndefined(isolate, 1); | 22 Handle<Object> fraction_digits = args.atOrUndefined(isolate, 1); |
23 | 23 |
24 // Unwrap the receiver {value}. | 24 // Unwrap the receiver {value}. |
25 if (value->IsJSValue()) { | 25 if (value->IsJSValue()) { |
26 value = handle(Handle<JSValue>::cast(value)->value(), isolate); | 26 value = handle(Handle<JSValue>::cast(value)->value(), isolate); |
27 } | 27 } |
28 if (!value->IsNumber()) { | 28 if (!value->IsNumber()) { |
29 THROW_NEW_ERROR_RETURN_FAILURE( | 29 THROW_NEW_ERROR_RETURN_FAILURE( |
30 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 30 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
31 isolate->factory()->NewStringFromAsciiChecked( | 31 isolate->factory()->NewStringFromAsciiChecked( |
32 "Number.prototype.toExponential"), | 32 "Number.prototype.toExponential"))); |
33 isolate->factory()->Number_string())); | |
34 } | 33 } |
35 double const value_number = value->Number(); | 34 double const value_number = value->Number(); |
36 | 35 |
37 // Convert the {fraction_digits} to an integer first. | 36 // Convert the {fraction_digits} to an integer first. |
38 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 37 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
39 isolate, fraction_digits, Object::ToInteger(isolate, fraction_digits)); | 38 isolate, fraction_digits, Object::ToInteger(isolate, fraction_digits)); |
40 double const fraction_digits_number = fraction_digits->Number(); | 39 double const fraction_digits_number = fraction_digits->Number(); |
41 | 40 |
42 if (std::isnan(value_number)) return isolate->heap()->nan_string(); | 41 if (std::isnan(value_number)) return isolate->heap()->nan_string(); |
43 if (std::isinf(value_number)) { | 42 if (std::isinf(value_number)) { |
(...skipping 22 matching lines...) Expand all Loading... |
66 Handle<Object> fraction_digits = args.atOrUndefined(isolate, 1); | 65 Handle<Object> fraction_digits = args.atOrUndefined(isolate, 1); |
67 | 66 |
68 // Unwrap the receiver {value}. | 67 // Unwrap the receiver {value}. |
69 if (value->IsJSValue()) { | 68 if (value->IsJSValue()) { |
70 value = handle(Handle<JSValue>::cast(value)->value(), isolate); | 69 value = handle(Handle<JSValue>::cast(value)->value(), isolate); |
71 } | 70 } |
72 if (!value->IsNumber()) { | 71 if (!value->IsNumber()) { |
73 THROW_NEW_ERROR_RETURN_FAILURE( | 72 THROW_NEW_ERROR_RETURN_FAILURE( |
74 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 73 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
75 isolate->factory()->NewStringFromAsciiChecked( | 74 isolate->factory()->NewStringFromAsciiChecked( |
76 "Number.prototype.toFixed"), | 75 "Number.prototype.toFixed"))); |
77 isolate->factory()->Number_string())); | |
78 } | 76 } |
79 double const value_number = value->Number(); | 77 double const value_number = value->Number(); |
80 | 78 |
81 // Convert the {fraction_digits} to an integer first. | 79 // Convert the {fraction_digits} to an integer first. |
82 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 80 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
83 isolate, fraction_digits, Object::ToInteger(isolate, fraction_digits)); | 81 isolate, fraction_digits, Object::ToInteger(isolate, fraction_digits)); |
84 double const fraction_digits_number = fraction_digits->Number(); | 82 double const fraction_digits_number = fraction_digits->Number(); |
85 | 83 |
86 // Check if the {fraction_digits} are in the supported range. | 84 // Check if the {fraction_digits} are in the supported range. |
87 if (fraction_digits_number < 0.0 || fraction_digits_number > 20.0) { | 85 if (fraction_digits_number < 0.0 || fraction_digits_number > 20.0) { |
(...skipping 21 matching lines...) Expand all Loading... |
109 Handle<Object> value = args.at(0); | 107 Handle<Object> value = args.at(0); |
110 | 108 |
111 // Unwrap the receiver {value}. | 109 // Unwrap the receiver {value}. |
112 if (value->IsJSValue()) { | 110 if (value->IsJSValue()) { |
113 value = handle(Handle<JSValue>::cast(value)->value(), isolate); | 111 value = handle(Handle<JSValue>::cast(value)->value(), isolate); |
114 } | 112 } |
115 if (!value->IsNumber()) { | 113 if (!value->IsNumber()) { |
116 THROW_NEW_ERROR_RETURN_FAILURE( | 114 THROW_NEW_ERROR_RETURN_FAILURE( |
117 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 115 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
118 isolate->factory()->NewStringFromAsciiChecked( | 116 isolate->factory()->NewStringFromAsciiChecked( |
119 "Number.prototype.toLocaleString"), | 117 "Number.prototype.toLocaleString"))); |
120 isolate->factory()->Number_string())); | |
121 } | 118 } |
122 | 119 |
123 // Turn the {value} into a String. | 120 // Turn the {value} into a String. |
124 return *isolate->factory()->NumberToString(value); | 121 return *isolate->factory()->NumberToString(value); |
125 } | 122 } |
126 | 123 |
127 // ES6 section 20.1.3.5 Number.prototype.toPrecision ( precision ) | 124 // ES6 section 20.1.3.5 Number.prototype.toPrecision ( precision ) |
128 BUILTIN(NumberPrototypeToPrecision) { | 125 BUILTIN(NumberPrototypeToPrecision) { |
129 HandleScope scope(isolate); | 126 HandleScope scope(isolate); |
130 Handle<Object> value = args.at(0); | 127 Handle<Object> value = args.at(0); |
131 Handle<Object> precision = args.atOrUndefined(isolate, 1); | 128 Handle<Object> precision = args.atOrUndefined(isolate, 1); |
132 | 129 |
133 // Unwrap the receiver {value}. | 130 // Unwrap the receiver {value}. |
134 if (value->IsJSValue()) { | 131 if (value->IsJSValue()) { |
135 value = handle(Handle<JSValue>::cast(value)->value(), isolate); | 132 value = handle(Handle<JSValue>::cast(value)->value(), isolate); |
136 } | 133 } |
137 if (!value->IsNumber()) { | 134 if (!value->IsNumber()) { |
138 THROW_NEW_ERROR_RETURN_FAILURE( | 135 THROW_NEW_ERROR_RETURN_FAILURE( |
139 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 136 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
140 isolate->factory()->NewStringFromAsciiChecked( | 137 isolate->factory()->NewStringFromAsciiChecked( |
141 "Number.prototype.toPrecision"), | 138 "Number.prototype.toPrecision"))); |
142 isolate->factory()->Number_string())); | |
143 } | 139 } |
144 double const value_number = value->Number(); | 140 double const value_number = value->Number(); |
145 | 141 |
146 // If no {precision} was specified, just return ToString of {value}. | 142 // If no {precision} was specified, just return ToString of {value}. |
147 if (precision->IsUndefined(isolate)) { | 143 if (precision->IsUndefined(isolate)) { |
148 return *isolate->factory()->NumberToString(value); | 144 return *isolate->factory()->NumberToString(value); |
149 } | 145 } |
150 | 146 |
151 // Convert the {precision} to an integer first. | 147 // Convert the {precision} to an integer first. |
152 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, precision, | 148 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, precision, |
(...skipping 23 matching lines...) Expand all Loading... |
176 Handle<Object> radix = args.atOrUndefined(isolate, 1); | 172 Handle<Object> radix = args.atOrUndefined(isolate, 1); |
177 | 173 |
178 // Unwrap the receiver {value}. | 174 // Unwrap the receiver {value}. |
179 if (value->IsJSValue()) { | 175 if (value->IsJSValue()) { |
180 value = handle(Handle<JSValue>::cast(value)->value(), isolate); | 176 value = handle(Handle<JSValue>::cast(value)->value(), isolate); |
181 } | 177 } |
182 if (!value->IsNumber()) { | 178 if (!value->IsNumber()) { |
183 THROW_NEW_ERROR_RETURN_FAILURE( | 179 THROW_NEW_ERROR_RETURN_FAILURE( |
184 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 180 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
185 isolate->factory()->NewStringFromAsciiChecked( | 181 isolate->factory()->NewStringFromAsciiChecked( |
186 "Number.prototype.toString"), | 182 "Number.prototype.toString"))); |
187 isolate->factory()->Number_string())); | |
188 } | 183 } |
189 double const value_number = value->Number(); | 184 double const value_number = value->Number(); |
190 | 185 |
191 // If no {radix} was specified, just return ToString of {value}. | 186 // If no {radix} was specified, just return ToString of {value}. |
192 if (radix->IsUndefined(isolate)) { | 187 if (radix->IsUndefined(isolate)) { |
193 return *isolate->factory()->NumberToString(value); | 188 return *isolate->factory()->NumberToString(value); |
194 } | 189 } |
195 | 190 |
196 // Convert the {radix} to an integer first. | 191 // Convert the {radix} to an integer first. |
197 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix, | 192 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix, |
(...skipping 26 matching lines...) Expand all Loading... |
224 } | 219 } |
225 char* const str = | 220 char* const str = |
226 DoubleToRadixCString(value_number, static_cast<int>(radix_number)); | 221 DoubleToRadixCString(value_number, static_cast<int>(radix_number)); |
227 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); | 222 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); |
228 DeleteArray(str); | 223 DeleteArray(str); |
229 return *result; | 224 return *result; |
230 } | 225 } |
231 | 226 |
232 } // namespace internal | 227 } // namespace internal |
233 } // namespace v8 | 228 } // namespace v8 |
OLD | NEW |