From d761ac91edbae79a5e61160406fb97eb6e95e49e Mon Sep 17 00:00:00 2001 From: songqi Date: Tue, 28 Mar 2023 18:05:47 +0800 Subject: [PATCH] Fix class method without functionBody emit failure Issue: I6R25C Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I301c4b5759458cd07f2f51a22f2f75a44ea25390 --- es2panda/ir/base/classDefinition.cpp | 9 +++++ .../classes/test-ts-classes-20-expected.txt | 2 + .../conformance/classes/test-ts-classes-20.ts | 30 +++++++++++++++ .../classes/test-ts-classes-21-expected.txt | 1 + .../conformance/classes/test-ts-classes-21.ts | 37 +++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20.ts create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21.ts diff --git a/es2panda/ir/base/classDefinition.cpp b/es2panda/ir/base/classDefinition.cpp index c34b33f456..14896cf5f3 100644 --- a/es2panda/ir/base/classDefinition.cpp +++ b/es2panda/ir/base/classDefinition.cpp @@ -161,6 +161,11 @@ int32_t ClassDefinition::CreateClassStaticProperties(compiler::PandaGen *pg, uti continue; } + if (prop->IsOptional() && prop->Value()->Function()->IsOverload()) { + compiled.Set(i); + continue; + } + util::StringView name = util::Helpers::LiteralToPropName(prop->Key()); compiler::LiteralBuffer *literalBuf = prop->IsStatic() ? &staticBuf : buf; auto &nameMap = prop->IsStatic() ? staticPropNameMap : propNameMap; @@ -237,6 +242,10 @@ void ClassDefinition::CompileMissingProperties(compiler::PandaGen *pg, const uti } const ir::MethodDefinition *prop = properties[i]->AsMethodDefinition(); + if (prop->IsOptional() && prop->Value()->Function()->IsOverload()) { + continue; + } + compiler::VReg dest = prop->IsStatic() ? classReg : protoReg; compiler::RegScope rs(pg); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20-expected.txt new file mode 100644 index 0000000000..fe1e163da1 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20-expected.txt @@ -0,0 +1,2 @@ +A +Hello diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20.ts new file mode 100644 index 0000000000..de37a7f103 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-20.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 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 A { + funcA(p: number): void; + funcA(p: any): void { + print("A"); + } +} + +class B { + funcC?(p: number): void; +} + +var a = new A(); +a.funcA(0); +print("Hello"); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21-expected.txt new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21-expected.txt @@ -0,0 +1 @@ +2 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21.ts new file mode 100644 index 0000000000..43306101c1 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-21.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 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 B { + a: number = 1; + b?: string; + c(): any { return 2; }; + d?(): string; + get x() { + return false; + } + set x(v: boolean) { + } + e?: number; + f?(): number; +} + +var b = new B(); + +if (b.e) { + print(b.a) +} else { + print(b.c()) +} -- Gitee