• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WEAK_REF_H
17 #define ECMASCRIPT_JS_WEAK_REF_H
18 
19 #include "ecmascript/js_object.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
23 class JSWeakRef : public JSObject {
24 public:
Cast(TaggedObject * object)25     static JSWeakRef *Cast(TaggedObject *object)
26     {
27         ASSERT(JSTaggedValue(object).IsJSWeakRef());
28         return static_cast<JSWeakRef *>(object);
29     }
30 
31     // 26.1.4.1 WeakRefDeref ( weakRef )
WeakRefDeref(JSThread * thread,const JSHandle<JSWeakRef> & weakRef)32     static JSTaggedValue WeakRefDeref(JSThread *thread, const JSHandle<JSWeakRef> &weakRef)
33     {
34         // 1. Let target be weakRef.[[WeakRefTarget]].
35         // 2. If target is not empty, then
36         //     a. Perform ! AddToKeptObjects(target).
37         //     b. Return target.
38         // 3. Return undefined.
39         JSHandle<JSTaggedValue> target(thread, weakRef->GetFromWeak());
40         if (!target->IsUndefined()) {
41             thread->GetEcmaVM()->GetHeap()->AddToKeptObjects(target);
42         }
43         return target.GetTaggedValue();
44     }
45 
SetToWeak(JSThread * thread,JSTaggedValue value)46     void SetToWeak(JSThread *thread, JSTaggedValue value)
47     {
48         JSTaggedValue weakObj = JSTaggedValue(value.CreateAndGetWeakRef());
49         ASSERT(weakObj.IsWeak());
50         SetWeakObject(thread, weakObj);
51     }
52 
GetFromWeak()53     JSTaggedValue GetFromWeak() const
54     {
55         JSTaggedValue weakObj = GetWeakObject();
56         if (!weakObj.IsUndefined()) {
57             return JSTaggedValue(weakObj.GetWeakReferent());
58         }
59         return weakObj;
60     }
61     static constexpr size_t WEAK_OBJECT_OFFSET = JSObject::SIZE;
62     ACCESSORS(WeakObject, WEAK_OBJECT_OFFSET, SIZE)
63 
64     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, WEAK_OBJECT_OFFSET, SIZE)
65     DECL_DUMP()
66 };
67 } // namespace
68 #endif // ECMASCRIPT_JS_WEAK_REF_H