Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/objects.cc

Issue 2579023002: [runtime] Improve Object::ToNumber, ToInteger, ToInt32, ToUint32, ToString (Closed)
Patch Set: fix Uint32 conversion Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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/objects.h" 5 #include "src/objects.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <iomanip> 8 #include <iomanip>
9 #include <memory> 9 #include <memory>
10 #include <sstream> 10 #include <sstream>
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 Handle<Object> object) { 132 Handle<Object> object) {
133 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object); 133 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
134 if (*object == isolate->heap()->null_value() || 134 if (*object == isolate->heap()->null_value() ||
135 object->IsUndefined(isolate)) { 135 object->IsUndefined(isolate)) {
136 return isolate->global_proxy(); 136 return isolate->global_proxy();
137 } 137 }
138 return Object::ToObject(isolate, object); 138 return Object::ToObject(isolate, object);
139 } 139 }
140 140
141 // static 141 // static
142 MaybeHandle<Object> Object::ToNumber(Handle<Object> input) { 142 MaybeHandle<Object> Object::ConvertToNumber(Isolate* isolate,
143 Handle<Object> input) {
143 while (true) { 144 while (true) {
144 if (input->IsNumber()) { 145 if (input->IsNumber()) {
145 return input; 146 return input;
146 } 147 }
147 if (input->IsString()) { 148 if (input->IsString()) {
148 return String::ToNumber(Handle<String>::cast(input)); 149 return String::ToNumber(Handle<String>::cast(input));
149 } 150 }
150 if (input->IsOddball()) { 151 if (input->IsOddball()) {
151 return Oddball::ToNumber(Handle<Oddball>::cast(input)); 152 return Oddball::ToNumber(Handle<Oddball>::cast(input));
152 } 153 }
153 Isolate* const isolate = Handle<HeapObject>::cast(input)->GetIsolate();
154 if (input->IsSymbol()) { 154 if (input->IsSymbol()) {
155 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber), 155 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber),
156 Object); 156 Object);
157 } 157 }
158 if (input->IsSimd128Value()) { 158 if (input->IsSimd128Value()) {
159 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber), 159 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber),
160 Object); 160 Object);
161 } 161 }
162 ASSIGN_RETURN_ON_EXCEPTION( 162 ASSIGN_RETURN_ON_EXCEPTION(
163 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), 163 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
164 ToPrimitiveHint::kNumber), 164 ToPrimitiveHint::kNumber),
165 Object); 165 Object);
166 } 166 }
167 } 167 }
168 168
169
170 // static 169 // static
171 MaybeHandle<Object> Object::ToInteger(Isolate* isolate, Handle<Object> input) { 170 MaybeHandle<Object> Object::ConvertToInteger(Isolate* isolate,
172 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 171 Handle<Object> input) {
172 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input),
173 Object);
174 if (input->IsSmi()) return input;
173 return isolate->factory()->NewNumber(DoubleToInteger(input->Number())); 175 return isolate->factory()->NewNumber(DoubleToInteger(input->Number()));
174 } 176 }
175 177
176
177 // static 178 // static
178 MaybeHandle<Object> Object::ToInt32(Isolate* isolate, Handle<Object> input) { 179 MaybeHandle<Object> Object::ConvertToInt32(Isolate* isolate,
179 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 180 Handle<Object> input) {
181 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input),
182 Object);
183 if (input->IsSmi()) return input;
180 return isolate->factory()->NewNumberFromInt(DoubleToInt32(input->Number())); 184 return isolate->factory()->NewNumberFromInt(DoubleToInt32(input->Number()));
181 } 185 }
182 186
183
184 // static 187 // static
185 MaybeHandle<Object> Object::ToUint32(Isolate* isolate, Handle<Object> input) { 188 MaybeHandle<Object> Object::ConvertToUint32(Isolate* isolate,
186 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 189 Handle<Object> input) {
190 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input),
191 Object);
192 if (input->IsSmi()) return handle(Smi::cast(*input)->ToUint32Smi(), isolate);
187 return isolate->factory()->NewNumberFromUint(DoubleToUint32(input->Number())); 193 return isolate->factory()->NewNumberFromUint(DoubleToUint32(input->Number()));
188 } 194 }
189 195
190
191 // static 196 // static
192 MaybeHandle<Name> Object::ConvertToName(Isolate* isolate, 197 MaybeHandle<Name> Object::ConvertToName(Isolate* isolate,
193 Handle<Object> input) { 198 Handle<Object> input) {
194 ASSIGN_RETURN_ON_EXCEPTION( 199 ASSIGN_RETURN_ON_EXCEPTION(
195 isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString), 200 isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString),
196 Name); 201 Name);
197 if (input->IsName()) return Handle<Name>::cast(input); 202 if (input->IsName()) return Handle<Name>::cast(input);
198 return ToString(isolate, input); 203 return ToString(isolate, input);
199 } 204 }
200 205
201 // static 206 // static
202 MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) { 207 MaybeHandle<String> Object::ConvertToString(Isolate* isolate,
208 Handle<Object> input) {
203 while (true) { 209 while (true) {
204 if (input->IsString()) {
205 return Handle<String>::cast(input);
206 }
207 if (input->IsOddball()) { 210 if (input->IsOddball()) {
208 return handle(Handle<Oddball>::cast(input)->to_string(), isolate); 211 return handle(Handle<Oddball>::cast(input)->to_string(), isolate);
209 } 212 }
210 if (input->IsNumber()) { 213 if (input->IsNumber()) {
211 return isolate->factory()->NumberToString(input); 214 return isolate->factory()->NumberToString(input);
212 } 215 }
213 if (input->IsSymbol()) { 216 if (input->IsSymbol()) {
214 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString), 217 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString),
215 String); 218 String);
216 } 219 }
217 if (input->IsSimd128Value()) { 220 if (input->IsSimd128Value()) {
218 return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); 221 return Simd128Value::ToString(Handle<Simd128Value>::cast(input));
219 } 222 }
220 ASSIGN_RETURN_ON_EXCEPTION( 223 ASSIGN_RETURN_ON_EXCEPTION(
221 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), 224 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
222 ToPrimitiveHint::kString), 225 ToPrimitiveHint::kString),
223 String); 226 String);
227 // The previous isString() check happened in Object::ToString and thus we
228 // put it at the end of the loop in this helper.
229 if (input->IsString()) {
230 return Handle<String>::cast(input);
231 }
224 } 232 }
225 } 233 }
226 234
227 namespace { 235 namespace {
228 236
229 bool IsErrorObject(Isolate* isolate, Handle<Object> object) { 237 bool IsErrorObject(Isolate* isolate, Handle<Object> object) {
230 if (!object->IsJSReceiver()) return false; 238 if (!object->IsJSReceiver()) return false;
231 Handle<Symbol> symbol = isolate->factory()->stack_trace_symbol(); 239 Handle<Symbol> symbol = isolate->factory()->stack_trace_symbol();
232 return JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol) 240 return JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol)
233 .FromMaybe(false); 241 .FromMaybe(false);
(...skipping 20177 matching lines...) Expand 10 before | Expand all | Expand 10 after
20411 // depend on this. 20419 // depend on this.
20412 return DICTIONARY_ELEMENTS; 20420 return DICTIONARY_ELEMENTS;
20413 } 20421 }
20414 DCHECK_LE(kind, LAST_ELEMENTS_KIND); 20422 DCHECK_LE(kind, LAST_ELEMENTS_KIND);
20415 return kind; 20423 return kind;
20416 } 20424 }
20417 } 20425 }
20418 20426
20419 } // namespace internal 20427 } // namespace internal
20420 } // namespace v8 20428 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698