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

Side by Side Diff: src/builtins.cc

Issue 6034003: Teach C++ ArraySlice builtin to deal with arguments object. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/array-slice.js » ('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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 508
509 return top; 509 return top;
510 } 510 }
511 511
512 512
513 BUILTIN(ArrayShift) { 513 BUILTIN(ArrayShift) {
514 Object* receiver = *args.receiver(); 514 Object* receiver = *args.receiver();
515 Object* elms_obj; 515 Object* elms_obj;
516 { MaybeObject* maybe_elms_obj = 516 { MaybeObject* maybe_elms_obj =
517 EnsureJSArrayWithWritableFastElements(receiver); 517 EnsureJSArrayWithWritableFastElements(receiver);
518 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayShift", args);
518 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 519 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
519 } 520 }
520 if (elms_obj == NULL || 521 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
521 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
522 return CallJsBuiltin("ArrayShift", args); 522 return CallJsBuiltin("ArrayShift", args);
523 } 523 }
524 FixedArray* elms = FixedArray::cast(elms_obj); 524 FixedArray* elms = FixedArray::cast(elms_obj);
525 JSArray* array = JSArray::cast(receiver); 525 JSArray* array = JSArray::cast(receiver);
526 ASSERT(array->HasFastElements()); 526 ASSERT(array->HasFastElements());
527 527
528 int len = Smi::cast(array->length())->value(); 528 int len = Smi::cast(array->length())->value();
529 if (len == 0) return Heap::undefined_value(); 529 if (len == 0) return Heap::undefined_value();
530 530
531 // Get first element 531 // Get first element
(...skipping 18 matching lines...) Expand all
550 550
551 return first; 551 return first;
552 } 552 }
553 553
554 554
555 BUILTIN(ArrayUnshift) { 555 BUILTIN(ArrayUnshift) {
556 Object* receiver = *args.receiver(); 556 Object* receiver = *args.receiver();
557 Object* elms_obj; 557 Object* elms_obj;
558 { MaybeObject* maybe_elms_obj = 558 { MaybeObject* maybe_elms_obj =
559 EnsureJSArrayWithWritableFastElements(receiver); 559 EnsureJSArrayWithWritableFastElements(receiver);
560 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayUnshift", args);
560 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 561 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
561 } 562 }
562 if (elms_obj == NULL || 563 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
563 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
564 return CallJsBuiltin("ArrayUnshift", args); 564 return CallJsBuiltin("ArrayUnshift", args);
565 } 565 }
566 FixedArray* elms = FixedArray::cast(elms_obj); 566 FixedArray* elms = FixedArray::cast(elms_obj);
567 JSArray* array = JSArray::cast(receiver); 567 JSArray* array = JSArray::cast(receiver);
568 ASSERT(array->HasFastElements()); 568 ASSERT(array->HasFastElements());
569 569
570 int len = Smi::cast(array->length())->value(); 570 int len = Smi::cast(array->length())->value();
571 int to_add = args.length() - 1; 571 int to_add = args.length() - 1;
572 int new_length = len + to_add; 572 int new_length = len + to_add;
573 // Currently fixed arrays cannot grow too big, so 573 // Currently fixed arrays cannot grow too big, so
(...skipping 30 matching lines...) Expand all
604 } 604 }
605 605
606 // Set the length. 606 // Set the length.
607 array->set_length(Smi::FromInt(new_length)); 607 array->set_length(Smi::FromInt(new_length));
608 return Smi::FromInt(new_length); 608 return Smi::FromInt(new_length);
609 } 609 }
610 610
611 611
612 BUILTIN(ArraySlice) { 612 BUILTIN(ArraySlice) {
613 Object* receiver = *args.receiver(); 613 Object* receiver = *args.receiver();
614 Object* elms_obj; 614 FixedArray* elms;
615 int len = -1;
615 { MaybeObject* maybe_elms_obj = 616 { MaybeObject* maybe_elms_obj =
616 EnsureJSArrayWithWritableFastElements(receiver); 617 EnsureJSArrayWithWritableFastElements(receiver);
617 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 618 Object* elms_obj;
619 if (maybe_elms_obj != NULL && maybe_elms_obj->ToObject(&elms_obj)) {
620 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
621 return CallJsBuiltin("ArraySlice", args);
622 }
623 elms = FixedArray::cast(elms_obj);
624 JSArray* array = JSArray::cast(receiver);
625 ASSERT(array->HasFastElements());
626
627 len = Smi::cast(array->length())->value();
628 } else {
629 // Array.slice(arguments, ...) is quite a common idiom (notably more
630 // than 50% of invocations in Web apps). Treat it in C++ as well.
631 Map* arguments_map =
632 Top::context()->global_context()->arguments_boilerplate()->map();
633
634 bool is_arguments_object_with_fast_elements =
635 receiver->IsJSObject()
636 && JSObject::cast(receiver)->map() == arguments_map
637 && JSObject::cast(receiver)->HasFastElements();
638 if (!is_arguments_object_with_fast_elements) {
639 return CallJsBuiltin("ArraySlice", args);
640 }
641 elms = FixedArray::cast(JSObject::cast(receiver)->elements());
642 len = elms->length();
643 #ifdef DEBUG
644 // Arguments object by construction should have no holes, check it.
645 if (FLAG_enable_slow_asserts) {
646 for (int i = 0; i < len; i++) {
647 ASSERT(elms->get(i) != Heap::the_hole_value());
648 }
649 }
650 #endif
651 }
618 } 652 }
619 if (elms_obj == NULL || 653 ASSERT(len >= 0);
620 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
621 return CallJsBuiltin("ArraySlice", args);
622 }
623 FixedArray* elms = FixedArray::cast(elms_obj);
624 JSArray* array = JSArray::cast(receiver);
625 ASSERT(array->HasFastElements());
626
627 int len = Smi::cast(array->length())->value();
628
629 int n_arguments = args.length() - 1; 654 int n_arguments = args.length() - 1;
630 655
631 // Note carefully choosen defaults---if argument is missing, 656 // Note carefully choosen defaults---if argument is missing,
632 // it's undefined which gets converted to 0 for relative_start 657 // it's undefined which gets converted to 0 for relative_start
633 // and to len for relative_end. 658 // and to len for relative_end.
634 int relative_start = 0; 659 int relative_start = 0;
635 int relative_end = len; 660 int relative_end = len;
636 if (n_arguments > 0) { 661 if (n_arguments > 0) {
637 Object* arg1 = args[1]; 662 Object* arg1 = args[1];
638 if (arg1->IsSmi()) { 663 if (arg1->IsSmi()) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 result_array->set_length(Smi::FromInt(result_len)); 711 result_array->set_length(Smi::FromInt(result_len));
687 return result_array; 712 return result_array;
688 } 713 }
689 714
690 715
691 BUILTIN(ArraySplice) { 716 BUILTIN(ArraySplice) {
692 Object* receiver = *args.receiver(); 717 Object* receiver = *args.receiver();
693 Object* elms_obj; 718 Object* elms_obj;
694 { MaybeObject* maybe_elms_obj = 719 { MaybeObject* maybe_elms_obj =
695 EnsureJSArrayWithWritableFastElements(receiver); 720 EnsureJSArrayWithWritableFastElements(receiver);
721 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArraySplice", args);
696 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 722 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
697 } 723 }
698 if (elms_obj == NULL || 724 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
699 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
700 return CallJsBuiltin("ArraySplice", args); 725 return CallJsBuiltin("ArraySplice", args);
701 } 726 }
702 FixedArray* elms = FixedArray::cast(elms_obj); 727 FixedArray* elms = FixedArray::cast(elms_obj);
703 JSArray* array = JSArray::cast(receiver); 728 JSArray* array = JSArray::cast(receiver);
704 ASSERT(array->HasFastElements()); 729 ASSERT(array->HasFastElements());
705 730
706 int len = Smi::cast(array->length())->value(); 731 int len = Smi::cast(array->length())->value();
707 732
708 int n_arguments = args.length() - 1; 733 int n_arguments = args.length() - 1;
709 734
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 if (entry->contains(pc)) { 1601 if (entry->contains(pc)) {
1577 return names_[i]; 1602 return names_[i];
1578 } 1603 }
1579 } 1604 }
1580 } 1605 }
1581 return NULL; 1606 return NULL;
1582 } 1607 }
1583 1608
1584 1609
1585 } } // namespace v8::internal 1610 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/array-slice.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698