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 629a7c8babf7823a7394c1246cef8736f159326d..d217a4b4fa7af69e226c03bc9f44c019b4946c40 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}; }; diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index b63e338fae4e77894034e056b38a07cbf44a4b91..8ea83e1e181970f3de477d0065f2a04b7282dd6e 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -52,6 +52,7 @@ export const ts2pandaOptions = [ { name: 'source-file', type: String, defaultValue: "", description: "specify the file path info recorded in generated abc" }, { name: 'generate-tmp-file', type: Boolean, defaultValue: false, description: "whether to generate intermediate temporary files"}, { name: 'record-name', type: String, defaultValue: "", description: "specify the record name, this option can only be used when [merge-abc] is enabled." }, + { name: 'package-name', type: String, defaultValue: "", description: "specify the package that the compiling file belongs to." }, { name: 'output-proto', type: Boolean, defaultValue: false, description: "Output protoBin file. Default: false" }, { name: 'merge-abc', type: Boolean, defaultValue: false, description: "Compile as merge abc" }, { name: 'input-file', type: String, defaultValue: "", description: "A file containing a list of source files to be compiled. Each line of this file should be constructed in such format: fileName;recordName;moduleType;sourceFile" }, @@ -232,11 +233,7 @@ export class CmdOptions { } static getRecordName(): string { - if (!this.options) { - return ""; - } - - if (!this.options["merge-abc"]) { + if (!this.options || !this.options["merge-abc"]) { return ""; } @@ -342,6 +339,14 @@ export class CmdOptions { return this.options["source-file"]; } + static getPackageName(): string { + if (!this.options || !this.options["merge-abc"]) { + return ""; + } + + return this.options["package-name"]; + } + static needGenerateTmpFile(): boolean { if (!this.options) { return false; diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 33cc688f982d4406e7b08f59843b41f653180675..ed7dcf5336f834366729da6682f5cb15ffe5f287 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -114,6 +114,8 @@ function main(fileNames: string[], options: ts.CompilerOptions, cmdArgsSet?: Map return node; } let originProcessArgs = process.argv.slice(0); + // insert specifiedCmdArgs's elements ahead the process.argv's element + // whose index is 2 (count from 0) Array.prototype.splice.apply(process.argv, specifiedCmdArgs); //@ts-ignore CmdOptions.options = commandLineArgs(ts2pandaOptions, { partial: true }); @@ -203,8 +205,9 @@ function transformSourcefilesList(parsed: ts.ParsedCommandLine | undefined) { for (let i = 0; i < sourceFileInfoArray.length; i++) { let sourceFileInfo = sourceFileInfoArray[i].split(";"); - if (sourceFileInfo.length != 4) { - throw new Error("Input info for each file need \"fileName;recordName;moduleType;sourceFile\" style."); + if (sourceFileInfo.length != 5) { + throw new Error( + "Input info for each file need \"fileName;recordName;moduleType;sourceFile;packageName\" style."); } let inputFileName = sourceFileInfo[0]; @@ -221,6 +224,9 @@ function transformSourcefilesList(parsed: ts.ParsedCommandLine | undefined) { throw new Error("The third info should be \"esm\" or \"commonjs\"."); } specifiedCmdArgs.push.apply(specifiedCmdArgs, ["--source-file", sourceFileInfo[3]]); + specifiedCmdArgs.push.apply(specifiedCmdArgs, ["--package-name", sourceFileInfo[4]]); + // insert two elements 2 and 0 at the beginning of the specifiedCmdArgs array, + // useful for Array.prototype.splice.apply specifiedCmdArgs.unshift(2, 0); cmdArgsSet.set(inputFileName, specifiedCmdArgs); } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 430e5dc51254e232edd14e313aec5d9468d67e0e..f7504ddd47c1778b61cf29c35ab48ea4ff6404cf 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -245,7 +245,8 @@ export class Ts2Panda { static dumpRecord(ts2abc: any, recordName: string): void { let record = { "t": JsonType.record, - "rb": new Record(recordName) + "rb": new Record(recordName), + "pn": CmdOptions.getPackageName() } let jsonRecord = escapeUnicode(JSON.stringify(record, null, 2)); jsonRecord = "$" + jsonRecord.replace(dollarSign, '#$') + "$"; diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 9b0be9940321f7d8618087d457a543c9e55b77bb..0c5593e91c2f8603c6d0a75d13973afd5c5e56f4 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1075,6 +1075,21 @@ static void ParseRec(const Json::Value &rootValue, panda::pandasm::Program &prog prog.record_table.emplace(record.name.c_str(), std::move(record)); } +static void SetPackageName(const std::string &packageName, panda::pandasm::Program &prog) +{ + auto iter = prog.record_table.find(g_recordName); + if (iter != prog.record_table.end()) { + auto &rec = iter->second; + auto pkgNameField = panda::pandasm::Field(LANG_EXT); + pkgNameField.name = "pkgName@" + packageName; + 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)); + } +} + static void ParseSingleStr(const Json::Value &rootValue, panda::pandasm::Program &prog) { auto strArr = rootValue["s"]; @@ -1295,6 +1310,10 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr if (rootValue.isMember("rb") && rootValue["rb"].isObject()) { ParseRec(rootValue, prog); } + if (rootValue.isMember("pn") && rootValue["pn"].isString()) { + std::string packageName = rootValue["pn"].asString(); + SetPackageName(packageName, prog); + } break; } case static_cast(JsonType::STRING): {