mirror of
https://github.com/openharmony/ark_js_runtime.git
synced 2026-07-21 15:15:22 -04:00
Reconstruct code for readability.
Signed-off-by: lichenshuai <lichenshuai@huawei.com>
This commit is contained in:
@@ -137,6 +137,29 @@ JSHandle<JSTaggedValue> 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();
|
||||
|
||||
@@ -79,6 +79,8 @@ public:
|
||||
|
||||
JSHandle<JSTaggedValue> GetModule(const JSThread *thread, JSHandle<JSTaggedValue> moduleName);
|
||||
|
||||
CString GenerateModuleFullPath(const std::string ¤tPathFile, const CString &relativeFile);
|
||||
|
||||
const CString &GetCurrentExportModuleName();
|
||||
|
||||
const CString &GetPrevExportModuleName();
|
||||
|
||||
+6
-19
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user