1 /*
2 * Copyright (c) 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 #include "ecmascript/debugger/hot_reload_manager.h"
17 #include "ecmascript/debugger/js_debugger_manager.h"
18 #include "ecmascript/jspandafile/js_pandafile_manager.h"
19
20 namespace panda::ecmascript::tooling {
21
NotifyPatchLoaded(const JSPandaFile * baseFile,const JSPandaFile * patchFile)22 void HotReloadManager::NotifyPatchLoaded(const JSPandaFile *baseFile, const JSPandaFile *patchFile)
23 {
24 if (vm_->GetJsDebuggerManager()->GetDebuggerHandler() == nullptr) {
25 return;
26 }
27 LOG_DEBUGGER(DEBUG) << "HotReloadManager::NotifyPatchLoaded";
28 baseJSPandaFiles_[patchFile] = baseFile;
29
30 ExtractPatch(patchFile);
31 vm_->GetJsDebuggerManager()->ClearSingleStepper();
32 }
33
NotifyPatchUnloaded(const JSPandaFile * patchFile)34 void HotReloadManager::NotifyPatchUnloaded(const JSPandaFile *patchFile)
35 {
36 if (vm_->GetJsDebuggerManager()->GetDebuggerHandler() == nullptr) {
37 return;
38 }
39 LOG_DEBUGGER(DEBUG) << "HotReloadManager::NotifyPatchUnloaded";
40 baseJSPandaFiles_.erase(patchFile);
41 patchExtractors_.clear();
42 vm_->GetJsDebuggerManager()->ClearSingleStepper();
43 }
44
GetBaseJSPandaFile(const JSPandaFile * jsPandaFile) const45 const JSPandaFile *HotReloadManager::GetBaseJSPandaFile(const JSPandaFile *jsPandaFile) const
46 {
47 auto iter = baseJSPandaFiles_.find(jsPandaFile);
48 if (iter == baseJSPandaFiles_.end()) {
49 // if the file is non-patch, the base file is itself
50 return jsPandaFile;
51 }
52 return iter->second;
53 }
54
GetPatchExtractor(const std::string & url) const55 DebugInfoExtractor *HotReloadManager::GetPatchExtractor(const std::string &url) const
56 {
57 auto iter = patchExtractors_.find(url);
58 if (iter == patchExtractors_.end()) {
59 return nullptr;
60 }
61 return iter->second;
62 }
63
ExtractPatch(const JSPandaFile * jsPandaFile)64 void HotReloadManager::ExtractPatch(const JSPandaFile *jsPandaFile)
65 {
66 auto *patchExtractor = JSPandaFileManager::GetInstance()->GetJSPtExtractor(jsPandaFile);
67 if (patchExtractor == nullptr) {
68 LOG_DEBUGGER(ERROR) << "ExtractPatch: patch extractor is nullptr";
69 return;
70 }
71
72 const auto &recordInfos = jsPandaFile->GetJSRecordInfo();
73 for (const auto &[recordName, _] : recordInfos) {
74 auto mainMethodIndex = panda_file::File::EntityId(jsPandaFile->GetMainMethodIndex(recordName));
75 auto *notificationMgr = vm_->GetJsDebuggerManager()->GetNotificationManager();
76 const std::string &url = patchExtractor->GetSourceFile(mainMethodIndex);
77 if (url.empty()) {
78 continue;
79 }
80 notificationMgr->LoadModuleEvent(jsPandaFile->GetJSPandaFileDesc(), recordName);
81 patchExtractors_[url] = patchExtractor;
82 }
83 }
84 } // namespace panda::ecmascript::tooling
85