1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_JS_FINALIZATION_REGISTRY_H 17 #define ECMASCRIPT_JS_FINALIZATION_REGISTRY_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/record.h" 21 #include "ecmascript/weak_vector.h" 22 23 namespace panda::ecmascript { 24 class CheckAndCallScope { 25 public: CheckAndCallScope(JSThread * thread)26 explicit CheckAndCallScope(JSThread *thread) : thread_(thread) 27 { 28 thread_->SetCheckAndCallEnterState(true); 29 } ~CheckAndCallScope()30 ~CheckAndCallScope() 31 { 32 thread_->SetCheckAndCallEnterState(false); 33 } 34 private: 35 JSThread *thread_; 36 }; 37 38 class CellRecord final : public Record { 39 public: 40 CAST_CHECK(CellRecord, IsCellRecord); SetToWeakRefTarget(JSThread * thread,JSTaggedValue value)41 void SetToWeakRefTarget(JSThread *thread, JSTaggedValue value) 42 { 43 ALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE; 44 JSTaggedValue weakObj = JSTaggedValue(value.CreateAndGetWeakRef()); 45 ASSERT(weakObj.IsWeak()); 46 SetWeakRefTarget(thread, weakObj); 47 } 48 GetFromWeakRefTarget()49 JSTaggedValue GetFromWeakRefTarget() const 50 { 51 JSTaggedValue weakObj = GetWeakRefTarget(); 52 if (!weakObj.IsUndefined()) { 53 return JSTaggedValue(weakObj.GetWeakReferent()); 54 } 55 return weakObj; 56 } 57 static constexpr size_t WEAKREF_TARGET_OFFSET = Record::SIZE; 58 ACCESSORS(WeakRefTarget, WEAKREF_TARGET_OFFSET, HELD_VALUE_OFFSET) 59 ACCESSORS(HeldValue, HELD_VALUE_OFFSET, SIZE) 60 61 DECL_VISIT_OBJECT(WEAKREF_TARGET_OFFSET, SIZE) 62 DECL_DUMP() 63 }; 64 65 class CellRecordVector : public WeakVector { 66 public: Cast(TaggedObject * object)67 static CellRecordVector *Cast(TaggedObject *object) 68 { 69 return static_cast<CellRecordVector *>(object); 70 } 71 static JSHandle<CellRecordVector> Append(const JSThread *thread, const JSHandle<CellRecordVector> &vector, 72 const JSHandle<JSTaggedValue> &value); 73 bool IsEmpty(); 74 }; 75 76 class JSFinalizationRegistry : public JSObject { 77 public: 78 CAST_CHECK(JSFinalizationRegistry, IsJSFinalizationRegistry); 79 80 static void Register(JSThread *thread, JSHandle<JSTaggedValue> target, JSHandle<JSTaggedValue> heldValue, 81 JSHandle<JSTaggedValue> unregisterToken, JSHandle<JSFinalizationRegistry> obj); 82 static bool Unregister(JSThread *thread, JSHandle<JSTaggedValue> UnregisterToken, 83 JSHandle<JSFinalizationRegistry> obj); 84 static void CheckAndCall(JSThread *thread); 85 static bool CleanupFinalizationRegistry(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 86 static void AddFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> next); 87 static void CleanFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 88 static constexpr size_t CLEANUP_CALLBACK_OFFSET = JSObject::SIZE; 89 ACCESSORS(CleanupCallback, CLEANUP_CALLBACK_OFFSET, NO_UNREGISTER_OFFSET) 90 ACCESSORS(NoUnregister, NO_UNREGISTER_OFFSET, MAYBE_UNREGISTER_OFFSET) 91 ACCESSORS(MaybeUnregister, MAYBE_UNREGISTER_OFFSET, NEXT_OFFSET) 92 ACCESSORS(Next, NEXT_OFFSET, PREV_OFFSET) 93 ACCESSORS(Prev, PREV_OFFSET, SIZE) 94 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, CLEANUP_CALLBACK_OFFSET, SIZE) 95 DECL_DUMP() 96 }; 97 } // namespace 98 #endif // ECMASCRIPT_JS_FINALIZATION_REGISTRY_H