1 /*
2 * Copyright (c) 2021 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 #include "debugger_service.h"
17
18 #include "protocol_handler.h"
19
20 #include "ecmascript/debugger/js_debugger_manager.h"
21 #include "ecmascript/ecma_vm.h"
22
23 namespace panda::ecmascript::tooling {
InitializeDebugger(::panda::ecmascript::EcmaVM * vm,const std::function<void (const void *,const std::string &)> & onResponse)24 void InitializeDebugger(::panda::ecmascript::EcmaVM *vm,
25 const std::function<void(const void *, const std::string &)> &onResponse)
26 {
27 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
28 if (handler != nullptr) {
29 LOG_DEBUGGER(ERROR) << "JS debugger was initialized";
30 return;
31 }
32 vm->GetJsDebuggerManager()->SetDebuggerHandler(new ProtocolHandler(onResponse, vm));
33 }
34
UninitializeDebugger(::panda::ecmascript::EcmaVM * vm)35 void UninitializeDebugger(::panda::ecmascript::EcmaVM *vm)
36 {
37 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
38 delete handler;
39 vm->GetJsDebuggerManager()->SetDebuggerHandler(nullptr);
40 }
41
WaitForDebugger(const::panda::ecmascript::EcmaVM * vm)42 void WaitForDebugger(const ::panda::ecmascript::EcmaVM *vm)
43 {
44 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
45 if (LIKELY(handler != nullptr)) {
46 handler->WaitForDebugger();
47 }
48 }
49
OnMessage(const::panda::ecmascript::EcmaVM * vm,std::string && message)50 void OnMessage(const ::panda::ecmascript::EcmaVM *vm, std::string &&message)
51 {
52 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
53 if (LIKELY(handler != nullptr)) {
54 handler->DispatchCommand(std::move(message));
55 }
56 }
57
ProcessMessage(const::panda::ecmascript::EcmaVM * vm)58 void ProcessMessage(const ::panda::ecmascript::EcmaVM *vm)
59 {
60 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
61 if (LIKELY(handler != nullptr)) {
62 handler->ProcessCommand();
63 }
64 }
65
GetDispatchStatus(const::panda::ecmascript::EcmaVM * vm)66 int32_t GetDispatchStatus(const ::panda::ecmascript::EcmaVM *vm)
67 {
68 ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler();
69 if (LIKELY(handler != nullptr)) {
70 return handler->GetDispatchStatus();
71 }
72 return ProtocolHandler::DispatchStatus::UNKNOWN;
73 }
74 } // namespace panda::ecmascript::tooling