1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 (function(global, utils) {
9 %CheckIsBootstrapping();
11 // -------------------------------------------------------------------
14 var GlobalString = global.String;
16 var ArrayIteratorCreateResultObject;
18 utils.Import(function(from) {
19 ArrayIteratorCreateResultObject = from.ArrayIteratorCreateResultObject;
22 // -------------------------------------------------------------------
24 var stringIteratorIteratedStringSymbol =
25 GLOBAL_PRIVATE("StringIterator#iteratedString");
26 var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE("StringIterator#next");
29 function StringIterator() {}
32 // 21.1.5.1 CreateStringIterator Abstract Operation
33 function CreateStringIterator(string) {
34 var s = TO_STRING_INLINE(string);
35 var iterator = new StringIterator;
36 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s);
37 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0);
42 // 21.1.5.2.1 %StringIteratorPrototype%.next( )
43 function StringIteratorNext() {
44 var iterator = TO_OBJECT(this);
46 if (!HAS_DEFINED_PRIVATE(iterator, stringIteratorNextIndexSymbol)) {
47 throw MakeTypeError(kIncompatibleMethodReceiver,
48 'String Iterator.prototype.next');
51 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol);
52 if (IS_UNDEFINED(s)) {
53 return ArrayIteratorCreateResultObject(UNDEFINED, true);
56 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol);
57 var length = TO_UINT32(s.length);
59 if (position >= length) {
60 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol,
62 return ArrayIteratorCreateResultObject(UNDEFINED, true);
65 var first = %_StringCharCodeAt(s, position);
66 var resultString = %_StringCharFromCode(first);
69 if (first >= 0xD800 && first <= 0xDBFF && position < length) {
70 var second = %_StringCharCodeAt(s, position);
71 if (second >= 0xDC00 && second <= 0xDFFF) {
72 resultString += %_StringCharFromCode(second);
77 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position);
79 return ArrayIteratorCreateResultObject(resultString, false);
83 // 21.1.3.27 String.prototype [ @@iterator ]( )
84 function StringPrototypeIterator() {
85 return CreateStringIterator(this);
88 //-------------------------------------------------------------------
90 %FunctionSetPrototype(StringIterator, {__proto__: $iteratorPrototype});
91 %FunctionSetInstanceClassName(StringIterator, 'String Iterator');
93 utils.InstallFunctions(StringIterator.prototype, DONT_ENUM, [
94 'next', StringIteratorNext
96 %AddNamedProperty(StringIterator.prototype, symbolToStringTag,
97 "String Iterator", READ_ONLY | DONT_ENUM);
99 utils.SetFunctionName(StringPrototypeIterator, symbolIterator);
100 %AddNamedProperty(GlobalString.prototype, symbolIterator,
101 StringPrototypeIterator, DONT_ENUM);