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_weak_map.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_map.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h"
214514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h"
224514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
234514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakMap::WeakMapConstructor(EcmaRuntimeCallInfo *argv)
244514f5e3Sopenharmony_ci{
254514f5e3Sopenharmony_ci    ASSERT(argv);
264514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), WeakMap, 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 WeakMap be OrdinaryCreateFromConstructor(NewTarget, "%WeakMapPrototype%", «‍[[WeakMapData]]» ).
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<JSWeakMap> weakMap = JSHandle<JSWeakMap>::Cast(obj);
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    // 4.Set weakmap’s [[WeakMapData]] internal slot to a new empty List.
444514f5e3Sopenharmony_ci    JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
454514f5e3Sopenharmony_ci    weakMap->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 weakMap.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(weakMap, "set").
584514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledSetString();
594514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> adder =
604514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(weakMap), adderKey).GetValue();
614514f5e3Sopenharmony_ci    // ReturnIfAbrupt(adder).
624514f5e3Sopenharmony_ci    RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
634514f5e3Sopenharmony_ci    // If IsCallable(adder) is false, throw a TypeError exception
644514f5e3Sopenharmony_ci    if (!adder->IsCallable()) {
654514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci    return BuiltinsMap::AddEntriesFromIterable(thread, obj, iterable, adder, factory);
684514f5e3Sopenharmony_ci}
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakMap::Delete(EcmaRuntimeCallInfo *argv)
714514f5e3Sopenharmony_ci{
724514f5e3Sopenharmony_ci    ASSERT(argv);
734514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Delete);
744514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
754514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
764514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
774514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
784514f5e3Sopenharmony_ci    // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
794514f5e3Sopenharmony_ci    if (!self->IsJSWeakMap()) {
804514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    JSHandle<JSWeakMap> weakMap(self);
844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
854514f5e3Sopenharmony_ci    // 5.If CanBeHeldWeakly(key) is false, return false.
864514f5e3Sopenharmony_ci    if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
874514f5e3Sopenharmony_ci        return GetTaggedBoolean(false);
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci    return GetTaggedBoolean(JSWeakMap::Delete(thread, weakMap, key));
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakMap::Has(EcmaRuntimeCallInfo *argv)
934514f5e3Sopenharmony_ci{
944514f5e3Sopenharmony_ci    ASSERT(argv);
954514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Has);
964514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
974514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
984514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
994514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1004514f5e3Sopenharmony_ci    // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
1014514f5e3Sopenharmony_ci    if (!self->IsJSWeakMap()) {
1024514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
1034514f5e3Sopenharmony_ci    }
1044514f5e3Sopenharmony_ci    JSWeakMap *jsWeakMap = JSWeakMap::Cast(self.GetTaggedValue().GetTaggedObject());
1054514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1064514f5e3Sopenharmony_ci    // 5.If CanBeHeldWeakly(key) is false, return false.
1074514f5e3Sopenharmony_ci    if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
1084514f5e3Sopenharmony_ci        return GetTaggedBoolean(false);
1094514f5e3Sopenharmony_ci    }
1104514f5e3Sopenharmony_ci    return GetTaggedBoolean(jsWeakMap->Has(thread, key.GetTaggedValue()));
1114514f5e3Sopenharmony_ci}
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakMap::Get(EcmaRuntimeCallInfo *argv)
1144514f5e3Sopenharmony_ci{
1154514f5e3Sopenharmony_ci    ASSERT(argv);
1164514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Get);
1174514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1184514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1194514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1204514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1214514f5e3Sopenharmony_ci    // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
1224514f5e3Sopenharmony_ci    if (!self->IsJSWeakMap()) {
1234514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
1244514f5e3Sopenharmony_ci    }
1254514f5e3Sopenharmony_ci    JSWeakMap *jsWeakMap = JSWeakMap::Cast(self.GetTaggedValue().GetTaggedObject());
1264514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1274514f5e3Sopenharmony_ci    // 4.If CanBeHeldWeakly(key) is false, return undefined.
1284514f5e3Sopenharmony_ci    if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
1294514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
1304514f5e3Sopenharmony_ci    }
1314514f5e3Sopenharmony_ci    return jsWeakMap->Get(thread, key.GetTaggedValue());
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakMap::Set(EcmaRuntimeCallInfo *argv)
1354514f5e3Sopenharmony_ci{
1364514f5e3Sopenharmony_ci    ASSERT(argv);
1374514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Set);
1384514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1394514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_ci    // 2.If Type(S) is not Object, throw a TypeError exception.
1434514f5e3Sopenharmony_ci    // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
1444514f5e3Sopenharmony_ci    if (!self->IsJSWeakMap()) {
1454514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
1464514f5e3Sopenharmony_ci    }
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1494514f5e3Sopenharmony_ci    // 4.If CanBeHeldWeakly(key) is false, throw a TypeError exception.
1504514f5e3Sopenharmony_ci    if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
1514514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "invalid value used as weak map key.", JSTaggedValue::Exception());
1524514f5e3Sopenharmony_ci    }
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
1554514f5e3Sopenharmony_ci
1564514f5e3Sopenharmony_ci    JSHandle<JSWeakMap> map(self);
1574514f5e3Sopenharmony_ci    JSWeakMap::Set(thread, map, key, value);
1584514f5e3Sopenharmony_ci    return map.GetTaggedValue();
1594514f5e3Sopenharmony_ci}
1604514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
161