deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / string-iterator.js
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() {
6
7 "use strict";
8
9 %CheckIsBootstrapping();
10
11 var GlobalArray = global.Array;
12 var GlobalObject = global.Object;
13 var GlobalString = global.String;
14
15 //-------------------------------------------------------------------
16
17 var stringIteratorIteratedStringSymbol =
18     GLOBAL_PRIVATE("StringIterator#iteratedString");
19 var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE("StringIterator#next");
20
21
22 function StringIterator() {}
23
24
25 // 21.1.5.1 CreateStringIterator Abstract Operation
26 function CreateStringIterator(string) {
27   var s = TO_STRING_INLINE(string);
28   var iterator = new StringIterator;
29   SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s);
30   SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0);
31   return iterator;
32 }
33
34
35 // 21.1.5.2.2 %StringIteratorPrototype%[@@iterator]
36 function StringIteratorIterator() {
37   return this;
38 }
39
40
41 // 21.1.5.2.1 %StringIteratorPrototype%.next( )
42 function StringIteratorNext() {
43   var iterator = ToObject(this);
44
45   if (!HAS_DEFINED_PRIVATE(iterator, stringIteratorNextIndexSymbol)) {
46     throw MakeTypeError('incompatible_method_receiver',
47                         ['String Iterator.prototype.next']);
48   }
49
50   var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol);
51   if (IS_UNDEFINED(s)) {
52     return CreateIteratorResultObject(UNDEFINED, true);
53   }
54
55   var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol);
56   var length = TO_UINT32(s.length);
57
58   if (position >= length) {
59     SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol,
60                 UNDEFINED);
61     return CreateIteratorResultObject(UNDEFINED, true);
62   }
63
64   var first = %_StringCharCodeAt(s, position);
65   var resultString = %_StringCharFromCode(first);
66   position++;
67
68   if (first >= 0xD800 && first <= 0xDBFF && position < length) {
69     var second = %_StringCharCodeAt(s, position);
70     if (second >= 0xDC00 && second <= 0xDFFF) {
71       resultString += %_StringCharFromCode(second);
72       position++;
73     }
74   }
75
76   SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position);
77
78   return CreateIteratorResultObject(resultString, false);
79 }
80
81
82 // 21.1.3.27 String.prototype [ @@iterator ]( )
83 function StringPrototypeIterator() {
84   return CreateStringIterator(this);
85 }
86
87 //-------------------------------------------------------------------
88
89 %FunctionSetPrototype(StringIterator, new GlobalObject());
90 %FunctionSetInstanceClassName(StringIterator, 'String Iterator');
91
92 InstallFunctions(StringIterator.prototype, DONT_ENUM, GlobalArray(
93   'next', StringIteratorNext
94 ));
95 %FunctionSetName(StringIteratorIterator, '[Symbol.iterator]');
96 %AddNamedProperty(StringIterator.prototype, symbolIterator,
97                   StringIteratorIterator, DONT_ENUM);
98 %AddNamedProperty(StringIterator.prototype, symbolToStringTag,
99                   "String Iterator", READ_ONLY | DONT_ENUM);
100
101 %FunctionSetName(StringPrototypeIterator, '[Symbol.iterator]');
102 %AddNamedProperty(GlobalString.prototype, symbolIterator,
103                   StringPrototypeIterator, DONT_ENUM);
104
105 })();