From d345aff398e3f06795bccd3770df78fa57946acc Mon Sep 17 00:00:00 2001 From: lixl9 Date: Wed, 11 Jun 2025 17:26:59 +0800 Subject: [PATCH] Add overload UT Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICEEJO Signed-off-by: lixinglong --- .../runtime/ets/java_style/15445_java.ets | 36 ++++ .../runtime/ets/java_style/15502_java.ets | 36 ++++ .../runtime/ets/java_style/Enum2_java.ets | 61 ++++++ .../ets/java_style/FixFunctionCall_java.ets | 27 +++ .../ets/java_style/FunctionOverload_java.ets | 44 ++++ .../InterfacePrivateMethod2_java.ets | 43 ++++ .../MostSpecificSignature01_java.ets | 28 +++ .../MostSpecificSignature02_java.ets | 28 +++ .../MostSpecificSignature03_java.ets | 28 +++ .../MostSpecificSignature04_java.ets | 28 +++ .../MostSpecificSignature05_java.ets | 32 +++ .../MostSpecificSignature06_java.ets | 32 +++ .../MostSpecificSignature07_java.ets | 33 +++ .../ets/java_style/Override-4_java.ets | 42 ++++ .../java_style/RecordKeyTypeCheck_java.ets | 62 ++++++ .../ets/java_style/RestTuple4_java.ets | 47 ++++ .../ets/java_style/RestTuple8_java.ets | 47 ++++ .../ets/java_style/SmartCast_03_java.ets | 74 +++++++ ...sync-func-overload-and-type-infer_java.ets | 41 ++++ .../async_method_with_same_name02_java.ets | 31 +++ .../conditionalExpressionLUB_java.ets | 94 ++++++++ .../ets/java_style/constructor-issue_java.ets | 61 ++++++ ...ructor_overload_sig_with_ref_type_java.ets | 41 ++++ .../java_style/default_parameter1_java.ets | 28 +++ ...ctionDuplicatedWithNormalFunction_java.ets | 49 +++++ ...nctionOverrideSuperclassExtension_java.ets | 48 +++++ ...extensionFunctionWithAsExpression_java.ets | 49 +++++ ...tensionFunctionWithTrailingLambda_java.ets | 52 +++++ .../ets/java_style/generic-set_java.ets | 98 +++++++++ .../ets/java_style/generic_union_java.ets | 38 ++++ .../keyword_get_set_as_identfier2_java.ets | 31 +++ ...receiver_trailing_name_duplicated_java.ets | 85 ++++++++ .../java_style/mostSpecificMethod_java.ets | 136 ++++++++++++ ...specific_method_with_rest_param_2_java.ets | 27 +++ ...specific_method_with_rest_param_3_java.ets | 21 ++ .../runtime/ets/java_style/notNull_java.ets | 169 +++++++++++++++ .../ets/java_style/objectLiteral_java.ets | 75 +++++++ .../overload-primitive-and-object_java.ets | 41 ++++ .../overload-resolution-rest-1_java.ets | 37 ++++ .../overload-resolution-rest-2_java.ets | 28 +++ .../ets/java_style/overload_1_java.ets | 25 +++ .../signature_match_lambda_java.ets | 43 ++++ .../java_style/visible_signatures_java.ets | 59 +++++ .../test/runtime/ets/ts_style/15445_ts.ets | 38 ++++ .../test/runtime/ets/ts_style/15502_ts.ets | 41 ++++ .../test/runtime/ets/ts_style/Enum2_ts.ets | 59 +++++ .../ets/ts_style/FixFunctionCall_ts.ets | 29 +++ .../ets/ts_style/FunctionOverload_ts.ets | 47 ++++ .../ts_style/InterfacePrivateMethod2_ts.ets | 49 +++++ .../ts_style/MostSpecificSignature01_ts.ets | 31 +++ .../ts_style/MostSpecificSignature02_ts.ets | 31 +++ .../ts_style/MostSpecificSignature03_ts.ets | 31 +++ .../ts_style/MostSpecificSignature04_ts.ets | 30 +++ .../ts_style/MostSpecificSignature05_ts.ets | 34 +++ .../ts_style/MostSpecificSignature06_ts.ets | 34 +++ .../ts_style/MostSpecificSignature07_ts.ets | 39 ++++ .../runtime/ets/ts_style/Override-4_ts.ets | 55 +++++ .../ets/ts_style/RecordKeyTypeCheck_ts.ets | 64 ++++++ .../runtime/ets/ts_style/RestTuple4_ts.ets | 52 +++++ .../runtime/ets/ts_style/RestTuple8_ts.ets | 50 +++++ .../runtime/ets/ts_style/SmartCast_03_ts.ets | 76 +++++++ .../async-func-overload-and-type-infer_ts.ets | 37 ++++ .../async_method_with_same_name02_ts.ets | 35 +++ .../ts_style/conditionalExpressionLUB_ts.ets | 98 +++++++++ .../ets/ts_style/constructor-issue_ts.ets | 72 +++++++ ...structor_overload_sig_with_ref_type_ts.ets | 41 ++++ .../ets/ts_style/default_parameter1_ts.ets | 33 +++ ...unctionDuplicatedWithNormalFunction_ts.ets | 51 +++++ ...FunctionOverrideSuperclassExtension_ts.ets | 52 +++++ .../extensionFunctionWithAsExpression_ts.ets | 53 +++++ ...extensionFunctionWithTrailingLambda_ts.ets | 58 +++++ .../runtime/ets/ts_style/generic-set_ts.ets | 99 +++++++++ .../runtime/ets/ts_style/generic_union_ts.ets | 42 ++++ .../keyword_get_set_as_identfier2_ts.ets | 31 +++ ...h_receiver_trailing_name_duplicated_ts.ets | 110 ++++++++++ .../ets/ts_style/mostSpecificMethod_ts.ets | 203 ++++++++++++++++++ ...t_specific_method_with_rest_param_2_ts.ets | 33 +++ ...t_specific_method_with_rest_param_3_ts.ets | 32 +++ .../test/runtime/ets/ts_style/notNull_ts.ets | 177 +++++++++++++++ .../runtime/ets/ts_style/objectLiteral_ts.ets | 84 ++++++++ .../overload-primitive-and-object_ts.ets | 53 +++++ .../overload-resolution-rest-1_ts.ets | 46 ++++ .../overload-resolution-rest-2_ts.ets | 49 +++++ .../runtime/ets/ts_style/overload_1_ts.ets | 33 +++ .../ts_style/signature_match_lambda_ts.ets | 51 +++++ .../ets/ts_style/visible_signatures_ts.ets | 75 +++++++ 86 files changed, 4573 insertions(+) create mode 100644 ets2panda/test/runtime/ets/java_style/15445_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/15502_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/Enum2_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/FixFunctionCall_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/FunctionOverload_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/InterfacePrivateMethod2_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature01_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature02_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature03_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature04_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature05_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature06_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/MostSpecificSignature07_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/Override-4_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/RecordKeyTypeCheck_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/RestTuple4_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/RestTuple8_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/SmartCast_03_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/async-func-overload-and-type-infer_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/async_method_with_same_name02_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/conditionalExpressionLUB_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/constructor-issue_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/constructor_overload_sig_with_ref_type_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/default_parameter1_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/extensionFunctionDuplicatedWithNormalFunction_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/extensionFunctionOverrideSuperclassExtension_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/extensionFunctionWithAsExpression_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/extensionFunctionWithTrailingLambda_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/generic-set_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/generic_union_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/keyword_get_set_as_identfier2_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/lambda_with_receiver_trailing_name_duplicated_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/mostSpecificMethod_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_2_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_3_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/notNull_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/objectLiteral_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/overload-primitive-and-object_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/overload-resolution-rest-1_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/overload-resolution-rest-2_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/overload_1_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/signature_match_lambda_java.ets create mode 100644 ets2panda/test/runtime/ets/java_style/visible_signatures_java.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/15445_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/15502_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/Enum2_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/FixFunctionCall_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/FunctionOverload_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/InterfacePrivateMethod2_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature01_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature02_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature03_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature04_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature05_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature06_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/MostSpecificSignature07_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/Override-4_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/RecordKeyTypeCheck_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/RestTuple4_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/RestTuple8_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/SmartCast_03_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/async-func-overload-and-type-infer_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/async_method_with_same_name02_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/conditionalExpressionLUB_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/constructor-issue_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/constructor_overload_sig_with_ref_type_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/default_parameter1_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/extensionFunctionDuplicatedWithNormalFunction_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/extensionFunctionOverrideSuperclassExtension_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/extensionFunctionWithAsExpression_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/extensionFunctionWithTrailingLambda_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/generic-set_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/generic_union_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/keyword_get_set_as_identfier2_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/lambda_with_receiver_trailing_name_duplicated_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/mostSpecificMethod_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_2_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_3_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/notNull_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/objectLiteral_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/overload-primitive-and-object_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-1_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-2_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/overload_1_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/signature_match_lambda_ts.ets create mode 100644 ets2panda/test/runtime/ets/ts_style/visible_signatures_ts.ets diff --git a/ets2panda/test/runtime/ets/java_style/15445_java.ets b/ets2panda/test/runtime/ets/java_style/15445_java.ets new file mode 100644 index 0000000000..ab9565c747 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/15445_java.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024-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 A { + public static foo1(fn: (a1: A1) => void, arg: A1): void { + fn(arg) + } + public static foo2(fn: (a1: A1, a2: A2) => void, arg1: A1, arg2: A2): void { + fn(arg1, arg2) + } + static overload foo{ foo1, foo2 } +} + +function t1(s: String): void { +} + +function t2(s: String, n: int): void { +} + +function main(): int { + A.foo(t1, "lll"); + A.foo(t2, "llll", 1) + return 0 +} diff --git a/ets2panda/test/runtime/ets/java_style/15502_java.ets b/ets2panda/test/runtime/ets/java_style/15502_java.ets new file mode 100644 index 0000000000..3548704a60 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/15502_java.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024-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 A { + public static foo1(fn: (a: A1) => void, arg: A1): void { + fn(arg) + } + public static foo2(fn: (a1: A1, a2: A2) => void, arg1: A1, arg2: A2): void { + fn(arg1, arg2) + } + overload foo{ foo1, foo2 } +} + +function t1(s: String): void { +} + +function t2(s: String, n: int): void { +} + +function main(): int { + A.foo(t1, "lll"); + A.foo(t2, "llll", 1) + return 0 +} diff --git a/ets2panda/test/runtime/ets/java_style/Enum2_java.ets b/ets2panda/test/runtime/ets/java_style/Enum2_java.ets new file mode 100644 index 0000000000..3e8254e7db --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/Enum2_java.ets @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-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. + */ + +enum Color { Red, Green, Blue } + +class ColorEnumDelegate { + private static values_: Color[] = [Color.Red, Color.Green, Color.Blue]; + private static names_: String[] = ["Red", "Green", "Blue"]; + + public static values(): Color[] { return ColorEnumDelegate.values_; } + + public static ordinal(x: Color): int { return x as int; } + + public static toString(x: Color): String { return ColorEnumDelegate.names_[x as int]; } + + public static valueOf1(name: String): Color { + for (let i = 0; i < ColorEnumDelegate.values_.length; i++) { + if (ColorEnumDelegate.names_[i] == name) { return ColorEnumDelegate.values_[i]; } + } + + assertTrue(false, "No enum constant Color") + return Color.Red; + } + + public static valueOf2(ordinal: int): Color { + if (0 <= ordinal && ordinal < ColorEnumDelegate.values_.length) { + return ColorEnumDelegate.values_[ordinal]; + } + + assertTrue(false, "No enum constant Color") + return Color.Red; + } + + overload valueOf{ valueOf1, valueOf2 } +} + +function main(): void { + let red: Color = Color.Red; + assertEQ(ColorEnumDelegate.toString(red), "Red") + + assertEQ(ColorEnumDelegate.ordinal(red), 0) + assertEQ(ColorEnumDelegate.ordinal(Color.Green), 1) + + assertEQ(ColorEnumDelegate.valueOf(2), Color.Blue) + assertEQ(ColorEnumDelegate.valueOf(ColorEnumDelegate.ordinal(Color.Red)), red) + + assertEQ(ColorEnumDelegate.valueOf("Green"), Color.Green) + assertEQ(ColorEnumDelegate.valueOf(ColorEnumDelegate.toString(Color.Blue)), Color.Blue) +} diff --git a/ets2panda/test/runtime/ets/java_style/FixFunctionCall_java.ets b/ets2panda/test/runtime/ets/java_style/FixFunctionCall_java.ets new file mode 100644 index 0000000000..fcae6c4ecf --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/FixFunctionCall_java.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +export type AsyncCallback = (err: T, data: T) => void; + +function getBundleInfo1(userId?: number): void { } +function getBundleInfo2(callback: AsyncCallback): void { + callback(666, 66); +} +overload getBundleInfo{ getBundleInfo1, getBundleInfo2 } + +getBundleInfo((err, data) => { + assertEQ(err, 666); + assertEQ(data, 66) +}); diff --git a/ets2panda/test/runtime/ets/java_style/FunctionOverload_java.ets b/ets2panda/test/runtime/ets/java_style/FunctionOverload_java.ets new file mode 100644 index 0000000000..997b4f3e42 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/FunctionOverload_java.ets @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2024-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 Overload { + public static func1(): string { + return "zero"; + } + + public static func2(a: number): string { + return "one"; + } + + public static func3(a: number, b: number): string { + return "two"; + } + + public static func4(...args: number[]): string { + return "variadic"; + } + + overload func{ func1, func2, func3, func4 } +} + +function main(): void { + assertEQ(Overload.func(), "zero") + assertEQ(Overload.func(1), "one") + assertEQ(Overload.func(1, 2), "two") + assertEQ(Overload.func(1, 2, 3), "variadic") + + let arr: number[] = [1, 2]; + assertEQ(Overload.func(...arr), "variadic") +} diff --git a/ets2panda/test/runtime/ets/java_style/InterfacePrivateMethod2_java.ets b/ets2panda/test/runtime/ets/java_style/InterfacePrivateMethod2_java.ets new file mode 100644 index 0000000000..4b8610b1d1 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/InterfacePrivateMethod2_java.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-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. + */ + +interface Vehicle { + getHorsePower31(rpm: int, trq: int): int{ + return (rpm * trq) / 5252; + } + + getPwrIndex1(trq: int): int{ + return this.getHorsePower3(5252, trq); + } + + getHorsePower32(rpm: int): int{ + return rpm; + } + + getPwrIndex2(trq: int, second: int): int{ + return this.getHorsePower3(trq + second); + } + + overload getHorsePower3{ getHorsePower31, getHorsePower32 } + + overload getPwrIndex{ getPwrIndex1, getPwrIndex2 } +} + +class Car implements Vehicle { } + +function main(): void { + assertEQ(new Car().getPwrIndex(1), 1) + assertEQ(new Car().getPwrIndex(1, 1), 2) +} diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature01_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature01_java.ets new file mode 100644 index 0000000000..6d738ba639 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature01_java.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo2(a: int, b: int = 10): int { + return a + b; +} + +function foo1(a: int): int { + return a; +} + +overload foo{ foo1, foo2 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature02_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature02_java.ets new file mode 100644 index 0000000000..c3d8145c2d --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature02_java.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo2(a: int, b?: int): int { + return a + 2; +} + +function foo1(a: int): int { + return a; +} + +overload foo{ foo1, foo2 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature03_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature03_java.ets new file mode 100644 index 0000000000..8742f62890 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature03_java.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo2(a: int, ...b: int[]): int { + return a + 2; +} + +function foo1(a: int): int { + return a; +} + +overload foo{ foo1, foo2 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature04_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature04_java.ets new file mode 100644 index 0000000000..6c197a3039 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature04_java.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), 7); +} + +function foo2(a: int, ...b: int[]): int { + return 0; +} + +function foo1(a: int, b: int): int { + return a + b; +} + +overload foo{ foo1, foo2 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature05_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature05_java.ets new file mode 100644 index 0000000000..181bad5497 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature05_java.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), "aaaa"); +} + +function foo1(a: int, b: int): string { + return "aaaa"; +} + +function foo3(a: int, ...b: int[]): string { + return "bbbb"; +} + +function foo2(a: int, b: int, c?: int): string { + return "cccc"; +} + +overload foo{ foo1, foo2, foo3 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature06_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature06_java.ets new file mode 100644 index 0000000000..0e9476c561 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature06_java.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), "aaaa"); +} + +function foo1(a: int, b: int): string { + return "aaaa"; +} + +function foo3(a: int, ...b: int[]): string { + return "bbbb"; +} + +function foo2(a: int, b: int, c: int = 10): string { + return "cccc"; +} + +overload foo{ foo1, foo2, foo3 } diff --git a/ets2panda/test/runtime/ets/java_style/MostSpecificSignature07_java.ets b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature07_java.ets new file mode 100644 index 0000000000..829076ef51 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/MostSpecificSignature07_java.ets @@ -0,0 +1,33 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo("1"), 3); +} + +class A { } +class B { } + +function foo2(x: string, y?: A) { + return 1 +} +function foo3(x: string, y?: B) { + return 2 +} +function foo1(x: string) { + return 3 +} + +overload foo{ foo1, foo2, foo3 } diff --git a/ets2panda/test/runtime/ets/java_style/Override-4_java.ets b/ets2panda/test/runtime/ets/java_style/Override-4_java.ets new file mode 100644 index 0000000000..4f6dc93cec --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/Override-4_java.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024-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 X { } +class Y extends X { } + +abstract class A { + foo1(x: Y): string { return "A.foo(Y)" } + abstract foo(x: Double): String + foo2(x: Y): Object { return "A.foo(Y)" } + overload foo{ foo1, foo } +} + +class B extends A { + foo1(x: X): string { return "B.foo(X)" } + foo3(x: Floating): String { return "B.foo(Floating)" } + overload foo{ foo1, foo3, foo } +} + +class C extends A { + foo1(x: Double): String { return "C.foo(Double)" } + override foo2(x: Y): String { return "C.foo2(Y)" } + overload foo{ foo1, foo } +} + +function main() { + assertEQ(new B().foo(new Y()), "B.foo(X)") + assertEQ(new B().foo(new Double()), "B.foo(Floating)") + assertEQ(new C().foo2(new Y()), "C.foo2(Y)") +} diff --git a/ets2panda/test/runtime/ets/java_style/RecordKeyTypeCheck_java.ets b/ets2panda/test/runtime/ets/java_style/RecordKeyTypeCheck_java.ets new file mode 100644 index 0000000000..9048313608 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/RecordKeyTypeCheck_java.ets @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024-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 A extends Numeric { + private value: int; + + public constructor1() { + this.value = 0; + } + + public constructor2(value: int) { + this.value = value; + } + + public override toByte(): byte { + return Int.toByte(this.value); + } + + public override toShort(): short { + return Int.toShort(this.value); + } + + public override toInt(): int { + return this.value; + } + + public override toLong(): long { + return Int.toLong(this.value); + } + + public override toFloat(): float { + return Int.toFloat(this.value); + } + + public override toDouble(): double { + return Int.toDouble(this.value); + } + + public override toString(): String { + return new String(this.value); + } + + overload constructor{ constructor1, constructor2 } +} + +function main() { + let r: Record = { + 1: 1, + } +} diff --git a/ets2panda/test/runtime/ets/java_style/RestTuple4_java.ets b/ets2panda/test/runtime/ets/java_style/RestTuple4_java.ets new file mode 100644 index 0000000000..017c4f9b7a --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/RestTuple4_java.ets @@ -0,0 +1,47 @@ +/* + * 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 A { + public field: number + constructor2(...numbers: [number, number, number]) { + this.field = numbers[0] + numbers[1] + numbers[2] + } + + constructor1(a: int, ...numbers: [number, number, number]) { + this.field = a + numbers[0] + numbers[1] + numbers[2] + } + + overload constructor{ constructor1, constructor2 } +} + +function main() { + let tuple: [number, number, number] = [30, 40, 50] + + let res: A = new A(10, 20, 30) + let res2: A = new A(10, 20, 30, 40) + + let res3: A = new A(...tuple) + let res4: A = new A(10, ...tuple) + + let res5: A = new A(...([30, 40, 50] as [number, number, number])) + let res6: A = new A(10, ...([30, 40, 50] as [number, number, number])) + + assertEQ(res.field, 60) + assertEQ(res2.field, 100) + assertEQ(res3.field, 120) + assertEQ(res4.field, 130) + assertEQ(res5.field, 120) + assertEQ(res6.field, 130) +} diff --git a/ets2panda/test/runtime/ets/java_style/RestTuple8_java.ets b/ets2panda/test/runtime/ets/java_style/RestTuple8_java.ets new file mode 100644 index 0000000000..87309fe725 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/RestTuple8_java.ets @@ -0,0 +1,47 @@ +/* + * 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 A { } +class B { } + +class C { + public field: boolean + + constructor2(...p: [A, B]) { + this.field = p[0] == p[1] + } + + constructor1(a: int, ...p: [A, B]) { + this.field = p[0] == p[1] + } + + overload constructor{ constructor1, constructor2 } +} + +function main() { + let a1: [A, B] = [new A, new B] + + assertTrue((new C(...a1)).field == false) + assertTrue((new C(22, ...a1)).field == false) + + assertTrue((new C(...[new A, new B])).field == false) + assertTrue((new C(22, ...[new A, new B])).field == false) + + assertTrue((new C(...[new A, new B] as [A, B])).field == false) + assertTrue((new C(22, ...[new A, new B] as [A, B])).field == false) + + assertTrue((new C(new A, new B)).field == false) + assertTrue((new C(22, new A, new B)).field == false) +} diff --git a/ets2panda/test/runtime/ets/java_style/SmartCast_03_java.ets b/ets2panda/test/runtime/ets/java_style/SmartCast_03_java.ets new file mode 100644 index 0000000000..39e552cfd8 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/SmartCast_03_java.ets @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2024-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 C { + constructor() { } + + constructor(a: int) { + this.x = a; + } + + overload constructor{ constructor1, constructor2 } + + bar(): string { + return "Class C"; + } + + baz(): int { + return this.x; + } + + private x: int = 7; +} + +function foo(c: Object | null | undefined): string { + if (c instanceof string && (c.length == 11 || c == "Test")) { + c = "Case 1"; + } else if (c instanceof C && c.baz() == 7) { + assertEQ(c.bar(), "Class C") + c = "Case 2"; + } else if (c instanceof Int && c >= 0) { + assertTrue(c >= 0) + c = "Case 3"; + } else if (c instanceof null) { + assertEQ(c, null) + c = "Case 4"; + } else { + c = "Case 5"; + } + + assertEQ(c.length, 6) + return c; +} + +function main(): void { + assertEQ(foo("Test string"), "Case 1") + assertEQ(foo("Test"), "Case 1") + assertEQ(foo("Test string 2"), "Case 5") + assertEQ(foo("test"), "Case 5") + + assertEQ(foo(new Int(5)), "Case 3") + assertEQ(foo(new Int(0)), "Case 3") + assertEQ(foo(new Int(-5)), "Case 5") + + assertEQ(foo(new C(7)), "Case 2") + assertEQ(foo(new C()), "Case 2") + assertEQ(foo(new C(17)), "Case 5") + + assertEQ(foo(null), "Case 4") + + assertEQ(foo(undefined), "Case 5") + assertEQ(foo(new Number(3.0)), "Case 5") +} diff --git a/ets2panda/test/runtime/ets/java_style/async-func-overload-and-type-infer_java.ets b/ets2panda/test/runtime/ets/java_style/async-func-overload-and-type-infer_java.ets new file mode 100644 index 0000000000..0c123b21f3 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/async-func-overload-and-type-infer_java.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024-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. + */ + +async function multipleReturns2(flag: boolean, foo: int) { + if (flag) { + return foo; + } + + return "string1" +} + + +async function multipleReturns1(flag: boolean): Promise { + if (flag) { + return 2; + } + + return "string2" +} + +overload multipleReturns{ multipleReturns1, multipleReturns2 } + +function main() { + let a: int | string = await multipleReturns(true, 42); + let b: int | string = await multipleReturns(false); + + assertEQ(a, 42) + assertEQ(b, "string2") +} diff --git a/ets2panda/test/runtime/ets/java_style/async_method_with_same_name02_java.ets b/ets2panda/test/runtime/ets/java_style/async_method_with_same_name02_java.ets new file mode 100644 index 0000000000..5807f03482 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/async_method_with_same_name02_java.ets @@ -0,0 +1,31 @@ +/* + * 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 A { + static async getData1() { + return "I'm a static method1 with name 'getData'"; + } + static async getData2(s: string) { + return s; + } + overload getData{ getData1, getData2 } +} + +function main() { + let res1: string = await A.getData() + let res2: string = await A.getData("I'm a static method2 with name 'getData'") + assertEQ(res1, "I'm a static method1 with name 'getData'") + assertEQ(res2, "I'm a static method2 with name 'getData'") +} diff --git a/ets2panda/test/runtime/ets/java_style/conditionalExpressionLUB_java.ets b/ets2panda/test/runtime/ets/java_style/conditionalExpressionLUB_java.ets new file mode 100644 index 0000000000..783a8e8846 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/conditionalExpressionLUB_java.ets @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021-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. + */ + +interface I { } + +class A { } +class B extends A implements I { } +final class C extends A implements I { } +final class D extends A { } +final class F extends B { } + +function foo6(p: Object): int { + return 1; +} + +function foo5(p: A): int { + return 2; +} + +function foo4(p: B): int { + return 3; +} + +function foo3(p: C): int { + return 4; +} + +function foo2(p: D): int { + return 5; +} + +function foo1(p: F): int { + return 6; +} + +overload foo{ foo1, foo2, foo3, foo4, foo5, foo6 } + +function getTrue(): boolean { + return true +} + +// #15276 foo(Object|null) and foo(Object) overloads +function foo7(p: Object | null): int { + return 7; +} + +function main(): void { + sameTypeLUB(); + objectLUB(); + forkSubtypeLUB(); +} + +function sameTypeLUB(): void { + let a: A = new A(); + let b: A = new A(); + let c = getTrue() ? a : b; + assertEQ(foo(c), 2) +} + +function objectLUB(): void { + let a: A = new A(); + let b: Int = 2; + let c = getTrue() ? a : b; + assertEQ(foo(c), 1) + + let arr: Int[] | null = null; + let d = getTrue() ? a : arr; + assertEQ(foo7(d), 7) +} + +function forkSubtypeLUB(): void { + let a: F = new F(); + let b: D = new D(); + let c = getTrue() ? a : b; + assertEQ(foo(c), 2) + let d: A = new A(); + let e = getTrue() ? a : b; + assertEQ(foo(e), 2) + let f: B = new B(); + let g = getTrue() ? a : f; + assertEQ(foo(g), 3) +} diff --git a/ets2panda/test/runtime/ets/java_style/constructor-issue_java.ets b/ets2panda/test/runtime/ets/java_style/constructor-issue_java.ets new file mode 100644 index 0000000000..2dad35c0a5 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/constructor-issue_java.ets @@ -0,0 +1,61 @@ +/* + * 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 A { + field: int = 5 + + constructor1(arg: boolean) { + } + + constructor2(arg?: int) { + if (arg == undefined) { + this.field += 20 + } else { + this.field += arg; + return; // Return statement is necessary to reproduce stack overflow and invalid constructor generation issue. + } + } + + overload constructor{ constructor1, constructor2 } +} + +class B { + field: int = 5; + + constructor2(arg: boolean) { + } + + constructor3(arg: int) { + this.field += arg; + return; // Return statement is necessary to reproduce stack overflow issue. + } + + constructor1() { + this(15); + this.field += 30; + } + + overload constructor{ constructor1, constructor2, constructor3 } +} + +function main(): void { + assertEQ(new A(true).field, 5); // field = 5 + assertEQ(new A(10).field, 15); // field = 5 + 10 + assertEQ(new A().field, 25); // field = 5 + 20 + + assertEQ(new B(true).field, 5); // field = 5 + assertEQ(new B(20).field, 25); // field = 5 + 20 + assertEQ(new B().field, 50); // field = 5 + 15 + 30 +} diff --git a/ets2panda/test/runtime/ets/java_style/constructor_overload_sig_with_ref_type_java.ets b/ets2panda/test/runtime/ets/java_style/constructor_overload_sig_with_ref_type_java.ets new file mode 100644 index 0000000000..c97b686eae --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/constructor_overload_sig_with_ref_type_java.ets @@ -0,0 +1,41 @@ +/* + * 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 A{ + a:Int = 1 + constructor1(){} + constructor2(a:Int){} + overload constructor{ constructor1, constructor2 } +} + +class B{ + a:Number = 1 + constructor1(){} + constructor2(a:Number){} + overload constructor{ constructor1, constructor2 } +} + +class C{ + a:String = "1" + constructor1(){} + constructor2(a:String){} + overload constructor{ constructor1, constructor2 } +} + +function main() : void { + let a = new A(5); + let b = new B(5); + let c = new C("5"); +} diff --git a/ets2panda/test/runtime/ets/java_style/default_parameter1_java.ets b/ets2panda/test/runtime/ets/java_style/default_parameter1_java.ets new file mode 100644 index 0000000000..6d738ba639 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/default_parameter1_java.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo2(a: int, b: int = 10): int { + return a + b; +} + +function foo1(a: int): int { + return a; +} + +overload foo{ foo1, foo2 } diff --git a/ets2panda/test/runtime/ets/java_style/extensionFunctionDuplicatedWithNormalFunction_java.ets b/ets2panda/test/runtime/ets/java_style/extensionFunctionDuplicatedWithNormalFunction_java.ets new file mode 100644 index 0000000000..6c8eb7c34a --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/extensionFunctionDuplicatedWithNormalFunction_java.ets @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023-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 Fruit { + +} + +function name2(this: Fruit): int { + return 1; +} + +function name1(): int { + return 2; +} + +overload name{ name1, name2 } + +function asFuncCall() { + let fruit = new Fruit(); + let res: int = name(fruit); + assertEQ(res, 1) + let res1: int = name(); + assertEQ(res1, 2) +} + +function asMethodCall() { + let fruit = new Fruit(); + let res: int = fruit.name(); + assertEQ(res, 1) + let res1: int = name(); + assertEQ(res1, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/java_style/extensionFunctionOverrideSuperclassExtension_java.ets b/ets2panda/test/runtime/ets/java_style/extensionFunctionOverrideSuperclassExtension_java.ets new file mode 100644 index 0000000000..80155489bb --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/extensionFunctionOverrideSuperclassExtension_java.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023-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 Fruit { +} + +class Banana extends Fruit { + +} + +function name2(this: Fruit): int { + return 1; +} + +function name1(this: Banana): int { + return 2; +} + +overload name{ name1, name2 } + +function asFuncCall() { + let banana = new Banana(); + let res: int = name(banana); + assertEQ(res, 2) +} + +function asMethodCall() { + let banana = new Banana(); + let res: int = banana.name(); + assertEQ(res, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/java_style/extensionFunctionWithAsExpression_java.ets b/ets2panda/test/runtime/ets/java_style/extensionFunctionWithAsExpression_java.ets new file mode 100644 index 0000000000..529296b639 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/extensionFunctionWithAsExpression_java.ets @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023-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. + */ + +interface Fruit { } + +function foo2(this: Fruit): int { + return 1; +} + +class Banana implements Fruit { } + +function foo1(this: Banana): int { + return 2 +} + +overload foo{ foo1, foo2 } + +function asFuncCall() { + let banana = new Banana(); + let res: int = foo(banana); + assertEQ(res, 2) + let res1: int = foo((banana as Fruit)); + assertEQ(res1, 1) +} + +function asMethodCall() { + let banana = new Banana(); + let res: int = banana.foo(); + assertEQ(res, 2) + let res1: int = (banana as Fruit).foo(); + assertEQ(res1, 1) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/java_style/extensionFunctionWithTrailingLambda_java.ets b/ets2panda/test/runtime/ets/java_style/extensionFunctionWithTrailingLambda_java.ets new file mode 100644 index 0000000000..5ab8c61682 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/extensionFunctionWithTrailingLambda_java.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023-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 Fruit { +} + +let x: int = 1; + +function foo2(this: Fruit, cb: () => void): int { + cb(); + return 3; +} + +function foo1(cb: () => void): int { + cb(); + return 1; +} + +overload foo{ foo1, foo2 } + +function asFuncCall() { + x = 1; + let fruit = new Fruit(); + let res: int = foo(fruit) { x++ }; + assertEQ(res, 3) + assertEQ(x, 2) +} + +function asMethodCall() { + x = 1; + let fruit = new Fruit(); + let res: int = fruit.foo() { x++ }; + assertEQ(res, 3) + assertEQ(x, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/java_style/generic-set_java.ets b/ets2panda/test/runtime/ets/java_style/generic-set_java.ets new file mode 100644 index 0000000000..70fa889b6b --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/generic-set_java.ets @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024-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. + */ + +interface MyStack { + getTop(): Item | null; + pop(): Item | null; + push(item: Item): void; + isEmpty(): boolean; + size(): int; +} + +class MyArrayStack implements MyStack { + + private container: (Item | undefined)[]; + private top: int = -1; + private readonly DEFAULT_SIZE: int = 100; + + constructor1() { + this.container = new (Item | undefined)[this.DEFAULT_SIZE]; + this.top = -1; + } + + constructor2(size: int) { + this.container = new (Item | undefined)[size]; + } + + overload constructor{ constructor1, constructor2 } + + override getTop(): Item | null { + if (this.top == -1) { + return null; + } + return this.container[this.top]!; + } + + override pop(): Item | null { + if (this.top == -1) { + return null; + } + return this.container[this.top--]!; + } + + override push(item: Item): void { + this.container[++this.top] = item; + } + + override isEmpty(): boolean { + return (this.top == -1); + } + + override size(): int { + return (this.top + 1); + } +} + +function main(): void { + let stack1: MyArrayStack; + stack1 = new MyArrayStack(); + assertEQ(stack1.isEmpty(), true) + assertEQ(stack1.size(), 0) + + stack1.push(new Int(10)); + assertEQ(stack1.isEmpty(), false) + assertEQ(stack1.size(), 1) + assertEQ((stack1.getTop() as Int), 10) + + stack1.push(new Int(20)); + stack1.push(new Int(30)); + assertEQ(stack1.isEmpty(), false) + assertEQ(stack1.size(), 3) + assertEQ((stack1.getTop() as Int), 30) + + assertEQ((stack1.pop() as Int), 30) + assertEQ(stack1.size(), 2) + assertEQ((stack1.pop() as Int), 20) + assertEQ(stack1.size(), 1) + assertEQ((stack1.pop() as Int), 10) + assertEQ(stack1.size(), 0) + assertEQ(stack1.isEmpty(), true) + + assertEQ(stack1.getTop(), null) + assertEQ(stack1.pop(), null) + assertEQ(stack1.size(), 0) + assertEQ(stack1.isEmpty(), true) + +} diff --git a/ets2panda/test/runtime/ets/java_style/generic_union_java.ets b/ets2panda/test/runtime/ets/java_style/generic_union_java.ets new file mode 100644 index 0000000000..b11df7b6ea --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/generic_union_java.ets @@ -0,0 +1,38 @@ +/* + * 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 A { + foo2(a: int) { } + foo1() { + return 1 + } + overload foo{ foo1, foo2 } +} + +class B { + foo2(a: int) { } + foo1() { + return 2 + } + overload foo{ foo1, foo2 } +} +type AAA = A | B +function foo(a: T) { + return a.foo() +} +function main() { + assertEQ(foo(new A()), 1) + assertEQ(foo(new B()), 2) +} diff --git a/ets2panda/test/runtime/ets/java_style/keyword_get_set_as_identfier2_java.ets b/ets2panda/test/runtime/ets/java_style/keyword_get_set_as_identfier2_java.ets new file mode 100644 index 0000000000..ce2382bd3b --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/keyword_get_set_as_identfier2_java.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +export class set { + constructor1() { } + constructor2(a: int) { }; + overload constructor{ constructor1, constructor2 } + get set(): int { return 1; } + $_get(index: number): set { return new set } + $_set(index: number, value: set) { } +} + +let get: set +get = new set(); + +new + set(1) + +get[get.set] = get[0] diff --git a/ets2panda/test/runtime/ets/java_style/lambda_with_receiver_trailing_name_duplicated_java.ets b/ets2panda/test/runtime/ets/java_style/lambda_with_receiver_trailing_name_duplicated_java.ets new file mode 100644 index 0000000000..e53c304692 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/lambda_with_receiver_trailing_name_duplicated_java.ets @@ -0,0 +1,85 @@ +/* + * 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 A { + data: number = 0; + foo1(n: number, f: (this: A) => void) { + this.f(); + } + + foo2(n: number, f: () => void) { + f(); + } + + overload foo{ foo1, foo2 } +} + +function foo1(this: A, f: (this: A) => void) { + this.f(); +} + +function foo2(this: A, n: number, n2: number, f: () => void) { + f(); +} + +function foo3(this: A, f: (this: A) => void, n: number) { + this.f(); +} + +function foo4(a: A, n: number, f: (this: A) => void) { + a.f(); +} + +function foo5(a: A, n: number, f: () => void) { + f(); +} + +overload foo{ foo1, foo2, foo3, foo4, foo5 } + +let a = new A(); +// function with receiver append trailing lambda with receiver. +a.foo(){ this.data = 1; } +assertEQ(a.data, 1); +foo(a) { this.data = 2; } +assertEQ(a.data, 2); + +// function with receiver append trailing lambda. +a.foo(1, 2){ a.data = -1; } +assertEQ(a.data, -1); +foo(a, 1, 2) { a.data = -2; } +assertEQ(a.data, -2); + +// function with receiver contain param as lambda with receiver. +let n: number = 3; +a.foo((this: A): void => { this.data = n }, n); +assertEQ(a.data, 3); +foo(a, (this: A): void => { this.data = n + 1 }, n); +assertEQ(a.data, 4); + +// normal function append trailing lambda with receiver. +foo(a, n + 2){ this.data = n + 2 }; +assertEQ(a.data, 5); + +// method append trailing lambda with receiver. +a.foo(n + 3){ this.data = n + 3 }; +assertEQ(a.data, 6); + +// method append normal trailing lambda. +a.foo(n + 4){ a.data = n + 4 } +assertEQ(a.data, 7); + +// normal function append normal trailing lambda. +foo(a, n + 5){ a.data = n + 5 } +assertEQ(a.data, 8); diff --git a/ets2panda/test/runtime/ets/java_style/mostSpecificMethod_java.ets b/ets2panda/test/runtime/ets/java_style/mostSpecificMethod_java.ets new file mode 100644 index 0000000000..a93047d41d --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/mostSpecificMethod_java.ets @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2023-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 A { } +class B extends A { } + +class KlassA { + public Foo(): String { + return "KlassA::Foo()"; + } + + public Bar(): String { + return "KlassA::Bar()"; + } + + // NOTE(vpukhov): Baz order affects override resoluition! + public Baz1(x: A, y: B): String { + return "KlassA::Baz(B, B)"; + } + + public final Baz2(x: A, y: A): String { + return "KlassA::Baz(A, A)"; + } + + overload Baz{ Baz1, Baz2 } +} + +class KlassB extends KlassA { + public override Foo(): String { + return "KlassB::Foo()"; + } + + public override Bar(): String { + return "KlassB::Bar()"; + } + //这个重写能生效吗 + public override Baz(x: A, y: B): String { + return "KlassB::Baz(A, B)"; + } +} + +class KlassC extends KlassB { + public override Foo(): String { + return "KlassC::Foo()"; + } + + //需要写重载吗 + public Baz(x: B, y: B): String { + return "KlassC::Baz(B, B)"; + } + + public final CheckThisAndSuper(): void { + assertEQ(this.Foo(), "KlassC::Foo()"); + assertEQ(super.Foo(), "KlassB::Foo()"); + } +} + +function Foo1(x: A, y: B): String { return "GLOBAL::Foo(A, B)"; } +function Foo2(x: B, y: B): String { return "GLOBAL::Foo(B, B)"; } + +overload Foo{ Foo1, Foo2 } + +function Bar11(x: int): String { return "GLOBAL::Bar(int)"; } +function Bar12(x: short): String { return "GLOBAL::Bar(short)"; } +function Bar13(x: char): String { return "GLOBAL::Bar(char)"; } +function Bar14(x: double): String { return "GLOBAL::Bar(double)"; } + +overload Bar{ Bar11, Bar12, Bar13, Bar14 } + +function Bar21(x: long): String { return "GLOBAL::Bar2(long)"; } +function Bar22(x: double): String { return "GLOBAL::Bar2(double)"; } + +overload Bar2{ Bar21, Bar22 } + +function Baz5(x: Object): String { return "GLOBAL::Baz(Object)"; } +function Baz1(x: Int): String { return "GLOBAL::Baz(Int)"; } +function Baz2(x: Short): String { return "GLOBAL::Baz(Short)"; } +function Baz3(x: Char): String { return "GLOBAL::Baz(Char)"; } +function Baz4(x: String): String { return "GLOBAL::Baz(String)"; } + +overload Baz{ Baz1, Baz2, Baz3, Baz4, Baz5 } + +function main(): void { + let a: KlassA = new KlassA(); + let b: KlassB = new KlassB(); + let c: KlassC = new KlassC(); + + assertEQ(a.Foo(), "KlassA::Foo()"); + assertEQ(b.Foo(), "KlassB::Foo()"); + assertEQ(c.Foo(), "KlassC::Foo()"); + + let d: KlassA = new KlassC(); + let f: KlassB = new KlassC(); + + assertEQ(d.Foo(), "KlassC::Foo()"); + assertEQ(f.Foo(), "KlassC::Foo()"); + assertEQ(c.Bar(), "KlassB::Bar()"); + + let objA: A = new A(); + let objB: B = new B(); + + assertEQ(c.Baz(objB, objB), "KlassC::Baz(B, B)"); + assertEQ(c.Baz(objA, objB), "KlassB::Baz(A, B)"); + assertEQ(c.Baz(objA, objA), "KlassA::Baz(A, A)"); + + c.CheckThisAndSuper(); + + assertEQ(Foo(objA, objB), "GLOBAL::Foo(A, B)"); + assertEQ(Foo(objB, objB), "GLOBAL::Foo(B, B)"); + + assertEQ(Baz(new Int(1)), "GLOBAL::Baz(Int)"); + assertEQ(Baz(new Char(c'1')), "GLOBAL::Baz(Char)"); + assertEQ(Baz(new Short(1 as short)), "GLOBAL::Baz(Short)"); + assertEQ(Baz("hello"), "GLOBAL::Baz(String)"); + + assertEQ(Bar(1), "GLOBAL::Bar(int)"); + assertEQ(Bar(c'1'), "GLOBAL::Bar(char)"); + assertEQ(Bar(3.14), "GLOBAL::Bar(double)"); + + assertEQ(Bar2(1), "GLOBAL::Bar2(long)"); + assertEQ(Bar2(1 as long), "GLOBAL::Bar2(long)"); + assertEQ(Bar2(1.1), "GLOBAL::Bar2(double)"); + +} diff --git a/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_2_java.ets b/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_2_java.ets new file mode 100644 index 0000000000..5001cbc577 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_2_java.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024-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. + */ + +function foo1(...x: FixedArray) { + return 1; +} + +function foo2(...x: FixedArray) { + return 2; +} + +overload foo{ foo1, foo2 } + +assertEQ(foo("bar"), 1) +assertEQ(foo(1), 2) diff --git a/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_3_java.ets b/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_3_java.ets new file mode 100644 index 0000000000..86eee8243b --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/most_specific_method_with_rest_param_3_java.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024-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. + */ + +type B = Object | null; +function foo2(...b : B[]) { return 1; } +function foo1(str: String) { return 2; } +overload foo{ foo1, foo2 } + +assertEQ(foo(new String()), 2) diff --git a/ets2panda/test/runtime/ets/java_style/notNull_java.ets b/ets2panda/test/runtime/ets/java_style/notNull_java.ets new file mode 100644 index 0000000000..20e69093fc --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/notNull_java.ets @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2023-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. + */ + +function main() { + testLocalIdentifier() + testArgument() + testCallExpression() + testNPE() + testFieldAccess() + testFieldAccess2() +} + +function typeOf1(i: Int): int { + return 1; +} + +function typeOf2(o: Object): int { + return -1; +} + +overload typeOf{ typeOf1, typeOf2 } + +function dummy(): Int | null { + return 5; +} + +function testLocalIdentifier(): void { + let a: Int | null = dummy(); + let b = a!; + + assertEQ(typeOf(b), 1, "b must be type Int") + assertEQ(b, 5, "b must be 5") + + let c = b + a! * 7; + + assertEQ(c, 40, "c should be 40") + + assertEQ(a!, (a)!) +} + +function bar(arg: Int | null): Int { + return arg!; +} + +function testArgument(): void { + let a: Int | null = 7; + let b = bar(a); + + assertEQ(typeOf(b), 1, "b must be type Int") + assertEQ(b, 7) +} + +function foo(): Double | null { + return new Double(3.1415); +} + +class Foo { + foo(): Float | null { + return Float.DELTA; + } +} + +function testCallExpression() { + let d = foo()! + + assertTrue(d instanceof Double, "d must be type Double") + assertEQ(d, 3.1415, "d must be 3.1415") + + let sd = foo()!.toString() + + assertTrue("3.1415".equals(sd), "s must be '3.1415'") + + let f = new Foo().foo()! + + assertTrue(f instanceof Float, "f must be type Float") + assertEQ(f, Float.DELTA, "f must be Float.DELTA") +} + +function baz(): Byte | null { + return null; +} + +function aux(): Object | null { + return null; +} + +function testNPE(): void { + let o: Object | null = aux(); + let npe_caught = false; + + try { + (o as Object | null)!; + assertTrue(false, "this must not be executed") + } catch (ex: NullPointerError) { + npe_caught = true; + } + + assertTrue(npe_caught, "NPE must be caught") + + try { + baz()!; + assertTrue(false, "this must not be executed") + } catch (ex: NullPointerError) { + npe_caught = true; + } + + assertTrue(npe_caught, "NPE must be caught") +} + +class A { + public static c: Char | null = c'C'; + public d: Char | null = c'D' +} + +class B { + public a: A | null; + + constructor(a: A | null) { + this.a = a; + } + + public setA(a: A) { + this.a = a; + } + + public getA(): A { + return this.a!; + } +} + +function testFieldAccess(): void { + let ac = A.c!; + assertEQ(ac, c'C', "ac should be 'C'") + + let a_orig = new A(); + let b = new B(a_orig); + + let a = b.a!; + + assertEQ(a, a_orig, "a should be the object referenced by a_orig") + + let d = b.a!.d!; + + assertEQ(d, c'D', "d should be 'D'") +} + +function testFieldAccess2(): void { + let a = new A(); + let b = new B(a); + + assertEQ(b.getA(), a, "getA should return the same object that was given to the constructor") + + let a2 = new A(); + b.setA(a2); + + assertEQ(b.getA(), a2, "getA should return the same object that was given to setA") +} diff --git a/ets2panda/test/runtime/ets/java_style/objectLiteral_java.ets b/ets2panda/test/runtime/ets/java_style/objectLiteral_java.ets new file mode 100644 index 0000000000..547318e3a0 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/objectLiteral_java.ets @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023-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 Base { + f: int +} + +class InnerValue { + v: int +} + +class C extends Base { + x: Int = 5 + s: String = "" + iv: InnerValue = {} +} + +function returnC(): C { + return { x: 99, f: 44, s: "qq", iv: { v: 77 } } // return statement +} + +function test1(c: int, f: int, x: int, s: String, ivv: int) { } // should not prevent calling the next fun +function test2(c: C, f: int, x: int, s: String, ivv: int) { + assertEQ(c.f, f) + assertEQ(c.x, x) + assertEQ(c.s, s) + assertEQ(c.iv.v, ivv) +} + +overload test{ test1, test2 } + +function main(): int { + let c: C = { // variable definition + "x": 7, + s: "sss", + }; + test(c, 0, 7, "sss", 0) + + let c2 = { // as construction + f: 4, + s: "qq" + } as C; + test(c2, 4, 5, "qq", 0) + + c = { f: 5, s: "zzz" } // assignment + test(c, 5, 5, "zzz", 0) + + test({ // function argument + f: 3, + x: 8, + s: "uhuh", + iv: { // object literal field + v: 55 + } + }, 3, 8, "uhuh", 55) + + test(returnC(), 44, 99, "qq", 77) + + let ca: C[] = [{ f: 42, s: "first" }, { f: 128, s: "second" }] // array elements + test(ca[1], 128, 5, "second", 0) + + return 0 +} diff --git a/ets2panda/test/runtime/ets/java_style/overload-primitive-and-object_java.ets b/ets2panda/test/runtime/ets/java_style/overload-primitive-and-object_java.ets new file mode 100644 index 0000000000..0c0b2c700a --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/overload-primitive-and-object_java.ets @@ -0,0 +1,41 @@ +/* + * 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. + */ + +let res: number = 0; +function foo1(i: int, j: int): void { + res = 19; +} +function foo2(i: number, j: Number): void { + res = 21; +} + +overload foo{ foo1, foo2 } + +function boo1(x: Number, y: number) { + res = 34; +} +function boo2(x: number, y: Number) { + res = 47; +} + +overload boo{ boo1, boo2 } + +function main(): void { + let n: Number = new Number(); + foo(1, n); + assertEQ(res, 21); + boo(6 as Number, 8); + assertEQ(res, 34); +} diff --git a/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-1_java.ets b/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-1_java.ets new file mode 100644 index 0000000000..4187ec7ecf --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-1_java.ets @@ -0,0 +1,37 @@ +/* + * 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. + */ + +interface I { + s: string; +} + +let res = 0; +function foo2(f1: number, ...args: (Object | null | undefined)[]) { + res = 1; +} + +function foo1(f1: number, i: I, ...args: (Object | null | undefined)[]) { + res = 2; +} + +overload foo{ foo1, foo2 } + +function main(): void { + const i: I = { s: "sss" }; + foo(1, "sss"); + assertEQ(res, 1); + foo(1, i as I, "sss"); + assertEQ(res, 2); +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-2_java.ets b/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-2_java.ets new file mode 100644 index 0000000000..9c1ef9585c --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/overload-resolution-rest-2_java.ets @@ -0,0 +1,28 @@ +/* + * 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 Base { } +class Derived extends Base { } +type BaseOrDerived = Base | Derived; +let res = 0; + +function bar2(p1: Base, p2: Derived, p3: BaseOrDerived) { res = 1; } +function bar1(p1: Derived, p2: Base, p3: BaseOrDerived) { res = 2; } +function bar3(p1: Base, p2: Base, ...p3: BaseOrDerived[]) { res = 3; } + +overload bar{ bar1, bar2, bar3 } + +bar(new Derived, new Base, new Derived); +assertEQ(res, 2); \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/java_style/overload_1_java.ets b/ets2panda/test/runtime/ets/java_style/overload_1_java.ets new file mode 100644 index 0000000000..3e6a3af3d2 --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/overload_1_java.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. + */ + +class C { + static $_invoke1() { return C('abc') } + static $_invoke2(p: string) { return p } + overload static $_invoke{ $_invoke1, $_invoke2 } +} + +function main(): int { + if (C() != 'abc') return 1 + return 0 +} diff --git a/ets2panda/test/runtime/ets/java_style/signature_match_lambda_java.ets b/ets2panda/test/runtime/ets/java_style/signature_match_lambda_java.ets new file mode 100644 index 0000000000..328a5c6abd --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/signature_match_lambda_java.ets @@ -0,0 +1,43 @@ +/* + * 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 A { + catch2(onRejected: () => U | PromiseLike): string { + onRejected() + return "first catch is matched" + } + catch1(onRejected: (error: Error) => U | PromiseLike): string { + onRejected(new Error()) + return "second catch is matched" + } + catch3(onRejected: (a: Error, b: Error) => U | PromiseLike): string { + onRejected(new Error(), new Error()) + return "third catch is matched" + } + catch4(onRejected: (a: Error, b: Error, c: Error) => U | PromiseLike): string { + onRejected(new Error(), new Error(), new Error()) + return "fourth catch is matched" + } + overload catch{ catch1, catch2, catch3, catch4 } +} + +function main() { + let a = new A() + assertEQ(a.catch(() => { }), "first catch is matched") + assertEQ(a.catch((e: Error | undefined | null) => { }), "second catch is matched") + assertEQ(a.catch((e: Error) => { }), "second catch is matched") + assertEQ(a.catch((e: Error, e2: Error) => { }), "third catch is matched") + assertEQ(a.catch((e: Error, e2: Error, e3: Error) => { }), "fourth catch is matched") +} diff --git a/ets2panda/test/runtime/ets/java_style/visible_signatures_java.ets b/ets2panda/test/runtime/ets/java_style/visible_signatures_java.ets new file mode 100644 index 0000000000..7e85d899eb --- /dev/null +++ b/ets2panda/test/runtime/ets/java_style/visible_signatures_java.ets @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024-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 A { + foo11(a: Long): Int { + return 1; + } + private foo12(a: Int): Int { + return 0; + } + + overload foo{ foo11, foo12 } + + foo21(a: Int): Int { + return 1; + } + private foo22(a: Long): Int { + return 0; + } + + overload foo2{ foo21, foo22 } + + foo31(a: Long): Int { + return 1; + } + private foo32(a: Int): Int { + return 0; + } + + overload foo3{ foo31, foo32 } + + foo41(a: Int): Int { + return 1; + } + protected foo42(a: Long): Int { + return 0; + } + + overload foo4{ foo41, foo42 } +} + +function main(): void { + assertEQ(new A().foo(3), 1) + assertEQ(new A().foo2(3), 1) + assertEQ(new A().foo3(3), 1) + assertEQ(new A().foo4(3), 1) +} diff --git a/ets2panda/test/runtime/ets/ts_style/15445_ts.ets b/ets2panda/test/runtime/ets/ts_style/15445_ts.ets new file mode 100644 index 0000000000..f1ce213fce --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/15445_ts.ets @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024-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 A { + public static foo(fn: (a1: A1) => void, arg: A1): void + public static foo(fn: (a1: A1, a2: A2) => void, arg1: A1, arg2: A2): void + public static foo(...args: Any[]): Any { + if (args.length === 2) { + return args[0](args[1]); + } else if (args.length === 3) { + return args[0](args[1], args[2]); + } + } +} + +function t1(s: String): void { +} + +function t2(s: String, n: int): void { +} + +function main(): int { + A.foo(t1, "lll"); + A.foo(t2, "llll", 1) + return 0 +} diff --git a/ets2panda/test/runtime/ets/ts_style/15502_ts.ets b/ets2panda/test/runtime/ets/ts_style/15502_ts.ets new file mode 100644 index 0000000000..bbd7c2e279 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/15502_ts.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024-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 A { + public static foo(fn: (a: A1) => void, arg: A1): void + public static foo(fn: (a1: A1, a2: A2) => void, arg1: A1, arg2: A2): void + public static foo(...args: Any[]): Any { + if (args.length < 2) { + throw new Error("At least 2 arguments required"); + } + if (args.length === 2) { + return args[0](args[1]); + } else if (args.length === 3) { + return args[0](args[1], args[2]); + } + } +} + +function t1(s: String): void { +} + +function t2(s: String, n: int): void { +} + +function main(): int { + A.foo(t1, "lll"); + A.foo(t2, "llll", 1) + return 0 +} diff --git a/ets2panda/test/runtime/ets/ts_style/Enum2_ts.ets b/ets2panda/test/runtime/ets/ts_style/Enum2_ts.ets new file mode 100644 index 0000000000..38f514a1be --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/Enum2_ts.ets @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023-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. + */ + +enum Color { Red, Green, Blue } + +class ColorEnumDelegate { + private static values_: Color[] = [Color.Red, Color.Green, Color.Blue]; + private static names_: String[] = ["Red", "Green", "Blue"]; + + public static values(): Color[] { return ColorEnumDelegate.values_; } + + public static ordinal(x: Color): int { return x as int; } + + public static toString(x: Color): String { return ColorEnumDelegate.names_[x as int]; } + + public static valueOf(name: String): Color + public static valueOf(name: int): Color + public static valueOf(...args: Any[]): Any { + if (args.length === 1 && typeof args[0] === 'string') { + for (let i = 0; i < ColorEnumDelegate.values_.length; i++) { + if (ColorEnumDelegate.names_[i] == args[0]) { + return ColorEnumDelegate.values_[i]; + } + } + } else if (args.length === 1 && typeof args[0] === 'int') { + if (0 <= args[0] && args[0] < ColorEnumDelegate.values_.length) { + return ColorEnumDelegate.values_[args[0]]; + } + } + assertTrue(false, "No enum constant Color") + return Color.Red; + } +} + +function main(): void { + let red: Color = Color.Red; + assertEQ(ColorEnumDelegate.toString(red), "Red") + + assertEQ(ColorEnumDelegate.ordinal(red), 0) + assertEQ(ColorEnumDelegate.ordinal(Color.Green), 1) + + assertEQ(ColorEnumDelegate.valueOf(2), Color.Blue) + assertEQ(ColorEnumDelegate.valueOf(ColorEnumDelegate.ordinal(Color.Red)), red) + + assertEQ(ColorEnumDelegate.valueOf("Green"), Color.Green) + assertEQ(ColorEnumDelegate.valueOf(ColorEnumDelegate.toString(Color.Blue)), Color.Blue) +} diff --git a/ets2panda/test/runtime/ets/ts_style/FixFunctionCall_ts.ets b/ets2panda/test/runtime/ets/ts_style/FixFunctionCall_ts.ets new file mode 100644 index 0000000000..483ba79c4b --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/FixFunctionCall_ts.ets @@ -0,0 +1,29 @@ +/* + * 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. + */ + +export type AsyncCallback = (err: T, data: T) => void; + +function getBundleInfo(userId?: number): void +function getBundleInfo(callback: AsyncCallback): void +function getBundleInfo(...args: Any[]): Any { + if (args.length === 1 && args[0] instanceof AsyncCallback) { + args[0](666, 66); + } +} + +getBundleInfo((err, data) => { + assertEQ(err, 666); + assertEQ(data, 66) +}); diff --git a/ets2panda/test/runtime/ets/ts_style/FunctionOverload_ts.ets b/ets2panda/test/runtime/ets/ts_style/FunctionOverload_ts.ets new file mode 100644 index 0000000000..09043fdae4 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/FunctionOverload_ts.ets @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024-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 Overload { + public static func(): string + + public static func(a: number): string + + public static func(a: number, b: number): string + + public static func(...args: number[]): string + + public static func(...args: Any[]): Any { + switch (args.length) { + case 0: + return "zero"; + case 1: + return "one"; + case 2: + return "two"; + default: + return "variadic"; + } + } +} + +function main(): void { + assertEQ(Overload.func(), "zero") + assertEQ(Overload.func(1), "one") + assertEQ(Overload.func(1, 2), "two") + assertEQ(Overload.func(1, 2, 3), "variadic") + + let arr: number[] = [1, 2]; + assertEQ(Overload.func(...arr), "variadic") +} diff --git a/ets2panda/test/runtime/ets/ts_style/InterfacePrivateMethod2_ts.ets b/ets2panda/test/runtime/ets/ts_style/InterfacePrivateMethod2_ts.ets new file mode 100644 index 0000000000..ca60941c58 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/InterfacePrivateMethod2_ts.ets @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023-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. + */ + +interface Vehicle { + getHorsePower3(rpm: int, trq: int): int + + getPwrIndex(trq: int): int + + getHorsePower3(rpm: int): int + + getPwrIndex(trq: int, second: int): int + + getHorsePower3(...args: Any[]): Any { + if (args.length === 1) { + return args[0]; + } else if (args.length === 2) { + return (args[0] * args[1]) / 5252; + } + throw new Error("Invalid number of arguments"); + } + + getPwrIndex(...args: Any[]): Any { + if (args.length === 1) { + return this.getHorsePower3(5252, args[0]); + } else if (args.length === 2) { + return this.getHorsePower3(args[0] + args[1]); + } + throw new Error("Invalid number of arguments"); + } +} + +class Car implements Vehicle { } + +function main(): void { + assertEQ(new Car().getPwrIndex(1), 1) + assertEQ(new Car().getPwrIndex(1, 1), 2) +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature01_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature01_ts.ets new file mode 100644 index 0000000000..300d23c436 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature01_ts.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo(a: int, b: int = 10): int + +function foo(a: int): int + +function foo(...args: Any[]): Any { + if (args.length === 1) { + return args[0]; + } else if (args.length === 2) { + return args[0] + args[1]; + } + throw new Error("Invalid arguments for foo()"); +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature02_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature02_ts.ets new file mode 100644 index 0000000000..55cfed808a --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature02_ts.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo(a: int, b?: int) + +function foo(a: int) + +function foo(...args: Any[]): Any { + if (args.length === 1) { + return args[0]; + } else if (args.length === 2) { + return args[0] + 2; + } + throw new Error("Invalid arguments for foo()"); +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature03_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature03_ts.ets new file mode 100644 index 0000000000..c24dbb200d --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature03_ts.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo(a: int, ...b: int[]): int + +function foo(a: int): int + +function foo(...args: Any[]): Any { + const a = args[0]; + if (args.length === 1) { + return a; + } else { + return a + 2; + } +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature04_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature04_ts.ets new file mode 100644 index 0000000000..88c32ae077 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature04_ts.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), 7); +} + +function foo(a: int, ...b: int[]): int + +function foo(a: int, b: int): int + +function foo(...args: Any[]): Any { + if (args.length === 2 && typeof args[0] === 'int') { + return args[0] + args[1]; + } else if (args.length > 2 || args.length === 1) { + return 0; + } +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature05_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature05_ts.ets new file mode 100644 index 0000000000..4718435af0 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature05_ts.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), "aaaa"); +} + +function foo(a: int, b: int): string + +function foo(a: int, ...b: int[]): string + +function foo(a: int, b: int, c?: int): string + +function foo(...args: Any[]): Any { + if (args.length === 2 && typeof args[0] === 'int' && typeof args[1] === 'int') { + return "aaaa"; + } else if (args.length === 3 && typeof args[0] === 'int' && typeof args[1] === 'int' && typeof args[2] === 'int') { + return "cccc" + } else if ((args.length === 1 && typeof args[0] === 'int') || args.length > 3) { + return "bbbb" + } +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature06_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature06_ts.ets new file mode 100644 index 0000000000..6897b44495 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature06_ts.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5, 2), "aaaa"); +} + +function foo(a: int, b: int): string + +function foo(a: int, ...b: int[]): string + +function foo(a: int, b: int, c: int = 10): string + +function foo(...args: Any[]): Any { + if (args.length === 2 && typeof args[0] === 'int' && typeof args[1] === 'int') { + return "aaaa"; + } else if (args.length === 3 && typeof args[0] === 'int' && typeof args[1] === 'int' && typeof args[2] === 'int') { + return "cccc" + } else if ((args.length === 1 && typeof args[0] === 'int') || args.length > 3) { + return "bbbb" + } +} diff --git a/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature07_ts.ets b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature07_ts.ets new file mode 100644 index 0000000000..3f6735fcc4 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/MostSpecificSignature07_ts.ets @@ -0,0 +1,39 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo("1"), 3); +} + +class A { } +class B { } + +function foo(x: string, y?: A) +function foo(x: string, y?: B) +function foo(x: string) + +function foo(...args: Any[]): Any { + if (args.length === 1 && typeof args[0] === 'string') { + return 3; + } else if (args.length === 2) { + if (args[1] instanceof A) { + return 1; + } + if (args[1] instanceof B) { + return 2; + } + } +} + diff --git a/ets2panda/test/runtime/ets/ts_style/Override-4_ts.ets b/ets2panda/test/runtime/ets/ts_style/Override-4_ts.ets new file mode 100644 index 0000000000..7d45a6fa27 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/Override-4_ts.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024-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 X {} +class Y extends X {} + +abstract class A { + foo(x: Y): string { return "A.foo(Y)" } + abstract foo(x: Double): String + foo2(x: Y): Object { return "A.foo(Y)" } +} + +class B extends A { + foo(x: X): string + foo(x: Floating): String + foo(...args: Any[]): Any { + if (args.length === 1 && args[0] instanceof X) { + return "B.foo(X)"; + } else if (args.length === 1 && args[0] instanceof Floating) { + return "B.foo(Floating)"; + } else if (args.length === 1 && args[0] instanceof Y) { + return "A.foo(Y)" + } + } +} + +class C extends A { + foo(x: Double): String + override foo2(x: Y): String + foo(...args: Any[]): Any { + if (args.length === 1 && args[0] instanceof Double) { + return "C.foo(Double)"; + } else if (args.length === 1 && args[0] instanceof Y) { + return "B.foo(Floating)"; + } + } +} + +function main() { + assertEQ(new B().foo(new Y()), "B.foo(X)") + assertEQ(new B().foo(new Double()), "B.foo(Floating)") + assertEQ(new C().foo2(new Y()), "C.foo2(Y)") +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/ts_style/RecordKeyTypeCheck_ts.ets b/ets2panda/test/runtime/ets/ts_style/RecordKeyTypeCheck_ts.ets new file mode 100644 index 0000000000..9aa165eb25 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/RecordKeyTypeCheck_ts.ets @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2024-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 A extends Numeric { + private value: int; + + public constructor() + + public constructor(value: int) + + public constructor(...args: Any[]): Any { + if (args.length === 0) { + this.value = 0; + } else if (args.length === 1 && typeof args[0] === 'int') { + this.value = args[0] + } + } + + public override toByte(): byte { + return Int.toByte(this.value); + } + + public override toShort(): short { + return Int.toShort(this.value); + } + + public override toInt(): int { + return this.value; + } + + public override toLong(): long { + return Int.toLong(this.value); + } + + public override toFloat(): float { + return Int.toFloat(this.value); + } + + public override toDouble(): double { + return Int.toDouble(this.value); + } + + public override toString(): String { + return new String(this.value); + } +} + +function main() { + let r: Record = { + 1: 1, + } +} diff --git a/ets2panda/test/runtime/ets/ts_style/RestTuple4_ts.ets b/ets2panda/test/runtime/ets/ts_style/RestTuple4_ts.ets new file mode 100644 index 0000000000..91a150efc4 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/RestTuple4_ts.ets @@ -0,0 +1,52 @@ +/* + * 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 A { + public field: number + constructor(...numbers: [number, number, number]) + + constructor(a: int, ...numbers: [number, number, number]) + + constructor(...args: Any[]): Any { + let sum = 0; + const flattenedArgs = args.flat(); + for (const arg of flattenedArgs) { + if (typeof arg === 'number') { + sum += arg; + } + } + this.field = sum; + } +} + +function main() { + let tuple: [number, number, number] = [30, 40, 50] + + let res: A = new A(10, 20, 30) + let res2: A = new A(10, 20, 30, 40) + + let res3: A = new A(...tuple) + let res4: A = new A(10, ...tuple) + + let res5: A = new A(...([30, 40, 50] as [number, number, number])) + let res6: A = new A(10, ...([30, 40, 50] as [number, number, number])) + + assertEQ(res.field, 60) + assertEQ(res2.field, 100) + assertEQ(res3.field, 120) + assertEQ(res4.field, 130) + assertEQ(res5.field, 120) + assertEQ(res6.field, 130) +} diff --git a/ets2panda/test/runtime/ets/ts_style/RestTuple8_ts.ets b/ets2panda/test/runtime/ets/ts_style/RestTuple8_ts.ets new file mode 100644 index 0000000000..30f23d56e0 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/RestTuple8_ts.ets @@ -0,0 +1,50 @@ +/* + * 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 A { } +class B { } + +class C { + public field: boolean + + constructor(...p: [A, B]) + + constructor(a: int, ...p: [A, B]) + + constructor(...args: Any[]): Any { + const flattenedArgs = args.flat(); + if (flattenedArgs.length === 2) { + this.field = flattenedArgs[0] == flattenedArgs[1] + } else if (flattenedArgs.length === 3) { + this.field = flattenedArgs[1] == flattenedArgs[2] + } + } +} + +function main() { + let a1: [A, B] = [new A, new B] + + assertTrue((new C(...a1)).field == false) + assertTrue((new C(22, ...a1)).field == false) + + assertTrue((new C(...[new A, new B])).field == false) + assertTrue((new C(22, ...[new A, new B])).field == false) + + assertTrue((new C(...[new A, new B] as [A, B])).field == false) + assertTrue((new C(22, ...[new A, new B] as [A, B])).field == false) + + assertTrue((new C(new A, new B)).field == false) + assertTrue((new C(22, new A, new B)).field == false) +} diff --git a/ets2panda/test/runtime/ets/ts_style/SmartCast_03_ts.ets b/ets2panda/test/runtime/ets/ts_style/SmartCast_03_ts.ets new file mode 100644 index 0000000000..31df51d82b --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/SmartCast_03_ts.ets @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024-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 C { + constructor() + + constructor(a: int) + + constructor(...args: Any[]): Any { + if (args.length === 1 && typeof args[0] === 'int') { + this.x = args[0] + } + } + + bar(): string { + return "Class C"; + } + + baz(): int { + return this.x; + } + + private x: int = 7; +} + +function foo(c: Object | null | undefined): string { + if (c instanceof string && (c.length == 11 || c == "Test")) { + c = "Case 1"; + } else if (c instanceof C && c.baz() == 7) { + assertEQ(c.bar(), "Class C") + c = "Case 2"; + } else if (c instanceof Int && c >= 0) { + assertTrue(c >= 0) + c = "Case 3"; + } else if (c instanceof null) { + assertEQ(c, null) + c = "Case 4"; + } else { + c = "Case 5"; + } + + assertEQ(c.length, 6) + return c; +} + +function main(): void { + assertEQ(foo("Test string"), "Case 1") + assertEQ(foo("Test"), "Case 1") + assertEQ(foo("Test string 2"), "Case 5") + assertEQ(foo("test"), "Case 5") + + assertEQ(foo(new Int(5)), "Case 3") + assertEQ(foo(new Int(0)), "Case 3") + assertEQ(foo(new Int(-5)), "Case 5") + + assertEQ(foo(new C(7)), "Case 2") + assertEQ(foo(new C()), "Case 2") + assertEQ(foo(new C(17)), "Case 5") + + assertEQ(foo(null), "Case 4") + + assertEQ(foo(undefined), "Case 5") + assertEQ(foo(new Number(3.0)), "Case 5") +} diff --git a/ets2panda/test/runtime/ets/ts_style/async-func-overload-and-type-infer_ts.ets b/ets2panda/test/runtime/ets/ts_style/async-func-overload-and-type-infer_ts.ets new file mode 100644 index 0000000000..f7d2a9a3f4 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/async-func-overload-and-type-infer_ts.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024-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. + */ + +async function multipleReturns(flag: boolean, foo: int) + +async function multipleReturns(flag: boolean): Promise + +async function multipleReturns(...args: Any[]): Any { + if (args.length === 2 && typeof args[0] === 'boolean' && typeof args[1] === 'number') { + return args[0] ? args[1] : "string1"; + } + else if (args.length === 1 && typeof args[0] === 'boolean') { + return args[0] ? 2 : "string2"; + } + + throw new Error("Invalid arguments"); +} + +function main() { + let a: int | string = await multipleReturns(true, 42); + let b: int | string = await multipleReturns(false); + + assertEQ(a, 42) + assertEQ(b, "string2") +} diff --git a/ets2panda/test/runtime/ets/ts_style/async_method_with_same_name02_ts.ets b/ets2panda/test/runtime/ets/ts_style/async_method_with_same_name02_ts.ets new file mode 100644 index 0000000000..fc571e5128 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/async_method_with_same_name02_ts.ets @@ -0,0 +1,35 @@ +/* + * 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 A { + static async getData() + static async getData(s: string) + static async getData(...args: Any[]): Any { + if (args.length === 0) { + return "I'm a static method1 with name 'getData'"; + } else if (args.length === 1 && typeof args[0] === "string") { + return args[0]; + } + throw new Error("Invalid arguments"); + } +} + +function main() { + let res1: string = await A.getData() + let res2: string = await A.getData("I'm a static method2 with name 'getData'") + assertEQ(res1, "I'm a static method1 with name 'getData'") + assertEQ(res2, "I'm a static method2 with name 'getData'") +} + diff --git a/ets2panda/test/runtime/ets/ts_style/conditionalExpressionLUB_ts.ets b/ets2panda/test/runtime/ets/ts_style/conditionalExpressionLUB_ts.ets new file mode 100644 index 0000000000..6521288ebc --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/conditionalExpressionLUB_ts.ets @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2021-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. + */ + +interface I { } + +class A { } +class B extends A implements I { } +final class C extends A implements I { } +final class D extends A { } +final class F extends B { } + +function foo(p: Object): int + +function foo(p: A): int + +function foo(p: B): int + +function foo(p: C): int + +function foo(p: D): int + +function foo(p: F): int + +function foo(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof F) { + return 6; + } else if (args[0] instanceof D) { + return 5; + } else if (args[0] instanceof C) { + return 4; + } else if (args[0] instanceof B) { + return 3; + } else if (args[0] instanceof A) { + return 2; + } else if (args[0] instanceof Object) { + return 1; + } + } +} + +function getTrue(): boolean { + return true +} + +// #15276 foo(Object|null) and foo(Object) overloads +function foo7(p: Object | null): int { + return 7; +} + +function main(): void { + sameTypeLUB(); + objectLUB(); + forkSubtypeLUB(); +} + +function sameTypeLUB(): void { + let a: A = new A(); + let b: A = new A(); + let c = getTrue() ? a : b; + assertEQ(foo(c), 2) +} + +function objectLUB(): void { + let a: A = new A(); + let b: Int = 2; + let c = getTrue() ? a : b; + assertEQ(foo(c), 1) + + let arr: Int[] | null = null; + let d = getTrue() ? a : arr; + assertEQ(foo7(d), 7) +} + +function forkSubtypeLUB(): void { + let a: F = new F(); + let b: D = new D(); + let c = getTrue() ? a : b; + assertEQ(foo(c), 2) + let d: A = new A(); + let e = getTrue() ? a : b; + assertEQ(foo(e), 2) + let f: B = new B(); + let g = getTrue() ? a : f; + assertEQ(foo(g), 3) +} diff --git a/ets2panda/test/runtime/ets/ts_style/constructor-issue_ts.ets b/ets2panda/test/runtime/ets/ts_style/constructor-issue_ts.ets new file mode 100644 index 0000000000..60b98dc969 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/constructor-issue_ts.ets @@ -0,0 +1,72 @@ +/* + * 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 A { + field: int = 5 + + constructor(arg: boolean) + + constructor(arg?: int) + + constructor(...args: Any[]): Any { + if (args.length === 0 || args[0] === undefined) { + this.field += 20; + } else { + if (typeof args[0] === 'boolean') { + } else { + this.field += args[0]; + return; + } + } + } +} + +class B { + field: int = 5; + + constructor(arg: boolean) + + constructor(arg: int) + + constructor() + + constructor(...args: Any[]): Any { + if (args.length === 0) { + this.field += 15; + this.field += 30; + return; + } + + if (typeof args[0] === 'boolean') { + return; + } + + if (typeof args[0] === 'number') { + this.field += args[0]; + return; + } + } + +} + +function main(): void { + assertEQ(new A(true).field, 5); // field = 5 + assertEQ(new A(10).field, 15); // field = 5 + 10 + assertEQ(new A().field, 25); // field = 5 + 20 + + assertEQ(new B(true).field, 5); // field = 5 + assertEQ(new B(20).field, 25); // field = 5 + 20 + assertEQ(new B().field, 50); // field = 5 + 15 + 30 +} diff --git a/ets2panda/test/runtime/ets/ts_style/constructor_overload_sig_with_ref_type_ts.ets b/ets2panda/test/runtime/ets/ts_style/constructor_overload_sig_with_ref_type_ts.ets new file mode 100644 index 0000000000..83826cf7c7 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/constructor_overload_sig_with_ref_type_ts.ets @@ -0,0 +1,41 @@ +/* + * 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 A { + a: Int = 1 + constructor() + constructor(a: Int) + constructor(...args: Any[]): Any { } +} + +class B { + a: Number = 1 + constructor() + constructor(a: Number) + constructor(...args: Any[]): Any { } +} + +class C { + a: String = "1" + constructor() + constructor(a: String) + constructor(...args: Any[]): Any { } +} + +function main(): void { + let a = new A(5); + let b = new B(5); + let c = new C("5"); +} diff --git a/ets2panda/test/runtime/ets/ts_style/default_parameter1_ts.ets b/ets2panda/test/runtime/ets/ts_style/default_parameter1_ts.ets new file mode 100644 index 0000000000..b4a14e7bde --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/default_parameter1_ts.ets @@ -0,0 +1,33 @@ +/* + * 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. + */ + +function main(): void { + assertEQ(foo(5), 5); +} + +function foo(a: int, b: int = 10): int + +function foo(a: int): int + +function foo(...args: Any[]): Any { + if (args.length === 1) { + return args[0]; + } else if (args.length === 2) { + const b = args[1] !== undefined ? args[1] : 10; + return args[0] + b; + } + throw new Error("Invalid number of arguments"); + +} diff --git a/ets2panda/test/runtime/ets/ts_style/extensionFunctionDuplicatedWithNormalFunction_ts.ets b/ets2panda/test/runtime/ets/ts_style/extensionFunctionDuplicatedWithNormalFunction_ts.ets new file mode 100644 index 0000000000..406811a3a7 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/extensionFunctionDuplicatedWithNormalFunction_ts.ets @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023-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 Fruit { + +} + +function name(this: Fruit): int + +function name(): int + +function name(...args: Any[]): Any { + if (args.length === 0) { + return 2; + } else if (args.length === 1 && args[0] instanceof Fruit) { + return 1; + } +} + +function asFuncCall() { + let fruit = new Fruit(); + let res: int = name(fruit); + assertEQ(res, 1) + let res1: int = name(); + assertEQ(res1, 2) +} + +function asMethodCall() { + let fruit = new Fruit(); + let res: int = fruit.name(); + assertEQ(res, 1) + let res1: int = name(); + assertEQ(res1, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/ts_style/extensionFunctionOverrideSuperclassExtension_ts.ets b/ets2panda/test/runtime/ets/ts_style/extensionFunctionOverrideSuperclassExtension_ts.ets new file mode 100644 index 0000000000..c9108a6e2a --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/extensionFunctionOverrideSuperclassExtension_ts.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023-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 Fruit { +} + +class Banana extends Fruit { + +} + +function name(this: Fruit): int + +function name(this: Banana): int + +function name(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Banana) { + return 2; + } else if (args[0] instanceof Fruit) { + return 1; + } + } +} + +function asFuncCall() { + let banana = new Banana(); + let res: int = name(banana); + assertEQ(res, 2) +} + +function asMethodCall() { + let banana = new Banana(); + let res: int = banana.name(); + assertEQ(res, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithAsExpression_ts.ets b/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithAsExpression_ts.ets new file mode 100644 index 0000000000..762678c6b0 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithAsExpression_ts.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023-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. + */ + +interface Fruit { } + +function foo(this: Fruit): int + +class Banana implements Fruit { } + +function foo(this: Banana): int + +function foo(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Banana) { + return 2; + } else if (args[0] instanceof Fruit) { + return 1; + } + } +} + +function asFuncCall() { + let banana = new Banana(); + let res: int = foo(banana); + assertEQ(res, 2) + let res1: int = foo((banana as Fruit)); + assertEQ(res1, 1) +} + +function asMethodCall() { + let banana = new Banana(); + let res: int = banana.foo(); + assertEQ(res, 2) + let res1: int = (banana as Fruit).foo(); + assertEQ(res1, 1) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithTrailingLambda_ts.ets b/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithTrailingLambda_ts.ets new file mode 100644 index 0000000000..827d0e335b --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/extensionFunctionWithTrailingLambda_ts.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023-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 Fruit { +} + +let x: int = 1; + +function foo(this: Fruit, cb: () => void): int + +function foo(cb: () => void): int + +function foo(...args: Any[]): Any { + const cb = args[0]; + + if (typeof cb !== 'function') { + throw new Error('Callback must be a function'); + } + cb(); + if (args.length === 1 && this === undefined) { + return 1; + } else { + return 3; + } +} + +function asFuncCall() { + x = 1; + let fruit = new Fruit(); + let res: int = foo(fruit) { x++ }; + assertEQ(res, 3) + assertEQ(x, 2) +} + +function asMethodCall() { + x = 1; + let fruit = new Fruit(); + let res: int = fruit.foo() { x++ }; + assertEQ(res, 3) + assertEQ(x, 2) +} + +function main() { + asFuncCall(); + asMethodCall(); +} diff --git a/ets2panda/test/runtime/ets/ts_style/generic-set_ts.ets b/ets2panda/test/runtime/ets/ts_style/generic-set_ts.ets new file mode 100644 index 0000000000..db74e61d6b --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/generic-set_ts.ets @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2024-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. + */ + +interface MyStack { + getTop(): Item | null; + pop(): Item | null; + push(item: Item): void; + isEmpty(): boolean; + size(): int; +} + +class MyArrayStack implements MyStack { + + private container: (Item | undefined)[]; + private top: int = -1; + private readonly DEFAULT_SIZE: int = 100; + + constructor() + constructor(size: int) + + constructor(...args: Any[]): Any { + if (args.length === 0) { + this.container = new (Item | undefined)[this.DEFAULT_SIZE]; + this.top = -1; + } else if (args.length === 1 && typeof args[0] === "int") { + this.container = new (Item | undefined)[args[0]]; + } + } + + override getTop(): Item | null { + if (this.top == -1) { + return null; + } + return this.container[this.top]!; + } + + override pop(): Item | null { + if (this.top == -1) { + return null; + } + return this.container[this.top--]!; + } + + override push(item: Item): void { + this.container[++this.top] = item; + } + + override isEmpty(): boolean { + return (this.top == -1); + } + + override size(): int { + return (this.top + 1); + } +} + +function main(): void { + let stack1: MyArrayStack; + stack1 = new MyArrayStack(); + assertEQ(stack1.isEmpty(), true) + assertEQ(stack1.size(), 0) + + stack1.push(new Int(10)); + assertEQ(stack1.isEmpty(), false) + assertEQ(stack1.size(), 1) + assertEQ((stack1.getTop() as Int), 10) + + stack1.push(new Int(20)); + stack1.push(new Int(30)); + assertEQ(stack1.isEmpty(), false) + assertEQ(stack1.size(), 3) + assertEQ((stack1.getTop() as Int), 30) + + assertEQ((stack1.pop() as Int), 30) + assertEQ(stack1.size(), 2) + assertEQ((stack1.pop() as Int), 20) + assertEQ(stack1.size(), 1) + assertEQ((stack1.pop() as Int), 10) + assertEQ(stack1.size(), 0) + assertEQ(stack1.isEmpty(), true) + + assertEQ(stack1.getTop(), null) + assertEQ(stack1.pop(), null) + assertEQ(stack1.size(), 0) + assertEQ(stack1.isEmpty(), true) + +} diff --git a/ets2panda/test/runtime/ets/ts_style/generic_union_ts.ets b/ets2panda/test/runtime/ets/ts_style/generic_union_ts.ets new file mode 100644 index 0000000000..b6f23a4cf1 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/generic_union_ts.ets @@ -0,0 +1,42 @@ +/* + * 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 A { + foo(a: int) + foo() + foo(...args: Any[]): Any { + if (args.length === 0) { + return 1 + } + } +} + +class B { + foo(a: int) + foo() + foo(...args: Any[]): Any { + if (args.length === 0) { + return 2 + } + } +} +type AAA = A | B +function foo(a: T) { + return a.foo() +} +function main() { + assertEQ(foo(new A()), 1) + assertEQ(foo(new B()), 2) +} diff --git a/ets2panda/test/runtime/ets/ts_style/keyword_get_set_as_identfier2_ts.ets b/ets2panda/test/runtime/ets/ts_style/keyword_get_set_as_identfier2_ts.ets new file mode 100644 index 0000000000..6b10ffe3e1 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/keyword_get_set_as_identfier2_ts.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +export class set { + constructor() + constructor(a: int); + constructor(...args: Any[]): Any { } + get set(): int { return 1; } + $_get(index: number): set { return new set } + $_set(index: number, value: set) { } +} + +let get: set +get = new set(); + +new + set(1) + +get[get.set] = get[0] diff --git a/ets2panda/test/runtime/ets/ts_style/lambda_with_receiver_trailing_name_duplicated_ts.ets b/ets2panda/test/runtime/ets/ts_style/lambda_with_receiver_trailing_name_duplicated_ts.ets new file mode 100644 index 0000000000..a305ef9af2 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/lambda_with_receiver_trailing_name_duplicated_ts.ets @@ -0,0 +1,110 @@ +/* + * 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 A { + data: number = 0; + foo(n: number, f: (this: A) => void) + + foo(n: number, f: () => void) + + foo(...args: Any[]): Any { + if (args.length === 2 && typeof args[0] === "number" && typeof args[1] === "function") { + const f = args[1] as Function; + if (args[1].length === 0) { + f(); + } else if (args[1].length === 1) { + f.call(this); + } + } + } +} + +function foo(this: A, f: (this: A) => void) + +function foo(this: A, n: number, n2: number, f: () => void) + +function foo(this: A, f: (this: A) => void, n: number) + +function foo(a: A, n: number, f: (this: A) => void) + +function foo(a: A, n: number, f: () => void) + +function foo(...args: any[]): any { + if (args.length >= 1 && args.length <= 4) { + const firstArg = args[0]; + if (args.length === 2 && typeof args[1] === 'function') { + const f = args[1] as (this: any) => void; + f.call(firstArg); + return; + } + if (args.length === 4 && typeof args[3] === 'function') { + const f = args[3] as () => void; + f(); + return; + } + if (args.length === 3 && typeof args[1] === 'function' && typeof args[2] === 'number') { + const f = args[1] as (this: any) => void; + f.call(firstArg); + return; + } + if (args.length === 3 && typeof args[2] === 'function') { + const a = args[0]; + const f = args[2] as (this: any) => void; + f.call(a); + return; + } + if (args.length === 3 && typeof args[2] === 'function') { + const f = args[2] as () => void; + f(); + return; + } + } +} + +let a = new A(); +// function with receiver append trailing lambda with receiver. +a.foo(){ this.data = 1; } +assertEQ(a.data, 1); +foo(a) { this.data = 2; } +assertEQ(a.data, 2); + +// function with receiver append trailing lambda. +a.foo(1, 2){ a.data = -1; } +assertEQ(a.data, -1); +foo(a, 1, 2) { a.data = -2; } +assertEQ(a.data, -2); + +// function with receiver contain param as lambda with receiver. +let n: number = 3; +a.foo((this: A): void => { this.data = n }, n); +assertEQ(a.data, 3); +foo(a, (this: A): void => { this.data = n + 1 }, n); +assertEQ(a.data, 4); + +// normal function append trailing lambda with receiver. +foo(a, n + 2){ this.data = n + 2 }; +assertEQ(a.data, 5); + +// method append trailing lambda with receiver. +a.foo(n + 3){ this.data = n + 3 }; +assertEQ(a.data, 6); + +// method append normal trailing lambda. +a.foo(n + 4){ a.data = n + 4 } +assertEQ(a.data, 7); + +// normal function append normal trailing lambda. +foo(a, n + 5){ a.data = n + 5 } +assertEQ(a.data, 8); diff --git a/ets2panda/test/runtime/ets/ts_style/mostSpecificMethod_ts.ets b/ets2panda/test/runtime/ets/ts_style/mostSpecificMethod_ts.ets new file mode 100644 index 0000000000..3c44950c17 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/mostSpecificMethod_ts.ets @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2023-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 A { } +class B extends A { } + +class KlassA { + public Foo(): String { + return "KlassA::Foo()"; + } + + public Bar(): String { + return "KlassA::Bar()"; + } + + // NOTE(vpukhov): Baz order affects override resoluition! + public Baz(x: A, y: B): String + + public final Baz(x: A, y: A): String + + public Baz(...args: Any[]): Any { + if (args.length === 2 && args[0] instanceof A) { + if (args[1] instanceof A) { + return "KlassA::Baz(A, A)"; + } else if (args[1] instanceof B) { + return "KlassA::Baz(B, B)"; + } + } + } +} + +class KlassB extends KlassA { + public override Foo(): String { + return "KlassB::Foo()"; + } + + public override Bar(): String { + return "KlassB::Bar()"; + } + + public override Baz(x: A, y: B): String { + return "KlassB::Baz(A, B)"; + } +} + +class KlassC extends KlassB { + public override Foo(): String { + return "KlassC::Foo()"; + } + + public Baz(x: B, y: B): String { + return "KlassC::Baz(B, B)"; + } + + public final CheckThisAndSuper(): void { + assertEQ(this.Foo(), "KlassC::Foo()"); + assertEQ(super.Foo(), "KlassB::Foo()"); + } +} + +function Foo(x: A, y: B): String +function Foo(x: B, y: B): String + +function Foo(...args: Any[]): Any { + if (args.length !== 2) { + throw new Error("Foo requires exactly 2 arguments"); + } + if (args[0] instanceof A && args[1] instanceof B) { + return "GLOBAL::Foo(A, B)"; + } + else if (args[0] instanceof B && args[1] instanceof B) { + return "GLOBAL::Foo(B, B)"; + } + + throw new Error("No matching overload found for given arguments"); +} + + +function Bar(x: int): String +function Bar(x: short): String +function Bar(x: char): String +function Bar(x: double): String + +function Bar(...args: Any[]): Any { + if (args.length !== 1) { + throw new Error("Bar requires exactly 1 argument"); + } + const x = args[0]; + const type = typeof x; + + if (type === 'int') { + return "GLOBAL::Bar(int)"; + } else if (type === 'short') { + return "GLOBAL::Bar(short)"; + } else if (type === 'char') { + return "GLOBAL::Bar(char)"; + } else if (type === 'double') { + return "GLOBAL::Bar(double)"; + } + + throw new Error("No matching overload found for given argument"); +} + +function Bar2(x: long): String +function Bar2(x: double): String + +function Bar2(...args: Any[]): Any { + if (args.length !== 1) { + throw new Error("Bar2 requires exactly 1 argument"); + } + const x = args[0]; + const type = typeof x; + + if (type === 'long') { + return "GLOBAL::Bar2(long)"; + } else if (type === 'double') { + return "GLOBAL::Bar2(double)"; + } + + throw new Error("No matching overload found for given argument"); +} + +function Baz(x: Object): String +function Baz(x: Int): String +function Baz(x: Short): String +function Baz(x: Char): String +function Baz(x: String): String + +function Baz(...args: Any[]): Any { + if (args.length !== 1) { + throw new Error("Baz requires exactly 1 argument"); + } + const x = args[0]; + + if (x instanceof Int) { // String 对象 + return "GLOBAL::Baz(Int)"; + } else if (x instanceof Short) { + return "GLOBAL::Baz(Short)"; + } else if (x instanceof Char) { + return "GLOBAL::Baz(Char)"; + } else if (typeof x === 'string') { + return "GLOBAL::Baz(String)"; + } else if (typeof x === 'object') { + return "GLOBAL::Baz(Object)"; + } + + throw new Error("No matching overload found for given argument"); +} + +function main(): void { + let a: KlassA = new KlassA(); + let b: KlassB = new KlassB(); + let c: KlassC = new KlassC(); + + assertEQ(a.Foo(), "KlassA::Foo()"); + assertEQ(b.Foo(), "KlassB::Foo()"); + assertEQ(c.Foo(), "KlassC::Foo()"); + + let d: KlassA = new KlassC(); + let f: KlassB = new KlassC(); + + assertEQ(d.Foo(), "KlassC::Foo()"); + assertEQ(f.Foo(), "KlassC::Foo()"); + assertEQ(c.Bar(), "KlassB::Bar()"); + + let objA: A = new A(); + let objB: B = new B(); + + assertEQ(c.Baz(objB, objB), "KlassC::Baz(B, B)"); + assertEQ(c.Baz(objA, objB), "KlassB::Baz(A, B)"); + assertEQ(c.Baz(objA, objA), "KlassA::Baz(A, A)"); + + c.CheckThisAndSuper(); + + assertEQ(Foo(objA, objB), "GLOBAL::Foo(A, B)"); + assertEQ(Foo(objB, objB), "GLOBAL::Foo(B, B)"); + + assertEQ(Baz(new Int(1)), "GLOBAL::Baz(Int)"); + assertEQ(Baz(new Char(c'1')), "GLOBAL::Baz(Char)"); + assertEQ(Baz(new Short(1 as short)), "GLOBAL::Baz(Short)"); + assertEQ(Baz("hello"), "GLOBAL::Baz(String)"); + + assertEQ(Bar(1), "GLOBAL::Bar(int)"); + assertEQ(Bar(c'1'), "GLOBAL::Bar(char)"); + assertEQ(Bar(3.14), "GLOBAL::Bar(double)"); + + assertEQ(Bar2(1), "GLOBAL::Bar2(long)"); + assertEQ(Bar2(1 as long), "GLOBAL::Bar2(long)"); + assertEQ(Bar2(1.1), "GLOBAL::Bar2(double)"); + +} diff --git a/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_2_ts.ets b/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_2_ts.ets new file mode 100644 index 0000000000..be08b434d2 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_2_ts.ets @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024-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. + */ + +function foo(...x: FixedArray) + +function foo(...x: FixedArray) + +function foo(...args: Any[]): Any { + if (args.length === 0) { + throw new Error("At least one argument is required."); + } + + if (typeof args[0] === 'string') { + return 1; + } else if (typeof args[0] === 'number') { + return 2; + } +} + +assertEQ(foo("bar"), 1) +assertEQ(foo(1), 2) diff --git a/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_3_ts.ets b/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_3_ts.ets new file mode 100644 index 0000000000..9256e46022 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/most_specific_method_with_rest_param_3_ts.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024-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. + */ + +type B = Object | null; +function foo(...b: B[]) +function foo(str: String) + +function foo(...args: Any[]): Any { + if (args.length === 1 && typeof args[0] === 'string') { + return 2; + } else if (Array.isArray(args[0]) && args.length === 1) { + return 1; + } else if (args.every(arg => arg instanceof B)) { + return 1; + } else { + throw new Error("Invalid arguments"); + } +} + +assertEQ(foo(new String()), 2) diff --git a/ets2panda/test/runtime/ets/ts_style/notNull_ts.ets b/ets2panda/test/runtime/ets/ts_style/notNull_ts.ets new file mode 100644 index 0000000000..0736782364 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/notNull_ts.ets @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2023-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. + */ + +function main() { + testLocalIdentifier() + testArgument() + testCallExpression() + testNPE() + testFieldAccess() + testFieldAccess2() +} + +function typeOf(i: Int): int + +function typeOf(o: Object): int + +function typeOf(...args: Any[]): Any { + if (args.length !== 1) { + throw new Error("typeOf() expects exactly 1 argument"); + } + const arg = args[0]; + if (arg instanceof Int) { + return 1; + } else if (typeof arg === 'object' && arg !== null) { + return -1; + } else { + throw new Error("Unsupported argument type"); + } +} + +function dummy(): Int | null { + return 5; +} + +function testLocalIdentifier(): void { + let a: Int | null = dummy(); + let b = a!; + + assertEQ(typeOf(b), 1, "b must be type Int") + assertEQ(b, 5, "b must be 5") + + let c = b + a! * 7; + + assertEQ(c, 40, "c should be 40") + + assertEQ(a!, (a)!) +} + +function bar(arg: Int | null): Int { + return arg!; +} + +function testArgument(): void { + let a: Int | null = 7; + let b = bar(a); + + assertEQ(typeOf(b), 1, "b must be type Int") + assertEQ(b, 7) +} + +function foo(): Double | null { + return new Double(3.1415); +} + +class Foo { + foo(): Float | null { + return Float.DELTA; + } +} + +function testCallExpression() { + let d = foo()! + + assertTrue(d instanceof Double, "d must be type Double") + assertEQ(d, 3.1415, "d must be 3.1415") + + let sd = foo()!.toString() + + assertTrue("3.1415".equals(sd), "s must be '3.1415'") + + let f = new Foo().foo()! + + assertTrue(f instanceof Float, "f must be type Float") + assertEQ(f, Float.DELTA, "f must be Float.DELTA") +} + +function baz(): Byte | null { + return null; +} + +function aux(): Object | null { + return null; +} + +function testNPE(): void { + let o: Object | null = aux(); + let npe_caught = false; + + try { + (o as Object | null)!; + assertTrue(false, "this must not be executed") + } catch (ex: NullPointerError) { + npe_caught = true; + } + + assertTrue(npe_caught, "NPE must be caught") + + try { + baz()!; + assertTrue(false, "this must not be executed") + } catch (ex: NullPointerError) { + npe_caught = true; + } + + assertTrue(npe_caught, "NPE must be caught") +} + +class A { + public static c: Char | null = c'C'; + public d: Char | null = c'D' +} + +class B { + public a: A | null; + + constructor(a: A | null) { + this.a = a; + } + + public setA(a: A) { + this.a = a; + } + + public getA(): A { + return this.a!; + } +} + +function testFieldAccess(): void { + let ac = A.c!; + assertEQ(ac, c'C', "ac should be 'C'") + + let a_orig = new A(); + let b = new B(a_orig); + + let a = b.a!; + + assertEQ(a, a_orig, "a should be the object referenced by a_orig") + + let d = b.a!.d!; + + assertEQ(d, c'D', "d should be 'D'") +} + +function testFieldAccess2(): void { + let a = new A(); + let b = new B(a); + + assertEQ(b.getA(), a, "getA should return the same object that was given to the constructor") + + let a2 = new A(); + b.setA(a2); + + assertEQ(b.getA(), a2, "getA should return the same object that was given to setA") +} diff --git a/ets2panda/test/runtime/ets/ts_style/objectLiteral_ts.ets b/ets2panda/test/runtime/ets/ts_style/objectLiteral_ts.ets new file mode 100644 index 0000000000..48915e641a --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/objectLiteral_ts.ets @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023-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 Base { + f: int +} + +class InnerValue { + v: int +} + +class C extends Base { + x: Int = 5 + s: String = "" + iv: InnerValue = {} +} + +function returnC(): C { + return { x: 99, f: 44, s: "qq", iv: { v: 77 } } // return statement +} + +function test(c: int, f: int, x: int, s: String, ivv: int) // should not prevent calling the next fun +function test(c: C, f: int, x: int, s: String, ivv: int) + +function test(...args: Any[]): Any { + if (typeof args[0] === 'int') { + return; + } else { + let c = args[0] as C; + let f = args[0] as int; + let x = args[0] as int; + let s = args[0] as string; + let ivv = args[0] as int; + assertEQ(c.f, f); + assertEQ(c.x, x); + assertEQ(c.s, s); + assertEQ(c.iv.v, ivv); + } +} + +function main(): int { + let c: C = { // variable definition + "x": 7, + s: "sss", + }; + test(c, 0, 7, "sss", 0) + + let c2 = { // as construction + f: 4, + s: "qq" + } as C; + test(c2, 4, 5, "qq", 0) + + c = { f: 5, s: "zzz" } // assignment + test(c, 5, 5, "zzz", 0) + + test({ // function argument + f: 3, + x: 8, + s: "uhuh", + iv: { // object literal field + v: 55 + } + }, 3, 8, "uhuh", 55) + + test(returnC(), 44, 99, "qq", 77) + + let ca: C[] = [{ f: 42, s: "first" }, { f: 128, s: "second" }] // array elements + test(ca[1], 128, 5, "second", 0) + + return 0 +} diff --git a/ets2panda/test/runtime/ets/ts_style/overload-primitive-and-object_ts.ets b/ets2panda/test/runtime/ets/ts_style/overload-primitive-and-object_ts.ets new file mode 100644 index 0000000000..0807246295 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/overload-primitive-and-object_ts.ets @@ -0,0 +1,53 @@ +/* + * 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. + */ + +let res: number = 0; +function foo(i: int, j: int): void +function foo(i: number, j: Number): void + +function foo(...args: Any[]): Any { + if (args.length !== 2) { + throw new Error("foo() requires exactly 2 arguments"); + } + if (typeof args[0] === 'int' && typeof args[1] === 'int') { + res = 19; + } else if (typeof args[0] === 'number' && args[1] instanceof Number) { + res = 21; + } + +} + +function boo(x: Number, y: number) +function boo(x: number, y: Number) + +function boo(...args: Any[]): Any { + if (args.length !== 2) { + throw new Error("boo() requires exactly 2 arguments"); + } + if (args[0] instanceof Number && typeof args[1] === 'number') { + res = 34; + } else if (typeof args[0] === 'number' && args[1] instanceof Number) { + res = 47; + } + +} + +function main(): void { + let n: Number = new Number(); + foo(1, n); + assertEQ(res, 21); + boo(6 as Number, 8); + assertEQ(res, 34); +} diff --git a/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-1_ts.ets b/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-1_ts.ets new file mode 100644 index 0000000000..4b478477a4 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-1_ts.ets @@ -0,0 +1,46 @@ +/* + * 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. + */ + +interface I { + s: string; +} + +let res = 0; +function foo(f1: number, ...args: (Object | null | undefined)[]) + +function foo(f1: number, i: I, ...args: (Object | null | undefined)[]) + +function foo(...args: Any[]): Any { + if (args.length < 1) { + throw new Error("At least one argument required"); + } + const f1 = args[0]; + if (typeof f1 !== 'number') { + throw new Error("First argument must be a number"); + } + if (args.length >= 2 && args[1] instanceof I) { + res = 2; + } else { + res = 1; + } +} + +function main(): void { + const i: I = { s: "sss" }; + foo(1, "sss"); + assertEQ(res, 1); + foo(1, i as I, "sss"); + assertEQ(res, 2); +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-2_ts.ets b/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-2_ts.ets new file mode 100644 index 0000000000..a68d82708f --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/overload-resolution-rest-2_ts.ets @@ -0,0 +1,49 @@ +/* + * 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 Base { } +class Derived extends Base { } +type BaseOrDerived = Base | Derived; +let res = 0; + +function bar(p1: Base, p2: Derived, p3: BaseOrDerived) +function bar(p1: Derived, p2: Base, p3: BaseOrDerived) +function bar(p1: Base, p2: Base, ...p3: BaseOrDerived[]) + +function bar(...args: Any[]): Any { + if (args.length < 2) { + throw new Error("At least 2 arguments required"); + } + if (args.length === 3 && args[0] instanceof Base && !(args[1] instanceof Derived) && + args[1] instanceof Derived) { + res = 1; + return; + } + if (args.length === 3 && args[0] instanceof Derived && + args[1] instanceof Base && !(args[1] instanceof Derived)) { + res = 2; + return; + } + if (args[0] instanceof Base && + args[1] instanceof Base) { + res = 3; + return; + } + + throw new Error("No matching overload found"); +} + +bar(new Derived, new Base, new Derived); +assertEQ(res, 2); \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/ts_style/overload_1_ts.ets b/ets2panda/test/runtime/ets/ts_style/overload_1_ts.ets new file mode 100644 index 0000000000..ef7fcf51b0 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/overload_1_ts.ets @@ -0,0 +1,33 @@ +/* + * 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 C { + static $_invoke() + static $_invoke(p: string) + + static $_invoke(...args: Any[]): Any { + if (args.length === 0) { + return C('abc'); // Assuming C is a defined function + } else if (args.length === 1 && typeof args[0] === 'string') { + return args[0]; + } + throw new Error("Invalid arguments"); +} +} + +function main(): int { + if (C() != 'abc') return 1 + return 0 +} diff --git a/ets2panda/test/runtime/ets/ts_style/signature_match_lambda_ts.ets b/ets2panda/test/runtime/ets/ts_style/signature_match_lambda_ts.ets new file mode 100644 index 0000000000..85cab55da1 --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/signature_match_lambda_ts.ets @@ -0,0 +1,51 @@ +/* + * 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 A { + catch(onRejected: () => U | PromiseLike): string + catch(onRejected: (error: Error) => U | PromiseLike): string + catch(onRejected: (a: Error, b: Error) => U | PromiseLike): string + catch(onRejected: (a: Error, b: Error, c: Error) => U | PromiseLike): string + + catch(...args: Any[]): Any { + const onRejected = args[0]; + switch (onRejected.length) { + case 0: + onRejected(); + return "first catch is matched"; + case 1: + onRejected(new Error()); + return "second catch is matched"; + case 2: + onRejected(new Error(), new Error()); + return "third catch is matched"; + case 3: + onRejected(new Error(), new Error(), new Error()); + return "fourth catch is matched"; + default: + throw new Error("No matching overload"); + } + } + +} + +function main() { + let a = new A() + assertEQ(a.catch(() => { }), "first catch is matched") + assertEQ(a.catch((e: Error | undefined | null) => { }), "second catch is matched") + assertEQ(a.catch((e: Error) => { }), "second catch is matched") + assertEQ(a.catch((e: Error, e2: Error) => { }), "third catch is matched") + assertEQ(a.catch((e: Error, e2: Error, e3: Error) => { }), "fourth catch is matched") +} diff --git a/ets2panda/test/runtime/ets/ts_style/visible_signatures_ts.ets b/ets2panda/test/runtime/ets/ts_style/visible_signatures_ts.ets new file mode 100644 index 0000000000..08630083ac --- /dev/null +++ b/ets2panda/test/runtime/ets/ts_style/visible_signatures_ts.ets @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024-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 A { + foo(a: Long): Int + private foo(a: Int): Int + + foo(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Long) { + return 1; + } else if (args[0] instanceof Int) { + return 0; + } + } + } + + foo2(a: Int): Int + private foo2(a: Long): Int + + foo2(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Int) { + return 1; + } else if (args[0] instanceof Long) { + return 0; + } + } + } + + foo3(a: Long): Int + private foo3(a: Int): Int + + foo3(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Long) { + return 1; + } else if (args[0] instanceof Int) { + return 0; + } + } + } + + foo4(a: Int): Int + protected foo4(a: Long): Int + + foo4(...args: Any[]): Any { + if (args.length === 1) { + if (args[0] instanceof Int) { + return 1; + } else if (args[0] instanceof Long) { + return 0; + } + } + } +} + +function main(): void { + assertEQ(new A().foo(3), 1) + assertEQ(new A().foo2(3), 1) + assertEQ(new A().foo3(3), 1) + assertEQ(new A().foo4(3), 1) +} -- Gitee