1/*
2 * Copyright (c) 2022 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/js_regexp_iterator.h"
17
18#include "ecmascript/builtins/builtins_regexp.h"
19#include "ecmascript/object_fast_operator-inl.h"
20
21
22namespace panda::ecmascript {
23using BuiltinsBase = base::BuiltinsBase;
24using BuiltinsRegExp = builtins::BuiltinsRegExp;
25JSTaggedValue JSRegExpIterator::Next(EcmaRuntimeCallInfo *argv)
26{
27    ASSERT(argv);
28    JSThread *thread = argv->GetThread();
29    [[maybe_unused]] EcmaHandleScope handleScope(thread);
30
31    // 1. Let O be the this value.
32    JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
33    // 2. If Type(O) is not Object, throw a TypeError exception.
34    // 3. If O does not have all of the internal slots of a RegExp String Iterator Object Instance
35    // (see 21.2.7.2), throw a TypeError exception.
36    if (!input->IsJSRegExpIterator()) {
37        THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a regExp iterator", JSTaggedValue::Exception());
38    }
39
40    JSHandle<JSTaggedValue> undefinedHandle(thread->GlobalConstants()->GetHandledUndefined());
41    JSHandle<JSRegExpIterator> jsIterator = JSHandle<JSRegExpIterator>::Cast(input);
42    // 4. If O.[[Done]] is true, then
43    //     a. Return ! CreateIterResultObject(undefined, true).
44    if (jsIterator->GetDone()) {
45        return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
46    }
47
48    // 5. Let R be O.[[IteratingRegExp]].
49    // 6. Let S be O.[[IteratedString]].
50    // 7. Let global be O.[[Global]].
51    // 8. Let fullUnicode be O.[[Unicode]].
52    JSHandle<JSTaggedValue> regexp(thread, jsIterator->GetIteratingRegExp());
53    JSHandle<JSTaggedValue> inputStr(thread, jsIterator->GetIteratedString());
54    bool global = jsIterator->GetGlobal();
55    bool fullUnicode = jsIterator->GetUnicode();
56
57    // 9. Let match be ? RegExpExec(R, S).
58    JSMutableHandle<JSTaggedValue> match(thread, JSTaggedValue::Undefined());
59    bool isFastPath = BuiltinsRegExp::IsFastRegExp(thread, regexp);
60    if (isFastPath) {
61        match.Update(BuiltinsRegExp::RegExpBuiltinExec(thread, regexp, inputStr, isFastPath, true));
62    } else {
63        match.Update(BuiltinsRegExp::RegExpExec(thread, regexp, inputStr, false));
64    }
65    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
66
67    // 10. If match is null, then
68    //     a. Set O.[[Done]] to true.
69    //     b. Return ! CreateIterResultObject(undefined, true).
70    // Else,
71    if (match->IsNull()) {
72        jsIterator->SetDone(true);
73        return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
74    }
75    if (!global) {
76        //  b. Else, if non global case
77        //    i. Set O.[[Done]] to true.
78        //    ii. Return ! CreateIterResultObject(match, false).
79        jsIterator->SetDone(true);
80        return JSIterator::CreateIterResultObject(thread, match, false).GetTaggedValue();
81    }
82    // a. If global is true, then
83    //    i. Let matchStr be ? ToString(? Get(match, "0")).
84    const GlobalEnvConstants *globalConstants = thread->GlobalConstants();
85    JSHandle<JSTaggedValue> zeroString(globalConstants->GetHandledZeroString());
86    JSHandle<JSTaggedValue> getZero(JSObject::GetProperty(thread, match, zeroString).GetValue());
87    JSHandle<EcmaString> matchStr = JSTaggedValue::ToString(thread, getZero);
88    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
89    // ii. If matchStr is the empty String, then
90    //     1. Let thisIndex be ? ToLength(? Get(R, "lastIndex")).
91    //     2. Let nextIndex be ! AdvanceStringIndex(S, thisIndex, fullUnicode).
92    //     3. Perform ? Set(R, "lastIndex", �(nextIndex), true).
93    if (EcmaStringAccessor(matchStr).GetLength() == 0) {
94        uint32_t lastIndex = static_cast<uint32_t>(BuiltinsRegExp::GetLastIndex(thread, regexp, isFastPath));
95        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
96        uint32_t nextIndex = static_cast<uint32_t>(
97            BuiltinsRegExp::AdvanceStringIndex(inputStr, lastIndex, fullUnicode));
98        BuiltinsRegExp::SetLastIndex(thread, regexp, JSTaggedValue(nextIndex), isFastPath);
99        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
100    }
101    // iii. Return ! CreateIterResultObject(match, false).
102    return JSIterator::CreateIterResultObject(thread, match, false).GetTaggedValue();
103}
104
105JSHandle<JSTaggedValue> JSRegExpIterator::CreateRegExpStringIterator(JSThread *thread,
106                                                                     const JSHandle<JSTaggedValue> &matcher,
107                                                                     const JSHandle<EcmaString> &inputStr,
108                                                                     bool global, bool fullUnicode)
109{
110    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
111    if (!matcher->IsJSRegExp()) {
112        JSHandle<JSTaggedValue> undefinedHandle(thread->GlobalConstants()->GetHandledUndefined());
113        THROW_TYPE_ERROR_AND_RETURN(thread, "matcher is not JSRegExp", undefinedHandle);
114    }
115    JSHandle<JSTaggedValue> iter(factory->NewJSRegExpIterator(matcher, inputStr, global, fullUnicode));
116    return iter;
117}
118}  // namespace panda::ecmascript
119