• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_TOOLING_INTERFACE_NOTIFICATION_MANAGER_H
17 #define ECMASCRIPT_TOOLING_INTERFACE_NOTIFICATION_MANAGER_H
18 
19 #include <string_view>
20 
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_thread.h"
23 
24 namespace panda::ecmascript::tooling {
25 class RuntimeListener {
26 public:
27     RuntimeListener() = default;
28     virtual ~RuntimeListener() = default;
29     DEFAULT_COPY_SEMANTIC(RuntimeListener);
30     DEFAULT_MOVE_SEMANTIC(RuntimeListener);
31 
32     virtual void LoadModule(std::string_view name, std::string_view) = 0;
33 
34     virtual void BytecodePcChanged(JSThread *thread, JSHandle<Method> method,
35                                    uint32_t bc_offset) = 0;
36     virtual void VmStart() = 0;
37     virtual void VmDeath() = 0;
38     virtual void PendingJobEntry() = 0;
39     virtual void NativeCalling(const void *nativeAddress) = 0;
40 };
41 
42 class NotificationManager {
43 public:
44     NotificationManager() = default;
45     ~NotificationManager() = default;
46     NO_COPY_SEMANTIC(NotificationManager);
47     NO_MOVE_SEMANTIC(NotificationManager);
48 
AddListener(RuntimeListener * listener)49     void AddListener(RuntimeListener *listener)
50     {
51         listener_ = listener;
52     }
RemoveListener()53     void RemoveListener()
54     {
55         listener_ = nullptr;
56     }
57 
LoadModuleEvent(std::string_view name,std::string_view entryPoint)58     void LoadModuleEvent(std::string_view name, std::string_view entryPoint) const
59     {
60         if (UNLIKELY(listener_ != nullptr)) {
61             listener_->LoadModule(name, entryPoint);
62         }
63     }
64 
BytecodePcChangedEvent(JSThread * thread,Method * method,uint32_t bcOffset)65     void BytecodePcChangedEvent(JSThread *thread, Method *method, uint32_t bcOffset) const
66     {
67         if (UNLIKELY(listener_ != nullptr)) {
68             [[maybe_unused]] EcmaHandleScope handleScope(thread);
69             JSHandle<Method> methodHandle(thread, method);
70             listener_->BytecodePcChanged(thread, methodHandle, bcOffset);
71         }
72     }
73 
NativeCallingEvent(const void * nativeAddress)74     void NativeCallingEvent(const void *nativeAddress) const
75     {
76         if (UNLIKELY(listener_ != nullptr)) {
77             listener_->NativeCalling(nativeAddress);
78         }
79     }
80 
PendingJobEntryEvent()81     void PendingJobEntryEvent() const
82     {
83         if (UNLIKELY(listener_ != nullptr)) {
84             listener_->PendingJobEntry();
85         }
86     }
87 
VmStartEvent()88     void VmStartEvent() const
89     {
90         if (UNLIKELY(listener_ != nullptr)) {
91             listener_->VmStart();
92         }
93     }
VmDeathEvent()94     void VmDeathEvent() const
95     {
96         if (UNLIKELY(listener_ != nullptr)) {
97             listener_->VmDeath();
98         }
99     }
100 
101 private:
102     RuntimeListener *listener_ {nullptr};
103 };
104 }  // panda::ecmascript::tooling
105 
106 #endif  // ECMASCRIPT_TOOLING_INTERFACE_NOTIFICATION_MANAGER_H