1 /**
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16
17 #include "checker/ETSAnalyzer.h"
18 #include "checker/ETSchecker.h"
19 #include "compiler/core/compilerImpl.h"
20 #include "compiler/core/ETSCompiler.h"
21 #include "compiler/core/ETSemitter.h"
22 #include "compiler/core/ETSGen.h"
23 #include "compiler/core/regSpiller.h"
24 #include "compiler/lowering/phase.h"
25 #include "es2panda.h"
26 #include "mem/arena_allocator.h"
27 #include "mem/pool_manager.h"
28 #include "public/public.h"
29 #include "util/arktsconfig.h"
30 #include "util/generateBin.h"
31 #include "varbinder/ETSBinder.h"
32 #include "test/utils/panda_executable_path_getter.h"
33 #include "checker/types/globalTypesHolder.h"
34 #include "test/utils/checker_test.h"
35
36 using GlobalETSObjectTypeTest = test::utils::CheckerTest;
37
38 namespace ark::es2panda {
TEST_F(GlobalETSObjectTypeTest,TypeDeclNodeTest)39 TEST_F(GlobalETSObjectTypeTest, TypeDeclNodeTest)
40 {
41 // The DeclNode of GlobalETSObjectType will be modified accidentally
42 // when we don't use explicit type declaration in for-statments (issue20054)
43 // Test case added to check the DeclNode of GlobalETSObjectType
44 std::stringstream src;
45 src << "class A {\n"
46 << " testObj: Object = {};\n"
47 << " testBool: boolean = false;\n"
48 << " prop: int = 1;\n}\n"
49 << "function main() {"
50 << " let arr = new A[10]\n;"
51 << " for (let a of arr) {\n"
52 << " if (a.testBool) {\n"
53 << "}\n}\n}" << std::endl;
54
55 InitializeChecker("_.ets", src.str());
56 auto *checker = Checker();
57 auto *globalETSObjectType = checker->GlobalETSObjectType();
58 [[maybe_unused]] auto *declNode = globalETSObjectType->Variable()->Declaration()->Node();
59 ASSERT(declNode->IsClassDefinition());
60 }
61
TEST_F(GlobalETSObjectTypeTest,ObjectPartialGenTest)62 TEST_F(GlobalETSObjectTypeTest, ObjectPartialGenTest)
63 {
64 std::stringstream src;
65 src << "class A<T> {\n"
66 << " constructor(obj: Object) {this.testObj = obj;}"
67 << " testObj: Object;\n}\n"
68 << "let a = new A<int>({})\n;" << std::endl;
69
70 InitializeChecker("_.ets", src.str());
71 auto checker = Checker();
72 ASSERT(checker);
73 auto *globalETSObjectType = checker->GlobalETSObjectType();
74 [[maybe_unused]] auto *declNode = globalETSObjectType->Variable()->Declaration()->Node();
75 ASSERT(declNode->IsClassDefinition());
76 }
77
TEST_F(GlobalETSObjectTypeTest,ETSArrayContainGlobalETSObject)78 TEST_F(GlobalETSObjectTypeTest, ETSArrayContainGlobalETSObject)
79 {
80 // The ETSArray which contains GlobalETSObject, its type is cached in the checker's ArrayTypes map
81 // We should ensure that it is not polluted by certain modifiers, such as "TypeFlag::READONLY"
82 std::stringstream src;
83 src << "class A<K> {\n"
84 << " private readonly prop: number;\n"
85 << " constructor() {\n"
86 << " this.prop = 0.0;}\n"
87 << " constructor(a: FixedArray<K>) {\n"
88 << " this.prop = 1.0;}\n}"
89 << "class B<K> {\n"
90 << " private readonly prop: number;\n"
91 << " constructor() {\n"
92 << " this.prop = 0.0;}\n"
93 << " constructor(a: readonly FixedArray<K>) {\n"
94 << " this.prop = 1.0;}\n}"
95 << "let a = new A<Object>();\n let b = new B<Object>();";
96
97 InitializeChecker("_.ets", src.str());
98 auto checker = Checker();
99 ASSERT(checker);
100 auto *globalETSObjectType = checker->GlobalETSObjectType();
101 [[maybe_unused]] auto arrayType = checker->CreateETSArrayType(globalETSObjectType, false);
102 [[maybe_unused]] auto readonlyArrayType = checker->CreateETSArrayType(globalETSObjectType, true);
103 ASSERT(!arrayType->HasTypeFlag(checker::TypeFlag::READONLY));
104 ASSERT(readonlyArrayType->HasTypeFlag(checker::TypeFlag::READONLY));
105 }
106 } // namespace ark::es2panda
107