From 96b8bad5fb37c05ee39100942e86a40420284264 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Tue, 2 Sep 2025 21:12:42 +0800 Subject: [PATCH] fix crash with Partial on ambient declarations Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICW25X Signed-off-by: anjiaqi --- ets2panda/compiler/core/ETSemitter.cpp | 34 ++++++++++++++++--- ets2panda/compiler/core/ETSfunction.cpp | 24 ++++++++----- .../ets/ambient_class_with_partial_type.ets | 25 ++++++++++++++ .../ambient_interface_with_partial_type.ets | 25 ++++++++++++++ 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 ets2panda/test/runtime/ets/ambient_class_with_partial_type.ets create mode 100644 ets2panda/test/runtime/ets/ambient_interface_with_partial_type.ets diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 69cac80ef6..bfe500347e 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -61,6 +61,7 @@ static constexpr auto EXTENSION = panda_file::SourceLang::ETS; // NOTE: temporary dummy gn buildfix until ETS plugin has gn build support static constexpr auto EXTENSION = panda_file::SourceLang::PANDA_ASSEMBLY; #endif +static constexpr std::string_view const STD_CORE_NAMESPACE = "std.core"; static uint32_t TranslateModifierFlags(ir::ModifierFlags modifierFlags) { @@ -285,7 +286,15 @@ pandasm::Function *ETSFunctionEmitter::GenFunctionSignature() } auto *emitter = static_cast(Cg()->Context()->emitter); auto func = GenScriptFunction(scriptFunc, emitter, false); - if (scriptFunc->IsExternal()) { // why do we emit an external method? + + bool isExternal = Cg()->RootNode()->AsScriptFunction()->IsExternal(); + const auto internalName = scriptFunc->Scope()->IsFunctionVariableScope() + ? scriptFunc->Scope()->AsFunctionVariableScope()->InternalName().Mutf8() + : nullptr; + if (isExternal && internalName.find("$partial") != std::string::npos) { + isExternal = false; + } + if (isExternal) { func.metadata->SetAttribute(Signatures::EXTERNAL); } @@ -551,7 +560,14 @@ void ETSEmitter::GenClassField(const ir::ClassProperty *prop, pandasm::Record &c field.metadata->SetAnnotations(GenCustomAnnotations(prop->Annotations(), field.name)); } - if (external || prop->IsDeclare()) { // #28197 + bool isPartial = false; + if (ToAssemblerType(prop->Parent()).find("$partial") != std::string::npos && + ToAssemblerType(prop->Parent()).find(STD_CORE_NAMESPACE) == std::string::npos && + ToAssemblerType(prop->Parent()).find("escompat") == std::string::npos) { + isPartial = true; + } + + if (!isPartial && (external || prop->IsDeclare())) { field.metadata->SetAttribute(Signatures::EXTERNAL); } else if (prop->TsType()->IsETSPrimitiveType() || prop->TsType()->IsETSStringType()) { EmitDefaultFieldValue(field, prop->Value()); @@ -631,8 +647,13 @@ void ETSEmitter::GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceD } } + auto isPartial = ToAssemblerType(interfaceDecl).find("$partial") != std::string::npos && + ToAssemblerType(interfaceDecl).find(STD_CORE_NAMESPACE) == std::string::npos && + ToAssemblerType(interfaceDecl).find("escompat") == std::string::npos; if (external) { - interfaceRecord.metadata->SetAttribute(Signatures::EXTERNAL); + if (!isPartial) { + interfaceRecord.metadata->SetAttribute(Signatures::EXTERNAL); + } Program()->AddToRecordTable(std::move(interfaceRecord)); return; } @@ -728,8 +749,13 @@ void ETSEmitter::GenClassRecord(const ir::ClassDefinition *classDef, bool extern } } + auto isPartial = ToAssemblerType(classDef).find("$partial") != std::string::npos && + ToAssemblerType(classDef).find(STD_CORE_NAMESPACE) == std::string::npos && + ToAssemblerType(classDef).find("escompat") == std::string::npos; if (external) { - classRecord.metadata->SetAttribute(Signatures::EXTERNAL); + if (!isPartial) { + classRecord.metadata->SetAttribute(Signatures::EXTERNAL); + } Program()->AddToRecordTable(std::move(classRecord)); return; } diff --git a/ets2panda/compiler/core/ETSfunction.cpp b/ets2panda/compiler/core/ETSfunction.cpp index af91c7d094..8ca3f9bccd 100644 --- a/ets2panda/compiler/core/ETSfunction.cpp +++ b/ets2panda/compiler/core/ETSfunction.cpp @@ -36,6 +36,8 @@ #include "checker/types/gradualType.h" namespace ark::es2panda::compiler { +// static constexpr std::string_view const STD_CORE_NAMESPACE = "std.core"; + // #22952: this should have been done in lowering void ETSFunction::CallImplicitCtor(ETSGen *etsg) { @@ -144,17 +146,23 @@ void ETSFunction::CompileAsConstructor(ETSGen *etsg, const ir::ScriptFunction *s } } +bool ShouldCompileFunctionBody(const ir::ScriptFunction *decl) +{ + const auto internalName = decl->Scope()->AsFunctionVariableScope()->InternalName().Mutf8(); + const bool isPartialFunction = + (decl->IsDeclare() || decl->IsExternal()) && internalName.find("$partial") != std::string::npos; + + return isPartialFunction || + (!decl->IsDeclare() && !decl->IsExternal() && !decl->Signature()->Owner()->GetDeclNode()->IsDeclare()); +} + void ETSFunction::CompileFunction(ETSGen *etsg) { const auto *decl = etsg->RootNode()->AsScriptFunction(); - if (decl->IsDeclare() || decl->IsExternal()) { - return; // such methods should've been filtered in advance - } - if (decl->Signature()->Owner()->GetDeclNode()->IsDeclare()) { - return; // AST inconsistency! - } - if (auto *const body = decl->Body(); body != nullptr && body->IsBlockStatement()) { - CompileSourceBlock(etsg, body->AsBlockStatement()); + if (ShouldCompileFunctionBody(decl)) { + if (auto *const body = decl->Body(); body != nullptr && body->IsBlockStatement()) { + CompileSourceBlock(etsg, body->AsBlockStatement()); + } } } diff --git a/ets2panda/test/runtime/ets/ambient_class_with_partial_type.ets b/ets2panda/test/runtime/ets/ambient_class_with_partial_type.ets new file mode 100644 index 0000000000..eaa08fd95c --- /dev/null +++ b/ets2panda/test/runtime/ets/ambient_class_with_partial_type.ets @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare class A { + field: string + title: string +} + +function main() { + let a: Partial = { field: "hello" } + console.log(a.field) + console.log(a.title) +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/ambient_interface_with_partial_type.ets b/ets2panda/test/runtime/ets/ambient_interface_with_partial_type.ets new file mode 100644 index 0000000000..3c9c90f772 --- /dev/null +++ b/ets2panda/test/runtime/ets/ambient_interface_with_partial_type.ets @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare interface I { + field: string + title: string +} + +function main() { + let a: Partial = { field: "hello" } + console.log(a.field) + console.log(a.title) +} \ No newline at end of file -- Gitee