From df2d86aa9532c2f61e45554801e2f55fea78307c Mon Sep 17 00:00:00 2001 From: hufeng Date: Sat, 3 Dec 2022 15:37:22 +0800 Subject: [PATCH] fixed a18252a from https://gitee.com/hufeng20/arkcompiler_ets_frontend/pulls/738 Set pkgName with ts2abc Signed-off-by: hufeng Change-Id: Ic89df8d36110e8e4ef365fe40c4a07e44c853065 --- ts2panda/src/cmdOptions.ts | 15 ++++++++++----- ts2panda/src/index.ts | 10 ++++++++-- ts2panda/src/ts2panda.ts | 3 ++- ts2panda/ts2abc/ts2abc.cpp | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index b63e338fae..8ea83e1e18 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 33cc688f98..ed7dcf5336 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 430e5dc512..f7504ddd47 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 9b0be99403..0c5593e91c 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): { -- Gitee