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

Side by Side Diff: src/string-iterator.js

Issue 335423002: Add @@iterator support for strings (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « src/bootstrapper.cc ('k') | test/mjsunit/harmony/iteration-semantics.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 'use strict';
6
7
8 // This file relies on the fact that the following declaration has been made
9 // in runtime.js:
10 // var $String = global.String;
11
12
13 var stringIteratorIteratedStringSymbol =
14 GLOBAL_PRIVATE("StringIterator#iteratedString");
15 var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE("StringIterator#next");
16
17
18 function StringIterator() {}
19
20
21 // 21.1.5.1 CreateStringIterator Abstract Operation
22 function CreateStringIterator(string) {
23 var s = TO_STRING_INLINE(string);
24 var iterator = new StringIterator;
25 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s);
26 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0);
27 return iterator;
28 }
29
30
31 // 21.1.5.2.2 %StringIteratorPrototype%[@@iterator]
32 function StringIteratorIterator() {
33 return this;
arv (Not doing code reviews) 2014/06/17 15:33:19 wrong indentation
wingo 2014/06/18 08:34:07 Done.
34 }
35
36
37 // 21.1.5.2.1 %StringIteratorPrototype%.next( )
38 function StringIteratorNext() {
39 var iterator = ToObject(this);
40
41 if (!HAS_PRIVATE(iterator, stringIteratorIteratedStringSymbol)) {
42 throw MakeTypeError('incompatible_method_receiver',
43 ['String Iterator.prototype.next']);
44 }
45
46 var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol);
47 if (IS_UNDEFINED(s)) {
48 return CreateIteratorResultObject(UNDEFINED, true);
49 }
50
51 var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol);
52 var length = TO_UINT32(s.length);
53
54 if (position >= length) {
55 SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, UNDEFINED);
56 return CreateIteratorResultObject(UNDEFINED, true);
57 }
58
59 var first = %_StringCharCodeAt(s, position);
60 var resultString = %_StringCharFromCode(first);
61 position++;
62
63 if (first >= 0xD800 && first <= 0xDBFF && position < length) {
64 var second = %_StringCharCodeAt(s, position);
65 if (second >= 0xDC00 && second <= 0xDFFF) {
66 resultString += %_StringCharFromCode(second);
67 position++;
68 }
69 }
70
71 SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position);
72
73 return CreateIteratorResultObject(resultString, false);
74 }
75
76
77 function SetUpStringIterator() {
78 %CheckIsBootstrapping();
79
80 %FunctionSetPrototype(StringIterator, new $Object());
81 %FunctionSetInstanceClassName(StringIterator, 'String Iterator');
82
83 InstallFunctions(StringIterator.prototype, DONT_ENUM, $Array(
84 'next', StringIteratorNext
85 ));
86 %FunctionSetName(StringIteratorIterator, '[Symbol.iterator]');
87 %SetProperty(StringIterator.prototype, symbolIterator, StringIteratorIterator,
88 DONT_ENUM | DONT_DELETE | READ_ONLY);
89 }
90 SetUpStringIterator();
91
92
93 // 21.1.3.27 String.prototype [ @@iterator ]( )
94 function StringPrototypeIterator() {
95 return CreateStringIterator(this);
96 }
97
98
99 function ExtendStringPrototypeWithIterator() {
100 %CheckIsBootstrapping();
101
102 %FunctionSetName(StringPrototypeIterator, '[Symbol.iterator]');
103 %SetProperty($String.prototype, symbolIterator, StringPrototypeIterator,
104 DONT_ENUM | DONT_DELETE | READ_ONLY);
arv (Not doing code reviews) 2014/06/17 15:33:19 Why read only and dont delete?
wingo 2014/06/18 08:34:06 Copy-paste cargo culting. Fixed.
105 }
106 ExtendStringPrototypeWithIterator();
OLDNEW
« no previous file with comments | « src/bootstrapper.cc ('k') | test/mjsunit/harmony/iteration-semantics.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698