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