diff --git a/ets2panda/checker/ETSchecker.cpp b/ets2panda/checker/ETSchecker.cpp index fcee9f460c42a61cb2f1b45cac30585e8f09cb62..c4626715c6610caeac5986241f1a11fd79225a15 100644 --- a/ets2panda/checker/ETSchecker.cpp +++ b/ets2panda/checker/ETSchecker.cpp @@ -790,6 +790,22 @@ void ETSChecker::InsertExtensionSetterToMap(util::StringView name, ETSObjectType GetGlobalTypesHolder()->InsertExtensionSetterToMap(name, type, sig); } +bool ETSChecker::HasParameterlessConstructor(checker::Type *type) +{ + if (!type->IsETSObjectType()) { + return false; + } + + auto *objType = type->AsETSObjectType(); + for (auto *ctorSig : objType->ConstructSignatures()) { + if (ctorSig != nullptr && ctorSig->Params().empty() && !ctorSig->HasRestParameter()) { + return true; + } + } + + return false; +} + void ETSChecker::InsertExtensionGetterToMap(util::StringView name, ETSObjectType *type, Signature *sig) { GetGlobalTypesHolder()->InsertExtensionGetterToMap(name, type, sig); diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 423de0971dfb0569f476fc09b2035ada3e78b540..386825ed19b1cfac337d8610f6d23868bc3cc779 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -561,7 +561,7 @@ public: static bool HasSameAssemblySignature(Signature const *const sig1, Signature const *const sig2) noexcept; static bool HasSameAssemblySignatures(ETSFunctionType const *const func1, ETSFunctionType const *const func2) noexcept; - + static bool HasParameterlessConstructor(checker::Type *type); Signature *AdjustForTypeParameters(Signature *source, Signature *target); void ReportOverrideError(Signature *signature, Signature *overriddenSignature, const OverrideErrorCode &errorCode); void CheckOverride(Signature *signature); diff --git a/ets2panda/compiler/lowering/ets/arrayLiteralLowering.cpp b/ets2panda/compiler/lowering/ets/arrayLiteralLowering.cpp index 3d4f3e5b155e837e206ffa224bc4eab29051e7d7..32f31d90b89c2e935de0fab0318d759dc4020de5 100644 --- a/ets2panda/compiler/lowering/ets/arrayLiteralLowering.cpp +++ b/ets2panda/compiler/lowering/ets/arrayLiteralLowering.cpp @@ -55,10 +55,17 @@ ArenaVector ArrayLiteralLowering::GenerateDefaultCallToConstruc newStmts.emplace_back(lengthSymbol->Clone(Allocator(), nullptr)); newStmts.emplace_back(indexSymbol->Clone(Allocator(), nullptr)); newStmts.emplace_back(indexSymbol->Clone(Allocator(), nullptr)); - ss << "@@I8[@@I9] = new @@T10() }"; + + if (checker_->HasParameterlessConstructor(eleType)) { + ss << "@@I8[@@I9] = new @@T10();"; + } else { + ss << "let restArgs = new Array(0);"; + ss << "@@I8[@@I9] = new @@T10(restArgs);"; + } newStmts.emplace_back(arraySymbol->Clone(Allocator(), nullptr)); newStmts.emplace_back(indexSymbol->Clone(Allocator(), nullptr)); newStmts.emplace_back(typeNode->Clone(Allocator(), nullptr)); + ss << "}"; } else { ArenaVector emptyStatement(Allocator()->Adapter()); return emptyStatement; diff --git a/ets2panda/test/runtime/ets/array_literal_with_rest_constructor.ets b/ets2panda/test/runtime/ets/array_literal_with_rest_constructor.ets new file mode 100644 index 0000000000000000000000000000000000000000..c7ba2c186f31ac09a7a087f57e86ac002417baa3 --- /dev/null +++ b/ets2panda/test/runtime/ets/array_literal_with_rest_constructor.ets @@ -0,0 +1,57 @@ +/* + * 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. + */ + +class ClassWithRestParams { + value : int = 0; + + constructor(... args : Any[]) + { + this.value = args.length; + } +} + +class ClassWithNormalConstructor { + value : int = 0; + + constructor() + { + this.value = 42; + } +} + +function +main() +{ + let restArray : ClassWithRestParams[] = /* @@ restArray */ new ClassWithRestParams[3]; + let normalArray : ClassWithNormalConstructor[] = /* @@ normalArray */ new ClassWithNormalConstructor[2]; + + // Check lengths + arktest.assertTrue(restArray.length == 3); + arktest.assertTrue(normalArray.length == 2); + + // Check each element is properly initialized + for (let i = 0; i < restArray.length; i++) { + arktest.assertTrue(restArray[i] != null); + arktest.assertTrue(restArray[i] instanceof ClassWithRestParams); + } + + for (let i = 0; i < normalArray.length; i++) { + arktest.assertTrue(normalArray[i] != null); + arktest.assertTrue(normalArray[i] instanceof ClassWithNormalConstructor); + } +} + +/* @@@ restArray Success */ +/* @@@ normalArray Success */ \ No newline at end of file