/* * 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 "tooling/base/pt_returns.h" namespace panda::ecmascript::tooling { std::unique_ptr DebuggerEnableReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("debuggerId", std::to_string(debuggerId_).c_str()); std::unique_ptr array = PtJson::CreateArray(); size_t len = protocols_.size(); for (size_t i = 0; i < len; i++) { array->Push(protocols_[i].c_str()); } result->Add("protocols", array); return result; } std::unique_ptr EnableReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = protocols_.size(); for (size_t i = 0; i < len; i++) { array->Push(protocols_[i].c_str()); } result->Add("protocols", array); return result; } std::unique_ptr SetBreakpointByUrlReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("breakpointId", id_.c_str()); std::unique_ptr array = PtJson::CreateArray(); size_t len = locations_.size(); for (size_t i = 0; i < len; i++) { ASSERT(locations_[i] != nullptr); std::unique_ptr location = locations_[i]->ToJson(); array->Push(location); } result->Add("locations", array); return result; } std::unique_ptr GetPossibleAndSetBreakpointByUrlReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = locations_.size(); for (size_t i = 0; i < len; i++) { ASSERT(locations_[i] != nullptr); std::unique_ptr location = locations_[i]->ToJson(); array->Push(location); } result->Add("locations", array); return result; } std::unique_ptr EvaluateOnCallFrameReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); ASSERT(result_ != nullptr); result->Add("result", result_->ToJson()); if (exceptionDetails_) { ASSERT(exceptionDetails_.value() != nullptr); result->Add("exceptionDetails", exceptionDetails_.value()->ToJson()); } return result; } std::unique_ptr GetPossibleBreakpointsReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = locations_.size(); for (size_t i = 0; i < len; i++) { ASSERT(locations_[i] != nullptr); std::unique_ptr location = locations_[i]->ToJson(); array->Push(location); } result->Add("locations", array); return result; } std::unique_ptr GetScriptSourceReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("scriptSource", scriptSource_.c_str()); if (bytecode_) { result->Add("bytecode", bytecode_->c_str()); } return result; } std::unique_ptr RestartFrameReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = callFrames_.size(); for (size_t i = 0; i < len; i++) { ASSERT(callFrames_[i] != nullptr); std::unique_ptr location = callFrames_[i]->ToJson(); array->Push(location); } result->Add("callFrames", array); return result; } std::unique_ptr SearchInContentReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = result_.size(); for (size_t i = 0; i < len; i++) { ASSERT(result_[i] != nullptr); std::unique_ptr res = result_[i]->ToJson(); array->Push(res); } result->Add("result", array); return result; } std::unique_ptr SetBreakpointReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("breakpointId", breakpointId_.c_str()); ASSERT(location_ != nullptr); result->Add("actualLocation", location_->ToJson()); return result; } std::unique_ptr SetInstrumentationBreakpointReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("breakpointId", breakpointId_.c_str()); return result; } std::unique_ptr SetScriptSourceReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); if (callFrames_) { std::unique_ptr array = PtJson::CreateArray(); size_t len = callFrames_->size(); for (size_t i = 0; i < len; i++) { ASSERT(callFrames_.value()[i] != nullptr); std::unique_ptr location = callFrames_.value()[i]->ToJson(); array->Push(location); } result->Add("callFrames", array); } if (stackChanged_) { result->Add("stackChanged", stackChanged_.value()); } if (exceptionDetails_) { ASSERT(exceptionDetails_.value() != nullptr); result->Add("exceptionDetails", exceptionDetails_.value()->ToJson()); } return result; } std::unique_ptr GetPropertiesReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = result_.size(); for (size_t i = 0; i < len; i++) { ASSERT(result_[i] != nullptr); std::unique_ptr location = result_[i]->ToJson(); array->Push(location); } result->Add("result", array); if (internalPropertyDescripties_) { array = PtJson::CreateArray(); len = internalPropertyDescripties_->size(); for (size_t i = 0; i < len; i++) { ASSERT(internalPropertyDescripties_.value()[i] != nullptr); std::unique_ptr location = internalPropertyDescripties_.value()[i]->ToJson(); array->Push(location); } result->Add("internalProperties", array); } if (privateProperties_) { array = PtJson::CreateArray(); len = privateProperties_->size(); for (size_t i = 0; i < len; i++) { ASSERT(privateProperties_.value()[i] != nullptr); std::unique_ptr location = privateProperties_.value()[i]->ToJson(); array->Push(location); } result->Add("privateProperties", array); } if (exceptionDetails_) { ASSERT(exceptionDetails_.value() != nullptr); result->Add("exceptionDetails", exceptionDetails_.value()->ToJson()); } return result; } std::unique_ptr CallFunctionOnReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); ASSERT(result_ != nullptr); result->Add("result", result_->ToJson()); if (exceptionDetails_) { ASSERT(exceptionDetails_.value() != nullptr); result->Add("exceptionDetails", exceptionDetails_.value()->ToJson()); } return result; } std::unique_ptr StopSamplingReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); ASSERT(profile_ != nullptr); result->Add("profile", profile_->ToJson()); return result; } std::unique_ptr GetHeapObjectIdReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("heapSnapshotObjectId", std::to_string(heapSnapshotObjectId_).c_str()); return result; } std::unique_ptr GetObjectByHeapObjectIdReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); ASSERT(remoteObjectResult_ != nullptr); result->Add("result", remoteObjectResult_->ToJson()); return result; } std::unique_ptr StopReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); ASSERT(profile_ != nullptr); result->Add("profile", profile_->ToJson()); return result; } std::unique_ptr GetHeapUsageReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("usedSize", usedSize_); result->Add("totalSize", totalSize_); return result; } std::unique_ptr GetHeapUsageReturns::Create(const PtJson ¶ms) { auto heapUsageReturns = std::make_unique(); std::string error; Result ret; double usedSize; ret = params.GetDouble("usedSize", &usedSize); if (ret == Result::SUCCESS) { heapUsageReturns->usedSize_ = usedSize; } else { error += "Unknown 'usedSize';"; } double totalSize; ret = params.GetDouble("totalSize", &totalSize); if (ret == Result::SUCCESS) { heapUsageReturns->totalSize_ = totalSize; } else { error += "Unknown 'totalSize';"; } if (!error.empty()) { LOG_DEBUGGER(ERROR) << "HeapUsageReturns::Create " << error; return nullptr; } return heapUsageReturns; } std::unique_ptr GetBestEffortCoverageReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = result_.size(); for (size_t i = 0; i < len; i++) { ASSERT(result_[i] != nullptr); std::unique_ptr scriptCoverage = result_[i]->ToJson(); array->Push(scriptCoverage); } result->Add("result", array); return result; } std::unique_ptr StartPreciseCoverageReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("timestamp", timestamp_); return result; } std::unique_ptr TakePreciseCoverageReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = result_.size(); for (size_t i = 0; i < len; i++) { ASSERT(result_[i] != nullptr); std::unique_ptr scriptTypeProfile = result_[i]->ToJson(); array->Push(scriptTypeProfile); } result->Add("result", array); result->Add("timestamp", timestamp_); return result; } std::unique_ptr TakeTypeProfileReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); size_t len = result_.size(); for (size_t i = 0; i < len; i++) { ASSERT(result_[i] != nullptr); std::unique_ptr scriptTypeProfile = result_[i]->ToJson(); array->Push(scriptTypeProfile); } result->Add("result", array); return result; } std::unique_ptr GetCategoriesReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr categories = PtJson::CreateArray(); size_t len = categories_.size(); for (size_t i = 0; i < len; i++) { categories->Push(categories_[i].c_str()); } result->Add("categories", categories); return result; } std::unique_ptr RequestMemoryDumpReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); result->Add("dumpGuid", dumpGuid_.c_str()); result->Add("success", success_); return result; } std::unique_ptr GetNavigationHistoryReturns::ToJson() const { std::unique_ptr result = PtJson::CreateObject(); std::unique_ptr array = PtJson::CreateArray(); result->Add("currentIndex", 0); array->Push(PtJson::CreateObject()); result->Add("entries", array); return result; } } // namespace panda::ecmascript::tooling