14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_errors.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
204514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace panda::ecmascript {
234514f5e3Sopenharmony_ciusing BuiltinsBase = base::BuiltinsBase;
244514f5e3Sopenharmony_ciJSTaggedValue JSSetIterator::Next(EcmaRuntimeCallInfo *argv)
254514f5e3Sopenharmony_ci{
264514f5e3Sopenharmony_ci    ASSERT(argv);
274514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
284514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
294514f5e3Sopenharmony_ci    // 1.If Type(O) is not Object, throw a TypeError exception.
304514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
314514f5e3Sopenharmony_ci    return NextInternal(thread, thisObj);
324514f5e3Sopenharmony_ci}
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ciJSTaggedValue JSSetIterator::NextInternal(JSThread *thread, JSHandle<JSTaggedValue> thisObj)
354514f5e3Sopenharmony_ci{
364514f5e3Sopenharmony_ci    // 3.If O does not have all of the internal slots of a Set Iterator Instance (23.2.5.3), throw a TypeError
374514f5e3Sopenharmony_ci    // exception.
384514f5e3Sopenharmony_ci    if (!thisObj->IsJSSetIterator()) {
394514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a set iterator", JSTaggedValue::Exception());
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    JSHandle<JSSetIterator> iter(thisObj);
424514f5e3Sopenharmony_ci    iter->Update(thread);
434514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
444514f5e3Sopenharmony_ci    // 4.Let s be O.[[IteratedSet]].
454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    // 5.Let index be O.[[SetNextIndex]].
484514f5e3Sopenharmony_ci    int index = static_cast<int>(iter->GetNextIndex());
494514f5e3Sopenharmony_ci    IterationKind itemKind = iter->GetIterationKind();
504514f5e3Sopenharmony_ci    // 7.If s is undefined, return CreateIterResultObject(undefined, true).
514514f5e3Sopenharmony_ci    if (iteratedSet->IsUndefined()) {
524514f5e3Sopenharmony_ci        return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
534514f5e3Sopenharmony_ci    }
544514f5e3Sopenharmony_ci    JSHandle<LinkedHashSet> set(iteratedSet);
554514f5e3Sopenharmony_ci    int totalElements = set->NumberOfElements() + set->NumberOfDeletedElements();
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    while (index < totalElements) {
584514f5e3Sopenharmony_ci        if (!set->GetKey(index).IsHole()) {
594514f5e3Sopenharmony_ci            iter->SetNextIndex(index + 1);
604514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> key(thread, set->GetKey(index));
614514f5e3Sopenharmony_ci            // If itemKind is value
624514f5e3Sopenharmony_ci            if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
634514f5e3Sopenharmony_ci                return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
644514f5e3Sopenharmony_ci            }
654514f5e3Sopenharmony_ci            // If itemKind is key+value, then
664514f5e3Sopenharmony_ci            ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
674514f5e3Sopenharmony_ci            JSHandle<TaggedArray> array(factory->NewTaggedArray(2));  // 2: key and value pair
684514f5e3Sopenharmony_ci            array->Set(thread, 0, key);
694514f5e3Sopenharmony_ci            array->Set(thread, 1, key);
704514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
714514f5e3Sopenharmony_ci            return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
724514f5e3Sopenharmony_ci        }
734514f5e3Sopenharmony_ci        index++;
744514f5e3Sopenharmony_ci    }
754514f5e3Sopenharmony_ci    // 13.Set O.[[IteratedSet]] to undefined.
764514f5e3Sopenharmony_ci    iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
774514f5e3Sopenharmony_ci    return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
784514f5e3Sopenharmony_ci}
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_civoid JSSetIterator::Update(const JSThread *thread)
814514f5e3Sopenharmony_ci{
824514f5e3Sopenharmony_ci    [[maybe_unused]] DisallowGarbageCollection noGc;
834514f5e3Sopenharmony_ci    JSTaggedValue iteratedSet = GetIteratedSet();
844514f5e3Sopenharmony_ci    if (iteratedSet.IsUndefined()) {
854514f5e3Sopenharmony_ci        return;
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci    LinkedHashSet *set = LinkedHashSet::Cast(iteratedSet.GetTaggedObject());
884514f5e3Sopenharmony_ci    if (set->GetNextTable().IsHole()) {
894514f5e3Sopenharmony_ci        return;
904514f5e3Sopenharmony_ci    }
914514f5e3Sopenharmony_ci    int index = static_cast<int>(GetNextIndex());
924514f5e3Sopenharmony_ci    JSTaggedValue nextTable = set->GetNextTable();
934514f5e3Sopenharmony_ci    while (!nextTable.IsHole()) {
944514f5e3Sopenharmony_ci        index -= set->GetDeletedElementsAt(index);
954514f5e3Sopenharmony_ci        set = LinkedHashSet::Cast(nextTable.GetTaggedObject());
964514f5e3Sopenharmony_ci        nextTable = set->GetNextTable();
974514f5e3Sopenharmony_ci    }
984514f5e3Sopenharmony_ci    SetIteratedSet(thread, JSTaggedValue(set));
994514f5e3Sopenharmony_ci    SetNextIndex(index);
1004514f5e3Sopenharmony_ci}
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> JSSetIterator::CreateSetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
1034514f5e3Sopenharmony_ci                                                         IterationKind kind)
1044514f5e3Sopenharmony_ci{
1054514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1064514f5e3Sopenharmony_ci    if (!obj->IsJSSet()) {
1074514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
1084514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", undefinedHandle);
1094514f5e3Sopenharmony_ci    }
1104514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter(factory->NewJSSetIterator(JSHandle<JSSet>(obj), kind));
1114514f5e3Sopenharmony_ci    return iter;
1124514f5e3Sopenharmony_ci}
1134514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
114