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 <gtest/gtest.h>
17 #include <cstring>
18
19 #include "libabckit/include/c/abckit.h"
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 #include "libabckit/include/c/metadata_core.h"
23 #include "libabckit/include/c/extensions/js/metadata_js.h"
24 #include "ir_impl.h"
25 #include "libabckit/include/c/isa/isa_dynamic.h"
26 #include "libabckit/include/c/statuses.h"
27
28 namespace libabckit::test {
29
30 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implJsI = AbckitGetJsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implJsM = AbckitGetJsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37
38 class LibAbcKitJSModifyApiModulesTest : public ::testing::Test {};
39
40 static constexpr int MAX_ITER = 256;
41
StByNameTest(AbckitFile * file,AbckitGraph * graph,bool isSuper)42 static void StByNameTest(AbckitFile *file, AbckitGraph *graph, bool isSuper)
43 {
44 AbckitInst *param;
45 if (isSuper) {
46 param = g_implG->iGetNext(
47 g_implG->iGetNext(helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER)));
48 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
49 ASSERT_NE(param, nullptr);
50 }
51
52 AbckitInst *newAge = g_implG->gFindOrCreateConstantI32(graph, 2);
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54 ASSERT_NE(newAge, nullptr);
55
56 AbckitString *fieldName = g_implM->createString(file, "age", strlen("age"));
57 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
58 ASSERT_NE(fieldName, nullptr);
59
60 AbckitInst *store;
61 if (isSuper) {
62 store = g_dynG->iCreateStsuperbyname(graph, newAge, fieldName, param);
63 } else {
64 store = g_dynG->iCreateStthisbyname(graph, newAge, fieldName);
65 }
66 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
67 ASSERT_NE(store, nullptr);
68
69 AbckitInst *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
70 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
71 ASSERT_NE(ret, nullptr);
72
73 g_implG->iInsertBefore(store, ret);
74 }
75
76 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStsuperbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStsuperbyname)77 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStsuperbyname)
78 {
79 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
80 "store_super");
81 auto expected = "1\n";
82 EXPECT_TRUE(helpers::Match(output, expected));
83
84 helpers::TransformMethod(
85 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
86 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "setAge",
87 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { StByNameTest(file, graph, true); },
88 [&]([[maybe_unused]] AbckitGraph *graph) {});
89 output = helpers::ExecuteDynamicAbc(
90 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "store_super");
91 expected = "2\n";
92 EXPECT_TRUE(helpers::Match(output, expected));
93 }
94
95 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStsuperbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStsuperbynameLarge)96 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStsuperbynameLarge)
97 {
98 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
99 "store_super");
100 auto expected = "1\n";
101 EXPECT_TRUE(helpers::Match(output, expected));
102
103 helpers::TransformMethod(
104 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
105 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "setAge",
106 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
107 StByNameTest(file, graph, true);
108 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
109 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
110 for (auto idx = 0; idx < MAX_ITER; idx++) {
111 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
112 g_implG->iInsertAfter(call, ldlexvar);
113 }
114 },
115 [&]([[maybe_unused]] AbckitGraph *graph) {});
116
117 helpers::TransformMethod(
118 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc",
119 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "func_main_0",
120 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
121 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
122 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
123 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
124 g_implG->iInsertBefore(newlexenv, deffunc);
125 g_implG->iInsertAfter(stlexvar, deffunc);
126 },
127 [&]([[maybe_unused]] AbckitGraph *graph) {});
128
129 output = helpers::ExecuteDynamicAbc(
130 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "store_super");
131 expected = "2\n";
132 EXPECT_TRUE(helpers::Match(output, expected));
133 }
134
135 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStthisbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStthisbyname)136 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStthisbyname)
137 {
138 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
139 "store_this");
140 auto expected = "1\n";
141 EXPECT_TRUE(helpers::Match(output, expected));
142
143 helpers::TransformMethod(
144 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
145 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "setAge",
146 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { StByNameTest(file, graph, false); },
147 [&]([[maybe_unused]] AbckitGraph *graph) {});
148 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
149 "store_this");
150 expected = "2\n";
151 EXPECT_TRUE(helpers::Match(output, expected));
152 }
153
154 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStthisbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStthisbynameLarge)155 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStthisbynameLarge)
156 {
157 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
158 "store_this");
159 auto expected = "1\n";
160 EXPECT_TRUE(helpers::Match(output, expected));
161
162 helpers::TransformMethod(
163 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
164 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "setAge",
165 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
166 StByNameTest(file, graph, false);
167 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x1, 0x0);
168 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
169 for (auto idx = 0; idx < MAX_ITER; idx++) {
170 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
171 g_implG->iInsertAfter(call, ldlexvar);
172 }
173 },
174 [&]([[maybe_unused]] AbckitGraph *graph) {});
175
176 helpers::TransformMethod(
177 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
178 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "func_main_0",
179 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
180 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
181 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
182 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
183 g_implG->iInsertBefore(newlexenv, deffunc);
184 g_implG->iInsertAfter(stlexvar, deffunc);
185 },
186 [&]([[maybe_unused]] AbckitGraph *graph) {});
187
188 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
189 "store_this");
190 expected = "2\n";
191 EXPECT_TRUE(helpers::Match(output, expected));
192 }
193
StByValueTest(AbckitFile * file,AbckitGraph * graph,bool isSuper)194 static void StByValueTest(AbckitFile *file, AbckitGraph *graph, bool isSuper)
195 {
196 AbckitInst *newAge = g_implG->gFindOrCreateConstantI32(graph, 2);
197 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
198 ASSERT_NE(newAge, nullptr);
199
200 AbckitInst *param;
201 if (isSuper) {
202 param = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
203 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
204 ASSERT_NE(param, nullptr);
205 }
206 AbckitString *fieldName = g_implM->createString(file, "age", strlen("age"));
207 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
208 ASSERT_NE(fieldName, nullptr);
209
210 AbckitInst *stringObj = g_dynG->iCreateLoadString(graph, fieldName);
211 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
212 ASSERT_NE(stringObj, nullptr);
213
214 AbckitInst *store;
215 if (isSuper) {
216 store = g_dynG->iCreateStsuperbyvalue(graph, newAge, param, stringObj);
217 } else {
218 store = g_dynG->iCreateStthisbyvalue(graph, newAge, stringObj);
219 }
220 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
221 ASSERT_NE(store, nullptr);
222
223 AbckitInst *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
224 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
225 ASSERT_NE(ret, nullptr);
226
227 g_implG->iInsertBefore(stringObj, ret);
228 g_implG->iInsertBefore(store, ret);
229 }
230
231 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStsuperbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStsuperbyvalue)232 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStsuperbyvalue)
233 {
234 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
235 "store_super");
236 auto expected = "1\n";
237 EXPECT_TRUE(helpers::Match(output, expected));
238
239 helpers::TransformMethod(
240 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
241 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "setAge",
242 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { StByValueTest(file, graph, true); },
243 [&]([[maybe_unused]] AbckitGraph *graph) {});
244
245 output = helpers::ExecuteDynamicAbc(
246 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "store_super");
247 expected = "2\n";
248 EXPECT_TRUE(helpers::Match(output, expected));
249 }
250
251 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStsuperbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStsuperbyvalueLarge)252 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStsuperbyvalueLarge)
253 {
254 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
255 "store_super");
256 auto expected = "1\n";
257 EXPECT_TRUE(helpers::Match(output, expected));
258
259 helpers::TransformMethod(
260 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super.abc",
261 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "setAge",
262 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
263 StByValueTest(file, graph, true);
264 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
265 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
266 for (auto idx = 0; idx < MAX_ITER; idx++) {
267 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
268 g_implG->iInsertAfter(call, ldlexvar);
269 }
270 },
271 [&]([[maybe_unused]] AbckitGraph *graph) {});
272
273 helpers::TransformMethod(
274 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc",
275 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "func_main_0",
276 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
277 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
278 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
279 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
280 g_implG->iInsertBefore(newlexenv, deffunc);
281 g_implG->iInsertAfter(stlexvar, deffunc);
282 },
283 [&]([[maybe_unused]] AbckitGraph *graph) {});
284
285 output = helpers::ExecuteDynamicAbc(
286 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_super_modified.abc", "store_super");
287 expected = "2\n";
288 EXPECT_TRUE(helpers::Match(output, expected));
289 }
290
291 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStthisbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStthisbyvalue)292 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStthisbyvalue)
293 {
294 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
295 "store_this");
296 auto expected = "1\n";
297 EXPECT_TRUE(helpers::Match(output, expected));
298
299 helpers::TransformMethod(
300 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
301 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "setAge",
302 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { StByValueTest(file, graph, false); },
303 [&]([[maybe_unused]] AbckitGraph *graph) {});
304
305 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
306 "store_this");
307 expected = "2\n";
308 EXPECT_TRUE(helpers::Match(output, expected));
309 }
310
311 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStthisbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStthisbyvalueLarge)312 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStthisbyvalueLarge)
313 {
314 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
315 "store_this");
316 auto expected = "1\n";
317 EXPECT_TRUE(helpers::Match(output, expected));
318
319 helpers::TransformMethod(
320 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this.abc",
321 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "setAge",
322 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
323 StByValueTest(file, graph, false);
324 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x1, 0x0);
325 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
326 for (auto idx = 0; idx < MAX_ITER; idx++) {
327 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
328 g_implG->iInsertAfter(call, ldlexvar);
329 }
330 },
331 [&]([[maybe_unused]] AbckitGraph *graph) {});
332
333 helpers::TransformMethod(
334 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
335 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc", "func_main_0",
336 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
337 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
338 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
339 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
340 g_implG->iInsertBefore(newlexenv, deffunc);
341 g_implG->iInsertAfter(stlexvar, deffunc);
342 },
343 [&]([[maybe_unused]] AbckitGraph *graph) {});
344
345 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/store_this_modified.abc",
346 "store_this");
347 expected = "2\n";
348 EXPECT_TRUE(helpers::Match(output, expected));
349 }
350
LdByNameTest(AbckitFile * file,AbckitGraph * graph,bool isSuper)351 static void LdByNameTest(AbckitFile *file, AbckitGraph *graph, bool isSuper)
352 {
353 AbckitInst *param;
354 if (isSuper) {
355 param = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
356 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
357 ASSERT_NE(param, nullptr);
358 }
359 AbckitString *funcName = g_implM->createString(file, "getName", strlen("getName"));
360 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
361 ASSERT_NE(funcName, nullptr);
362
363 AbckitInst *load;
364 if (isSuper) {
365 load = g_dynG->iCreateLdsuperbyname(graph, param, funcName);
366 } else {
367 load = g_dynG->iCreateLdthisbyname(graph, funcName);
368 }
369 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
370 ASSERT_NE(load, nullptr);
371
372 AbckitInst *call0 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS0);
373 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
374 ASSERT_NE(call0, nullptr);
375
376 g_implG->iInsertBefore(load, call0);
377 g_implG->iSetInput(call0, load, 0);
378 }
379
380 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdsuperbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdsuperbyname)381 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdsuperbyname)
382 {
383 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
384 "load_super");
385 auto expected = "3\n";
386 EXPECT_TRUE(helpers::Match(output, expected));
387
388 helpers::TransformMethod(
389 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
390 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc", "getInfo",
391 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { LdByNameTest(file, graph, true); },
392 [&]([[maybe_unused]] AbckitGraph *graph) {});
393 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc",
394 "load_super");
395 expected = "doggo\n";
396 EXPECT_TRUE(helpers::Match(output, expected));
397 }
398
399 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdthisbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdthisbyname)400 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdthisbyname)
401 {
402 auto output =
403 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc", "load_this");
404 auto expected = "3\n";
405 EXPECT_TRUE(helpers::Match(output, expected));
406
407 helpers::TransformMethod(
408 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc",
409 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "getInfo",
410 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { LdByNameTest(file, graph, false); },
411 [&]([[maybe_unused]] AbckitGraph *graph) {});
412 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
413 "load_this");
414 expected = "doggo\n";
415 EXPECT_TRUE(helpers::Match(output, expected));
416 }
417
418 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdthisbyname, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdthisbynameLarge)419 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdthisbynameLarge)
420 {
421 auto output =
422 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc", "load_this");
423 auto expected = "3\n";
424 EXPECT_TRUE(helpers::Match(output, expected));
425
426 helpers::TransformMethod(
427 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc",
428 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "getInfo",
429 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
430 LdByNameTest(file, graph, false);
431 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
432 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
433 for (auto idx = 0; idx < MAX_ITER; idx++) {
434 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
435 g_implG->iInsertAfter(call, ldlexvar);
436 }
437 },
438 [&]([[maybe_unused]] AbckitGraph *graph) {});
439
440 helpers::TransformMethod(
441 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
442 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "func_main_0",
443 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
444 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
445 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
446 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
447 g_implG->iInsertBefore(newlexenv, deffunc);
448 g_implG->iInsertAfter(stlexvar, deffunc);
449 },
450 [&]([[maybe_unused]] AbckitGraph *graph) {});
451
452 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
453 "load_this");
454 expected = "doggo\n";
455 EXPECT_TRUE(helpers::Match(output, expected));
456 }
457
LdByValueTest(AbckitFile * file,AbckitGraph * graph,bool isSuper)458 static void LdByValueTest(AbckitFile *file, AbckitGraph *graph, bool isSuper)
459 {
460 AbckitInst *param;
461
462 if (isSuper) {
463 param = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
464 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
465 ASSERT_NE(param, nullptr);
466 }
467
468 AbckitString *funcName = g_implM->createString(file, "getName", strlen("getName"));
469 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
470 ASSERT_NE(funcName, nullptr);
471
472 AbckitInst *stringObj = g_dynG->iCreateLoadString(graph, funcName);
473 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
474 ASSERT_NE(stringObj, nullptr);
475
476 AbckitInst *load;
477 if (isSuper) {
478 load = g_dynG->iCreateLdsuperbyvalue(graph, stringObj, param);
479 } else {
480 load = g_dynG->iCreateLdthisbyvalue(graph, stringObj);
481 }
482 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
483 ASSERT_NE(load, nullptr);
484
485 AbckitInst *call0 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS0);
486 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
487 ASSERT_NE(call0, nullptr);
488
489 g_implG->iInsertBefore(stringObj, call0);
490 g_implG->iInsertBefore(load, call0);
491 g_implG->iSetInput(call0, load, 0);
492 }
493
494 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdsuperbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdsuperbyvalue)495 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdsuperbyvalue)
496 {
497 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
498 "load_super");
499 auto expected = "3\n";
500 EXPECT_TRUE(helpers::Match(output, expected));
501
502 helpers::TransformMethod(
503 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
504 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc", "getInfo",
505 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { LdByValueTest(file, graph, true); },
506 [&]([[maybe_unused]] AbckitGraph *graph) {});
507
508 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc",
509 "load_super");
510 expected = "doggo\n";
511 EXPECT_TRUE(helpers::Match(output, expected));
512 }
513
514 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdsuperbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdsuperbyvalueLarge)515 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdsuperbyvalueLarge)
516 {
517 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
518 "load_super");
519 auto expected = "3\n";
520 EXPECT_TRUE(helpers::Match(output, expected));
521
522 helpers::TransformMethod(
523 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super.abc",
524 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc", "getInfo",
525 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
526 LdByValueTest(file, graph, true);
527 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
528 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
529 for (auto idx = 0; idx < MAX_ITER; idx++) {
530 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
531 g_implG->iInsertAfter(call, ldlexvar);
532 }
533 },
534 [&]([[maybe_unused]] AbckitGraph *graph) {});
535
536 helpers::TransformMethod(
537 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc",
538 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc", "func_main_0",
539 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
540 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
541 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
542 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
543 g_implG->iInsertBefore(newlexenv, deffunc);
544 g_implG->iInsertAfter(stlexvar, deffunc);
545 },
546 [&]([[maybe_unused]] AbckitGraph *graph) {});
547
548 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_super_modified.abc",
549 "load_super");
550 expected = "doggo\n";
551 EXPECT_TRUE(helpers::Match(output, expected));
552 }
553
554 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdthisbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdthisbyvalue)555 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdthisbyvalue)
556 {
557 auto output =
558 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc", "load_this");
559 auto expected = "3\n";
560 EXPECT_TRUE(helpers::Match(output, expected));
561
562 helpers::TransformMethod(
563 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc",
564 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "getInfo",
565 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) { LdByValueTest(file, graph, false); },
566 [&]([[maybe_unused]] AbckitGraph *graph) {});
567
568 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
569 "load_this");
570 expected = "doggo\n";
571 EXPECT_TRUE(helpers::Match(output, expected));
572 }
573
574 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLdthisbyvalue, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdthisbyvalueLarge)575 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdthisbyvalueLarge)
576 {
577 auto output =
578 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc", "load_this");
579 auto expected = "3\n";
580 EXPECT_TRUE(helpers::Match(output, expected));
581
582 helpers::TransformMethod(
583 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this.abc",
584 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "getInfo",
585 [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
586 LdByValueTest(file, graph, false);
587 auto *ldlexvar = g_dynG->iCreateLdlexvar(graph, 0x0, 0x0);
588 g_implG->bbAddInstFront(g_implG->gGetBasicBlock(graph, 0), ldlexvar);
589 for (auto idx = 0; idx < MAX_ITER; idx++) {
590 auto *call = g_dynG->iCreateCallarg0(graph, ldlexvar);
591 g_implG->iInsertAfter(call, ldlexvar);
592 }
593 },
594 [&]([[maybe_unused]] AbckitGraph *graph) {});
595
596 helpers::TransformMethod(
597 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
598 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc", "func_main_0",
599 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
600 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 0x1);
601 auto *deffunc = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
602 auto *stlexvar = g_dynG->iCreateStlexvar(graph, deffunc, 0x0, 0x0);
603 g_implG->iInsertBefore(newlexenv, deffunc);
604 g_implG->iInsertAfter(stlexvar, deffunc);
605 },
606 [&]([[maybe_unused]] AbckitGraph *graph) {});
607
608 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/super_this/load_this_modified.abc",
609 "load_this");
610 expected = "doggo\n";
611 EXPECT_TRUE(helpers::Match(output, expected));
612 }
613
614 } // namespace libabckit::test
615