From 035ef766f5461a51c96002b986f14c4222531717 Mon Sep 17 00:00:00 2001 From: dongchao Date: Wed, 27 Aug 2025 16:13:40 +0800 Subject: [PATCH] Generate top-level initModule glue code Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICV2CR Signed-off-by: dongchao Change-Id: I859baeef3e4d1b4d7006f6c2e822404731970a90 --- ets2panda/declgen_ets2ts/declgenEts2Ts.cpp | 24 +++++++++++++++++++++- ets2panda/declgen_ets2ts/declgenEts2Ts.h | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index 5864e03905..30b22ef0be 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -403,6 +403,24 @@ void TSDeclGen::GenExportNamedDeclarations() } } +void TSDeclGen::GenInitModuleGlueCode() +{ + for (auto *stmt : program_->Ast()->Statements()) { + if (!stmt->IsExpressionStatement()) { + continue; + } + if (!stmt->AsExpressionStatement()->GetExpression()->IsCallExpression()) { + continue; + } + auto *callExpr = stmt->AsExpressionStatement()->GetExpression()->AsCallExpression(); + if (callExpr->Callee()->IsIdentifier() && + callExpr->Callee()->AsIdentifier()->Name() == compiler::Signatures::INIT_MODULE_METHOD) { + OutTs("import \"", callExpr->Arguments()[0]->ToString(), "\""); + OutEndlTs(); + } + } +} + void TSDeclGen::GenImportDeclarations() { for (auto *globalStatement : program_->Ast()->Statements()) { @@ -2575,13 +2593,17 @@ bool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::p declBuilder.ResetTsOutput(); declBuilder.ResetDtsOutput(); + declBuilder.GenInitModuleGlueCode(); + std::string initModuleOutputEts = declBuilder.GetTsOutput(); + declBuilder.ResetTsOutput(); + compiler::GetPhaseManager()->SetCurrentPhaseIdWithoutReCheck(afterCheckerId); declBuilder.GenImportDeclarations(); std::string importOutputEts = declBuilder.GetTsOutput(); std::string importOutputDEts = declBuilder.GetDtsOutput(); - std::string combineEts = importOutputEts + outputEts + exportOutputEts; + std::string combineEts = importOutputEts + initModuleOutputEts + outputEts + exportOutputEts; std::string combinedDEts = importOutputDEts + outputDEts + exportOutputDEts; if (!declBuilder.GetDeclgenOptions().recordFile.empty()) { diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 363f74c8ab..7e79a37f53 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -75,6 +75,7 @@ public: bool Generate(); void GenImportDeclarations(); void GenExportNamedDeclarations(); + void GenInitModuleGlueCode(); void GenImportRecordDeclarations(const std::string &source); std::string GetDtsOutput() const -- Gitee