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