• 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 "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 }  // namespace
35 
GetCollectionFunction(napi_env env,napi_value global,const std::string collectionName,napi_value & collectionFunction)36 static bool GetCollectionFunction(napi_env env, napi_value global, const std::string collectionName,
37                                   napi_value &collectionFunction)
38 {
39     napi_value collectionKey;
40     NAPI_CALL_BASE(env, napi_create_string_utf8(env, collectionName.c_str(),
41                    collectionName.size(), &collectionKey), false);
42     NAPI_CALL_BASE(env, napi_get_property(env, global, collectionKey, &collectionFunction), false);
43     bool validFunction = false;
44     NAPI_CALL_BASE(env, napi_is_callable(env, collectionFunction, &validFunction), false);
45     if (!validFunction) {
46         HILOG_ERROR("Get function for %{public}s failed.", collectionName.c_str());
47     }
48     return validFunction;
49 }
50 
GetBitVectorFunction(napi_env env,napi_value global,napi_value & bitVector)51 static void GetBitVectorFunction(napi_env env, napi_value global, napi_value &bitVector)
52 {
53     napi_value arkPrivateClass = nullptr;
54     napi_value arkPrivateKey = nullptr;
55     std::string arkPrivateStr = "ArkPrivate";
56     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, arkPrivateStr.c_str(),
57                           arkPrivateStr.size(), &arkPrivateKey));
58     NAPI_CALL_RETURN_VOID(env, napi_get_property(env, global, arkPrivateKey, &arkPrivateClass));
59 
60     napi_value loadFunction = nullptr;
61     napi_value loadKey = nullptr;
62     std::string loadStr = "Load";
63     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, loadStr.c_str(), loadStr.size(), &loadKey));
64     NAPI_CALL_RETURN_VOID(env, napi_get_property(env, arkPrivateClass, loadKey, &loadFunction));
65 
66     napi_value bitVectorIndex = nullptr;
67     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, ARK_PRIVATE_BIT_VECTOR_INDEX, &bitVectorIndex));
68     napi_value argv[1] = { bitVectorIndex };
69     napi_call_function(env, arkPrivateClass, loadFunction, 1, argv, &bitVector);
70 }
71 
InitArkTSCollections(napi_env env,napi_value exports)72 static napi_value InitArkTSCollections(napi_env env, napi_value exports)
73 {
74     napi_value global;
75     napi_value sendableArrayValue;
76     napi_value sendableSetValue;
77     napi_value sendableMapValue;
78     napi_value sendableInt8Array;
79     napi_value sendableUint8Array;
80     napi_value sendableInt16Array;
81     napi_value sendableUint16Array;
82     napi_value sendableInt32Array;
83     napi_value sendableUint32Array;
84     napi_value sendableArrayBuffer;
85     napi_value bitVector;
86     napi_value sendableUint8ClampedArray;
87     napi_value sendableFloat32Array;
88 
89     napi_get_global(env, &global);
90     if (!GetCollectionFunction(env, global, SENDABLE_ARRAY_NAME, sendableArrayValue)) {
91         return exports;
92     }
93     if (!GetCollectionFunction(env, global, SENDABLE_SET_NAME, sendableSetValue)) {
94         return exports;
95     }
96     if (!GetCollectionFunction(env, global, SENDABLE_MAP_NAME, sendableMapValue)) {
97         return exports;
98     }
99     if (!GetCollectionFunction(env, global, SENDABLE_ARRAY_BUFFER, sendableArrayBuffer)) {
100         return exports;
101     }
102     if (!GetCollectionFunction(env, global, SENDABLE_INT8_ARRAY, sendableInt8Array)) {
103         return exports;
104     }
105     if (!GetCollectionFunction(env, global, SENDABLE_UINT8_ARRAY, sendableUint8Array)) {
106         return exports;
107     }
108     if (!GetCollectionFunction(env, global, SENDABLE_INT16_ARRAY, sendableInt16Array)) {
109         return exports;
110     }
111     if (!GetCollectionFunction(env, global, SENDABLE_UINT16_ARRAY, sendableUint16Array)) {
112         return exports;
113     }
114     if (!GetCollectionFunction(env, global, SENDABLE_INT32_ARRAY, sendableInt32Array)) {
115         return exports;
116     }
117     if (!GetCollectionFunction(env, global, SENDABLE_UINT32_ARRAY, sendableUint32Array)) {
118         return exports;
119     }
120     if (!GetCollectionFunction(env, global, SENDABLE_UINT8_CLAMPED_ARRAY, sendableUint8ClampedArray)) {
121         return exports;
122     }
123 
124     if (!GetCollectionFunction(env, global, SENDABLE_FLOAT32_ARRAY, sendableFloat32Array)) {
125         return exports;
126     }
127 
128     GetBitVectorFunction(env, global, bitVector);
129 
130     napi_property_descriptor desc[] = {
131         DECLARE_NAPI_PROPERTY("Array", sendableArrayValue),
132         DECLARE_NAPI_PROPERTY("Set", sendableSetValue),
133         DECLARE_NAPI_PROPERTY("Map", sendableMapValue),
134         DECLARE_NAPI_PROPERTY("ArrayBuffer", sendableArrayBuffer),
135         DECLARE_NAPI_PROPERTY("Int8Array", sendableInt8Array),
136         DECLARE_NAPI_PROPERTY("Uint8Array", sendableUint8Array),
137         DECLARE_NAPI_PROPERTY("Int16Array", sendableInt16Array),
138         DECLARE_NAPI_PROPERTY("Uint16Array", sendableUint16Array),
139         DECLARE_NAPI_PROPERTY("Int32Array", sendableInt32Array),
140         DECLARE_NAPI_PROPERTY("Uint32Array", sendableUint32Array),
141         DECLARE_NAPI_PROPERTY("BitVector", bitVector),
142         DECLARE_NAPI_PROPERTY("Uint8ClampedArray", sendableUint8ClampedArray),
143         DECLARE_NAPI_PROPERTY("Float32Array", sendableFloat32Array),
144     };
145     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
146     return exports;
147 }
148 
149 static napi_module_with_js sendableArrayModule = {
150     .nm_version = 1,
151     .nm_flags = 0,
152     .nm_filename = nullptr,
153     .nm_register_func = InitArkTSCollections,
154     .nm_modname = "arkts.collections",  // @ohos.arkts.collections
155     .nm_priv = ((void *)0),
156 };
157 
ArkTSCollectionsRegisterModule()158 extern "C" __attribute__((constructor)) void ArkTSCollectionsRegisterModule()
159 {
160     napi_module_with_js_register(&sendableArrayModule);
161 }
162