OLD | NEW |
---|---|
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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 if (input->IsSimd128Value()) { | 212 if (input->IsSimd128Value()) { |
213 return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); | 213 return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); |
214 } | 214 } |
215 ASSIGN_RETURN_ON_EXCEPTION( | 215 ASSIGN_RETURN_ON_EXCEPTION( |
216 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), | 216 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), |
217 ToPrimitiveHint::kString), | 217 ToPrimitiveHint::kString), |
218 String); | 218 String); |
219 } | 219 } |
220 } | 220 } |
221 | 221 |
222 namespace { | |
223 | |
224 bool IsErrorObject(Isolate* isolate, Handle<Object> object) { | |
225 if (!object->IsJSReceiver()) return false; | |
226 Handle<Symbol> symbol = isolate->factory()->stack_trace_symbol(); | |
227 return JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol) | |
228 .FromMaybe(false); | |
229 } | |
230 | |
231 Handle<String> NoSideEffectsErrorToString(Isolate* isolate, | |
232 Handle<Object> input) { | |
233 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(input); | |
234 | |
235 Handle<Name> name_key = isolate->factory()->name_string(); | |
236 Handle<Object> name = JSReceiver::GetDataProperty(receiver, name_key); | |
237 Handle<String> name_str = (name->IsUndefined(isolate)) | |
238 ? isolate->factory()->Error_string() | |
239 : Object::NoSideEffectsToString(isolate, name); | |
240 | |
241 Handle<Name> msg_key = isolate->factory()->message_string(); | |
242 Handle<Object> msg = JSReceiver::GetDataProperty(receiver, msg_key); | |
243 Handle<String> msg_str = (msg->IsUndefined(isolate)) | |
244 ? isolate->factory()->empty_string() | |
245 : Object::NoSideEffectsToString(isolate, msg); | |
246 | |
247 if (name_str->length() == 0) return msg_str; | |
248 if (msg_str->length() == 0) return name_str; | |
249 | |
250 IncrementalStringBuilder builder(isolate); | |
251 builder.AppendString(name_str); | |
252 builder.AppendCString(": "); | |
253 builder.AppendString(msg_str); | |
254 | |
255 return builder.Finish().ToHandleChecked(); | |
256 } | |
257 | |
258 } // namespace | |
259 | |
260 // static | |
261 Handle<String> Object::NoSideEffectsToString(Isolate* isolate, | |
262 Handle<Object> input) { | |
263 if (input->IsString()) { | |
264 // -- S t r i n g | |
265 return Handle<String>::cast(input); | |
266 } else if (input->IsNumber()) { | |
267 // -- N u m b e r | |
268 return isolate->factory()->NumberToString(input); | |
269 } else if (input->IsBoolean()) { | |
270 // -- B o o l e a n | |
271 return input->BooleanValue() ? isolate->factory()->true_string() | |
272 : isolate->factory()->false_string(); | |
273 } else if (input->IsUndefined(isolate)) { | |
274 // -- U n d e f i n e d | |
275 return isolate->factory()->undefined_string(); | |
276 } else if (input->IsNull(isolate)) { | |
277 // -- N u l l | |
278 return isolate->factory()->null_string(); | |
Yang
2016/08/03 09:13:18
Can we defer to Object::ToString for these cases a
jgruber
2016/08/03 11:17:15
Done.
| |
279 } else if (input->IsFunction()) { | |
280 // -- F u n c t i o n | |
281 Handle<String> fun_str; | |
282 if (input->IsJSBoundFunction()) { | |
283 fun_str = JSBoundFunction::ToString(Handle<JSBoundFunction>::cast(input)); | |
284 } else { | |
285 DCHECK(input->IsJSFunction()); | |
286 fun_str = JSFunction::ToString(Handle<JSFunction>::cast(input)); | |
287 } | |
288 | |
289 if (fun_str->length() > 128) { | |
290 IncrementalStringBuilder builder(isolate); | |
291 builder.AppendString(isolate->factory()->NewSubString(fun_str, 0, 111)); | |
292 builder.AppendCString("...<omitted>..."); | |
293 builder.AppendString(isolate->factory()->NewSubString( | |
294 fun_str, fun_str->length() - 2, fun_str->length())); | |
295 | |
296 return builder.Finish().ToHandleChecked(); | |
297 } | |
298 return fun_str; | |
299 } else if (input->IsSymbol()) { | |
300 // -- S y m b o l | |
301 Handle<Symbol> symbol = Handle<Symbol>::cast(input); | |
302 | |
303 IncrementalStringBuilder builder(isolate); | |
304 builder.AppendCString("Symbol("); | |
305 if (symbol->name()->IsString()) { | |
306 builder.AppendString(handle(String::cast(symbol->name()), isolate)); | |
307 } | |
308 builder.AppendCharacter(')'); | |
309 | |
310 return builder.Finish().ToHandleChecked(); | |
311 } else if (input->IsSimd128Value()) { | |
Yang
2016/08/03 09:13:18
Same for simd
jgruber
2016/08/03 11:17:15
Done.
| |
312 // -- S i m d 1 2 8 V a l u e | |
313 return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); | |
314 } else if (input->IsJSReceiver()) { | |
315 // -- J S R e c e i v e r | |
316 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(input); | |
317 Handle<Object> to_string = JSReceiver::GetDataProperty( | |
318 receiver, isolate->factory()->toString_string()); | |
319 | |
320 if (IsErrorObject(isolate, input) || | |
321 *to_string == *isolate->error_to_string()) { | |
322 // When internally formatting error objects, use a side-effects-free | |
323 // version of Error.prototype.toString independent of the actually | |
324 // installed toString method. | |
325 return NoSideEffectsErrorToString(isolate, input); | |
326 } else if (*to_string == *isolate->object_to_string()) { | |
327 Handle<Object> ctor = JSReceiver::GetDataProperty( | |
328 receiver, isolate->factory()->constructor_string()); | |
329 if (ctor->IsFunction()) { | |
330 Handle<String> ctor_name; | |
331 if (ctor->IsJSBoundFunction()) { | |
332 ctor_name = JSBoundFunction::GetName( | |
333 isolate, Handle<JSBoundFunction>::cast(ctor)) | |
334 .ToHandleChecked(); | |
335 } else if (ctor->IsJSFunction()) { | |
336 Handle<Object> ctor_name_obj = | |
337 JSFunction::GetName(isolate, Handle<JSFunction>::cast(ctor)); | |
338 ctor_name = NoSideEffectsToString(isolate, ctor_name_obj); | |
339 } | |
340 | |
341 if (ctor_name->length() != 0) { | |
342 IncrementalStringBuilder builder(isolate); | |
343 builder.AppendCString("#<"); | |
344 builder.AppendString(ctor_name); | |
345 builder.AppendCString(">"); | |
346 | |
347 return builder.Finish().ToHandleChecked(); | |
348 } | |
349 } | |
350 } | |
351 } | |
352 | |
353 // At this point, input is either none of the above or a JSReceiver. | |
354 | |
355 Handle<JSReceiver> receiver; | |
356 if (input->IsJSReceiver()) { | |
357 receiver = Handle<JSReceiver>::cast(input); | |
358 } else { | |
359 // This is the only case where Object::ToObject throws. | |
360 DCHECK(!input->IsSmi()); | |
361 int constructor_function_index = | |
362 Handle<HeapObject>::cast(input)->map()->GetConstructorFunctionIndex(); | |
363 if (constructor_function_index == Map::kNoConstructorFunctionIndex) { | |
364 return isolate->factory()->NewStringFromAsciiChecked("[object Unknown]"); | |
365 } | |
366 | |
367 receiver = Object::ToObject(isolate, input, isolate->native_context()) | |
368 .ToHandleChecked(); | |
369 } | |
370 | |
371 Handle<String> builtin_tag = handle(receiver->class_name(), isolate); | |
372 Handle<Object> tag_obj = JSReceiver::GetDataProperty( | |
373 receiver, isolate->factory()->to_string_tag_symbol()); | |
374 Handle<String> tag = | |
375 tag_obj->IsString() ? Handle<String>::cast(tag_obj) : builtin_tag; | |
376 | |
377 IncrementalStringBuilder builder(isolate); | |
378 builder.AppendCString("[object "); | |
379 builder.AppendString(tag); | |
380 builder.AppendCString("]"); | |
381 | |
382 return builder.Finish().ToHandleChecked(); | |
383 } | |
222 | 384 |
223 // static | 385 // static |
224 MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { | 386 MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { |
225 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); | 387 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
226 double len = DoubleToInteger(input->Number()); | 388 double len = DoubleToInteger(input->Number()); |
227 if (len <= 0.0) { | 389 if (len <= 0.0) { |
228 len = 0.0; | 390 len = 0.0; |
229 } else if (len >= kMaxSafeInteger) { | 391 } else if (len >= kMaxSafeInteger) { |
230 len = kMaxSafeInteger; | 392 len = kMaxSafeInteger; |
231 } | 393 } |
(...skipping 18795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
19027 for (PrototypeIterator iter(isolate, this, kStartAtReceiver, | 19189 for (PrototypeIterator iter(isolate, this, kStartAtReceiver, |
19028 PrototypeIterator::END_AT_NULL); | 19190 PrototypeIterator::END_AT_NULL); |
19029 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) { | 19191 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) { |
19030 if (iter.GetCurrent<Object>()->IsJSProxy()) return true; | 19192 if (iter.GetCurrent<Object>()->IsJSProxy()) return true; |
19031 } | 19193 } |
19032 return false; | 19194 return false; |
19033 } | 19195 } |
19034 | 19196 |
19035 } // namespace internal | 19197 } // namespace internal |
19036 } // namespace v8 | 19198 } // namespace v8 |
OLD | NEW |