Reconstruct code for readability.

Signed-off-by: lichenshuai <lichenshuai@huawei.com>
This commit is contained in:
lichenshuai
2021-10-19 14:51:14 +08:00
parent 9c05d45a12
commit c6b37545d7
3 changed files with 31 additions and 19 deletions
+23
View File
@@ -137,6 +137,29 @@ JSHandle<JSTaggedValue> ModuleManager::GetModule(const JSThread *thread,
return thread->GlobalConstants()->GetHandledUndefined();
}
CString ModuleManager::GenerateModuleFullPath(const std::string &currentPathFile, const CString &relativeFile)
{
if (relativeFile.find("./") != 0 && relativeFile.find("../") != 0) { // not start with "./" or "../"
return relativeFile; // not relative
}
auto slashPos = currentPathFile.find_last_of('/');
if (slashPos == std::string::npos) {
return relativeFile; // no need to process
}
auto dotPos = relativeFile.find_last_of(".");
if (dotPos == std::string::npos) {
dotPos = 0;
}
CString fullPath;
fullPath.append(currentPathFile.substr(0, slashPos + 1)); // 1: with "/"
fullPath.append(relativeFile.substr(0, dotPos));
fullPath.append(".abc"); // ".js" -> ".abc"
return fullPath;
}
const CString &ModuleManager::GetCurrentExportModuleName()
{
return moduleStack_.GetTop();
+2
View File
@@ -79,6 +79,8 @@ public:
JSHandle<JSTaggedValue> GetModule(const JSThread *thread, JSHandle<JSTaggedValue> moduleName);
CString GenerateModuleFullPath(const std::string &currentPathFile, const CString &relativeFile);
const CString &GetCurrentExportModuleName();
const CString &GetPrevExportModuleName();
+6 -19
View File
@@ -692,26 +692,13 @@ void EcmaVM::SetMicroJobQueue(job::MicroJobQueue *queue)
JSHandle<JSTaggedValue> EcmaVM::GetModuleByName(JSHandle<JSTaggedValue> moduleName)
{
CString dirPath;
CString scriptName = ConvertToString(EcmaString::Cast(moduleName->GetTaggedObject()));
auto currentFileTuple = pandaFileWithProgram_.back();
auto currentFileInfo = std::get<1>(currentFileTuple);
std::string currentPathFile = currentFileInfo->GetFilename();
CString relativeFile = ConvertToString(EcmaString::Cast(moduleName->GetTaggedObject()));
// need to check abc file
auto pos = scriptName.find_last_of('.');
CString abcPath = dirPath.append(scriptName.substr(0, pos == std::string::npos ? 0 : pos)).append(".abc");
// handle relative path
if (abcPath.find("./") == 0) { // starts with "./"
std::string fullPath = std::get<1>(pandaFileWithProgram_.back())->GetFilename();
auto lastSlash = fullPath.find_last_of('/');
if (lastSlash != std::string::npos) {
abcPath = fullPath.substr(0, lastSlash).append(abcPath.substr(1)); // 1: ignore "."
}
} else if (abcPath.find("../") == 0) { // starts with "../"
std::string fullPath = std::get<1>(pandaFileWithProgram_.back())->GetFilename();
auto lastSlash = fullPath.find_last_of('/');
if (lastSlash != std::string::npos) {
abcPath = fullPath.substr(0, lastSlash + 1).append(abcPath); // 1: with "/"
}
}
// generate full path
CString abcPath = moduleManager_->GenerateModuleFullPath(currentPathFile, relativeFile);
// Uniform module name
JSHandle<EcmaString> abcModuleName = factory_->NewFromString(abcPath);