14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 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/builtins/builtins_map.h"
174514f5e3Sopenharmony_ci#include "ecmascript/interpreter/interpreter.h"
184514f5e3Sopenharmony_ci#include "ecmascript/js_function.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_map.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_map_iterator.h"
214514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
244514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::MapConstructor(EcmaRuntimeCallInfo *argv)
254514f5e3Sopenharmony_ci{
264514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Constructor);
274514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
284514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
294514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
304514f5e3Sopenharmony_ci    // 1.If NewTarget is undefined, throw a TypeError exception
314514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
324514f5e3Sopenharmony_ci    if (newTarget->IsUndefined()) {
334514f5e3Sopenharmony_ci        // throw type error
344514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci    // 2.Let Map be OrdinaryCreateFromConstructor(NewTarget, "%MapPrototype%", «‍[[MapData]]» ).
374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
384514f5e3Sopenharmony_ci    JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
394514f5e3Sopenharmony_ci    // 3.returnIfAbrupt()
404514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
414514f5e3Sopenharmony_ci    JSHandle<JSMap> map = JSHandle<JSMap>::Cast(obj);
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    // 4.Set map’s [[MapData]] internal slot to a new empty List.
444514f5e3Sopenharmony_ci    JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
454514f5e3Sopenharmony_ci    map->SetLinkedMap(thread, linkedMap);
464514f5e3Sopenharmony_ci    // add data into set from iterable
474514f5e3Sopenharmony_ci    // 5.If iterable is not present, let iterable be undefined.
484514f5e3Sopenharmony_ci    // 6.If iterable is either undefined or null, let iter be undefined.
494514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iterable = GetCallArg(argv, 0);
504514f5e3Sopenharmony_ci    // 8.If iter is undefined, return set
514514f5e3Sopenharmony_ci    if (iterable->IsUndefined() || iterable->IsNull()) {
524514f5e3Sopenharmony_ci        return map.GetTaggedValue();
534514f5e3Sopenharmony_ci    }
544514f5e3Sopenharmony_ci    if (!iterable->IsECMAObject()) {
554514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "iterable is not object", JSTaggedValue::Exception());
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci    // Let adder be Get(map, "set").
584514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledSetString();
594514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> adder = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(map), adderKey).GetValue();
604514f5e3Sopenharmony_ci    // ReturnIfAbrupt(adder).
614514f5e3Sopenharmony_ci    RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
624514f5e3Sopenharmony_ci    return AddEntriesFromIterable(thread, obj, iterable, adder, factory);
634514f5e3Sopenharmony_ci}
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Set(EcmaRuntimeCallInfo *argv)
664514f5e3Sopenharmony_ci{
674514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Set);
684514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
694514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
704514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
734514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
744514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
754514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
794514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    JSHandle<JSMap> map(self);
824514f5e3Sopenharmony_ci    JSMap::Set(thread, map, key, value);
834514f5e3Sopenharmony_ci    return map.GetTaggedValue();
844514f5e3Sopenharmony_ci}
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Clear(EcmaRuntimeCallInfo *argv)
874514f5e3Sopenharmony_ci{
884514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Clear);
894514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
904514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
954514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
964514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
974514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci    JSHandle<JSMap> map(self);
1004514f5e3Sopenharmony_ci    JSMap::Clear(thread, map);
1014514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
1024514f5e3Sopenharmony_ci}
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Delete(EcmaRuntimeCallInfo *argv)
1054514f5e3Sopenharmony_ci{
1064514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Delete);
1074514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1084514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1094514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
1104514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1114514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
1124514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
1134514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
1144514f5e3Sopenharmony_ci    }
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    JSHandle<JSMap> map(self);
1174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1184514f5e3Sopenharmony_ci    bool flag = JSMap::Delete(thread, map, key);
1194514f5e3Sopenharmony_ci    return GetTaggedBoolean(flag);
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Has(EcmaRuntimeCallInfo *argv)
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Has);
1254514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1264514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1274514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1284514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1294514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
1304514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
1314514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
1324514f5e3Sopenharmony_ci    }
1334514f5e3Sopenharmony_ci    JSMap *jsMap = JSMap::Cast(self.GetTaggedValue().GetTaggedObject());
1344514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1354514f5e3Sopenharmony_ci    bool flag = jsMap->Has(thread, key.GetTaggedValue());
1364514f5e3Sopenharmony_ci    return GetTaggedBoolean(flag);
1374514f5e3Sopenharmony_ci}
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Get(EcmaRuntimeCallInfo *argv)
1404514f5e3Sopenharmony_ci{
1414514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Get);
1424514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1434514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1444514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1454514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1464514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
1474514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
1484514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
1494514f5e3Sopenharmony_ci    }
1504514f5e3Sopenharmony_ci    JSMap *jsMap = JSMap::Cast(self.GetTaggedValue().GetTaggedObject());
1514514f5e3Sopenharmony_ci    if (jsMap->GetSize() == 0) {
1524514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
1534514f5e3Sopenharmony_ci    }
1544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1554514f5e3Sopenharmony_ci    JSTaggedValue value = jsMap->Get(thread, key.GetTaggedValue());
1564514f5e3Sopenharmony_ci    return value;
1574514f5e3Sopenharmony_ci}
1584514f5e3Sopenharmony_ci
1594514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::ForEach(EcmaRuntimeCallInfo *argv)
1604514f5e3Sopenharmony_ci{
1614514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1624514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Map, ForEach);
1634514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1644514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
1654514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1664514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
1674514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
1684514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
1694514f5e3Sopenharmony_ci    }
1704514f5e3Sopenharmony_ci    JSHandle<JSMap> map(self);
1714514f5e3Sopenharmony_ci
1724514f5e3Sopenharmony_ci    // 4.If IsCallable(callbackfn) is false, throw a TypeError exception.
1734514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> func(GetCallArg(argv, 0));
1744514f5e3Sopenharmony_ci    if (!func->IsCallable()) {
1754514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not Callable", JSTaggedValue::Exception());
1764514f5e3Sopenharmony_ci    }
1774514f5e3Sopenharmony_ci    // 5.If thisArg was supplied, let T be thisArg; else let T be undefined.
1784514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1);
1794514f5e3Sopenharmony_ci
1804514f5e3Sopenharmony_ci    JSMutableHandle<LinkedHashMap> hashMap(thread, map->GetLinkedMap());
1814514f5e3Sopenharmony_ci    const uint32_t argsLength = 3;
1824514f5e3Sopenharmony_ci    int index = 0;
1834514f5e3Sopenharmony_ci    int totalElements = hashMap->NumberOfElements() + hashMap->NumberOfDeletedElements();
1844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
1854514f5e3Sopenharmony_ci    // 7.Repeat for each e that is an element of entries, in original insertion order
1864514f5e3Sopenharmony_ci    while (index < totalElements) {
1874514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(thread, hashMap->GetKey(index++));
1884514f5e3Sopenharmony_ci        // a. If e is not empty, then
1894514f5e3Sopenharmony_ci        if (!key->IsHole()) {
1904514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> value(thread, hashMap->GetValue(index - 1));
1914514f5e3Sopenharmony_ci            EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(
1924514f5e3Sopenharmony_ci                thread, func, thisArg, undefined, argsLength);
1934514f5e3Sopenharmony_ci            RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1944514f5e3Sopenharmony_ci            info->SetCallArg(value.GetTaggedValue(), key.GetTaggedValue(), map.GetTaggedValue());
1954514f5e3Sopenharmony_ci            // i. Let funcResult be Call(callbackfn, T, «e, e, S»).
1964514f5e3Sopenharmony_ci            JSTaggedValue ret = JSFunction::Call(info);  // 3: three args
1974514f5e3Sopenharmony_ci            // ii. ReturnIfAbrupt(funcResult).
1984514f5e3Sopenharmony_ci            RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
1994514f5e3Sopenharmony_ci            // Maybe add or delete
2004514f5e3Sopenharmony_ci            JSTaggedValue nextTable = hashMap->GetNextTable();
2014514f5e3Sopenharmony_ci            while (!nextTable.IsHole()) {
2024514f5e3Sopenharmony_ci                index -= hashMap->GetDeletedElementsAt(index);
2034514f5e3Sopenharmony_ci                hashMap.Update(nextTable);
2044514f5e3Sopenharmony_ci                nextTable = hashMap->GetNextTable();
2054514f5e3Sopenharmony_ci            }
2064514f5e3Sopenharmony_ci            totalElements = hashMap->NumberOfElements() + hashMap->NumberOfDeletedElements();
2074514f5e3Sopenharmony_ci        }
2084514f5e3Sopenharmony_ci    }
2094514f5e3Sopenharmony_ci
2104514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
2114514f5e3Sopenharmony_ci}
2124514f5e3Sopenharmony_ci
2134514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Species(EcmaRuntimeCallInfo *argv)
2144514f5e3Sopenharmony_ci{
2154514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Species);
2164514f5e3Sopenharmony_ci    return GetThis(argv).GetTaggedValue();
2174514f5e3Sopenharmony_ci}
2184514f5e3Sopenharmony_ci
2194514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::GetSize(EcmaRuntimeCallInfo *argv)
2204514f5e3Sopenharmony_ci{
2214514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, GetSize);
2224514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2234514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2244514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
2254514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
2264514f5e3Sopenharmony_ci    // 3.If S does not have a [[MapData]] internal slot, throw a TypeError exception.
2274514f5e3Sopenharmony_ci    if (!self->IsJSMap()) {
2284514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", JSTaggedValue::Exception());
2294514f5e3Sopenharmony_ci    }
2304514f5e3Sopenharmony_ci    JSMap *jsMap = JSMap::Cast(self.GetTaggedValue().GetTaggedObject());
2314514f5e3Sopenharmony_ci    uint32_t count = jsMap->GetSize();
2324514f5e3Sopenharmony_ci    return JSTaggedValue(count);
2334514f5e3Sopenharmony_ci}
2344514f5e3Sopenharmony_ci
2354514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Entries(EcmaRuntimeCallInfo *argv)
2364514f5e3Sopenharmony_ci{
2374514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Entries);
2384514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2394514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
2414514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY_AND_VALUE);
2424514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
2434514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
2444514f5e3Sopenharmony_ci}
2454514f5e3Sopenharmony_ci
2464514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Keys(EcmaRuntimeCallInfo *argv)
2474514f5e3Sopenharmony_ci{
2484514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Keys);
2494514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2504514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2514514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
2524514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::KEY);
2534514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
2544514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
2554514f5e3Sopenharmony_ci}
2564514f5e3Sopenharmony_ci
2574514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::Values(EcmaRuntimeCallInfo *argv)
2584514f5e3Sopenharmony_ci{
2594514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Map, Values);
2604514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2614514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2624514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
2634514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter = JSMapIterator::CreateMapIterator(thread, self, IterationKind::VALUE);
2644514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
2654514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
2664514f5e3Sopenharmony_ci}
2674514f5e3Sopenharmony_ci
2684514f5e3Sopenharmony_ciJSTaggedValue BuiltinsMap::AddEntriesFromIterable(JSThread *thread, const JSHandle<JSObject> &target,
2694514f5e3Sopenharmony_ci                                                  const JSHandle<JSTaggedValue> &iterable,
2704514f5e3Sopenharmony_ci                                                  const JSHandle<JSTaggedValue> &adder, ObjectFactory *factory)
2714514f5e3Sopenharmony_ci{
2724514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Map, AddEntriesFromIterable);
2734514f5e3Sopenharmony_ci    // If IsCallable(adder) is false, throw a TypeError exception
2744514f5e3Sopenharmony_ci    if (!adder->IsCallable()) {
2754514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
2764514f5e3Sopenharmony_ci    }
2774514f5e3Sopenharmony_ci    // Let iter be GetIterator(iterable).
2784514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
2794514f5e3Sopenharmony_ci    // ReturnIfAbrupt(iter).
2804514f5e3Sopenharmony_ci    RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue());
2814514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> keyIndex(thread, JSTaggedValue(0));
2824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> valueIndex(thread, JSTaggedValue(1));
2834514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> next = JSIterator::IteratorStep(thread, iter);
2844514f5e3Sopenharmony_ci    RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
2854514f5e3Sopenharmony_ci    while (!next->IsFalse()) {
2864514f5e3Sopenharmony_ci        // Let nextValue be IteratorValue(next).
2874514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
2884514f5e3Sopenharmony_ci        // ReturnIfAbrupt(nextValue).
2894514f5e3Sopenharmony_ci        RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
2904514f5e3Sopenharmony_ci
2914514f5e3Sopenharmony_ci        // If Type(nextItem) is not Object
2924514f5e3Sopenharmony_ci        if (!nextValue->IsECMAObject()) {
2934514f5e3Sopenharmony_ci            JSHandle<JSObject> typeError = factory->GetJSError(ErrorType::TYPE_ERROR,
2944514f5e3Sopenharmony_ci                                                               "nextItem is not Object", StackCheck::NO);
2954514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> record(
2964514f5e3Sopenharmony_ci                factory->NewCompletionRecord(CompletionRecordType::THROW, JSHandle<JSTaggedValue>(typeError)));
2974514f5e3Sopenharmony_ci            JSTaggedValue ret = JSIterator::IteratorClose(thread, iter, record).GetTaggedValue();
2984514f5e3Sopenharmony_ci            if (!thread->HasPendingException()) {
2994514f5e3Sopenharmony_ci                THROW_NEW_ERROR_AND_RETURN_VALUE(thread, typeError.GetTaggedValue(), ret);
3004514f5e3Sopenharmony_ci            }
3014514f5e3Sopenharmony_ci            return ret;
3024514f5e3Sopenharmony_ci        }
3034514f5e3Sopenharmony_ci        // Let k be Get(nextItem, "0").
3044514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key = JSTaggedValue::GetProperty(thread, nextValue, keyIndex).GetValue();
3054514f5e3Sopenharmony_ci        // If k is an abrupt completion, return IteratorClose(iter, k).
3064514f5e3Sopenharmony_ci        if (thread->HasPendingException()) {
3074514f5e3Sopenharmony_ci            return JSIterator::IteratorCloseAndReturn(thread, iter);
3084514f5e3Sopenharmony_ci        }
3094514f5e3Sopenharmony_ci        // Let v be Get(nextItem, "1").
3104514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, nextValue, valueIndex).GetValue();
3114514f5e3Sopenharmony_ci        // If v is an abrupt completion, return IteratorClose(iter, v).
3124514f5e3Sopenharmony_ci        if (thread->HasPendingException()) {
3134514f5e3Sopenharmony_ci            return JSIterator::IteratorCloseAndReturn(thread, iter);
3144514f5e3Sopenharmony_ci        }
3154514f5e3Sopenharmony_ci        const uint32_t argsLength = 2;  // 2: key and value pair
3164514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
3174514f5e3Sopenharmony_ci        EcmaRuntimeCallInfo *info =
3184514f5e3Sopenharmony_ci            EcmaInterpreter::NewRuntimeCallInfo(thread, adder, JSHandle<JSTaggedValue>(target), undefined, argsLength);
3194514f5e3Sopenharmony_ci        RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
3204514f5e3Sopenharmony_ci        info->SetCallArg(key.GetTaggedValue(), value.GetTaggedValue());
3214514f5e3Sopenharmony_ci        JSFunction::Call(info);
3224514f5e3Sopenharmony_ci        // If status is an abrupt completion, return IteratorClose(iter, status).
3234514f5e3Sopenharmony_ci        if (thread->HasPendingException()) {
3244514f5e3Sopenharmony_ci            return JSIterator::IteratorCloseAndReturn(thread, iter);
3254514f5e3Sopenharmony_ci        }
3264514f5e3Sopenharmony_ci        // Let next be IteratorStep(iter).
3274514f5e3Sopenharmony_ci        next = JSIterator::IteratorStep(thread, iter);
3284514f5e3Sopenharmony_ci        // ReturnIfAbrupt(next).
3294514f5e3Sopenharmony_ci        RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
3304514f5e3Sopenharmony_ci    }
3314514f5e3Sopenharmony_ci    return target.GetTaggedValue();
3324514f5e3Sopenharmony_ci}
3334514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
334