1 /*
2 * Copyright (c) 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
16 #include <array>
17 #include <iostream>
18
19 #include "ani.h"
20
21 // NOLINTBEGIN(readability-magic-numbers)
22
23 // CC-OFFNXT(G.PRE.02-CPP) testing macro with source line info for finding errors
24 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
25 #define CHECK_EQUAL(a, b) \
26 { \
27 auto aValue = (a); \
28 auto bValue = (b); \
29 if (aValue != bValue) { \
30 ++errorCount; \
31 std::cerr << "LINE " << __LINE__ << ", " << #a << ": " << aValue << " != " << bValue << '\n'; \
32 } \
33 }
34
35 // CC-OFFNXT(G.FUN.01-CPP, readability-function-size_parameters) function for testing
Direct(ani_int im1,ani_byte i0,ani_short i1,ani_int i2,ani_long i3,ani_float f0,ani_double f1,ani_double f2,ani_int i4,ani_int i5,ani_int i6,ani_long i7,ani_int i8,ani_short i9,ani_int i10,ani_int i11,ani_int i12,ani_int i13,ani_int i14,ani_float f3,ani_float f4,ani_double f5,ani_double f6,ani_double f7,ani_double f8,ani_double f9,ani_double f10,ani_double f11,ani_double f12,ani_double f13,ani_double f14)36 static ani_int Direct(ani_int im1, ani_byte i0, ani_short i1, ani_int i2, ani_long i3, ani_float f0, ani_double f1,
37 ani_double f2, ani_int i4, ani_int i5, ani_int i6, ani_long i7, ani_int i8, ani_short i9,
38 ani_int i10, ani_int i11, ani_int i12, ani_int i13, ani_int i14, ani_float f3, ani_float f4,
39 ani_double f5, ani_double f6, ani_double f7, ani_double f8, ani_double f9, ani_double f10,
40 ani_double f11, ani_double f12, ani_double f13, ani_double f14)
41 {
42 ani_int errorCount = 0L;
43
44 CHECK_EQUAL(im1, -1L);
45 CHECK_EQUAL(static_cast<ani_int>(i0), 0L);
46 CHECK_EQUAL(i1, 1L);
47 CHECK_EQUAL(i2, 2L);
48 CHECK_EQUAL(i3, 3L);
49 CHECK_EQUAL(i4, 4L);
50 CHECK_EQUAL(i5, 5L);
51 CHECK_EQUAL(i6, 6L);
52 CHECK_EQUAL(i7, 7L);
53 CHECK_EQUAL(i8, 8L);
54 CHECK_EQUAL(i9, 9L);
55 CHECK_EQUAL(i10, 10L);
56 CHECK_EQUAL(i11, 11L);
57 CHECK_EQUAL(i12, 12L);
58 CHECK_EQUAL(i13, 13L);
59 CHECK_EQUAL(i14, 14L);
60
61 CHECK_EQUAL(f0, 0.0L);
62 CHECK_EQUAL(f1, 1.0L);
63 CHECK_EQUAL(f2, 2.0L);
64 CHECK_EQUAL(f3, 3.0L);
65 CHECK_EQUAL(f4, 4.0L);
66 CHECK_EQUAL(f5, 5.0L);
67 CHECK_EQUAL(f6, 6.0L);
68 CHECK_EQUAL(f7, 7.0L);
69 CHECK_EQUAL(f8, 8.0L);
70 CHECK_EQUAL(f9, 9.0L);
71 CHECK_EQUAL(f10, 10.0L);
72 CHECK_EQUAL(f11, 11.0L);
73 CHECK_EQUAL(f12, 12.0L);
74 CHECK_EQUAL(f13, 13.0L);
75 CHECK_EQUAL(f14, 14.0L);
76
77 return errorCount;
78 }
79
ANI_Constructor(ani_vm * vm,uint32_t * result)80 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
81 {
82 ani_env *env;
83 if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
84 std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
85 return ANI_ERROR;
86 }
87
88 static const char *className = "Ldirect_native/NativeModule;";
89 ani_class cls;
90 if (ANI_OK != env->FindClass(className, &cls)) {
91 std::cerr << "Not found '" << className << "'" << std::endl;
92 return ANI_ERROR;
93 }
94
95 std::array methods = {
96 ani_native_function {"direct", nullptr, reinterpret_cast<void *>(Direct)},
97 };
98
99 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
100 std::cerr << "Cannot bind native methods to '" << className << "'" << std::endl;
101 return ANI_ERROR;
102 };
103
104 *result = ANI_VERSION_1;
105 return ANI_OK;
106 }
107
108 // NOLINTEND(readability-magic-numbers)
109