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_set.h" 174514f5e3Sopenharmony_ci#include "ecmascript/interpreter/interpreter.h" 184514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 194514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h" 214514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 244514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakSet::WeakSetConstructor(EcmaRuntimeCallInfo *argv) 254514f5e3Sopenharmony_ci{ 264514f5e3Sopenharmony_ci ASSERT(argv); 274514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Constructor); 284514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 294514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 304514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 314514f5e3Sopenharmony_ci // 1.If NewTarget is undefined, throw a TypeError exception 324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 334514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 344514f5e3Sopenharmony_ci // throw type error 354514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception()); 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci // 2.Let weakset be OrdinaryCreateFromConstructor(NewTarget, "%WeakSetPrototype%", «[[WeakSetData]]» ). 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 394514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 404514f5e3Sopenharmony_ci // 3.returnIfAbrupt() 414514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 424514f5e3Sopenharmony_ci JSHandle<JSWeakSet> weakSet = JSHandle<JSWeakSet>::Cast(obj); 434514f5e3Sopenharmony_ci // 3.ReturnIfAbrupt(weakSet). 444514f5e3Sopenharmony_ci // 4.WeakSet set’s [[WeakSetData]] internal slot to a new empty List. 454514f5e3Sopenharmony_ci JSHandle<LinkedHashSet> linkedSet = LinkedHashSet::Create(thread); 464514f5e3Sopenharmony_ci weakSet->SetLinkedSet(thread, linkedSet); 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci // add data into weakset from iterable 494514f5e3Sopenharmony_ci // 5.If iterable is not present, let iterable be undefined. 504514f5e3Sopenharmony_ci // 6.If iterable is either undefined or null, let iter be undefined. 514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iterable(GetCallArg(argv, 0)); 524514f5e3Sopenharmony_ci // 8.If iter is undefined, return weakset 534514f5e3Sopenharmony_ci if (iterable->IsUndefined() || iterable->IsNull()) { 544514f5e3Sopenharmony_ci return weakSet.GetTaggedValue(); 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci // Let adder be Get(weakset, "add"). 574514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledAddString(); 584514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> weakSetHandle(weakSet); 594514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> adder = JSObject::GetProperty(thread, weakSetHandle, adderKey).GetValue(); 604514f5e3Sopenharmony_ci // ReturnIfAbrupt(adder). 614514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue()); 624514f5e3Sopenharmony_ci // If IsCallable(adder) is false, throw a TypeError exception 634514f5e3Sopenharmony_ci if (!adder->IsCallable()) { 644514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue()); 654514f5e3Sopenharmony_ci } 664514f5e3Sopenharmony_ci // Let iter be GetIterator(iterable). 674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable)); 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ci // ReturnIfAbrupt(iter). 704514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue()); 714514f5e3Sopenharmony_ci // values in iterator_result may be a JSArray, values[0] = key values[1]=value, used valueIndex to get value from 724514f5e3Sopenharmony_ci // jsarray 734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueIndex(thread, JSTaggedValue(1)); 744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> next = JSIterator::IteratorStep(thread, iter); 754514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue()); 764514f5e3Sopenharmony_ci JSMutableHandle<JSTaggedValue> status(thread, JSTaggedValue::Undefined()); 774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 784514f5e3Sopenharmony_ci while (!next->IsFalse()) { 794514f5e3Sopenharmony_ci // Let nextValue be IteratorValue(next). 804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next)); 814514f5e3Sopenharmony_ci // ReturnIfAbrupt(nextValue). 824514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, nextValue.GetTaggedValue()); 834514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *info = 844514f5e3Sopenharmony_ci EcmaInterpreter::NewRuntimeCallInfo(thread, adder, JSHandle<JSTaggedValue>(weakSet), undefined, 1); 854514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 864514f5e3Sopenharmony_ci info->SetCallArg(nextValue.GetTaggedValue()); 874514f5e3Sopenharmony_ci // Let status be Call(adder, weakset, «nextValue.[[value]]»). 884514f5e3Sopenharmony_ci JSFunction::Call(info); 894514f5e3Sopenharmony_ci // If status is an abrupt completion, return IteratorClose(iter, status). 904514f5e3Sopenharmony_ci if (thread->HasPendingException()) { 914514f5e3Sopenharmony_ci return JSIterator::IteratorCloseAndReturn(thread, iter); 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci // Let next be IteratorStep(iter). 944514f5e3Sopenharmony_ci next = JSIterator::IteratorStep(thread, iter); 954514f5e3Sopenharmony_ci // ReturnIfAbrupt(next). 964514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue()); 974514f5e3Sopenharmony_ci } 984514f5e3Sopenharmony_ci return weakSet.GetTaggedValue(); 994514f5e3Sopenharmony_ci} 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakSet::Add(EcmaRuntimeCallInfo *argv) 1024514f5e3Sopenharmony_ci{ 1034514f5e3Sopenharmony_ci ASSERT(argv); 1044514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Add); 1054514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1064514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci // 2.If Type(S) is not Object, throw a TypeError exception. 1104514f5e3Sopenharmony_ci // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception. 1114514f5e3Sopenharmony_ci if (!self->IsJSWeakSet()) { 1124514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception()); 1134514f5e3Sopenharmony_ci } 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 1164514f5e3Sopenharmony_ci // 4.If CanBeHeldWeakly(value) is false, throw a TypeError exception. 1174514f5e3Sopenharmony_ci if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) { 1184514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "invalid value used in weak set", JSTaggedValue::Exception()); 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci 1214514f5e3Sopenharmony_ci JSHandle<JSWeakSet> weakSet(self); 1224514f5e3Sopenharmony_ci JSWeakSet::Add(thread, weakSet, value); 1234514f5e3Sopenharmony_ci return weakSet.GetTaggedValue(); 1244514f5e3Sopenharmony_ci} 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakSet::Delete(EcmaRuntimeCallInfo *argv) 1274514f5e3Sopenharmony_ci{ 1284514f5e3Sopenharmony_ci ASSERT(argv); 1294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Delete); 1304514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1314514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1334514f5e3Sopenharmony_ci // 2.If Type(S) is not Object, throw a TypeError exception. 1344514f5e3Sopenharmony_ci // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception. 1354514f5e3Sopenharmony_ci if (!self->IsJSWeakSet()) { 1364514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception()); 1374514f5e3Sopenharmony_ci } 1384514f5e3Sopenharmony_ci 1394514f5e3Sopenharmony_ci JSHandle<JSWeakSet> weakSet(self); 1404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 1414514f5e3Sopenharmony_ci // 4.If CanBeHeldWeakly(value) is false, return false. 1424514f5e3Sopenharmony_ci if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) { 1434514f5e3Sopenharmony_ci GetTaggedBoolean(false); 1444514f5e3Sopenharmony_ci } 1454514f5e3Sopenharmony_ci return GetTaggedBoolean(JSWeakSet::Delete(thread, weakSet, value)); 1464514f5e3Sopenharmony_ci} 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakSet::Has(EcmaRuntimeCallInfo *argv) 1494514f5e3Sopenharmony_ci{ 1504514f5e3Sopenharmony_ci ASSERT(argv); 1514514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Has); 1524514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1534514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1544514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1554514f5e3Sopenharmony_ci // 2.If Type(S) is not Object, throw a TypeError exception. 1564514f5e3Sopenharmony_ci // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception. 1574514f5e3Sopenharmony_ci if (!self->IsJSWeakSet()) { 1584514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception()); 1594514f5e3Sopenharmony_ci } 1604514f5e3Sopenharmony_ci JSWeakSet *jsWeakSet = JSWeakSet::Cast(self.GetTaggedValue().GetTaggedObject()); 1614514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 1624514f5e3Sopenharmony_ci // 4.If CanBeHeldWeakly(value) is false, return false. 1634514f5e3Sopenharmony_ci if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) { 1644514f5e3Sopenharmony_ci GetTaggedBoolean(false); 1654514f5e3Sopenharmony_ci } 1664514f5e3Sopenharmony_ci return GetTaggedBoolean(jsWeakSet->Has(thread, value.GetTaggedValue())); 1674514f5e3Sopenharmony_ci} 1684514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 169