diff --git a/CMakeLists.txt b/CMakeLists.txt index 6feeeaed18a5bc4bb167a18a3680ec97beadd0db..dd2020f884773893d7f8c6277c2af37a2eb71cb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,12 +20,19 @@ set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(GENERATED_DIR ${OUTPUT_DIR}/generated) set(GENERATED_STAMP ${OUTPUT_DIR}/gen_dir.stamp) file(MAKE_DIRECTORY "${GENERATED_DIR}") + +if(PANDA_TARGET_WINDOWS) + set(DELIM "\\\\") +else() + set(DELIM "/") +endif() + file(WRITE "${GENERATED_DIR}/arktsconfig.json" "{\n" " \"baseUrl\": \"${PANDA_ROOT}\",\n" " \"paths\": {\n" - " \"std\": \"${PANDA_ROOT}/plugins/ets/stdlib/std\",\n" - " \"import_tests\": \"${CMAKE_CURRENT_SOURCE_DIR}/test/parser/ets/import_tests\"\n" + " \"std\": \"${PANDA_ROOT}${DELIM}plugins${DELIM}ets${DELIM}stdlib${DELIM}std\",\n" + " \"import_tests\": \"${CMAKE_CURRENT_SOURCE_DIR}${DELIM}test${DELIM}parser${DELIM}ets${DELIM}import_tests\"\n" " }\n" "}\n" ) diff --git a/aot/options.h b/aot/options.h index 923ce3fff92d3bf6990c6840c5d1b20302ebfa28..9115a7ab30f56066ffa60fd041e5ea638c24360e 100644 --- a/aot/options.h +++ b/aot/options.h @@ -16,6 +16,7 @@ #ifndef ES2PANDA_AOT_OPTIONS_H #define ES2PANDA_AOT_OPTIONS_H +#include "libpandabase/os/file.h" #include "plugins/ecmascript/es2panda/es2panda.h" #include @@ -50,9 +51,9 @@ inline OptionFlags &operator|=(OptionFlags &a, OptionFlags b) } template -T BaseName(T const &path, T const &delims = "/") +T BaseName(T const &path) { - return path.substr(path.find_last_of(delims) + 1); + return path.substr(path.find_last_of(panda::os::file::File::GetPathDelim()) + 1); } class Options { diff --git a/es2panda.cpp b/es2panda.cpp index 461f4880085a45f54be5c92e2ea5e2a8b91f002c..fd0d42707069022d9fa9e7b2e995325d59dd6c29 100644 --- a/es2panda.cpp +++ b/es2panda.cpp @@ -24,7 +24,7 @@ namespace panda::es2panda { constexpr size_t DEFAULT_THREAD_COUNT = 2; template -T DirName(T const &path, T const &delims = "/") +T DirName(T const &path, T const &delims = panda::os::file::File::GetPathDelim()) { std::size_t pos = path.find_last_of(delims); diff --git a/parser/ETSparser.cpp b/parser/ETSparser.cpp index 4883cecbd1356a00b7d214813fee2fd77e07d837..1457d90522827abafb00a254b5829d1f23083467 100644 --- a/parser/ETSparser.cpp +++ b/parser/ETSparser.cpp @@ -236,15 +236,16 @@ void ETSParser::CollectExternalSourcesAndFiles(Program::ExternalSource &extSourc util::StringView ETSParser::CreateAbsolutePath(const util::StringView &source) { - const auto wholeNameLit = CreateSubPath(source, "."); - const auto subName = util::Helpers::RemovePreFix(wholeNameLit.Mutf8(), ".", false); + const auto wholeNameLit = CreateSubPath(source, '.'); + const auto subName = util::Helpers::RemovePreFix(wholeNameLit.Mutf8(), '.', false); util::UString fileName(Allocator()); fileName.Append(subName); GetProgram()->SetFileName(fileName.View()); - const util::StringView subPath = CreateSubPath(source, "/"); - auto sub = util::Helpers::RemovePreFix(subPath.Mutf8(), ".", false); + char pathDelimiter = panda::os::file::File::GetPathDelim().at(0); + const util::StringView subPath = CreateSubPath(source, pathDelimiter); + auto sub = util::Helpers::RemovePreFix(subPath.Mutf8(), '.', false); bool inInternal = (GetContext().Status() & ParserStatus::IN_EXTERNAL) == 0; if (inInternal) { @@ -262,7 +263,7 @@ util::StringView ETSParser::CreateAbsolutePath(const util::StringView &source) return util::Helpers::CreateImportPath(Allocator(), absoluteLit); #else - if (source.Mutf8().find("/") == 0 || inInternal) { + if (source.Mutf8().find(pathDelimiter) == 0 || inInternal) { return util::Helpers::CreateImportPath(Allocator(), sub); } @@ -275,15 +276,15 @@ util::StringView ETSParser::CreateAbsolutePath(const util::StringView &source) ASSERT(util::Helpers::IsRelativePath(source.Mutf8())); std::string abspath = cwd; - abspath.append("/"); - abspath.append(util::Helpers::RemovePreFix(source.Mutf8(), "/", true)); - abspath = abspath.substr(0, abspath.find_last_of("\\/")).c_str(); + abspath.append(pathDelimiter); + abspath.append(util::Helpers::RemovePreFix(source.Mutf8(), pathDelimiter, true)); + abspath = abspath.substr(0, abspath.find_last_of(pathDelimiter)).c_str(); return util::Helpers::CreateImportPath(Allocator(), abspath); #endif } -util::StringView ETSParser::CreateSubPath(const util::StringView &source, const char *delimiter) +util::StringView ETSParser::CreateSubPath(const util::StringView &source, const char delimiter) { auto subName = util::Helpers::RemovePostFix(source.Mutf8(), delimiter); return util::Helpers::CreateImportPath(Allocator(), subName); @@ -375,14 +376,16 @@ std::vector ETSParser::CollectExternalSources() std::string ETSParser::ResolveImportPath(const std::string &path) { + char pathDelimiter = panda::os::file::File::GetPathDelim().at(0); + if (util::Helpers::IsRelativePath(path)) { - return GetProgram()->SourceFilePath().Mutf8() + "/" + path; + return GetProgram()->SourceFilePath().Mutf8() + pathDelimiter + path; } const std::string *baseUrl = nullptr; std::string resolvedPath; - // Resolve slash character to basePath. - if (path.find(lexer::LEX_CHAR_SLASH) == 0) { + // Resolve delimiter character to basePath. + if (path.find('/') == 0) { baseUrl = arkTsConfig_->GetValue("baseUrl"); if (baseUrl == nullptr) { ThrowSyntaxError({"'baseUrl' is not defined in ", ArkTSConfig()}); @@ -400,9 +403,9 @@ std::string ETSParser::ResolveImportPath(const std::string &path) ThrowSyntaxError({"'paths' is not defined in ", ArkTSConfig()}); } - std::string::size_type pos = path.find(lexer::LEX_CHAR_SLASH); - bool containsSlash = (pos != std::string::npos); - std::string rootPart = containsSlash ? path.substr(0, pos) : path; + std::string::size_type pos = path.find('/'); + bool containsDelim = (pos != std::string::npos); + std::string rootPart = containsDelim ? path.substr(0, pos) : path; baseUrl = paths->get()->GetValue(rootPart); if (baseUrl == nullptr) { @@ -410,8 +413,9 @@ std::string ETSParser::ResolveImportPath(const std::string &path) } resolvedPath = *baseUrl; - if (containsSlash) { - resolvedPath.append(path, rootPart.length(), path.length()); + if (containsDelim) { + resolvedPath.append(1, pathDelimiter); + resolvedPath.append(path, rootPart.length() + 1, path.length()); } return resolvedPath; diff --git a/parser/ETSparser.h b/parser/ETSparser.h index b30f91461f392871c29fd79c5caaaafb7595c542..b3adbe044a4593e2715d0721ff00c32fe1b19949 100644 --- a/parser/ETSparser.h +++ b/parser/ETSparser.h @@ -71,7 +71,7 @@ private: ArenaVector PrepareExternalGlobalClass(const SourceFile &sourceFile); void CollectExternalSourcesAndFiles(Program::ExternalSource &extSources, const util::StringView &name); util::StringView CreateAbsolutePath(const util::StringView &source); - util::StringView CreateSubPath(const util::StringView &source, const char *delimiter); + util::StringView CreateSubPath(const util::StringView &source, const char delimiter); void ParseETSGlobalScript(lexer::SourcePosition startLoc, ArenaVector &statements, const std::vector &externalSources); void AddGlobalDeclaration(ir::AstNode *node); diff --git a/util/helpers.cpp b/util/helpers.cpp index 94e383f902a9ddeaf23e73c49b501b586688ef8e..311a30c08a5a97bd3322e79ee937d39a661a4e71 100644 --- a/util/helpers.cpp +++ b/util/helpers.cpp @@ -151,26 +151,35 @@ util::StringView Helpers::ToStringView(ArenaAllocator *allocator, int32_t number bool Helpers::IsRelativePath(const std::string &path) { - return ((path.find("./") == 0) || (path.find("../") == 0)); + auto pathDelimiter = panda::os::file::File::GetPathDelim(); + + std::string currentDirReference = "."; + std::string parentDirReference = ".."; + + currentDirReference.append(pathDelimiter); + parentDirReference.append(pathDelimiter); + + return ((path.find(currentDirReference) == 0) || (path.find(parentDirReference) == 0)); } util::StringView Helpers::CreateImportPath(ArenaAllocator *allocator, std::string &path) { util::UString packageName(allocator); - std::replace(path.begin(), path.end(), '/', '.'); + std::replace(path.begin(), path.end(), panda::os::file::File::GetPathDelim().at(0), '.'); - packageName.Append(RemovePreFix(path, ".", false)); + packageName.Append(RemovePreFix(path, '.', false)); return packageName.View(); } -std::string Helpers::RemovePreFix(const std::string &path, const char *delimiter, bool isRelative) +std::string Helpers::RemovePreFix(const std::string &path, const char delimiter, bool isRelative) { - auto idx = isRelative ? path.find_first_of('/') + 1 : path.find_first_not_of(delimiter); + auto idx = isRelative ? path.find_first_of(panda::os::file::File::GetPathDelim().at(0)) + 1 + : path.find_first_not_of(delimiter); return path.substr(idx, path.size()); } -std::string Helpers::RemovePostFix(const std::string &path, const char *delimiter) +std::string Helpers::RemovePostFix(const std::string &path, const char delimiter) { auto idx = path.find_last_of(delimiter); return path.substr(0, idx); diff --git a/util/helpers.h b/util/helpers.h index bbcf7dbfa84b8ade6c316e6f5c76ea2bd5896150..5a01aa078b69f5f738307a52797acdafe77aac1a 100644 --- a/util/helpers.h +++ b/util/helpers.h @@ -63,8 +63,8 @@ public: static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number); static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number); static util::StringView CreateImportPath(ArenaAllocator *allocator, std::string &path); - static std::string RemovePreFix(const std::string &path, const char *delimiter, bool isRelative); - static std::string RemovePostFix(const std::string &path, const char *delimiter); + static std::string RemovePreFix(const std::string &path, const char delimiter, bool isRelative); + static std::string RemovePostFix(const std::string &path, const char delimiter); static bool IsRelativePath(const std::string &path); static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node);