• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_SUSTAINING_JS_HANDLE_H
17 #define ECMASCRIPT_SUSTAINING_JS_HANDLE_H
18 
19 #include "ecmascript/common.h"
20 #include "ecmascript/platform/mutex.h"
21 #include "ecmascript/ecma_vm.h"
22 
23 namespace panda::ecmascript {
24 
25 class SustainingJSHandle {
26 public:
27     SustainingJSHandle(EcmaVM *vm);
28     ~SustainingJSHandle();
29     template <typename T>
NewHandle(JSHandle<T> value)30     JSHandle<T> NewHandle(JSHandle<T> value)
31     {
32         return JSHandle<T>(GetJsHandleSlot(value.GetTaggedType()));
33     }
34 
35     template <typename T>
NewHandle(JSTaggedType value)36     JSHandle<T> NewHandle(JSTaggedType value)
37     {
38         return JSHandle<T>(GetJsHandleSlot(value));
39     }
40 
41     template <typename T>
NewHandle(const JitThread * thread,JSTaggedType value)42     static JSHandle<T> NewHandle(const JitThread *thread, JSTaggedType value)
43     {
44         return JSHandle<T>(GetJsHandleSlot(thread, value));
45     }
46 
47     void Iterate(RootVisitor &v);
48 
49 private:
50     uintptr_t GetJsHandleSlot(JSTaggedType value);
51     static uintptr_t GetJsHandleSlot(const JitThread *thread, JSTaggedType value);
52     uintptr_t Expand();
53 
54     EcmaVM *vm_ { nullptr };
55 
56     JSTaggedType *blockNext_ { nullptr };
57     JSTaggedType *blockLimit_ { nullptr };
58     SustainingJSHandle *pre_ { nullptr };
59     SustainingJSHandle *next_ { nullptr };
60 
61     static constexpr uint32_t BLOCK_SIZE = 256L;
62     std::vector<std::array<JSTaggedType, BLOCK_SIZE>*> handleBlocks_;
63     friend class SustainingJSHandleList;
64 };
65 
66 class SustainingJSHandleList {
67 public:
68     void AddSustainingJSHandle(SustainingJSHandle *sustainingJSHandle);
69     void RemoveSustainingJSHandle(SustainingJSHandle *sustainingJSHandle);
70     void Iterate(RootVisitor &v);
71 
72 private:
73     SustainingJSHandle *listHead_ { nullptr };
74     Mutex mutex_;
75 };
76 }  // namespace panda::ecmascript
77 #endif  // ECMASCRIPT_SUSTAINING_JS_HANDLE_H
78