• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/builtins/builtins_string_iterator.h"
17 
18 #include "ecmascript/base/string_helper.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/js_iterator.h"
21 #include "ecmascript/js_string_iterator.h"
22 #include "ecmascript/js_tagged_number.h"
23 #include "ecmascript/js_tagged_value.h"
24 #include "ecmascript/object_factory.h"
25 
26 namespace panda::ecmascript::builtins {
Next(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsStringIterator::Next(EcmaRuntimeCallInfo *argv)
28 {
29     ASSERT(argv);
30     BUILTINS_API_TRACE(argv->GetThread(), StringIterator, Next);
31     JSThread *thread = argv->GetThread();
32     [[maybe_unused]] EcmaHandleScope handleScope(thread);
33     // 1. Let O be the this value.
34     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
35     // 2. If Type(O) is not Object, throw a TypeError exception.
36     // 3. If O does not have all of the internal slots of an String Iterator Instance (21.1.5.3),
37     // throw a TypeError exception.
38     if (!thisValue->IsStringIterator()) {
39         THROW_TYPE_ERROR_AND_RETURN(thread, "is not StringIterator", JSTaggedValue::Exception());
40     }
41     // 4. Let s be the value of the [[IteratedString]] internal slot of O.
42     JSHandle<JSTaggedValue> string(thread, thisValue.GetObject<JSStringIterator>()->GetIteratedString());
43     // 5. If s is undefined, return CreateIterResultObject(undefined, true).
44     if (string->IsUndefined()) {
45         return JSIterator::CreateIterResultObject(thread, string, true).GetTaggedValue();
46     }
47     // 6. Let position be the value of the [[StringIteratorNextIndex]] internal slot of O.
48     uint32_t position = thisValue.GetObject<JSStringIterator>()->GetStringIteratorNextIndex();
49 
50     // 7. Let len be the number of elements in s.
51     uint32_t len = EcmaStringAccessor(string.GetObject<EcmaString>()).GetLength();
52     // If position ≥ len, then
53     // a. Set the value of the [[IteratedString]] internal slot of O to
54     // b. Return CreateIterResultObject(undefined, true).
55     if (position >= len) {
56         thisValue.GetObject<JSStringIterator>()->SetIteratedString(thread, JSTaggedValue::Undefined());
57         JSHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
58         return JSIterator::CreateIterResultObject(thread, result, true).GetTaggedValue();
59     }
60 
61     // 9. Let first be the code unit value at index position in s.
62     uint16_t first = EcmaStringAccessor(string.GetObject<EcmaString>()).Get<false>(position);
63     JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
64     uint32_t resultSize = 1;
65     // 10. If first < 0xD800 or first > 0xDBFF or position+1 = len, let resultString be the string consisting of the
66     // single code unit first.
67     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
68     if (position + 1 == len || first < base::utf_helper::DECODE_LEAD_LOW ||
69         first > base::utf_helper::DECODE_LEAD_HIGH) {
70         std::vector<uint16_t> resultString {first, 0x0};
71         result.Update(factory->NewFromUtf16(resultString.data(), 1).GetTaggedValue());
72     } else {
73         // 11. Else,
74         // a. Let second be the code unit value at index position+1 in the String S.
75         // b. If second < 0xDC00 or second > 0xDFFF, let resultString be the string consisting of the single code unit
76         // first.
77         // c. Else, let resultString be the string consisting of the code unit first followed by the code unit second.
78         uint16_t second = EcmaStringAccessor(string.GetObject<EcmaString>()).Get<false>(position + 1);
79         if (second < base::utf_helper::DECODE_TRAIL_LOW || second > base::utf_helper::DECODE_TRAIL_HIGH) {
80             std::vector<uint16_t> resultString {first, 0x0};
81             result.Update(factory->NewFromUtf16NotCompress(resultString.data(), 1).GetTaggedValue());
82         } else {
83             std::vector<uint16_t> resultString {first, second, 0x0};
84             result.Update(
85                 factory->NewFromUtf16NotCompress(resultString.data(), 2).GetTaggedValue());  // 2: two bytes
86             resultSize = 2;  // 2: 2 means that two bytes represent a character string
87         }
88     }
89     // 12. Let resultSize be the number of code units in resultString.
90     // 13. Set the value of the [[StringIteratorNextIndex]] internal slot of O to position+ resultSize.
91     thisValue.GetObject<JSStringIterator>()->SetStringIteratorNextIndex(position + resultSize);
92 
93     // 14. Return CreateIterResultObject(resultString, false).
94     return JSIterator::CreateIterResultObject(thread, result, false).GetTaggedValue();
95 }
96 }  // namespace panda::ecmascript::builtins
97