OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var ArrayIteratorCreateResultObject; | |
15 var GlobalString = global.String; | 14 var GlobalString = global.String; |
16 var iteratorSymbol = utils.ImportNow("iterator_symbol"); | 15 var iteratorSymbol = utils.ImportNow("iterator_symbol"); |
17 var stringIteratorIteratedStringSymbol = | 16 var stringIteratorIteratedStringSymbol = |
18 utils.ImportNow("string_iterator_iterated_string_symbol"); | 17 utils.ImportNow("string_iterator_iterated_string_symbol"); |
19 var stringIteratorNextIndexSymbol = | 18 var stringIteratorNextIndexSymbol = |
20 utils.ImportNow("string_iterator_next_index_symbol"); | 19 utils.ImportNow("string_iterator_next_index_symbol"); |
21 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 20 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
22 | 21 |
23 utils.Import(function(from) { | |
24 ArrayIteratorCreateResultObject = from.ArrayIteratorCreateResultObject; | |
25 }); | |
26 | |
27 // ------------------------------------------------------------------- | 22 // ------------------------------------------------------------------- |
28 | 23 |
29 function StringIterator() {} | 24 function StringIterator() {} |
30 | 25 |
31 | 26 |
32 // 21.1.5.1 CreateStringIterator Abstract Operation | 27 // 21.1.5.1 CreateStringIterator Abstract Operation |
33 function CreateStringIterator(string) { | 28 function CreateStringIterator(string) { |
34 var s = TO_STRING_INLINE(string); | 29 var s = TO_STRING_INLINE(string); |
35 var iterator = new StringIterator; | 30 var iterator = new StringIterator; |
36 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s); | 31 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s); |
37 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0); | 32 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0); |
38 return iterator; | 33 return iterator; |
39 } | 34 } |
40 | 35 |
41 | 36 |
42 // 21.1.5.2.1 %StringIteratorPrototype%.next( ) | 37 // ES6 section 21.1.5.2.1 %StringIteratorPrototype%.next ( ) |
43 function StringIteratorNext() { | 38 function StringIteratorNext() { |
44 var iterator = this; | 39 var iterator = this; |
| 40 var value = UNDEFINED; |
| 41 var done = true; |
45 | 42 |
46 if (!IS_SPEC_OBJECT(iterator) || | 43 if (!IS_SPEC_OBJECT(iterator) || |
47 !HAS_DEFINED_PRIVATE(iterator, stringIteratorNextIndexSymbol)) { | 44 !HAS_DEFINED_PRIVATE(iterator, stringIteratorNextIndexSymbol)) { |
48 throw MakeTypeError(kIncompatibleMethodReceiver, | 45 throw MakeTypeError(kIncompatibleMethodReceiver, |
49 'String Iterator.prototype.next'); | 46 'String Iterator.prototype.next'); |
50 } | 47 } |
51 | 48 |
52 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol); | 49 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol); |
53 if (IS_UNDEFINED(s)) { | 50 if (!IS_UNDEFINED(s)) { |
54 return ArrayIteratorCreateResultObject(UNDEFINED, true); | 51 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol); |
55 } | 52 var length = TO_UINT32(s.length); |
| 53 if (position >= length) { |
| 54 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, UNDEFINED); |
| 55 } else { |
| 56 var first = %_StringCharCodeAt(s, position); |
| 57 value = %_StringCharFromCode(first); |
| 58 done = false; |
| 59 position++; |
56 | 60 |
57 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol); | 61 if (first >= 0xD800 && first <= 0xDBFF && position < length) { |
58 var length = TO_UINT32(s.length); | 62 var second = %_StringCharCodeAt(s, position); |
| 63 if (second >= 0xDC00 && second <= 0xDFFF) { |
| 64 value += %_StringCharFromCode(second); |
| 65 position++; |
| 66 } |
| 67 } |
59 | 68 |
60 if (position >= length) { | 69 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position); |
61 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, | |
62 UNDEFINED); | |
63 return ArrayIteratorCreateResultObject(UNDEFINED, true); | |
64 } | |
65 | |
66 var first = %_StringCharCodeAt(s, position); | |
67 var resultString = %_StringCharFromCode(first); | |
68 position++; | |
69 | |
70 if (first >= 0xD800 && first <= 0xDBFF && position < length) { | |
71 var second = %_StringCharCodeAt(s, position); | |
72 if (second >= 0xDC00 && second <= 0xDFFF) { | |
73 resultString += %_StringCharFromCode(second); | |
74 position++; | |
75 } | 70 } |
76 } | 71 } |
77 | 72 return %_CreateIterResultObject(value, done); |
78 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position); | |
79 | |
80 return ArrayIteratorCreateResultObject(resultString, false); | |
81 } | 73 } |
82 | 74 |
83 | 75 |
84 // 21.1.3.27 String.prototype [ @@iterator ]( ) | 76 // 21.1.3.27 String.prototype [ @@iterator ]( ) |
85 function StringPrototypeIterator() { | 77 function StringPrototypeIterator() { |
86 return CreateStringIterator(this); | 78 return CreateStringIterator(this); |
87 } | 79 } |
88 | 80 |
89 //------------------------------------------------------------------- | 81 //------------------------------------------------------------------- |
90 | 82 |
91 %FunctionSetPrototype(StringIterator, {__proto__: $iteratorPrototype}); | 83 %FunctionSetPrototype(StringIterator, {__proto__: $iteratorPrototype}); |
92 %FunctionSetInstanceClassName(StringIterator, 'String Iterator'); | 84 %FunctionSetInstanceClassName(StringIterator, 'String Iterator'); |
93 | 85 |
94 utils.InstallFunctions(StringIterator.prototype, DONT_ENUM, [ | 86 utils.InstallFunctions(StringIterator.prototype, DONT_ENUM, [ |
95 'next', StringIteratorNext | 87 'next', StringIteratorNext |
96 ]); | 88 ]); |
97 %AddNamedProperty(StringIterator.prototype, toStringTagSymbol, | 89 %AddNamedProperty(StringIterator.prototype, toStringTagSymbol, |
98 "String Iterator", READ_ONLY | DONT_ENUM); | 90 "String Iterator", READ_ONLY | DONT_ENUM); |
99 | 91 |
100 utils.SetFunctionName(StringPrototypeIterator, iteratorSymbol); | 92 utils.SetFunctionName(StringPrototypeIterator, iteratorSymbol); |
101 %AddNamedProperty(GlobalString.prototype, iteratorSymbol, | 93 %AddNamedProperty(GlobalString.prototype, iteratorSymbol, |
102 StringPrototypeIterator, DONT_ENUM); | 94 StringPrototypeIterator, DONT_ENUM); |
103 | 95 |
104 }) | 96 }) |
OLD | NEW |