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

Side by Side Diff: src/objects.cc

Issue 2587013002: [runtime] Add fast-paths for common conversion methods (Closed)
Patch Set: 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 // static 196 // static
197 MaybeHandle<Name> Object::ConvertToName(Isolate* isolate, 197 MaybeHandle<Name> Object::ConvertToName(Isolate* isolate,
198 Handle<Object> input) { 198 Handle<Object> input) {
199 ASSIGN_RETURN_ON_EXCEPTION( 199 ASSIGN_RETURN_ON_EXCEPTION(
200 isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString), 200 isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString),
201 Name); 201 Name);
202 if (input->IsName()) return Handle<Name>::cast(input); 202 if (input->IsName()) return Handle<Name>::cast(input);
203 return ToString(isolate, input); 203 return ToString(isolate, input);
204 } 204 }
205 205
206 // ES6 7.1.14
207 // static
208 MaybeHandle<Object> Object::ConvertToPropertyKey(Isolate* isolate,
209 Handle<Object> value) {
210 // 1. Let key be ToPrimitive(argument, hint String).
211 MaybeHandle<Object> maybe_key =
212 Object::ToPrimitive(value, ToPrimitiveHint::kString);
213 // 2. ReturnIfAbrupt(key).
214 Handle<Object> key;
215 if (!maybe_key.ToHandle(&key)) return key;
216 // 3. If Type(key) is Symbol, then return key.
217 if (key->IsSymbol()) return key;
218 // 4. Return ToString(key).
219 // Extending spec'ed behavior, we'd be happy to return an element index.
220 if (key->IsSmi()) return key;
221 if (key->IsHeapNumber()) {
222 uint32_t uint_value;
223 if (value->ToArrayLength(&uint_value) &&
224 uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) {
225 return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate);
226 }
227 }
228 return Object::ToString(isolate, key);
229 }
230
206 // static 231 // static
207 MaybeHandle<String> Object::ConvertToString(Isolate* isolate, 232 MaybeHandle<String> Object::ConvertToString(Isolate* isolate,
208 Handle<Object> input) { 233 Handle<Object> input) {
209 while (true) { 234 while (true) {
210 if (input->IsOddball()) { 235 if (input->IsOddball()) {
211 return handle(Handle<Oddball>::cast(input)->to_string(), isolate); 236 return handle(Handle<Oddball>::cast(input)->to_string(), isolate);
212 } 237 }
213 if (input->IsNumber()) { 238 if (input->IsNumber()) {
214 return isolate->factory()->NumberToString(input); 239 return isolate->factory()->NumberToString(input);
215 } 240 }
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 401
377 IncrementalStringBuilder builder(isolate); 402 IncrementalStringBuilder builder(isolate);
378 builder.AppendCString("[object "); 403 builder.AppendCString("[object ");
379 builder.AppendString(tag); 404 builder.AppendString(tag);
380 builder.AppendCString("]"); 405 builder.AppendCString("]");
381 406
382 return builder.Finish().ToHandleChecked(); 407 return builder.Finish().ToHandleChecked();
383 } 408 }
384 409
385 // static 410 // static
386 MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { 411 MaybeHandle<Object> Object::ConvertToLength(Isolate* isolate,
412 Handle<Object> input) {
387 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 413 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
414 if (input->IsSmi()) {
415 int value = std::max(Smi::cast(*input)->value(), 0);
416 return handle(Smi::FromInt(value), isolate);
417 }
388 double len = DoubleToInteger(input->Number()); 418 double len = DoubleToInteger(input->Number());
389 if (len <= 0.0) { 419 if (len <= 0.0) {
390 len = 0.0; 420 return handle(Smi::kZero, isolate);
391 } else if (len >= kMaxSafeInteger) { 421 } else if (len >= kMaxSafeInteger) {
392 len = kMaxSafeInteger; 422 len = kMaxSafeInteger;
393 } 423 }
394 return isolate->factory()->NewNumber(len); 424 return isolate->factory()->NewNumber(len);
395 } 425 }
396 426
397 // static 427 // static
398 MaybeHandle<Object> Object::ToIndex(Isolate* isolate, Handle<Object> input, 428 MaybeHandle<Object> Object::ConvertToIndex(
399 MessageTemplate::Template error_index) { 429 Isolate* isolate, Handle<Object> input,
400 if (input->IsUndefined(isolate)) return isolate->factory()->NewNumber(0.0); 430 MessageTemplate::Template error_index) {
431 if (input->IsUndefined(isolate)) return handle(Smi::kZero, isolate);
401 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); 432 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
433 if (input->IsSmi() && Smi::cast(*input)->value() >= 0) return input;
402 double len = DoubleToInteger(input->Number()) + 0.0; 434 double len = DoubleToInteger(input->Number()) + 0.0;
403 auto js_len = isolate->factory()->NewNumber(len); 435 auto js_len = isolate->factory()->NewNumber(len);
404 if (len < 0.0 || len > kMaxSafeInteger) { 436 if (len < 0.0 || len > kMaxSafeInteger) {
405 THROW_NEW_ERROR(isolate, NewRangeError(error_index, js_len), Object); 437 THROW_NEW_ERROR(isolate, NewRangeError(error_index, js_len), Object);
406 } 438 }
407 return js_len; 439 return js_len;
408 } 440 }
409 441
410 bool Object::BooleanValue() { 442 bool Object::BooleanValue() {
411 if (IsSmi()) return Smi::cast(this)->value() != 0; 443 if (IsSmi()) return Smi::cast(this)->value() != 0;
(...skipping 6074 matching lines...) Expand 10 before | Expand all | Expand 10 after
6486 6518
6487 6519
6488 Maybe<bool> JSReceiver::DeletePropertyOrElement(Handle<JSReceiver> object, 6520 Maybe<bool> JSReceiver::DeletePropertyOrElement(Handle<JSReceiver> object,
6489 Handle<Name> name, 6521 Handle<Name> name,
6490 LanguageMode language_mode) { 6522 LanguageMode language_mode) {
6491 LookupIterator it = LookupIterator::PropertyOrElement( 6523 LookupIterator it = LookupIterator::PropertyOrElement(
6492 name->GetIsolate(), object, name, object, LookupIterator::OWN); 6524 name->GetIsolate(), object, name, object, LookupIterator::OWN);
6493 return DeleteProperty(&it, language_mode); 6525 return DeleteProperty(&it, language_mode);
6494 } 6526 }
6495 6527
6496
6497 // ES6 7.1.14
6498 // static
6499 MaybeHandle<Object> Object::ToPropertyKey(Isolate* isolate,
6500 Handle<Object> value) {
6501 // 1. Let key be ToPrimitive(argument, hint String).
6502 MaybeHandle<Object> maybe_key =
6503 Object::ToPrimitive(value, ToPrimitiveHint::kString);
6504 // 2. ReturnIfAbrupt(key).
6505 Handle<Object> key;
6506 if (!maybe_key.ToHandle(&key)) return key;
6507 // 3. If Type(key) is Symbol, then return key.
6508 if (key->IsSymbol()) return key;
6509 // 4. Return ToString(key).
6510 // Extending spec'ed behavior, we'd be happy to return an element index.
6511 if (key->IsSmi()) return key;
6512 if (key->IsHeapNumber()) {
6513 uint32_t uint_value;
6514 if (value->ToArrayLength(&uint_value) &&
6515 uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) {
6516 return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate);
6517 }
6518 }
6519 return Object::ToString(isolate, key);
6520 }
6521
6522
6523 // ES6 19.1.2.4 6528 // ES6 19.1.2.4
6524 // static 6529 // static
6525 Object* JSReceiver::DefineProperty(Isolate* isolate, Handle<Object> object, 6530 Object* JSReceiver::DefineProperty(Isolate* isolate, Handle<Object> object,
6526 Handle<Object> key, 6531 Handle<Object> key,
6527 Handle<Object> attributes) { 6532 Handle<Object> attributes) {
6528 // 1. If Type(O) is not Object, throw a TypeError exception. 6533 // 1. If Type(O) is not Object, throw a TypeError exception.
6529 if (!object->IsJSReceiver()) { 6534 if (!object->IsJSReceiver()) {
6530 Handle<String> fun_name = 6535 Handle<String> fun_name =
6531 isolate->factory()->InternalizeUtf8String("Object.defineProperty"); 6536 isolate->factory()->InternalizeUtf8String("Object.defineProperty");
6532 THROW_NEW_ERROR_RETURN_FAILURE( 6537 THROW_NEW_ERROR_RETURN_FAILURE(
(...skipping 13907 matching lines...) Expand 10 before | Expand all | Expand 10 after
20440 // depend on this. 20445 // depend on this.
20441 return DICTIONARY_ELEMENTS; 20446 return DICTIONARY_ELEMENTS;
20442 } 20447 }
20443 DCHECK_LE(kind, LAST_ELEMENTS_KIND); 20448 DCHECK_LE(kind, LAST_ELEMENTS_KIND);
20444 return kind; 20449 return kind;
20445 } 20450 }
20446 } 20451 }
20447 20452
20448 } // namespace internal 20453 } // namespace internal
20449 } // namespace v8 20454 } // 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