diff --git a/ecmascript/ecma_module.cpp b/ecmascript/ecma_module.cpp index 3dc94b52..63d6f4e2 100644 --- a/ecmascript/ecma_module.cpp +++ b/ecmascript/ecma_module.cpp @@ -137,6 +137,29 @@ JSHandle ModuleManager::GetModule(const JSThread *thread, return thread->GlobalConstants()->GetHandledUndefined(); } +CString ModuleManager::GenerateModuleFullPath(const std::string ¤tPathFile, 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(); diff --git a/ecmascript/ecma_module.h b/ecmascript/ecma_module.h index 707724d7..c2a34974 100644 --- a/ecmascript/ecma_module.h +++ b/ecmascript/ecma_module.h @@ -79,6 +79,8 @@ public: JSHandle GetModule(const JSThread *thread, JSHandle moduleName); + CString GenerateModuleFullPath(const std::string ¤tPathFile, const CString &relativeFile); + const CString &GetCurrentExportModuleName(); const CString &GetPrevExportModuleName(); diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index 569edd1c..711f2601 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -692,26 +692,13 @@ void EcmaVM::SetMicroJobQueue(job::MicroJobQueue *queue) JSHandle EcmaVM::GetModuleByName(JSHandle 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 abcModuleName = factory_->NewFromString(abcPath);