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

Side by Side Diff: src/objects.cc

Issue 2206573002: Move NoSideEffectToString to C++ (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Address comments Created 4 years, 4 months 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/runtime/runtime.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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 DisallowJavascriptExecution no_js(isolate);
264
265 if (input->IsString() || input->IsNumber() || input->IsOddball() ||
266 input->IsSimd128Value()) {
267 return Object::ToString(isolate, input).ToHandleChecked();
268 } else if (input->IsFunction()) {
269 // -- F u n c t i o n
270 Handle<String> fun_str;
271 if (input->IsJSBoundFunction()) {
272 fun_str = JSBoundFunction::ToString(Handle<JSBoundFunction>::cast(input));
273 } else {
274 DCHECK(input->IsJSFunction());
275 fun_str = JSFunction::ToString(Handle<JSFunction>::cast(input));
276 }
277
278 if (fun_str->length() > 128) {
279 IncrementalStringBuilder builder(isolate);
280 builder.AppendString(isolate->factory()->NewSubString(fun_str, 0, 111));
281 builder.AppendCString("...<omitted>...");
282 builder.AppendString(isolate->factory()->NewSubString(
283 fun_str, fun_str->length() - 2, fun_str->length()));
284
285 return builder.Finish().ToHandleChecked();
286 }
287 return fun_str;
288 } else if (input->IsSymbol()) {
289 // -- S y m b o l
290 Handle<Symbol> symbol = Handle<Symbol>::cast(input);
291
292 IncrementalStringBuilder builder(isolate);
293 builder.AppendCString("Symbol(");
294 if (symbol->name()->IsString()) {
295 builder.AppendString(handle(String::cast(symbol->name()), isolate));
296 }
297 builder.AppendCharacter(')');
298
299 return builder.Finish().ToHandleChecked();
300 } else if (input->IsJSReceiver()) {
301 // -- J S R e c e i v e r
302 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(input);
303 Handle<Object> to_string = JSReceiver::GetDataProperty(
304 receiver, isolate->factory()->toString_string());
305
306 if (IsErrorObject(isolate, input) ||
307 *to_string == *isolate->error_to_string()) {
308 // When internally formatting error objects, use a side-effects-free
309 // version of Error.prototype.toString independent of the actually
310 // installed toString method.
311 return NoSideEffectsErrorToString(isolate, input);
312 } else if (*to_string == *isolate->object_to_string()) {
313 Handle<Object> ctor = JSReceiver::GetDataProperty(
314 receiver, isolate->factory()->constructor_string());
315 if (ctor->IsFunction()) {
316 Handle<String> ctor_name;
317 if (ctor->IsJSBoundFunction()) {
318 ctor_name = JSBoundFunction::GetName(
319 isolate, Handle<JSBoundFunction>::cast(ctor))
320 .ToHandleChecked();
321 } else if (ctor->IsJSFunction()) {
322 Handle<Object> ctor_name_obj =
323 JSFunction::GetName(isolate, Handle<JSFunction>::cast(ctor));
324 ctor_name = NoSideEffectsToString(isolate, ctor_name_obj);
325 }
326
327 if (ctor_name->length() != 0) {
328 IncrementalStringBuilder builder(isolate);
329 builder.AppendCString("#<");
330 builder.AppendString(ctor_name);
331 builder.AppendCString(">");
332
333 return builder.Finish().ToHandleChecked();
334 }
335 }
336 }
337 }
338
339 // At this point, input is either none of the above or a JSReceiver.
340
341 Handle<JSReceiver> receiver;
342 if (input->IsJSReceiver()) {
343 receiver = Handle<JSReceiver>::cast(input);
344 } else {
345 // This is the only case where Object::ToObject throws.
346 DCHECK(!input->IsSmi());
347 int constructor_function_index =
348 Handle<HeapObject>::cast(input)->map()->GetConstructorFunctionIndex();
349 if (constructor_function_index == Map::kNoConstructorFunctionIndex) {
350 return isolate->factory()->NewStringFromAsciiChecked("[object Unknown]");
351 }
352
353 receiver = Object::ToObject(isolate, input, isolate->native_context())
354 .ToHandleChecked();
355 }
356
357 Handle<String> builtin_tag = handle(receiver->class_name(), isolate);
358 Handle<Object> tag_obj = JSReceiver::GetDataProperty(
359 receiver, isolate->factory()->to_string_tag_symbol());
360 Handle<String> tag =
361 tag_obj->IsString() ? Handle<String>::cast(tag_obj) : builtin_tag;
362
363 IncrementalStringBuilder builder(isolate);
364 builder.AppendCString("[object ");
365 builder.AppendString(tag);
366 builder.AppendCString("]");
367
368 return builder.Finish().ToHandleChecked();
369 }
222 370
223 // static 371 // static
224 MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { 372 MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) {
225 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 373 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
226 double len = DoubleToInteger(input->Number()); 374 double len = DoubleToInteger(input->Number());
227 if (len <= 0.0) { 375 if (len <= 0.0) {
228 len = 0.0; 376 len = 0.0;
229 } else if (len >= kMaxSafeInteger) { 377 } else if (len >= kMaxSafeInteger) {
230 len = kMaxSafeInteger; 378 len = kMaxSafeInteger;
231 } 379 }
(...skipping 18795 matching lines...) Expand 10 before | Expand all | Expand 10 after
19027 for (PrototypeIterator iter(isolate, this, kStartAtReceiver, 19175 for (PrototypeIterator iter(isolate, this, kStartAtReceiver,
19028 PrototypeIterator::END_AT_NULL); 19176 PrototypeIterator::END_AT_NULL);
19029 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) { 19177 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) {
19030 if (iter.GetCurrent<Object>()->IsJSProxy()) return true; 19178 if (iter.GetCurrent<Object>()->IsJSProxy()) return true;
19031 } 19179 }
19032 return false; 19180 return false;
19033 } 19181 }
19034 19182
19035 } // namespace internal 19183 } // namespace internal
19036 } // namespace v8 19184 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698