From 1700853459ac585c1152ca42414f3f164837c558 Mon Sep 17 00:00:00 2001 From: liyue Date: Sun, 3 Aug 2025 21:03:54 +0800 Subject: [PATCH] Adjust the version number in the bytecode Signed-off-by: liyue Change-Id: I4d90f4a5d4ef8d4c87d1b6154ad14ce47dd686c8 --- es2panda/aot/options.cpp | 54 +++++++++++++++++++++++++ es2panda/aot/options.h | 1 + es2panda/compiler/core/compileQueue.cpp | 32 +++++++++++++++ es2panda/compiler/core/compileQueue.h | 1 + es2panda/es2panda.cpp | 3 ++ es2panda/es2panda.h | 2 + es2panda/util/helpers.h | 6 +++ 7 files changed, 99 insertions(+) diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index ccaa1d2a9a..5db93d895e 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -273,6 +273,53 @@ void Options::ParseUpdateVersionInfo(nlohmann::json &compileContextInfoJson) } } +void Options::ParseAbcCompileContextInfo(const std::string compileAbcContextInfoPath) +{ + std::stringstream ss; + std::string buffer; + if (!util::Helpers::ReadFileToBuffer(compileAbcContextInfoPath, ss)) { + return; + } + + buffer = ss.str(); + if (buffer.empty() || !nlohmann::json::accept(buffer)) { + std::cerr << "The input file '" << compileAbcContextInfoPath <<"' is incomplete format of json" << std::endl; + return; + } + // Parser compile context info base on the input json file. + nlohmann::json compileContextInfoJson = nlohmann::json::parse(buffer); + + if (compileContextInfoJson.contains("packageName") && + compileContextInfoJson["packageName"].is_string()) { + compilerOptions_.compileAbcContextInfo.packageName = compileContextInfoJson["packageName"]; + } + if (compileContextInfoJson.contains("version") && + compileContextInfoJson["version"].is_string()) { + compilerOptions_.compileAbcContextInfo.version = compileContextInfoJson["version"]; + } + if (compileContextInfoJson.contains("updateVersionInfo") && + compileContextInfoJson["updateVersionInfo"].is_object()) { + std::unordered_map> updateVersionInfo {}; + std::map pkgContextMap {}; + + for (const auto& [pkgName, version] : compileContextInfoJson["updateVersionInfo"].items()) { + PkgInfo pkgInfo; + pkgInfo.version = version; + pkgInfo.packageName = pkgName; + pkgContextMap[pkgName] = pkgInfo; + } + updateVersionInfo[compilerOptions_.compileAbcContextInfo.packageName] = pkgContextMap; + compilerOptions_.compileAbcContextInfo.updateVersionInfo = updateVersionInfo; + } + + std::cerr << "[DEBUG] Parsed packageName = " << compilerOptions_.compileAbcContextInfo.packageName << std::endl; + std::cerr << "[DEBUG] updateVersionInfo contains keys: "; + for (const auto& [key, _] : compilerOptions_.compileAbcContextInfo.updateVersionInfo) { + std::cerr << key << " "; + } + std::cerr << std::endl; +} + void Options::ParseCompileContextInfo(const std::string compileContextInfoPath) { std::stringstream ss; @@ -434,6 +481,8 @@ bool Options::Parse(int argc, const char **argv) // compile entries and pkg context info panda::PandArg compileContextInfoPath("compile-context-info", "", "The path to compile context"\ "info file"); + panda::PandArg compileAbcContextInfoPath("compile-abc-context-info", "", "The path to compile abc"\ + "context info file"); panda::PandArg opDumpDepsInfo("dump-deps-info", false, "Dump all dependency files and records "\ "including source files and bytecode files"); panda::PandArg opRemoveRedundantFile("remove-redundant-file", false, "Remove redundant info"\ @@ -510,6 +559,7 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&enableAnnotations); argparser_->Add(&compileContextInfoPath); + argparser_->Add(&compileAbcContextInfoPath); argparser_->Add(&opDumpDepsInfo); argparser_->Add(&opRemoveRedundantFile); argparser_->Add(&opDumpString); @@ -712,6 +762,10 @@ bool Options::Parse(int argc, const char **argv) if (!compileContextInfoPath.GetValue().empty()) { ParseCompileContextInfo(compileContextInfoPath.GetValue()); } + compilerOptions_.compileAbcContextInfoPath = compileAbcContextInfoPath.GetValue(); + if (!compileAbcContextInfoPath.GetValue().empty()) { + ParseAbcCompileContextInfo(compileAbcContextInfoPath.GetValue()); + } compilerOptions_.dumpDepsInfo = opDumpDepsInfo.GetValue(); compilerOptions_.updatePkgVersionForAbcInput = compilerOptions_.enableAbcInput && (!compilerOptions_.compileContextInfo.pkgContextInfo.empty() || diff --git a/es2panda/aot/options.h b/es2panda/aot/options.h index 6f2291732c..d729fda1ad 100644 --- a/es2panda/aot/options.h +++ b/es2panda/aot/options.h @@ -149,6 +149,7 @@ public: bool CollectInputFilesFromFileDirectory(const std::string &input, const std::string &extension); void ParseCacheFileOption(const std::string &cacheInput); void ParseCompileContextInfo(const std::string compileContextInfoPath); + void ParseAbcCompileContextInfo(const std::string compileAbcContextInfoPath); bool NeedCollectDepsRelation(); bool NeedRemoveRedundantRecord(); diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index 5abe071217..8b85ffcdde 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -231,6 +231,18 @@ void CompileAbcClassJob::Run() if (!options_.modifiedPkgName.empty()) { UpdatePkgNameOfImportOhmurl(program, options_); } + + if (!options_.compileAbcContextInfoPath.empty()) { + UpdateAbcImportOhmurl(program, options_); + if (hasOhmurlBeenChanged_) { + program->strings.clear(); + for (const auto &[_, function] : program->function_table) { + const auto &funcStringSet = function.CollectStringsFromFunctionInsns(); + program->strings.insert(funcStringSet.begin(), funcStringSet.end()); + } + } + } + // Update ohmurl for abc input when needed if (options_.compileContextInfo.needModifyRecord || (options_.updatePkgVersionForAbcInput && pkgVersionUpdateRequiredInAbc_)) { @@ -326,6 +338,26 @@ void CompileAbcClassJob::UpdateImportOhmurl(panda::pandasm::Program *prog, // Replace for dynamic import UpdateDynamicImport(prog, pkgContextInfo); } + +void CompileAbcClassJob::UpdateAbcImportOhmurl(panda::pandasm::Program *prog, + const panda::es2panda::CompilerOptions &options) +{ + const auto &updateVersionInfo = options.compileAbcContextInfo.updateVersionInfo; + const std::string &pkgName = options.compileAbcContextInfo.packageName; + + auto it = updateVersionInfo.find(pkgName); + if (it == updateVersionInfo.end()) { + std::cerr << "Error: package name not found in updateVersionInfo: " << pkgName << std::endl; + return; + } + + const auto &pkgContextInfo = it->second; + // Replace for esm module static import + UpdateStaticImport(prog, pkgContextInfo); + // Replace for dynamic import + UpdateDynamicImport(prog, pkgContextInfo); +} + /** * Need to modify the package name of the original package to the package name of the target package when * you merging two packages. diff --git a/es2panda/compiler/core/compileQueue.h b/es2panda/compiler/core/compileQueue.h index d0bd89cce4..2f99cc0f61 100644 --- a/es2panda/compiler/core/compileQueue.h +++ b/es2panda/compiler/core/compileQueue.h @@ -122,6 +122,7 @@ public: hasOhmurlBeenChanged_ = hasOhmurlBeenChanged; } void UpdatePkgNameOfImportOhmurl(panda::pandasm::Program *prog, const panda::es2panda::CompilerOptions &options); + void UpdateAbcImportOhmurl(panda::pandasm::Program *prog, const panda::es2panda::CompilerOptions &options); NO_COPY_SEMANTIC(CompileAbcClassJob); NO_MOVE_SEMANTIC(CompileAbcClassJob); diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index ddd62b5d9e..0c07a59a66 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -111,6 +111,9 @@ void Compiler::CompileAbcFileInParallel(SourceFile *src, const CompilerOptions & if (!options.modifiedPkgName.empty()) { abcToAsmCompiler_->SetModifyPkgName(options.modifiedPkgName); } + if (!options.compileAbcContextInfoPath.empty()) { + abcToAsmCompiler_->SetFixVersion(options.compileAbcContextInfo.version); + } auto *compileAbcClassQueue = new compiler::CompileAbcClassQueue(options.abcClassThreadCount, options, diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index 1715ae289b..713f5966d1 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -117,6 +117,8 @@ struct CompilerOptions { bool requireGlobalOptimization {false}; std::string compileContextInfoPath {}; CompileContextInfo compileContextInfo {}; + std::string compileAbcContextInfoPath {}; + CompileAbcContextInfo compileAbcContextInfo {}; bool dumpDepsInfo {false}; bool updatePkgVersionForAbcInput {false}; bool removeRedundantFile {false}; diff --git a/es2panda/util/helpers.h b/es2panda/util/helpers.h index 67b31ec16d..a6f9cffa31 100644 --- a/es2panda/util/helpers.h +++ b/es2panda/util/helpers.h @@ -61,6 +61,12 @@ struct CompileContextInfo { bool needModifyRecord {false}; std::string bundleName {}; }; + +struct CompileAbcContextInfo { + std::string packageName {}; + std::string version {}; + std::unordered_map> updateVersionInfo {}; +}; } // namespace panda::es2panda namespace panda::es2panda::binder { -- Gitee