/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/tooling/agent/runtime_impl.h" #include "ecmascript/tooling/base/pt_returns.h" #include "libpandabase/utils/logger.h" namespace panda::tooling::ecmascript { RuntimeImpl::DispatcherImpl::DispatcherImpl(FrontEnd *frontend, std::unique_ptr runtime) : DispatcherBase(frontend), runtime_(std::move(runtime)) { dispatcherTable_["enable"] = &RuntimeImpl::DispatcherImpl::Enable; dispatcherTable_["getProperties"] = &RuntimeImpl::DispatcherImpl::GetProperties; dispatcherTable_["runIfWaitingForDebugger"] = &RuntimeImpl::DispatcherImpl::RunIfWaitingForDebugger; } void RuntimeImpl::DispatcherImpl::Dispatch(const DispatchRequest &request) { CString method = request.GetMethod(); LOG(DEBUG, DEBUGGER) << "dispatch [" << method << "] to RuntimeImpl"; auto entry = dispatcherTable_.find(method); if (entry != dispatcherTable_.end()) { (this->*(entry->second))(request); } else { LOG(ERROR, DEBUGGER) << "unknown method: " << method; SendResponse(request, DispatchResponse::Fail("unknown method: " + method), nullptr); } } void RuntimeImpl::DispatcherImpl::Enable(const DispatchRequest &request) { DispatchResponse response = runtime_->Enable(); SendResponse(request, response, nullptr); } void RuntimeImpl::DispatcherImpl::RunIfWaitingForDebugger(const DispatchRequest &request) { DispatchResponse response = runtime_->RunIfWaitingForDebugger(); SendResponse(request, response, nullptr); } void RuntimeImpl::DispatcherImpl::GetProperties(const DispatchRequest &request) { std::unique_ptr params = GetPropertiesParams::Create(request.GetEcmaVM(), request.GetParams()); if (params == nullptr) { SendResponse(request, DispatchResponse::Fail("Debugger got wrong params"), nullptr); return; } CVector> outPropertyDesc; std::optional>> outInternalDescs; std::optional>> outPrivateProperties; std::optional> outExceptionDetails; DispatchResponse response = runtime_->GetProperties(std::move(params), &outPropertyDesc, &outInternalDescs, &outPrivateProperties, &outExceptionDetails); if (outExceptionDetails) { LOG(WARNING, DEBUGGER) << "GetProperties thrown an exception"; } std::unique_ptr result = std::make_unique(std::move(outPropertyDesc), std::move(outInternalDescs), std::move(outPrivateProperties), std::move(outExceptionDetails)); SendResponse(request, response, std::move(result)); } DispatchResponse RuntimeImpl::Enable() { return DispatchResponse::Ok(); } DispatchResponse RuntimeImpl::RunIfWaitingForDebugger() { return DispatchResponse::Create(backend_->Resume()); } DispatchResponse RuntimeImpl::GetProperties(std::unique_ptr params, CVector> *outPropertyDesc, [[maybe_unused]] std::optional>> *outInternalDescs, [[maybe_unused]] std::optional>> *outPrivateProps, [[maybe_unused]] std::optional> *outExceptionDetails) { backend_->GetProperties(DebuggerApi::CStringToULL(params->GetObjectId()), params->GetOwnProperties(), params->GetAccessPropertiesOnly(), outPropertyDesc); return DispatchResponse::Ok(); } } // namespace panda::tooling::ecmascript