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 JSTaggedValue weakObj = JSTaggedValue(value.CreateAndGetWeakRef()); 44 ASSERT(weakObj.IsWeak()); 45 SetWeakRefTarget(thread, weakObj); 46 } 47 GetFromWeakRefTarget()48 JSTaggedValue GetFromWeakRefTarget() const 49 { 50 JSTaggedValue weakObj = GetWeakRefTarget(); 51 if (!weakObj.IsUndefined()) { 52 return JSTaggedValue(weakObj.GetWeakReferent()); 53 } 54 return weakObj; 55 } 56 static constexpr size_t WEAKREF_TARGET_OFFSET = Record::SIZE; 57 ACCESSORS(WeakRefTarget, WEAKREF_TARGET_OFFSET, HELD_VALUE_OFFSET) 58 ACCESSORS(HeldValue, HELD_VALUE_OFFSET, SIZE) 59 60 DECL_VISIT_OBJECT(WEAKREF_TARGET_OFFSET, SIZE) 61 DECL_DUMP() 62 }; 63 64 class CellRecordVector : public WeakVector { 65 public: Cast(TaggedObject * object)66 static CellRecordVector *Cast(TaggedObject *object) 67 { 68 return static_cast<CellRecordVector *>(object); 69 } 70 static JSHandle<CellRecordVector> Append(const JSThread *thread, const JSHandle<CellRecordVector> &vector, 71 const JSHandle<JSTaggedValue> &value); 72 bool IsEmpty(); 73 }; 74 75 class JSFinalizationRegistry : public JSObject { 76 public: 77 CAST_CHECK(JSFinalizationRegistry, IsJSFinalizationRegistry); 78 79 static void Register(JSThread *thread, JSHandle<JSTaggedValue> target, JSHandle<JSTaggedValue> heldValue, 80 JSHandle<JSTaggedValue> unregisterToken, JSHandle<JSFinalizationRegistry> obj); 81 static bool Unregister(JSThread *thread, JSHandle<JSTaggedValue> UnregisterToken, 82 JSHandle<JSFinalizationRegistry> obj); 83 static void CheckAndCall(JSThread *thread); 84 static bool CleanupFinalizationRegistry(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 85 static void AddFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> next); 86 static void CleanFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 87 static constexpr size_t CLEANUP_CALLBACK_OFFSET = JSObject::SIZE; 88 ACCESSORS(CleanupCallback, CLEANUP_CALLBACK_OFFSET, NO_UNREGISTER_OFFSET) 89 ACCESSORS(NoUnregister, NO_UNREGISTER_OFFSET, MAYBE_UNREGISTER_OFFSET) 90 ACCESSORS(MaybeUnregister, MAYBE_UNREGISTER_OFFSET, NEXT_OFFSET) 91 ACCESSORS(Next, NEXT_OFFSET, PREV_OFFSET) 92 ACCESSORS(Prev, PREV_OFFSET, SIZE) 93 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, CLEANUP_CALLBACK_OFFSET, SIZE) 94 DECL_DUMP() 95 }; 96 } // namespace 97 #endif // ECMASCRIPT_JS_FINALIZATION_REGISTRY_H