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
CopyrestargsTest(AbckitGraph * graph,bool isWide)40 static void CopyrestargsTest(AbckitGraph *graph, bool isWide)
41 {
42 AbckitInst *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
43 AbckitInst *cp;
44 if (isWide) {
45 cp = g_dynG->iCreateWideCopyrestargs(graph, 0);
46 } else {
47 cp = g_dynG->iCreateCopyrestargs(graph, 0);
48 }
49 g_implG->iInsertBefore(cp, ret);
50 g_implG->iSetInput(ret, cp, 0);
51 }
52
53 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCopydataproperties, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,IcreateCopydatapropertiesTest)54 TEST_F(LibAbcKitJSModifyApiModulesTest, IcreateCopydatapropertiesTest)
55 {
56 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copy_properties_test.abc",
57 "copy_properties_test");
58 auto expected = "a\n";
59 EXPECT_TRUE(helpers::Match(output, expected));
60
61 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copy_properties_test.abc",
62 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copy_properties_test_modified.abc", "foo",
63 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
64 auto *add = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_ADD2);
65 auto *param = g_implG->iGetInput(add, 1);
66
67 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68 ASSERT_NE(param, nullptr);
69 auto *emptyObj = g_dynG->iCreateCreateemptyobject(graph);
70 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
71 ASSERT_NE(emptyObj, nullptr);
72 AbckitInst *cp = g_dynG->iCreateCopydataproperties(graph, param, emptyObj);
73 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
74 ASSERT_NE(cp, nullptr);
75 AbckitInst *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
76 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
77 ASSERT_NE(ret, nullptr);
78
79 g_implG->iInsertBefore(emptyObj, ret);
80 g_implG->iInsertBefore(cp, ret);
81 g_implG->iSetInput(ret, cp, 0);
82 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
83 });
84 output = helpers::ExecuteDynamicAbc(
85 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copy_properties_test_modified.abc", "copy_properties_test");
86 expected = "b\n";
87 EXPECT_TRUE(helpers::Match(output, expected));
88 }
89
90 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCopyrestargs, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateCopyrestargs)91 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateCopyrestargs)
92 {
93 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test.abc",
94 "copyrestargs_test");
95 auto expected = "3\n";
96 EXPECT_TRUE(helpers::Match(output, expected));
97
98 helpers::TransformMethod(
99 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test.abc",
100 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test_modified.abc", "f",
101 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) { CopyrestargsTest(graph, false); });
102 output = helpers::ExecuteDynamicAbc(
103 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test_modified.abc", "copyrestargs_test");
104 expected = "1\n";
105 EXPECT_TRUE(helpers::Match(output, expected));
106 }
107
108 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateWideCopyrestargs, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateWideCopyrestargs)109 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateWideCopyrestargs)
110 {
111 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test.abc",
112 "copyrestargs_test");
113 auto expected = "3\n";
114 EXPECT_TRUE(helpers::Match(output, expected));
115
116 helpers::TransformMethod(
117 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test.abc",
118 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test_modified.abc", "f",
119 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) { CopyrestargsTest(graph, true); },
120 [&](AbckitGraph *) {});
121 output = helpers::ExecuteDynamicAbc(
122 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/copy/copyrestargs_test_modified.abc", "copyrestargs_test");
123 expected = "1\n";
124 EXPECT_TRUE(helpers::Match(output, expected));
125 }
126
127 } // namespace libabckit::test
128