• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetPatchExtractors(const std::string & url) const55 std::vector<DebugInfoExtractor *> HotReloadManager::GetPatchExtractors(const std::string &url) const
56 {
57     std::vector<DebugInfoExtractor *> extractors;
58     auto iter = patchExtractors_.find(url);
59     if (iter == patchExtractors_.end()) {
60         return extractors;
61     }
62     return iter->second;
63 }
64 
ExtractPatch(const JSPandaFile * jsPandaFile)65 void HotReloadManager::ExtractPatch(const JSPandaFile *jsPandaFile)
66 {
67     auto *patchExtractor = JSPandaFileManager::GetInstance()->GetJSPtExtractor(jsPandaFile);
68     if (patchExtractor == nullptr) {
69         LOG_DEBUGGER(ERROR) << "ExtractPatch: patch extractor is nullptr";
70         return;
71     }
72 
73     const auto &recordInfos = jsPandaFile->GetJSRecordInfo();
74     for (const auto &[recordName, _] : recordInfos) {
75         auto mainMethodIndex = panda_file::File::EntityId(jsPandaFile->GetMainMethodIndex(recordName));
76         auto *notificationMgr = vm_->GetJsDebuggerManager()->GetNotificationManager();
77         const std::string &url = patchExtractor->GetSourceFile(mainMethodIndex);
78         if (url.empty()) {
79             continue;
80         }
81         notificationMgr->LoadModuleEvent(jsPandaFile->GetJSPandaFileDesc(), recordName);
82         patchExtractors_[url].emplace_back(patchExtractor);
83     }
84 }
85 }  // namespace panda::ecmascript::tooling
86