From 836ec2d72bf942f45a4e3041fdba8fe47162641b Mon Sep 17 00:00:00 2001 From: huangjiaqi Date: Fri, 31 Dec 2021 12:31:41 +0800 Subject: [PATCH] js api, export enums via c++, no-js-mixed. Signed-off-by: huangjiaqi --- .../distributeddata/include/async_call.h | 2 +- .../include/js_const_properties.h | 27 ++++ .../distributeddata/include/js_util.h | 42 +++-- .../distributeddata/include/kv_manager.h | 2 +- .../distributeddata/include/single_kv_store.h | 2 +- .../distributeddata/src/async_call.cpp | 2 +- .../distributeddata/src/entry_point.cpp | 37 ++--- .../src/js_const_properties.cpp | 148 ++++++++++++++++++ .../distributeddata/src/js_util.cpp | 8 +- .../distributeddata/src/kv_manager.cpp | 2 +- .../distributeddata/src/single_kv_store.cpp | 2 +- interfaces/jskits/distributeddata/BUILD.gn | 5 +- 12 files changed, 228 insertions(+), 51 deletions(-) create mode 100644 frameworks/jskitsimpl/distributeddata/include/js_const_properties.h create mode 100644 frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp diff --git a/frameworks/jskitsimpl/distributeddata/include/async_call.h b/frameworks/jskitsimpl/distributeddata/include/async_call.h index 892ff9c3b..475ca2f8a 100644 --- a/frameworks/jskitsimpl/distributeddata/include/async_call.h +++ b/frameworks/jskitsimpl/distributeddata/include/async_call.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/jskitsimpl/distributeddata/include/js_const_properties.h b/frameworks/jskitsimpl/distributeddata/include/js_const_properties.h new file mode 100644 index 000000000..68e31db76 --- /dev/null +++ b/frameworks/jskitsimpl/distributeddata/include/js_const_properties.h @@ -0,0 +1,27 @@ + +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_JS_CONST_PROPERTIES_H +#define OHOS_JS_CONST_PROPERTIES_H + +#include "napi/native_common.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS::DistributedData { +napi_status InitConstProperties(napi_env env, napi_value exports); +} + +#endif // OHOS_JS_CONST_PROPERTIES_H diff --git a/frameworks/jskitsimpl/distributeddata/include/js_util.h b/frameworks/jskitsimpl/distributeddata/include/js_util.h index c47682a4a..7df24a96a 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_util.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -27,6 +27,31 @@ namespace OHOS::DistributedData { class JSUtil { public: + enum ValueType : uint8_t { + /** Indicates that the value type is string. */ + STRING = 0, + /** Indicates that the value type is int. */ + INTEGER = 1, + /** Indicates that the value type is float. */ + FLOAT = 2, + /** Indicates that the value type is byte array. */ + BYTE_ARRAY = 3, + /** Indicates that the value type is boolean. */ + BOOLEAN = 4, + /** Indicates that the value type is double. */ + DOUBLE = 5, + + INVALID = 255, + }; + + enum { + /* exported js SubscribeType is (DistributedKv::SubscribeType-1) */ + SUBSCRIBE_LOCAL = 0, /* i.e. SubscribeType::SUBSCRIBE_TYPE_LOCAL-1 */ + SUBSCRIBE_REMOTE = 1, /* i.e. SubscribeType::SUBSCRIBE_TYPE_REMOTE-1 */ + SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL--1 */ + SUBSCRIBE_COUNT = 3 + }; + static constexpr int32_t MAX_ARGC = 6; static constexpr int32_t MAX_NUMBER_BYTES = 8; static constexpr int32_t MAX_LEN = 4096; @@ -48,22 +73,7 @@ public: private: static napi_value GetJSEntries(napi_env env, const std::list &entries); static napi_value GetJSEntries(napi_env env, const std::vector &entries); - enum ValueType : uint8_t { - /** Indicates that the value type is string. */ - STRING = 0, - /** Indicates that the value type is int. */ - INTEGER = 1, - /** Indicates that the value type is float. */ - FLOAT = 2, - /** Indicates that the value type is byte array. */ - BYTE_ARRAY = 3, - /** Indicates that the value type is boolean. */ - BOOLEAN = 4, - /** Indicates that the value type is double. */ - DOUBLE = 5, - INVALID = 255, - }; static constexpr int32_t TYPE_POS = 0; static constexpr int32_t DATA_POS = TYPE_POS + 1; static constexpr int32_t TUPLE_SIZE = 2; diff --git a/frameworks/jskitsimpl/distributeddata/include/kv_manager.h b/frameworks/jskitsimpl/distributeddata/include/kv_manager.h index 90bd20c57..53a4b2f95 100644 --- a/frameworks/jskitsimpl/distributeddata/include/kv_manager.h +++ b/frameworks/jskitsimpl/distributeddata/include/kv_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h index f9a273751..a83d9d525 100644 --- a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/jskitsimpl/distributeddata/src/async_call.cpp b/frameworks/jskitsimpl/distributeddata/src/async_call.cpp index 41f588809..052d3dfdf 100644 --- a/frameworks/jskitsimpl/distributeddata/src/async_call.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/async_call.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/jskitsimpl/distributeddata/src/entry_point.cpp b/frameworks/jskitsimpl/distributeddata/src/entry_point.cpp index ce56ad52e..4ced3afd3 100644 --- a/frameworks/jskitsimpl/distributeddata/src/entry_point.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/entry_point.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,36 +13,27 @@ * limitations under the License. */ #define LOG_TAG "EntryPoint" -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "kv_manager.h" +#include "js_const_properties.h" #include "js_util.h" +#include "kv_manager.h" #include "log_print.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + using namespace OHOS::DistributedData; using namespace OHOS::DistributedKv; -extern const char _binary_distributed_data_js_start[]; -extern const char _binary_distributed_data_js_end[]; - static napi_value Init(napi_env env, napi_value exports) { - napi_property_descriptor desc = DECLARE_NAPI_METHOD("createKVManager", KVManager::CreateKVManager); - napi_status status = napi_define_properties(env, exports, 1, &desc); - ZLOGI("init distributedData %{public}d", status); - return exports; -} - -// function name: NAPI_{ModuleName}_GetJSCode -extern "C" __attribute__((visibility("default"))) void NAPI_data_distributedData_GetJSCode(const char** buf, - int* bufLen) -{ - if (buf != nullptr) { - *buf = _binary_distributed_data_js_start; - } + const napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("createKVManager", KVManager::CreateKVManager) + }; + napi_status status = napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + ZLOGI("init createKVManager %{public}d", status); - if (bufLen != nullptr) { - *bufLen = _binary_distributed_data_js_end - _binary_distributed_data_js_start; - } + status = InitConstProperties(env, exports); + ZLOGI("init Enumerate Constants %{public}d", status); + return exports; } static __attribute__((constructor)) void RegisterModule() diff --git a/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp b/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp new file mode 100644 index 000000000..e23b47160 --- /dev/null +++ b/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "Const_Properties" +#include "js_const_properties.h" +#include "js_util.h" +#include "log_print.h" +#include "types.h" + +using namespace OHOS::DistributedKv; +namespace OHOS::DistributedData { +static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value) +{ + napi_value property = nullptr; + napi_status status = napi_create_int32(env, value, &property); + if (status != napi_ok) { + ZLOGE("int32_t to napi_value failed!"); + return status; + } + status = napi_set_named_property(env, obj, name.c_str(), property); + if (status != napi_ok) { + ZLOGE("napi_set_named_property failed!"); + return status; + } + return status; +} + +static napi_value ExportUserType(napi_env env) +{ + constexpr int32_t SAME_USER_ID = 0; + + napi_value userType = nullptr; + napi_create_object(env, &userType); + SetNamedProperty(env, userType, "SAME_USER_ID", SAME_USER_ID); + napi_object_freeze(env, userType); + return userType; +} + +static napi_value ExportConstants(napi_env env) +{ + constexpr int32_t MAX_KEY_LENGTH = 1024; + constexpr int32_t MAX_VALUE_LENGTH = 4194303; + constexpr int32_t MAX_KEY_LENGTH_DEVICE = 896; + constexpr int32_t MAX_STORE_ID_LENGTH = 128; + constexpr int32_t MAX_QUERY_LENGTH = 512000; + constexpr int32_t MAX_BATCH_SIZE = 128; + + napi_value constants = nullptr; + napi_create_object(env, &constants); + SetNamedProperty(env, constants, "MAX_KEY_LENGTH", MAX_KEY_LENGTH); + SetNamedProperty(env, constants, "MAX_VALUE_LENGTH", MAX_VALUE_LENGTH); + SetNamedProperty(env, constants, "MAX_KEY_LENGTH_DEVICE", MAX_KEY_LENGTH_DEVICE); + SetNamedProperty(env, constants, "MAX_STORE_ID_LENGTH", MAX_STORE_ID_LENGTH); + SetNamedProperty(env, constants, "MAX_QUERY_LENGTH", MAX_QUERY_LENGTH); + SetNamedProperty(env, constants, "MAX_BATCH_SIZE", MAX_BATCH_SIZE); + napi_object_freeze(env, constants); + return constants; +} + +static napi_value ExportValueType(napi_env env) +{ + napi_value valueType = nullptr; + napi_create_object(env, &valueType); + SetNamedProperty(env, valueType, "STRING", (int32_t)JSUtil::STRING); + SetNamedProperty(env, valueType, "INTEGER", (int32_t)JSUtil::INTEGER); + SetNamedProperty(env, valueType, "FLOAT", (int32_t)JSUtil::FLOAT); + SetNamedProperty(env, valueType, "BYTE_ARRAY", (int32_t)JSUtil::BYTE_ARRAY); + SetNamedProperty(env, valueType, "BOOLEAN", (int32_t)JSUtil::BOOLEAN); + SetNamedProperty(env, valueType, "DOUBLE", (int32_t)JSUtil::DOUBLE); + napi_object_freeze(env, valueType); + return valueType; +} + +static napi_value ExportSyncMode(napi_env env) +{ + napi_value syncMode = nullptr; + napi_create_object(env, &syncMode); + SetNamedProperty(env, syncMode, "PULL_ONLY", (int32_t)SyncMode::PULL); + SetNamedProperty(env, syncMode, "PUSH_ONLY", (int32_t)SyncMode::PUSH); + SetNamedProperty(env, syncMode, "PUSH_PULL", (int32_t)SyncMode::PUSH_PULL); + napi_object_freeze(env, syncMode); + return syncMode; +} + +static napi_value ExportSubscribeType(napi_env env) +{ + napi_value subscribeType = nullptr; + napi_create_object(env, &subscribeType); + + SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_LOCAL", (int32_t)JSUtil::SUBSCRIBE_LOCAL); + SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_REMOTE", (int32_t)JSUtil::SUBSCRIBE_REMOTE); + SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_ALL", (int32_t)JSUtil::SUBSCRIBE_LOCAL_REMOTE); + napi_object_freeze(env, subscribeType); + return subscribeType; +} + +static napi_value ExportKVStoreType(napi_env env) +{ + napi_value kvStoreType = nullptr; + napi_create_object(env, &kvStoreType); + SetNamedProperty(env, kvStoreType, "DEVICE_COLLABORATION", (int32_t)KvStoreType::DEVICE_COLLABORATION); + SetNamedProperty(env, kvStoreType, "SINGLE_VERSION", (int32_t)KvStoreType::SINGLE_VERSION); + SetNamedProperty(env, kvStoreType, "MULTI_VERSION", (int32_t)KvStoreType::MULTI_VERSION); + napi_object_freeze(env, kvStoreType); + return kvStoreType; +} + +static napi_value ExportSecurityLevel(napi_env env) +{ + napi_value securityLevel = nullptr; + napi_create_object(env, &securityLevel); + SetNamedProperty(env, securityLevel, "NO_LEVEL", (int32_t)SecurityLevel::NO_LABEL); + SetNamedProperty(env, securityLevel, "S0", (int32_t)SecurityLevel::S0); + SetNamedProperty(env, securityLevel, "S1", (int32_t)SecurityLevel::S1); + SetNamedProperty(env, securityLevel, "S2", (int32_t)SecurityLevel::S2); + SetNamedProperty(env, securityLevel, "S3", (int32_t)SecurityLevel::S3); + SetNamedProperty(env, securityLevel, "S4", (int32_t)SecurityLevel::S4); + napi_object_freeze(env, securityLevel); + return securityLevel; +} + +napi_status InitConstProperties(napi_env env, napi_value exports) +{ + const napi_property_descriptor properties[] = { + DECLARE_NAPI_PROPERTY("UserType", ExportUserType(env)), + DECLARE_NAPI_PROPERTY("Constants", ExportConstants(env)), + DECLARE_NAPI_PROPERTY("ValueType", ExportValueType(env)), + DECLARE_NAPI_PROPERTY("SyncMode", ExportSyncMode(env)), + DECLARE_NAPI_PROPERTY("SubscribeType", ExportSubscribeType(env)), + DECLARE_NAPI_PROPERTY("KVStoreType", ExportKVStoreType(env)), + DECLARE_NAPI_PROPERTY("SecurityLevel", ExportSecurityLevel(env)), + }; + size_t count = sizeof(properties) / sizeof(properties[0]); + + return napi_define_properties(env, exports, count, properties); +} +} \ No newline at end of file diff --git a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index 3e2f913d1..98a1efb6a 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -308,15 +308,15 @@ napi_value JSUtil::Convert2JSNumber(napi_env env, const std::vector &da switch (data[0]) { case INTEGER: memcpy_s(byteValue, sizeof(byteValue), data.data() + DATA_POS, sizeof(int32_t)); - value = *(reinterpret_cast(byteValue)); + value = *(reinterpret_cast((void*)byteValue)); break; case FLOAT: memcpy_s(byteValue, sizeof(byteValue), data.data() + DATA_POS, sizeof(float)); - value = *(reinterpret_cast(byteValue)); + value = *(reinterpret_cast((void*)byteValue)); break; case DOUBLE: memcpy_s(byteValue, sizeof(byteValue), data.data() + DATA_POS, sizeof(double)); - value = *(reinterpret_cast(byteValue)); + value = *(reinterpret_cast((void*)byteValue)); break; default: napi_get_undefined(env, &jsValue); diff --git a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp index 520b731d6..f5020ddea 100644 --- a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp index 64692d958..40f57e091 100644 --- a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/jskits/distributeddata/BUILD.gn b/interfaces/jskits/distributeddata/BUILD.gn index 109fcc69f..eccbe5e73 100644 --- a/interfaces/jskits/distributeddata/BUILD.gn +++ b/interfaces/jskits/distributeddata/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -46,13 +46,14 @@ ohos_shared_library("distributeddata") { sources = [ "../../../frameworks/jskitsimpl/distributeddata/src/async_call.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/entry_point.cpp", + "../../../frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/js_util.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp", ] deps = [ - ":distributed_data_js", + #":distributed_data_js", "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//third_party/libuv:uv_static", -- Gitee