• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 
16 #include "libabckit/include/c/metadata_core.h"
17 #include "libabckit/include/c/ir_core.h"
18 #include "libabckit/include/c/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20 
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23 
24 #include <gtest/gtest.h>
25 
26 // NOLINTBEGIN(readability-magic-numbers)
27 namespace libabckit::test {
28 
29 namespace {
30 
31 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 
__anon254457fa0202(AbckitFile *file, AbckitCoreFunction * , AbckitGraph *graph) 37 auto g_icreateCallruntimeCallinit1Lambda = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
38     auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
39     auto *poplexenv = g_dynG->iCreatePoplexenv(graph);
40 
41     auto *classConstr = helpers::FindMethodByName(file, "innerConstr");
42     ASSERT_NE(classConstr, nullptr);
43     auto arr = std::vector<AbckitLiteral *>();
44     arr.emplace_back(g_implM->createLiteralU32(file, 0));
45     auto *litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
46 
47     auto *ldhole = g_dynG->iCreateLdhole(graph);
48     auto *defineClass = g_dynG->iCreateDefineclasswithbuffer(graph, classConstr, litArr, 0x0, ldhole);
49     auto *stringProt = g_implM->createString(file, "prototype", strlen("prototype"));
50     auto *ldobjbyname = g_dynG->iCreateLdobjbyname(graph, defineClass, stringProt);
51 
52     auto *innerInit = helpers::FindMethodByName(file, "innerInit");
53     auto *definemethod = g_dynG->iCreateDefinemethod(graph, ldobjbyname, innerInit, 0x0);
54     auto *stlexvar = g_dynG->iCreateStlexvar(graph, definemethod, 0x0, 0x0);
55     auto *stringD = g_implM->createString(file, "D", strlen("D"));
56     auto *definepropertybyname = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEPROPERTYBYNAME);
57     g_implG->iSetString(definepropertybyname, stringD);
58     g_implG->iSetInput(definepropertybyname, defineClass, 0);
59 
60     g_implG->iInsertBefore(newlexenv, definepropertybyname);
61     g_implG->iInsertBefore(ldhole, definepropertybyname);
62     g_implG->iInsertBefore(defineClass, definepropertybyname);
63     g_implG->iInsertBefore(ldobjbyname, definepropertybyname);
64     g_implG->iInsertBefore(definemethod, definepropertybyname);
65     g_implG->iInsertBefore(stlexvar, definepropertybyname);
66     g_implG->iInsertBefore(poplexenv, definepropertybyname);
67 };
68 
69 }  // namespace
70 
71 class LibAbcKitCreateDynCallsRuntime : public ::testing::Test {};
72 
73 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCallruntimeTopropertykey, abc-kind=ArkTS1, category=positive,
74 // extension=c
TEST_F(LibAbcKitCreateDynCallsRuntime,IcreateCallruntimeTopropertykey_1)75 TEST_F(LibAbcKitCreateDynCallsRuntime, IcreateCallruntimeTopropertykey_1)
76 {
77     auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/topropertykey_dynamic.abc",
78                                              "topropertykey_dynamic");
79     EXPECT_TRUE(helpers::Match(output, "undefined\n"));
80 
81     helpers::TransformMethod(
82         ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/topropertykey_dynamic.abc",
83         ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/topropertykey_dynamic_modified.abc", "func_main_0",
84         [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
85             auto *ldundefFirts = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_LDUNDEFINED);
86             auto *callCreate = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS1);
87 
88             auto *ldhole = g_dynG->iCreateLdhole(graph);
89             auto *stringX = g_implM->createString(file, "x", strlen("x"));
90             auto *loadString = g_dynG->iCreateLoadString(graph, stringX);
91             auto *isin = g_dynG->iCreateIsin(graph, callCreate, loadString);
92             auto *topropertykey = g_dynG->iCreateCallruntimeTopropertykey(graph, isin);
93             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
94 
95             auto arr = std::vector<AbckitLiteral *>();
96             arr.emplace_back(g_implM->createLiteralU32(file, 0));
97             auto *litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
98             auto *classConstr = helpers::FindMethodByName(file, "classConstr");
99             ASSERT_NE(classConstr, nullptr);
100 
101             auto *defineclasswithbuffer = g_dynG->iCreateDefineclasswithbuffer(graph, classConstr, litArr, 0, ldhole);
102             auto *stringProt = g_implM->createString(file, "prototype", strlen("prototype"));
103             auto *ldobjbyname = g_dynG->iCreateLdobjbyname(graph, defineclasswithbuffer, stringProt);
104             auto *funcGetter = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
105             auto *ldtrue = g_dynG->iCreateLdtrue(graph);
106             auto *definegetter = g_dynG->iCreateDefinegettersetterbyvalue(graph, ldtrue, ldobjbyname, topropertykey,
107                                                                           funcGetter, ldundefFirts);
108             auto *stringFalse = g_implM->createString(file, "false", strlen("false"));
109             auto *ldobjbynameFalse = g_dynG->iCreateLdobjbyname(graph, ldobjbyname, stringFalse);
110             auto *callarg1 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
111             g_implG->iSetInput(callarg1, ldobjbynameFalse, 1);
112 
113             g_implG->iInsertBefore(ldhole, callarg1);
114             g_implG->iInsertBefore(loadString, callarg1);
115             g_implG->iInsertBefore(isin, callarg1);
116             g_implG->iInsertBefore(topropertykey, callarg1);
117             g_implG->iInsertBefore(defineclasswithbuffer, callarg1);
118             g_implG->iInsertBefore(ldobjbyname, callarg1);
119             g_implG->iInsertBefore(ldtrue, callarg1);
120             g_implG->iInsertBefore(definegetter, callarg1);
121             g_implG->iInsertBefore(ldobjbynameFalse, callarg1);
122         });
123 
124     output = helpers::ExecuteDynamicAbc(
125         ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/topropertykey_dynamic_modified.abc", "topropertykey_dynamic");
126     EXPECT_TRUE(helpers::Match(output, "via get\n"));
127 }
128 
129 // CC-OFFNXT(huge_method, C_RULE_ID_FUNCTION_SIZE) test, solid logic
130 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCallruntimeCallinit, abc-kind=ArkTS1, category=positive,
131 // extension=c
TEST_F(LibAbcKitCreateDynCallsRuntime,IcreateCallruntimeCallinit_1)132 TEST_F(LibAbcKitCreateDynCallsRuntime, IcreateCallruntimeCallinit_1)
133 {
134     auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic.abc",
135                                              "callinit_dynamic");
136     EXPECT_TRUE(helpers::Match(output, ""));
137 
138     helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic.abc",
139                              ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc",
140                              "static_initializer", g_icreateCallruntimeCallinit1Lambda);
141 
142     helpers::TransformMethod(
143         ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc",
144         ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc", "innerConstr",
145         [&](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
146             auto *firstBB = helpers::BBgetSuccBlocks(g_implG->gGetStartBasicBlock(graph))[0];
147             auto *removeInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
148             g_implG->iRemove(removeInst);
149 
150             auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
151             auto *param = g_implG->gGetParameter(graph, 0x2);
152             auto *callinit = g_dynG->iCreateCallruntimeCallinit(graph, ldlexvar, param);
153             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
154             auto *returnInst = g_dynG->iCreateReturn(graph, param);
155 
156             g_implG->bbAddInstBack(firstBB, ldlexvar);
157             g_implG->bbAddInstBack(firstBB, callinit);
158             g_implG->bbAddInstBack(firstBB, returnInst);
159         });
160 
161     auto cb = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
162         auto *defineclasswithbuffer =
163             helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINECLASSWITHBUFFER);
164         // CC-OFFNXT(G.FMT.02)
165         auto *stringD = g_implM->createString(file, "D", strlen("D"));
166         auto *ldobjbyname = g_dynG->iCreateLdobjbyname(graph, defineclasswithbuffer, stringD);
167         auto *newobjrange = g_dynG->iCreateNewobjrange(graph, 0x1, ldobjbyname);
168 
169         auto *returnundefined = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
170         // CC-OFFNXT(G.FMT.02)
171         g_implG->iInsertBefore(ldobjbyname, returnundefined);
172         g_implG->iInsertBefore(newobjrange, returnundefined);
173     };
174 
175     helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc",
176                              ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc",
177                              "func_main_0", cb);
178 
179     output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/call_runtime/callinit_dynamic_modified.abc",
180                                         "callinit_dynamic");
181     EXPECT_TRUE(helpers::Match(output, "innerConstr\ninnerInit\n"));
182 }
183 
184 }  // namespace libabckit::test
185 // NOLINTEND(readability-magic-numbers)
186