11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cinamespace string {
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cimacro NewJSStringIterator(implicit context: Context)(
81cb0ef41Sopenharmony_ci    string: String, nextIndex: Smi): JSStringIterator {
91cb0ef41Sopenharmony_ci  return new JSStringIterator{
101cb0ef41Sopenharmony_ci    map: GetInitialStringIteratorMap(),
111cb0ef41Sopenharmony_ci    properties_or_hash: kEmptyFixedArray,
121cb0ef41Sopenharmony_ci    elements: kEmptyFixedArray,
131cb0ef41Sopenharmony_ci    string: string,
141cb0ef41Sopenharmony_ci    index: nextIndex
151cb0ef41Sopenharmony_ci  };
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// ES6 #sec-string.prototype-@@iterator
191cb0ef41Sopenharmony_citransitioning javascript builtin StringPrototypeIterator(
201cb0ef41Sopenharmony_ci    js-implicit context: NativeContext, receiver: JSAny)(): JSStringIterator {
211cb0ef41Sopenharmony_ci  const name: String =
221cb0ef41Sopenharmony_ci      ToThisString(receiver, 'String.prototype[Symbol.iterator]');
231cb0ef41Sopenharmony_ci  const index: Smi = 0;
241cb0ef41Sopenharmony_ci  return NewJSStringIterator(name, index);
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// ES6 #sec-%stringiteratorprototype%.next
281cb0ef41Sopenharmony_citransitioning javascript builtin StringIteratorPrototypeNext(
291cb0ef41Sopenharmony_ci    js-implicit context: NativeContext, receiver: JSAny)(): JSObject {
301cb0ef41Sopenharmony_ci  const iterator = Cast<JSStringIterator>(receiver) otherwise ThrowTypeError(
311cb0ef41Sopenharmony_ci      MessageTemplate::kIncompatibleMethodReceiver,
321cb0ef41Sopenharmony_ci      'String Iterator.prototype.next', receiver);
331cb0ef41Sopenharmony_ci  const string = iterator.string;
341cb0ef41Sopenharmony_ci  const position: intptr = SmiUntag(iterator.index);
351cb0ef41Sopenharmony_ci  const length: intptr = string.length_intptr;
361cb0ef41Sopenharmony_ci  if (position >= length) {
371cb0ef41Sopenharmony_ci    return AllocateJSIteratorResult(Undefined, True);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  // Move to next codepoint.
401cb0ef41Sopenharmony_ci  const encoding = UnicodeEncoding::UTF16;
411cb0ef41Sopenharmony_ci  const ch = string::LoadSurrogatePairAt(string, length, position, encoding);
421cb0ef41Sopenharmony_ci  const value: String = string::StringFromSingleUTF16EncodedCodePoint(ch);
431cb0ef41Sopenharmony_ci  iterator.index = SmiTag(position + value.length_intptr);
441cb0ef41Sopenharmony_ci  return AllocateJSIteratorResult(value, False);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci}
47