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 "napi/native_node_api.h"
17 #include "tools/log.h"
18
19 namespace {
20 const std::string SENDABLE_ARRAY_NAME = "SendableArray";
21 const std::string SENDABLE_SET_NAME = "SendableSet";
22 const std::string SENDABLE_MAP_NAME = "SendableMap";
23 const std::string SENDABLE_INT8_ARRAY = "SendableInt8Array";
24 const std::string SENDABLE_UINT8_ARRAY = "SendableUint8Array";
25 const std::string SENDABLE_INT16_ARRAY = "SendableInt16Array";
26 const std::string SENDABLE_UINT16_ARRAY = "SendableUint16Array";
27 const std::string SENDABLE_INT32_ARRAY = "SendableInt32Array";
28 const std::string SENDABLE_UINT32_ARRAY = "SendableUint32Array";
29 const std::string SENDABLE_ARRAY_BUFFER = "SendableArrayBuffer";
30 const std::string BIT_VECTOR = "BitVector";
31 const int ARK_PRIVATE_BIT_VECTOR_INDEX = 14;
32 const std::string SENDABLE_UINT8_CLAMPED_ARRAY = "SendableUint8ClampedArray";
33 const std::string SENDABLE_FLOAT32_ARRAY = "SendableFloat32Array";
34 const std::string SENDABLE_FLOAT64_ARRAY = "SendableFloat64Array";
35 const std::string SENDABLE_BIGINT64_ARRAY = "SendableBigInt64Array";
36 const std::string SENDABLE_BIGUINT64_ARRAY = "SendableBigUint64Array";
37 } // namespace
38
GetCollectionFunction(napi_env env,napi_value global,const std::string collectionName,napi_value & collectionFunction)39 static bool GetCollectionFunction(napi_env env, napi_value global, const std::string collectionName,
40 napi_value &collectionFunction)
41 {
42 napi_value collectionKey;
43 NAPI_CALL_BASE(env, napi_create_string_utf8(env, collectionName.c_str(),
44 collectionName.size(), &collectionKey), false);
45 NAPI_CALL_BASE(env, napi_get_property(env, global, collectionKey, &collectionFunction), false);
46 bool validFunction = false;
47 NAPI_CALL_BASE(env, napi_is_callable(env, collectionFunction, &validFunction), false);
48 if (!validFunction) {
49 HILOG_ERROR("Get function for %{public}s failed.", collectionName.c_str());
50 }
51 return validFunction;
52 }
53
GetBitVectorFunction(napi_env env,napi_value global,napi_value & bitVector)54 static void GetBitVectorFunction(napi_env env, napi_value global, napi_value &bitVector)
55 {
56 napi_value arkPrivateClass = nullptr;
57 napi_value arkPrivateKey = nullptr;
58 std::string arkPrivateStr = "ArkPrivate";
59 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, arkPrivateStr.c_str(),
60 arkPrivateStr.size(), &arkPrivateKey));
61 NAPI_CALL_RETURN_VOID(env, napi_get_property(env, global, arkPrivateKey, &arkPrivateClass));
62
63 napi_value loadFunction = nullptr;
64 napi_value loadKey = nullptr;
65 std::string loadStr = "Load";
66 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, loadStr.c_str(), loadStr.size(), &loadKey));
67 NAPI_CALL_RETURN_VOID(env, napi_get_property(env, arkPrivateClass, loadKey, &loadFunction));
68
69 napi_value bitVectorIndex = nullptr;
70 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, ARK_PRIVATE_BIT_VECTOR_INDEX, &bitVectorIndex));
71 napi_value argv[1] = { bitVectorIndex };
72 napi_call_function(env, arkPrivateClass, loadFunction, 1, argv, &bitVector);
73 }
74
InitArkTSCollections(napi_env env,napi_value exports)75 static napi_value InitArkTSCollections(napi_env env, napi_value exports)
76 {
77 napi_value global;
78 napi_value sendableArrayValue;
79 napi_value sendableSetValue;
80 napi_value sendableMapValue;
81 napi_value sendableInt8Array;
82 napi_value sendableUint8Array;
83 napi_value sendableInt16Array;
84 napi_value sendableUint16Array;
85 napi_value sendableInt32Array;
86 napi_value sendableUint32Array;
87 napi_value sendableArrayBuffer;
88 napi_value bitVector;
89 napi_value sendableUint8ClampedArray;
90 napi_value sendableFloat32Array;
91 napi_value sendableFloat64Array;
92 napi_value sendableBigInt64Array;
93 napi_value sendableBigUint64Array;
94
95 napi_get_global(env, &global);
96 if (!GetCollectionFunction(env, global, SENDABLE_ARRAY_NAME, sendableArrayValue)) {
97 return exports;
98 }
99 if (!GetCollectionFunction(env, global, SENDABLE_SET_NAME, sendableSetValue)) {
100 return exports;
101 }
102 if (!GetCollectionFunction(env, global, SENDABLE_MAP_NAME, sendableMapValue)) {
103 return exports;
104 }
105 if (!GetCollectionFunction(env, global, SENDABLE_ARRAY_BUFFER, sendableArrayBuffer)) {
106 return exports;
107 }
108 if (!GetCollectionFunction(env, global, SENDABLE_INT8_ARRAY, sendableInt8Array)) {
109 return exports;
110 }
111 if (!GetCollectionFunction(env, global, SENDABLE_UINT8_ARRAY, sendableUint8Array)) {
112 return exports;
113 }
114 if (!GetCollectionFunction(env, global, SENDABLE_INT16_ARRAY, sendableInt16Array)) {
115 return exports;
116 }
117 if (!GetCollectionFunction(env, global, SENDABLE_UINT16_ARRAY, sendableUint16Array)) {
118 return exports;
119 }
120 if (!GetCollectionFunction(env, global, SENDABLE_INT32_ARRAY, sendableInt32Array)) {
121 return exports;
122 }
123 if (!GetCollectionFunction(env, global, SENDABLE_UINT32_ARRAY, sendableUint32Array)) {
124 return exports;
125 }
126 if (!GetCollectionFunction(env, global, SENDABLE_UINT8_CLAMPED_ARRAY, sendableUint8ClampedArray)) {
127 return exports;
128 }
129
130 if (!GetCollectionFunction(env, global, SENDABLE_FLOAT32_ARRAY, sendableFloat32Array)) {
131 return exports;
132 }
133
134 if (!GetCollectionFunction(env, global, SENDABLE_FLOAT64_ARRAY, sendableFloat64Array)) {
135 return exports;
136 }
137
138 if (!GetCollectionFunction(env, global, SENDABLE_BIGINT64_ARRAY, sendableBigInt64Array)) {
139 return exports;
140 }
141
142 if (!GetCollectionFunction(env, global, SENDABLE_BIGUINT64_ARRAY, sendableBigUint64Array)) {
143 return exports;
144 }
145
146 GetBitVectorFunction(env, global, bitVector);
147
148 napi_property_descriptor desc[] = {
149 DECLARE_NAPI_PROPERTY("Array", sendableArrayValue),
150 DECLARE_NAPI_PROPERTY("Set", sendableSetValue),
151 DECLARE_NAPI_PROPERTY("Map", sendableMapValue),
152 DECLARE_NAPI_PROPERTY("ArrayBuffer", sendableArrayBuffer),
153 DECLARE_NAPI_PROPERTY("Int8Array", sendableInt8Array),
154 DECLARE_NAPI_PROPERTY("Uint8Array", sendableUint8Array),
155 DECLARE_NAPI_PROPERTY("Int16Array", sendableInt16Array),
156 DECLARE_NAPI_PROPERTY("Uint16Array", sendableUint16Array),
157 DECLARE_NAPI_PROPERTY("Int32Array", sendableInt32Array),
158 DECLARE_NAPI_PROPERTY("Uint32Array", sendableUint32Array),
159 DECLARE_NAPI_PROPERTY("BitVector", bitVector),
160 DECLARE_NAPI_PROPERTY("Uint8ClampedArray", sendableUint8ClampedArray),
161 DECLARE_NAPI_PROPERTY("Float32Array", sendableFloat32Array),
162 DECLARE_NAPI_PROPERTY("Float64Array", sendableFloat64Array),
163 DECLARE_NAPI_PROPERTY("BigInt64Array", sendableBigInt64Array),
164 DECLARE_NAPI_PROPERTY("BigUint64Array", sendableBigUint64Array),
165 };
166 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
167 return exports;
168 }
169
170 static napi_module_with_js sendableArrayModule = {
171 .nm_version = 1,
172 .nm_flags = 0,
173 .nm_filename = nullptr,
174 .nm_register_func = InitArkTSCollections,
175 .nm_modname = "arkts.collections", // @ohos.arkts.collections
176 .nm_priv = ((void *)0),
177 };
178
ArkTSCollectionsRegisterModule()179 extern "C" __attribute__((constructor)) void ArkTSCollectionsRegisterModule()
180 {
181 napi_module_with_js_register(&sendableArrayModule);
182 }
183