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

Side by Side Diff: src/builtins/builtins-string.cc

Issue 2348493003: [builtins] move String.prototype[@@iterator] to C++ builtin (Closed)
Patch Set: Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 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/builtins/builtins.h" 5 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h" 6 #include "src/builtins/builtins-utils.h"
7 7
8 #include "src/code-factory.h" 8 #include "src/code-factory.h"
9 9
10 namespace v8 { 10 namespace v8 {
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 typedef compiler::Node Node; 600 typedef compiler::Node Node;
601 601
602 Node* receiver = assembler->Parameter(0); 602 Node* receiver = assembler->Parameter(0);
603 Node* context = assembler->Parameter(3); 603 Node* context = assembler->Parameter(3);
604 604
605 Node* result = assembler->ToThisValue( 605 Node* result = assembler->ToThisValue(
606 context, receiver, PrimitiveType::kString, "String.prototype.valueOf"); 606 context, receiver, PrimitiveType::kString, "String.prototype.valueOf");
607 assembler->Return(result); 607 assembler->Return(result);
608 } 608 }
609 609
610 BUILTIN(StringCreateIterator) {
611 HandleScope scope(isolate);
612 TO_THIS_STRING(object, "String.prototype[Symbol.iterator]");
613
614 Handle<String> string;
615 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
616 Object::ToString(isolate, object));
617
618 return *isolate->factory()->NewJSStringIterator(string);
619 }
620
621 BUILTIN(StringIteratorNext) {
622 HandleScope scope(isolate);
623
624 if (!args.receiver()->IsJSStringIterator()) {
625 Handle<String> reason = isolate->factory()->NewStringFromAsciiChecked(
626 "String Iterator.prototype.next");
627 THROW_NEW_ERROR_RETURN_FAILURE(
628 isolate,
629 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, reason));
630 }
631 Handle<JSStringIterator> iterator =
632 Handle<JSStringIterator>::cast(args.receiver());
633 Handle<String> string(iterator->string());
634
635 if (*string != isolate->heap()->empty_string()) {
636 int position;
637 int length = string->length();
638
639 do {
640 if (!iterator->index()->ToUint32(reinterpret_cast<uint32_t*>(&position)))
641 break;
642 if (position >= length) break;
643
644 uint16_t lead = string->Get(position);
645 if (lead >= 0xD800 && lead <= 0xDBFF && position + 1 < length) {
646 uint16_t trail = string->Get(position + 1);
647 if (V8_LIKELY(trail >= 0xDC00 && trail <= 0xDFFF)) {
648 // Return combined code units
649 iterator->set_index(Smi::FromInt(position + 2));
650 Handle<String> value = isolate->factory()->NewProperSubString(
651 string, position, position + 2);
652 return *isolate->factory()->NewJSIteratorResult(value, false);
653 }
654 }
655
656 // Return single code unit
657 iterator->set_index(Smi::FromInt(position + 1));
658 Handle<String> value = isolate->factory()->NewProperSubString(
659 string, position, position + 1);
660 return *isolate->factory()->NewJSIteratorResult(value, false);
661 } while (false);
662
663 iterator->set_string(isolate->heap()->empty_string());
664 }
665
666 return *isolate->factory()->NewJSIteratorResult(
667 isolate->factory()->undefined_value(), true);
668 }
669
610 } // namespace internal 670 } // namespace internal
611 } // namespace v8 671 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698