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 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 constexpr uint32_t DEFAULT_GROW_SIZE = 5; // If the capacity is not enough, we Expansion five each time 71 static JSHandle<CellRecordVector> Append(const JSThread *thread, const JSHandle<CellRecordVector> &array, 72 const JSHandle<JSTaggedValue> &value); 73 bool IsEmpty(); 74 private: 75 static uint32_t CheckHole(const JSHandle<CellRecordVector> &array); 76 }; 77 78 class JSFinalizationRegistry : public JSObject { 79 public: 80 CAST_CHECK(JSFinalizationRegistry, IsJSFinalizationRegistry); 81 82 static void Register(JSThread *thread, JSHandle<JSTaggedValue> target, JSHandle<JSTaggedValue> heldValue, 83 JSHandle<JSTaggedValue> unregisterToken, JSHandle<JSFinalizationRegistry> obj); 84 static bool Unregister(JSThread *thread, JSHandle<JSTaggedValue> UnregisterToken, 85 JSHandle<JSFinalizationRegistry> obj); 86 static void CheckAndCall(JSThread *thread); 87 static bool CleanupFinalizationRegistry(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 88 static void AddFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> next); 89 static void CleanFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> obj); 90 static constexpr size_t CLEANUP_CALLBACK_OFFSET = JSObject::SIZE; 91 ACCESSORS(CleanupCallback, CLEANUP_CALLBACK_OFFSET, NO_UNREGISTER_OFFSET) 92 ACCESSORS(NoUnregister, NO_UNREGISTER_OFFSET, MAYBE_UNREGISTER_OFFSET) 93 ACCESSORS(MaybeUnregister, MAYBE_UNREGISTER_OFFSET, NEXT_OFFSET) 94 ACCESSORS(Next, NEXT_OFFSET, PREV_OFFSET) 95 ACCESSORS(Prev, PREV_OFFSET, SIZE) 96 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, CLEANUP_CALLBACK_OFFSET, SIZE) 97 DECL_DUMP() 98 }; 99 } // namespace 100 #endif // ECMASCRIPT_JS_FINALIZATION_REGISTRY_H