diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 6b591b2694baa992f8637dd8d890e6de1a201c07..e8d2e3ead687fb1d89491ce5bb3adfc151e7914b 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -66,7 +66,7 @@ bool Options::CollectInputFilesFromFileList(const std::string &input) return false; } - constexpr size_t ITEM_COUNT_MERGE = 4; // item list: [filePath; recordName; moduleKind; sourceFile] + constexpr size_t ITEM_COUNT_MERGE = 5; // item list: [filePath; recordName; moduleKind; sourceFile, pkgName] constexpr size_t ITEM_COUNT_NOT_MERGE = 5; // item list: [filePath; recordName; moduleKind; sourceFile; outputfile] while (std::getline(ifs, line)) { const std::string seperator = ";"; @@ -90,6 +90,10 @@ bool Options::CollectInputFilesFromFileList(const std::string &input) es2panda::SourceFile src(fileName, recordName, scriptKind); src.sourcefile = itemList[3]; + if (compilerOptions_.mergeAbc) { + src.pkgName = itemList[4]; + } + sourceFiles_.push_back(src); if (!compilerOptions_.mergeAbc) { outputFiles_.insert({fileName, itemList[4]}); diff --git a/es2panda/compiler/core/compilerContext.cpp b/es2panda/compiler/core/compilerContext.cpp index 96ea7c675c23d714314d17666017927001568ab0..5120ab97adb30453df23329cb44c306587841a93 100644 --- a/es2panda/compiler/core/compilerContext.cpp +++ b/es2panda/compiler/core/compilerContext.cpp @@ -22,10 +22,10 @@ namespace panda::es2panda::compiler { CompilerContext::CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, bool isMergeAbc, bool isTypeExtractorEnabled, std::string sourceFile, - util::StringView recordName) + std::string pkgName, util::StringView recordName) : binder_(binder), isDebug_(isDebug), isDebuggerEvaluateExpressionMode_(isDebuggerEvaluateExpressionMode), isMergeAbc_(isMergeAbc), isTypeExtractorEnabled_(isTypeExtractorEnabled), sourceFile_(sourceFile), - emitter_(std::make_unique(this)), recordName_(recordName) + pkgName_(pkgName), emitter_(std::make_unique(this)), recordName_(recordName) { } diff --git a/es2panda/compiler/core/compilerContext.h b/es2panda/compiler/core/compilerContext.h index b43cbcadcdea1d5079cec290fd84fb7e098a45ae..4332c9a1b20033c8dd37fc8af6dbb1e3c3c575a1 100644 --- a/es2panda/compiler/core/compilerContext.h +++ b/es2panda/compiler/core/compilerContext.h @@ -43,7 +43,8 @@ class Emitter; class CompilerContext { public: CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, bool isTypeExtractorEnabled, std::string sourceFile, util::StringView recordName); + bool isMergeAbc, bool isTypeExtractorEnabled, std::string sourceFile, + std::string pkgName, util::StringView recordName); NO_COPY_SEMANTIC(CompilerContext); NO_MOVE_SEMANTIC(CompilerContext); ~CompilerContext() = default; @@ -94,6 +95,11 @@ public: return sourceFile_; } + std::string PkgName() const + { + return pkgName_; + } + void AddHotfixHelper(util::Hotfix *hotfixHelper) { hotfixHelper_ = hotfixHelper; @@ -132,6 +138,7 @@ private: bool isTypeExtractorEnabled_; extractor::TypeRecorder *recorder_ {}; std::string sourceFile_; + std::string pkgName_; std::unique_ptr emitter_; util::Hotfix *hotfixHelper_ {nullptr}; util::StringView recordName_; diff --git a/es2panda/compiler/core/compilerImpl.cpp b/es2panda/compiler/core/compilerImpl.cpp index 3225df90d88df0fc18c4a25f7c6623a2d20b9e3d..bd7ce35e97013ffe92a4c0ac9ca484972750d579 100644 --- a/es2panda/compiler/core/compilerImpl.cpp +++ b/es2panda/compiler/core/compilerImpl.cpp @@ -37,10 +37,11 @@ CompilerImpl::~CompilerImpl() } panda::pandasm::Program *CompilerImpl::Compile(parser::Program *program, const es2panda::CompilerOptions &options, - const std::string &debugInfoSourceFile) + const std::string &debugInfoSourceFile, const std::string &pkgName) { CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode, - options.mergeAbc, options.typeExtractor, debugInfoSourceFile, program->RecordName()); + options.mergeAbc, options.typeExtractor, debugInfoSourceFile, pkgName, + program->RecordName()); if (hotfixHelper_ != nullptr) { context.AddHotfixHelper(hotfixHelper_); diff --git a/es2panda/compiler/core/compilerImpl.h b/es2panda/compiler/core/compilerImpl.h index fbf7e6253a42d6b02ec9da103d96bf82db52561f..595e9521cf072030242e3eaab3a19fe8539559d9 100644 --- a/es2panda/compiler/core/compilerImpl.h +++ b/es2panda/compiler/core/compilerImpl.h @@ -44,7 +44,7 @@ public: NO_MOVE_SEMANTIC(CompilerImpl); panda::pandasm::Program *Compile(parser::Program *program, const es2panda::CompilerOptions &options, - const std::string &debugInfoSourceFile); + const std::string &debugInfoSourceFile, const std::string &pkgName); static void DumpAsm(const panda::pandasm::Program *prog); void AddHotfixHelper(util::Hotfix *hotfixHelper) diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 114d43fdced1e2ab3e9c5c90064fb34a6079c798..9a3dde65413b33b545469f06d1b923dfea7abd1d 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -295,6 +295,7 @@ Emitter::Emitter(const CompilerContext *context) if (context->IsMergeAbc()) { auto recordName = context->Binder()->Program()->FormatedRecordName().Mutf8(); rec_ = new panda::pandasm::Record(recordName.substr(0, recordName.find_last_of('.')), LANG_EXT); + SetPkgNameField(context->PkgName()); SetCommonjsField(context->Binder()->Program()->Kind() == parser::ScriptKind::COMMONJS); } else { rec_ = nullptr; @@ -309,6 +310,16 @@ Emitter::~Emitter() delete prog_; } +void Emitter::SetPkgNameField(std::string pkgName) +{ + auto pkgNameField = panda::pandasm::Field(LANG_EXT); + pkgNameField.name = "pkgName@" + pkgName; + pkgNameField.type = panda::pandasm::Type("u8", 0); + pkgNameField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(static_cast(0))); + rec_->field_list.emplace_back(std::move(pkgNameField)); +} + void Emitter::GenTypeInfoRecord() const { auto typeInfoRecord = panda::pandasm::Record(TypeExtractorEmitter::TYPE_INFO_RECORD, LANG_EXT); diff --git a/es2panda/compiler/core/emitter/emitter.h b/es2panda/compiler/core/emitter/emitter.h index d766694ff7b6bb4e431401745f51d8d6fd7eac25..3ef8387bbec61d5b8cdfcfb6b2dc9489ecc032c5 100644 --- a/es2panda/compiler/core/emitter/emitter.h +++ b/es2panda/compiler/core/emitter/emitter.h @@ -116,6 +116,7 @@ public: private: void SetCommonjsField(bool isCommonjs); + void SetPkgNameField(std::string pkgName); void GenCommonjsRecord() const; void GenTypeInfoRecord() const; void GenESTypeAnnotationRecord() const; diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 5ab01d0d6bf64d0ba68b31121d1e364e8e10c788..49d2bf80ff58b26268b2972b4b965e3805b1437a 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -59,6 +59,7 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil std::string src(input.source); std::string rname(input.recordName); std::string sourcefile(input.sourcefile); + std::string pkgName(input.pkgName); parser::ScriptKind kind(input.scriptKind); bool needDumpSymbolFile = !options.hotfixOptions.dumpSymbolTable.empty(); @@ -95,7 +96,7 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil std::string debugInfoSourceFile = options.debugInfoSourceFile.empty() ? sourcefile : options.debugInfoSourceFile; - auto *prog = compiler_->Compile(&ast, options, debugInfoSourceFile); + auto *prog = compiler_->Compile(&ast, options, debugInfoSourceFile, pkgName); if (hotfixHelper) { delete hotfixHelper; diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index 766d15c0938fcab32328c7ece6ef2f315370b0db..8ac8084008a2c8b4016fef985cb682f46c537d8b 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -56,6 +56,7 @@ struct SourceFile { std::string_view source {}; parser::ScriptKind scriptKind {}; std::string sourcefile {}; + std::string pkgName {}; uint32_t hash {0}; };