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