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