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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 | 7 |
8 // This file relies on the fact that the following declaration has been made | 8 // This file relies on the fact that the following declaration has been made |
9 // in runtime.js: | 9 // in runtime.js: |
10 // var $String = global.String; | 10 // var $String = global.String; |
11 | 11 |
12 | 12 |
13 var stringIteratorIteratedStringSymbol = | 13 var stringIteratorIteratedStringSymbol = |
14 GLOBAL_PRIVATE("StringIterator#iteratedString"); | 14 GLOBAL_PRIVATE_OWN("StringIterator#iteratedString"); |
15 var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE("StringIterator#next"); | 15 var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE_OWN("StringIterator#next"); |
16 | |
17 var stringEndOfIterationMarker = new $Object(); | |
16 | 18 |
17 | 19 |
18 function StringIterator() {} | 20 function StringIterator() {} |
19 | 21 |
20 | 22 |
21 // 21.1.5.1 CreateStringIterator Abstract Operation | 23 // 21.1.5.1 CreateStringIterator Abstract Operation |
22 function CreateStringIterator(string) { | 24 function CreateStringIterator(string) { |
23 var s = TO_STRING_INLINE(string); | 25 var s = TO_STRING_INLINE(string); |
24 var iterator = new StringIterator; | 26 var iterator = new StringIterator; |
25 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s); | 27 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s); |
26 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0); | 28 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0); |
27 return iterator; | 29 return iterator; |
28 } | 30 } |
29 | 31 |
30 | 32 |
31 // 21.1.5.2.2 %StringIteratorPrototype%[@@iterator] | 33 // 21.1.5.2.2 %StringIteratorPrototype%[@@iterator] |
32 function StringIteratorIterator() { | 34 function StringIteratorIterator() { |
33 return this; | 35 return this; |
34 } | 36 } |
35 | 37 |
36 | 38 |
37 // 21.1.5.2.1 %StringIteratorPrototype%.next( ) | 39 // 21.1.5.2.1 %StringIteratorPrototype%.next( ) |
38 function StringIteratorNext() { | 40 function StringIteratorNext() { |
39 var iterator = ToObject(this); | 41 var iterator = ToObject(this); |
40 | 42 |
41 if (!HAS_PRIVATE(iterator, stringIteratorIteratedStringSymbol)) { | 43 if (!HAS_DEFINED_PRIVATE(iterator, stringIteratorIteratedStringSymbol)) { |
42 throw MakeTypeError('incompatible_method_receiver', | 44 throw MakeTypeError('incompatible_method_receiver', |
43 ['String Iterator.prototype.next']); | 45 ['String Iterator.prototype.next']); |
44 } | 46 } |
45 | 47 |
46 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol); | 48 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol); |
47 if (IS_UNDEFINED(s)) { | 49 if (s === stringEndOfIterationMarker) { |
wingo
2014/08/20 15:32:53
Wouldn't stringEndOfIterationMarker be per-realm?
arv (Not doing code reviews)
2014/08/20 15:39:26
You are right. This is not going to work.
I don't
Dmitry Lomov (no reviews)
2014/08/20 17:01:37
Thanks, good catch, I haven't thought of that.
arv (Not doing code reviews)
2014/08/20 18:07:07
In this case and for the ArrayIterator we can chec
| |
48 return CreateIteratorResultObject(UNDEFINED, true); | 50 return CreateIteratorResultObject(UNDEFINED, true); |
49 } | 51 } |
50 | 52 |
51 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol); | 53 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol); |
52 var length = TO_UINT32(s.length); | 54 var length = TO_UINT32(s.length); |
53 | 55 |
54 if (position >= length) { | 56 if (position >= length) { |
55 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, UNDEFINED); | 57 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, |
58 stringEndOfIterationMarker); | |
56 return CreateIteratorResultObject(UNDEFINED, true); | 59 return CreateIteratorResultObject(UNDEFINED, true); |
57 } | 60 } |
58 | 61 |
59 var first = %_StringCharCodeAt(s, position); | 62 var first = %_StringCharCodeAt(s, position); |
60 var resultString = %_StringCharFromCode(first); | 63 var resultString = %_StringCharFromCode(first); |
61 position++; | 64 position++; |
62 | 65 |
63 if (first >= 0xD800 && first <= 0xDBFF && position < length) { | 66 if (first >= 0xD800 && first <= 0xDBFF && position < length) { |
64 var second = %_StringCharCodeAt(s, position); | 67 var second = %_StringCharCodeAt(s, position); |
65 if (second >= 0xDC00 && second <= 0xDFFF) { | 68 if (second >= 0xDC00 && second <= 0xDFFF) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 | 100 |
98 | 101 |
99 function ExtendStringPrototypeWithIterator() { | 102 function ExtendStringPrototypeWithIterator() { |
100 %CheckIsBootstrapping(); | 103 %CheckIsBootstrapping(); |
101 | 104 |
102 %FunctionSetName(StringPrototypeIterator, '[Symbol.iterator]'); | 105 %FunctionSetName(StringPrototypeIterator, '[Symbol.iterator]'); |
103 %AddNamedProperty($String.prototype, symbolIterator, | 106 %AddNamedProperty($String.prototype, symbolIterator, |
104 StringPrototypeIterator, DONT_ENUM); | 107 StringPrototypeIterator, DONT_ENUM); |
105 } | 108 } |
106 ExtendStringPrototypeWithIterator(); | 109 ExtendStringPrototypeWithIterator(); |
OLD | NEW |