From a33cde0778be4fdb589a63e768a61bfb54e97c9f Mon Sep 17 00:00:00 2001 From: dongchao Date: Sat, 30 Aug 2025 18:42:46 +0800 Subject: [PATCH] Convert default methods to optional Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICVKXS Signed-off-by: dongchao Change-Id: Ib4d432f5ce7915dfae5f567ea5c86fe76585cf1f --- ets2panda/declgen_ets2ts/declgenEts2Ts.cpp | 39 +++++++------------ ets2panda/declgen_ets2ts/declgenEts2Ts.h | 1 + .../declgen-ets2ts-runtime-ignored.txt | 7 ++++ .../test_ets2ts_isolated_class-expected.txt | 4 +- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index d31b763895..12f59e3dd2 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -699,6 +699,16 @@ void TSDeclGen::ProcessFuncParameter(varbinder::LocalVariable *param) OutDts("ESObject"); } +void TSDeclGen::GenOptionalFlag(const checker::Signature *sig, const ir::MethodDefinition *methodDef) +{ + if (sig->HasSignatureFlag(checker::SignatureFlags::DEFAULT) || + (state_.inInterface && methodDef != nullptr && methodDef->Value()->IsFunctionExpression() && + methodDef->Value()->AsFunctionExpression()->Function()->IsScriptFunction() && + methodDef->Value()->AsFunctionExpression()->Function()->AsScriptFunction()->HasBody())) { + OutDts("?"); + } +} + void TSDeclGen::ProcessFuncParameters(const checker::Signature *sig) { GenSeparated(sig->Params(), [this](varbinder::LocalVariable *param) { ProcessFuncParameter(param); }); @@ -712,6 +722,7 @@ void TSDeclGen::GenFunctionType(const checker::ETSFunctionType *etsFunctionType, // CC-OFFNXT(G.FMT.14-CPP) project code style const auto *sig = GetFuncSignature(etsFunctionType, methodDef); ES2PANDA_ASSERT(sig != nullptr); + GenOptionalFlag(sig, methodDef); if (sig->HasFunction()) { GenTypeParameters(sig->Function()->TypeParams(), isStatic, sig->Owner()->AsETSObjectType()); const auto *funcBody = sig->Function()->Body(); @@ -2079,28 +2090,6 @@ void TSDeclGen::EmitClassGlueCode(const ir::ClassDefinition *classDef, const std } } -void TSDeclGen::ProcessMethodsFromInterfaces(std::unordered_set &processedMethods, - const ArenaVector &interfaces) -{ - if (interfaces.empty()) { - return; - } - for (const auto &interface : interfaces) { - auto methods = interface->Methods(); - std::unordered_set processedInterfaceMethods; - for (const auto &method : methods) { - if ((method->Flags() & (varbinder::VariableFlags::PUBLIC)) != 0U && - (method->Flags() & (varbinder::VariableFlags::STATIC)) == 0U && - processedMethods.find(method->Name().Mutf8()) == processedMethods.end()) { - ProcessMethodDefinition( - method->AsLocalVariable()->Declaration()->AsFunctionDecl()->Node()->AsMethodDefinition(), - processedInterfaceMethods); - } - } - ProcessMethodsFromInterfaces(processedMethods, interface->Interfaces()); - } -} - void TSDeclGen::ProcessClassBody(const ir::ClassDefinition *classDef) { state_.inClass = true; @@ -2143,9 +2132,6 @@ void TSDeclGen::ProcessClassBody(const ir::ClassDefinition *classDef) GenClassDeclaration(prop->AsClassDeclaration()); } } - if (classDef->TsType() != nullptr && classDef->TsType()->IsETSObjectType()) { - ProcessMethodsFromInterfaces(processedMethods, classDef->TsType()->AsETSObjectType()->Interfaces()); - } } void TSDeclGen::CloseClassBlock(const bool isDts) @@ -2261,7 +2247,6 @@ void TSDeclGen::GenMethodDeclaration(const ir::MethodDefinition *methodDef) if (ShouldSkipMethodDeclaration(methodDef)) { return; } - GenAnnotations(methodDef->Function()); const auto methodIdent = GetKeyIdent(methodDef->Key()); auto methodName = methodIdent->Name().Mutf8(); if (methodName == "$_iterator") { @@ -2289,6 +2274,7 @@ bool TSDeclGen::GenMethodDeclarationPrefix(const ir::MethodDefinition *methodDef if (!ShouldEmitDeclaration(methodDef)) { return true; } + GenAnnotations(methodDef->Function()); if (methodDef->IsDefaultExported()) { OutDts("declare function "); } else { @@ -2300,6 +2286,7 @@ bool TSDeclGen::GenMethodDeclarationPrefix(const ir::MethodDefinition *methodDef !ShouldEmitDeclaration(methodDef) && !methodDef->IsConstructor()) { return true; } + GenAnnotations(methodDef->Function()); ProcessIndent(); GenModifier(methodDef); } diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 2ebb6c77e1..22e5f28b72 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -204,6 +204,7 @@ private: void ProcessParameterName(varbinder::LocalVariable *param); void ProcessFuncParameter(varbinder::LocalVariable *param); void ProcessFuncParameters(const checker::Signature *sig); + void GenOptionalFlag(const checker::Signature *sig, const ir::MethodDefinition *methodDef); void ProcessClassPropertyType(const ir::ClassProperty *classProp); std::vector FilterValidImportSpecifiers(const ArenaVector &specifiers); std::vector FilterValidExportSpecifiers(const ArenaVector &specifiers); diff --git a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt index 307994e6fe..447fc1f322 100644 --- a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt +++ b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt @@ -6,6 +6,13 @@ overload_declaration/constructor_overload_sig_with_ref_type.ets type_param_in_union.ets mypackage/implicit_package_import_2.ets #FailKind.TSC_FAIL - 21 tests: +InterfacePrivateMethod.ets +InterfaceWithDefaultFunction1.ets +overload_declaration/InterfacePrivateMethod2.ets +this_override.ets +trailing_lambda_and_optional_parameterets7.ets +trailing_lambda_and_optional_parameterets8.ets +trailing_lambda_and_optional_parameterets9.ets ClassNewInstance.ets Enum7.ets GenericBridges_01.ets diff --git a/ets2panda/test/unit/declgen/ets2ts_isolated/test_ets2ts_isolated_class-expected.txt b/ets2panda/test/unit/declgen/ets2ts_isolated/test_ets2ts_isolated_class-expected.txt index 4ad14cb500..b23303ea38 100644 --- a/ets2panda/test/unit/declgen/ets2ts_isolated/test_ets2ts_isolated_class-expected.txt +++ b/ets2panda/test/unit/declgen/ets2ts_isolated/test_ets2ts_isolated_class-expected.txt @@ -1,8 +1,8 @@ export declare interface I0 { - I0Method(a: string): string; + I0Method?(a: string): string; } export declare interface I1 { - I1Method(a: number): number; + I1Method?(a: number): number; } export declare class Base { public get a(): number; -- Gitee