2014-06-25 07:43:14 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
(function() {
|
|
|
|
|
2014-12-10 14:40:54 +00:00
|
|
|
"use strict";
|
2014-06-25 07:43:14 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
%CheckIsBootstrapping();
|
2014-06-25 07:43:14 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
var GlobalObject = global.Object;
|
|
|
|
var GlobalString = global.String;
|
2014-06-25 07:43:14 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
//-------------------------------------------------------------------
|
2014-06-25 07:43:14 +00:00
|
|
|
|
|
|
|
var stringIteratorIteratedStringSymbol =
|
|
|
|
GLOBAL_PRIVATE("StringIterator#iteratedString");
|
|
|
|
var stringIteratorNextIndexSymbol = GLOBAL_PRIVATE("StringIterator#next");
|
|
|
|
|
|
|
|
|
|
|
|
function StringIterator() {}
|
|
|
|
|
|
|
|
|
|
|
|
// 21.1.5.1 CreateStringIterator Abstract Operation
|
|
|
|
function CreateStringIterator(string) {
|
|
|
|
var s = TO_STRING_INLINE(string);
|
|
|
|
var iterator = new StringIterator;
|
|
|
|
SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol, s);
|
|
|
|
SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, 0);
|
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 21.1.5.2.2 %StringIteratorPrototype%[@@iterator]
|
|
|
|
function StringIteratorIterator() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 21.1.5.2.1 %StringIteratorPrototype%.next( )
|
|
|
|
function StringIteratorNext() {
|
|
|
|
var iterator = ToObject(this);
|
|
|
|
|
2014-08-20 19:21:56 +00:00
|
|
|
if (!HAS_DEFINED_PRIVATE(iterator, stringIteratorNextIndexSymbol)) {
|
2014-06-25 07:43:14 +00:00
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['String Iterator.prototype.next']);
|
|
|
|
}
|
|
|
|
|
|
|
|
var s = GET_PRIVATE(iterator, stringIteratorIteratedStringSymbol);
|
|
|
|
if (IS_UNDEFINED(s)) {
|
2015-04-15 10:36:20 +00:00
|
|
|
return $iteratorCreateResultObject(UNDEFINED, true);
|
2014-06-25 07:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var position = GET_PRIVATE(iterator, stringIteratorNextIndexSymbol);
|
|
|
|
var length = TO_UINT32(s.length);
|
|
|
|
|
|
|
|
if (position >= length) {
|
2014-08-20 19:21:56 +00:00
|
|
|
SET_PRIVATE(iterator, stringIteratorIteratedStringSymbol,
|
|
|
|
UNDEFINED);
|
2015-04-15 10:36:20 +00:00
|
|
|
return $iteratorCreateResultObject(UNDEFINED, true);
|
2014-06-25 07:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var first = %_StringCharCodeAt(s, position);
|
|
|
|
var resultString = %_StringCharFromCode(first);
|
|
|
|
position++;
|
|
|
|
|
|
|
|
if (first >= 0xD800 && first <= 0xDBFF && position < length) {
|
|
|
|
var second = %_StringCharCodeAt(s, position);
|
|
|
|
if (second >= 0xDC00 && second <= 0xDFFF) {
|
|
|
|
resultString += %_StringCharFromCode(second);
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_PRIVATE(iterator, stringIteratorNextIndexSymbol, position);
|
|
|
|
|
2015-04-15 10:36:20 +00:00
|
|
|
return $iteratorCreateResultObject(resultString, false);
|
2014-06-25 07:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 21.1.3.27 String.prototype [ @@iterator ]( )
|
|
|
|
function StringPrototypeIterator() {
|
|
|
|
return CreateStringIterator(this);
|
|
|
|
}
|
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
//-------------------------------------------------------------------
|
2014-06-25 07:43:14 +00:00
|
|
|
|
2015-03-12 14:47:35 +00:00
|
|
|
%FunctionSetPrototype(StringIterator, new GlobalObject());
|
|
|
|
%FunctionSetInstanceClassName(StringIterator, 'String Iterator');
|
2014-06-25 07:43:14 +00:00
|
|
|
|
2015-04-10 20:51:33 +00:00
|
|
|
InstallFunctions(StringIterator.prototype, DONT_ENUM, [
|
2015-03-12 14:47:35 +00:00
|
|
|
'next', StringIteratorNext
|
2015-04-10 20:51:33 +00:00
|
|
|
]);
|
2015-03-12 14:47:35 +00:00
|
|
|
%FunctionSetName(StringIteratorIterator, '[Symbol.iterator]');
|
|
|
|
%AddNamedProperty(StringIterator.prototype, symbolIterator,
|
|
|
|
StringIteratorIterator, DONT_ENUM);
|
|
|
|
%AddNamedProperty(StringIterator.prototype, symbolToStringTag,
|
|
|
|
"String Iterator", READ_ONLY | DONT_ENUM);
|
|
|
|
|
|
|
|
%FunctionSetName(StringPrototypeIterator, '[Symbol.iterator]');
|
|
|
|
%AddNamedProperty(GlobalString.prototype, symbolIterator,
|
|
|
|
StringPrototypeIterator, DONT_ENUM);
|
|
|
|
|
|
|
|
})();
|