14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022 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_ref.h" 174514f5e3Sopenharmony_ci#include "ecmascript/js_weak_ref.h" 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 204514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakRef::WeakRefConstructor(EcmaRuntimeCallInfo *argv) 214514f5e3Sopenharmony_ci{ 224514f5e3Sopenharmony_ci ASSERT(argv); 234514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 244514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, WeakRef, Constructor); 254514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 264514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 274514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 284514f5e3Sopenharmony_ci // 1. If NewTarget is undefined, throw a TypeError exception. 294514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 304514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception()); 314514f5e3Sopenharmony_ci } 324514f5e3Sopenharmony_ci // 2. If CanBeHeldWeakly(target) is false, throw a TypeError exception. 334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> target = GetCallArg(argv, 0); 344514f5e3Sopenharmony_ci if (!JSTaggedValue::CanBeHeldWeakly(thread, target)) { 354514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "target invalid", JSTaggedValue::Exception()); 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci // 3. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »). 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 394514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 404514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 414514f5e3Sopenharmony_ci // 4. Perform ! AddToKeptObjects(target). 424514f5e3Sopenharmony_ci thread->GetCurrentEcmaContext()->AddToKeptObjects(target); 434514f5e3Sopenharmony_ci // 5. Set weakRef.[[WeakRefTarget]] to target. 444514f5e3Sopenharmony_ci // 6. Return weakRef. 454514f5e3Sopenharmony_ci JSHandle<JSWeakRef> weakRef = JSHandle<JSWeakRef>::Cast(obj); 464514f5e3Sopenharmony_ci weakRef->SetToWeak(thread, target.GetTaggedValue()); 474514f5e3Sopenharmony_ci return weakRef.GetTaggedValue(); 484514f5e3Sopenharmony_ci} 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ciJSTaggedValue BuiltinsWeakRef::Deref(EcmaRuntimeCallInfo *argv) 514514f5e3Sopenharmony_ci{ 524514f5e3Sopenharmony_ci ASSERT(argv); 534514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 544514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, WeakRef, Deref); 554514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 564514f5e3Sopenharmony_ci // 1. Let weakRef be the this value. 574514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 584514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(weakRef, [[WeakRefTarget]]). 594514f5e3Sopenharmony_ci if (!thisValue->IsJSWeakRef()) { 604514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot", 614514f5e3Sopenharmony_ci JSTaggedValue::Exception()); 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci // 3. Return ! WeakRefDeref(weakRef). 644514f5e3Sopenharmony_ci JSHandle<JSWeakRef> weakRef = JSHandle<JSWeakRef>::Cast(thisValue); 654514f5e3Sopenharmony_ci return JSWeakRef::WeakRefDeref(thread, weakRef); 664514f5e3Sopenharmony_ci} 674514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 68