From 0fdd15c507b7d4ed4aafde992d0c3271951ae639 Mon Sep 17 00:00:00 2001 From: huangjiaqi Date: Tue, 4 Jan 2022 20:13:34 +0800 Subject: [PATCH] js api8 update. Signed-off-by: huangjiaqi --- .../include/js_const_properties.h | 27 ++ .../include/js_device_kv_store.h | 2 +- .../distributeddata/include/js_field_node.h | 18 +- .../distributeddata/include/js_kv_manager.h | 2 +- .../distributeddata/include/js_kv_store.h | 12 +- .../include/js_kv_store_resultset.h | 2 +- .../distributeddata/include/js_query.h | 2 +- .../distributeddata/include/js_schema.h | 20 +- .../include/js_single_kv_store.h | 2 +- .../distributeddata/include/js_util.h | 17 +- .../distributeddata/include/napi_queue.h | 17 +- .../distributeddata/include/uv_queue.h | 2 +- .../distributeddata/src/entry_point.cpp | 119 +-------- .../src/js_const_properties.cpp | 143 +++++++++++ .../src/js_device_kv_store.cpp | 88 +++---- .../distributeddata/src/js_field_node.cpp | 218 ++++++++++++---- .../distributeddata/src/js_kv_manager.cpp | 81 +++--- .../distributeddata/src/js_kv_store.cpp | 138 +++++----- .../src/js_kv_store_resultset.cpp | 50 ++-- .../distributeddata/src/js_query.cpp | 167 ++++++------ .../distributeddata/src/js_schema.cpp | 146 +++++++++-- .../src/js_single_kv_store.cpp | 96 +++---- .../distributeddata/src/js_util.cpp | 237 +++++++++--------- .../distributeddata/src/napi_queue.cpp | 20 +- .../distributeddata/src/uv_queue.cpp | 2 +- interfaces/jskits/distributeddata/BUILD.gn | 3 +- 26 files changed, 1002 insertions(+), 629 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/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_device_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_device_kv_store.h index 4ad679afa..aa016aeb9 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_device_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_device_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/include/js_field_node.h b/frameworks/jskitsimpl/distributeddata/include/js_field_node.h index a21b5b851..a861306ec 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_field_node.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_field_node.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 @@ -14,18 +14,23 @@ */ #ifndef OHOS_FIELD_NODE_H #define OHOS_FIELD_NODE_H -#include #include +#include #include "js_util.h" #include "napi_queue.h" namespace OHOS::DistributedData { +using json = nlohmann::json; class JsFieldNode { public: JsFieldNode(const std::string& fName); ~JsFieldNode() = default; + std::string GetFieldName(); + std::string Dump(); + json GetValueForJson(); + static napi_value Constructor(napi_env env); static napi_value New(napi_env env, napi_callback_info info); @@ -35,13 +40,20 @@ private: static napi_value ToJson(napi_env env, napi_callback_info info); static napi_value GetDefaultValue(napi_env env, napi_callback_info info); static napi_value SetDefaultValue(napi_env env, napi_callback_info info); + static napi_value GetNullable(napi_env env, napi_callback_info info); + static napi_value SetNullable(napi_env env, napi_callback_info info); + static napi_value GetValueType(napi_env env, napi_callback_info info); + static napi_value SetValueType(napi_env env, napi_callback_info info); - std::string Dump(); + std::string ValueToString(JSUtil::KvStoreVariant value); + std::string ValueTypeToString(uint32_t type); std::list fields; std::string fieldName; + uint32_t valueType = JSUtil::INVALID; JSUtil::KvStoreVariant defaultValue; bool isWithDefaultValue = false; + bool isNullable = false; }; } #endif // OHOS_FIELD_NODE_H diff --git a/frameworks/jskitsimpl/distributeddata/include/js_kv_manager.h b/frameworks/jskitsimpl/distributeddata/include/js_kv_manager.h index fab69a69e..678250f6c 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_kv_manager.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_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/js_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h index 58c33f735..8f2afd144 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_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 @@ -56,11 +56,11 @@ public: private: /* private static members */ - static napi_status OnDataChange(napi_env env, size_t argc, napi_value* argv, JsKVStore* kvStore); - static napi_status OffDataChange(napi_env env, size_t argc, napi_value* argv, JsKVStore* kvStore); + static void OnDataChange(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt); + static void OffDataChange(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt); - static napi_status OnSyncComplete(napi_env env, size_t argc, napi_value* argv, JsKVStore* kvStore); - static napi_status OffSyncComplete(napi_env env, size_t argc, napi_value* argv, JsKVStore* kvStore); + static void OnSyncComplete(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt); + static void OffSyncComplete(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt); /* private non-static members */ napi_status Subscribe(uint8_t type, std::shared_ptr observer); @@ -73,7 +73,7 @@ private: std::shared_ptr kvStore_ = nullptr; std::string storeId_; - using Exec = std::function; + using Exec = std::function)>; static std::map onEventHandlers_; static std::map offEventHandlers_; diff --git a/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h b/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h index 45ba972df..19d042a49 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.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_query.h b/frameworks/jskitsimpl/distributeddata/include/js_query.h index 528cd32c3..6600c1370 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_query.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_query.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_schema.h b/frameworks/jskitsimpl/distributeddata/include/js_schema.h index 95f331d6d..860d3cea5 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_schema.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_schema.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 @@ -33,14 +33,26 @@ private: static napi_value ToJson(napi_env env, napi_callback_info info); static napi_value GetRootNode(napi_env env, napi_callback_info info); static napi_value SetRootNode(napi_env env, napi_callback_info info); + static napi_value GetMode(napi_env env, napi_callback_info info); + static napi_value SetMode(napi_env env, napi_callback_info info); + static napi_value GetSkip(napi_env env, napi_callback_info info); + static napi_value SetSkip(napi_env env, napi_callback_info info); + static napi_value GetIndexes(napi_env env, napi_callback_info info); + static napi_value SetIndexes(napi_env env, napi_callback_info info); std::string Dump(); - JsFieldNode* rootFieldNode = nullptr; + enum { + SCHEMA_MODE_SLOPPY, + SCHEMA_MODE_STRICT, + }; + JsFieldNode* rootNode = nullptr; napi_env env = nullptr; // manage the root. set/get. napi_ref ref = nullptr; // manage the root. set/get. - std::list indexes; - std::list> compositeIndexes; + + std::vector indexes; + uint32_t mode = SCHEMA_MODE_SLOPPY; + uint32_t skip = 0; }; } #endif // OHOS_SCHEMA_H diff --git a/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h index c1fb1aef3..cff7d9b9e 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_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/include/js_util.h b/frameworks/jskitsimpl/distributeddata/include/js_util.h index e7dd53f82..a601b3541 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 @@ -22,6 +22,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "log_print.h" namespace OHOS::DistributedData { using namespace OHOS::DistributedKv; @@ -135,12 +136,16 @@ public: template static inline napi_status GetNamedProperty(napi_env env, napi_value in, const std::string& prop, T& value) { - napi_value inner = nullptr; - napi_status status = napi_get_named_property(env, in, prop.data(), &inner); - if ((status == napi_ok) && (inner != nullptr)) { - return GetValue(env, inner, value); + bool hasProp = false; + napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); + if ((status == napi_ok) && hasProp) { + napi_value inner = nullptr; + status = napi_get_named_property(env, in, prop.c_str(), &inner); + if ((status == napi_ok) && (inner != nullptr)) { + return GetValue(env, inner, value); + } } - return status; + return napi_invalid_arg; }; /* napi_define_class wrapper */ diff --git a/frameworks/jskitsimpl/distributeddata/include/napi_queue.h b/frameworks/jskitsimpl/distributeddata/include/napi_queue.h index ff07403d9..66270b107 100644 --- a/frameworks/jskitsimpl/distributeddata/include/napi_queue.h +++ b/frameworks/jskitsimpl/distributeddata/include/napi_queue.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 @@ -34,12 +34,10 @@ struct ContextBase { void GetCbInfo( napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser(), bool sync = false); - inline napi_status GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser()) + inline void GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser()) { /* sync = true, means no callback, not AsyncWork. */ GetCbInfo(env, info, parse, true); - NAPI_ASSERT_BASE(env, status == napi_ok, "invalid arguments!", status); - return status; // return napi_status for NAPI_CALL(). } napi_env env = nullptr; @@ -63,8 +61,8 @@ private: friend class NapiQueue; }; -/* ZLOGE on condition related to argc/argv, */ -#define ZLOGE_ON_ARGS(ctxt, condition, message) \ +/* check condition related to argc/argv, return and logging. */ +#define CHECK_ARGS(ctxt, condition, message) \ do { \ if (!(condition)) { \ (ctxt)->status = napi_invalid_arg; \ @@ -74,7 +72,7 @@ private: } \ } while (0) -#define ZLOGE_ON_STATUS(ctxt, message) \ +#define CHECK_STATUS(ctxt, message) \ do { \ if ((ctxt)->status != napi_ok) { \ (ctxt)->error = std::string(message); \ @@ -83,7 +81,8 @@ private: } \ } while (0) -#define ZLOGE_RETURN(condition, message, retVal) \ +/* check condition, return and logging if condition not true. */ +#define CHECK_RETURN(condition, message, retVal) \ do { \ if (!(condition)) { \ ZLOGE("test (" #condition ") failed: " message); \ @@ -91,7 +90,7 @@ private: } \ } while (0) -#define ZLOGE_RETURN_VOID(condition, message) \ +#define CHECK_RETURN_VOID(condition, message) \ do { \ if (!(condition)) { \ ZLOGE("test (" #condition ") failed: " message); \ diff --git a/frameworks/jskitsimpl/distributeddata/include/uv_queue.h b/frameworks/jskitsimpl/distributeddata/include/uv_queue.h index 87dc11702..58a5b2915 100644 --- a/frameworks/jskitsimpl/distributeddata/include/uv_queue.h +++ b/frameworks/jskitsimpl/distributeddata/include/uv_queue.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/entry_point.cpp b/frameworks/jskitsimpl/distributeddata/src/entry_point.cpp index 49e6f3ff2..972653176 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,9 +13,9 @@ * limitations under the License. */ #define LOG_TAG "EntryPoint" +#include "js_const_properties.h" #include "js_field_node.h" #include "js_kv_manager.h" -#include "js_kv_store.h" #include "js_query.h" #include "js_schema.h" #include "js_util.h" @@ -24,119 +24,6 @@ using namespace OHOS::DistributedData; using namespace OHOS::DistributedKv; -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); - ZLOGE_RETURN(status == napi_ok, "int32_t to napi_value failed!", status); - status = napi_set_named_property(env, obj, name.c_str(), property); - ZLOGE_RETURN(status == napi_ok, "napi_set_named_property failed!", 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); - 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); - 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); - 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); - 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)SUBSCRIBE_LOCAL); - SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_REMOTE", (int32_t)SUBSCRIBE_REMOTE); - SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_ALL", (int32_t)SUBSCRIBE_LOCAL_REMOTE); - 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); - 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); - return securityLevel; -} - -static napi_status InitEnumerates(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); -} - static napi_value Init(napi_env env, napi_value exports) { const napi_property_descriptor desc[] = { @@ -154,7 +41,7 @@ static napi_value Init(napi_env env, napi_value exports) status = napi_set_named_property(env, exports, "Query", JsQuery::Constructor(env)); ZLOGI("init Query %{public}d", status); - status = InitEnumerates(env, exports); + status = InitConstProperties(env, exports); ZLOGI("init Enumerate Constants %{public}d", status); return exports; } 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..c559973e9 --- /dev/null +++ b/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp @@ -0,0 +1,143 @@ +/* + * 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 "js_kv_store.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); + CHECK_RETURN(status == napi_ok, "int32_t to napi_value failed!", status); + status = napi_set_named_property(env, obj, name.c_str(), property); + CHECK_RETURN(status == napi_ok, "napi_set_named_property failed!", 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)SUBSCRIBE_LOCAL); + SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_REMOTE", (int32_t)SUBSCRIBE_REMOTE); + SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_ALL", (int32_t)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_device_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp index db2fc9f30..15e513ff3 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_device_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 @@ -86,11 +86,11 @@ napi_value JsDeviceKVStore::Get(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // number 2 means: required 2 arguments, + - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->key); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid key!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid key!"); }; ctxt->GetCbInfo(env, info, input); @@ -103,11 +103,11 @@ napi_value JsDeviceKVStore::Get(napi_env env, napi_callback_info info) ZLOGD("kvStore->Get return %{public}d", status); ctxt->value = JSUtil::Blob2VariantValue(value); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Get() failed!"); + CHECK_STATUS(ctxt, "kvStore->Get() failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, ctxt->value, result); - ZLOGE_ON_STATUS(ctxt, "output failed"); + CHECK_STATUS(ctxt, "output failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -129,32 +129,32 @@ struct VariantArgs { static napi_status GetVariantArgs(napi_env env, size_t argc, napi_value* argv, VariantArgs& va) { - ZLOGE_RETURN(argc > 0, "invalid arguments!", napi_invalid_arg); + CHECK_RETURN(argc > 0, "invalid arguments!", napi_invalid_arg); napi_valuetype type = napi_undefined; napi_status status = napi_typeof(env, argv[0], &type); - ZLOGE_RETURN((type == napi_string) || (type == napi_object), "invalid arg[0], type error!", napi_invalid_arg); + CHECK_RETURN((type == napi_string) || (type == napi_object), "invalid arg[0], type error!", napi_invalid_arg); if (type == napi_string) { // number 2 means: required 2 arguments, - ZLOGE_RETURN(argc == 2, "invalid arguments!", napi_invalid_arg); + CHECK_RETURN(argc == 2, "invalid arguments!", napi_invalid_arg); status = JSUtil::GetValue(env, argv[0], va.deviceId); - ZLOGE_RETURN(!va.deviceId.empty(), "invalid arg[0], i.e. invalid deviceId!", napi_invalid_arg); + CHECK_RETURN(!va.deviceId.empty(), "invalid arg[0], i.e. invalid deviceId!", napi_invalid_arg); status = napi_typeof(env, argv[1], &type); - ZLOGE_RETURN((type == napi_string) || (type == napi_object), "invalid arg[1], type error!", napi_invalid_arg); + CHECK_RETURN((type == napi_string) || (type == napi_object), "invalid arg[1], type error!", napi_invalid_arg); if (type == napi_string) { status = JSUtil::GetValue(env, argv[1], va.keyPrefix); - ZLOGE_RETURN(!va.keyPrefix.empty(), "invalid arg[1], i.e. invalid keyPrefix!", napi_invalid_arg); + CHECK_RETURN(!va.keyPrefix.empty(), "invalid arg[1], i.e. invalid keyPrefix!", napi_invalid_arg); va.type = ArgsType::DEVICEID_KEYPREFIX; } else if (type == napi_object) { status = JSUtil::Unwrap(env, argv[1], (void**)(&va.query), JsQuery::Constructor(env)); - ZLOGE_RETURN(va.query != nullptr, "invalid arg[1], i.e. invalid query!", napi_invalid_arg); + CHECK_RETURN(va.query != nullptr, "invalid arg[1], i.e. invalid query!", napi_invalid_arg); va.type = ArgsType::DEVICEID_QUERY; } } else if (type == napi_object) { // number 1 means: required 1 arguments, - ZLOGE_RETURN(argc == 1, "invalid arguments!", napi_invalid_arg); + CHECK_RETURN(argc == 1, "invalid arguments!", napi_invalid_arg); status = JSUtil::Unwrap(env, argv[0], (void**)(&va.query), JsQuery::Constructor(env)); - ZLOGE_RETURN(va.query != nullptr, "invalid arg[0], i.e. invalid query!", napi_invalid_arg); + CHECK_RETURN(va.query != nullptr, "invalid arg[0], i.e. invalid query!", napi_invalid_arg); va.type = ArgsType::QUERY; } return status; @@ -181,7 +181,7 @@ napi_value JsDeviceKVStore::GetEntries(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { ctxt->status = GetVariantArgs(env, argc, argv, ctxt->va); - ZLOGE_ON_STATUS(ctxt, "invalid arguments!"); + CHECK_STATUS(ctxt, "invalid arguments!"); }; ctxt->GetCbInfo(env, info, input); @@ -204,11 +204,11 @@ napi_value JsDeviceKVStore::GetEntries(napi_env env, napi_callback_info info) ZLOGD("kvStore->GetEntriesWithQuery() return %{public}d", status); } ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetEntries() failed!"); + CHECK_STATUS(ctxt, "kvStore->GetEntries() failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, ctxt->entries, result); - ZLOGE_ON_STATUS(ctxt, "output failed!"); + CHECK_STATUS(ctxt, "output failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -235,10 +235,10 @@ napi_value JsDeviceKVStore::GetResultSet(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { ctxt->status = GetVariantArgs(env, argc, argv, ctxt->va); - ZLOGE_ON_STATUS(ctxt, "invalid arguments!"); + CHECK_STATUS(ctxt, "invalid arguments!"); ctxt->ref = JSUtil::NewWithRef(env, 0, nullptr, (void**)(&ctxt->resultSet), JsKVStoreResultSet::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->resultSet != nullptr, "KVStoreResultSet::New failed!"); + CHECK_ARGS(ctxt, ctxt->resultSet != nullptr, "KVStoreResultSet::New failed!"); }; ctxt->GetCbInfo(env, info, input); @@ -262,13 +262,13 @@ napi_value JsDeviceKVStore::GetResultSet(napi_env env, napi_callback_info info) ZLOGD("kvStore->GetEntriesWithQuery() return %{public}d", status); } ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetResultSet() failed!"); + CHECK_STATUS(ctxt, "kvStore->GetResultSet() failed!"); ctxt->resultSet->SetNative(kvResultSet); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = napi_get_reference_value(env, ctxt->ref, &result); napi_delete_reference(env, ctxt->ref); - ZLOGE_ON_STATUS(ctxt, "output KvResultSet failed"); + CHECK_STATUS(ctxt, "output KvResultSet failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -287,13 +287,13 @@ napi_value JsDeviceKVStore::CloseResultSet(napi_env env, napi_callback_info info auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); napi_valuetype type = napi_undefined; ctxt->status = napi_typeof(env, argv[0], &type); - ZLOGE_ON_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid resultSet!"); + CHECK_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid resultSet!"); ctxt->status = JSUtil::Unwrap(env, argv[0], (void**)(&ctxt->resultSet), JsKVStoreResultSet::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->resultSet != nullptr, "invalid arg[0], i.e. invalid resultSet!"); + CHECK_ARGS(ctxt, ctxt->resultSet != nullptr, "invalid arg[0], i.e. invalid resultSet!"); }; ctxt->GetCbInfo(env, info, input); @@ -302,7 +302,7 @@ napi_value JsDeviceKVStore::CloseResultSet(napi_env env, napi_callback_info info Status status = kvStore->CloseResultSet(ctxt->resultSet->GetNative()); ZLOGD("kvStore->CloseResultSet return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->CloseResultSet failed!"); + CHECK_STATUS(ctxt, "kvStore->CloseResultSet failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -325,9 +325,9 @@ napi_value JsDeviceKVStore::GetResultSize(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { ctxt->status = GetVariantArgs(env, argc, argv, ctxt->va); - ZLOGE_ON_ARGS(ctxt, (ctxt->va.type == ArgsType::DEVICEID_QUERY) || (ctxt->va.type == ArgsType::QUERY), + CHECK_ARGS(ctxt, (ctxt->va.type == ArgsType::DEVICEID_QUERY) || (ctxt->va.type == ArgsType::QUERY), "invalid arguments!"); - ZLOGE_ON_STATUS(ctxt, "invalid arguments!"); + CHECK_STATUS(ctxt, "invalid arguments!"); }; ctxt->GetCbInfo(env, info, input); @@ -340,11 +340,11 @@ napi_value JsDeviceKVStore::GetResultSize(napi_env env, napi_callback_info info) Status status = kvStore->GetCountWithQuery(query.ToString(), ctxt->resultSize); ZLOGD("kvStore->GetCountWithQuery() return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetCountWithQuery() failed!"); + CHECK_STATUS(ctxt, "kvStore->GetCountWithQuery() failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, static_cast(ctxt->resultSize), result); - ZLOGE_ON_STATUS(ctxt, "output resultSize failed!"); + CHECK_STATUS(ctxt, "output resultSize failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -363,9 +363,9 @@ napi_value JsDeviceKVStore::RemoveDeviceData(napi_env env, napi_callback_info in auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); }; ctxt->GetCbInfo(env, info, input); @@ -374,7 +374,7 @@ napi_value JsDeviceKVStore::RemoveDeviceData(napi_env env, napi_callback_info in Status status = kvStore->RemoveDeviceData(ctxt->deviceId); ZLOGD("kvStore->RemoveDeviceData return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->RemoveDeviceData() failed!"); + CHECK_STATUS(ctxt, "kvStore->RemoveDeviceData() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -392,14 +392,15 @@ napi_value JsDeviceKVStore::Sync(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: + - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceIdList); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceIdList!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceIdList!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->mode); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid mode!"); - ZLOGE_ON_ARGS(ctxt, ctxt->mode <= uint32_t(SyncMode::PUSH_PULL), "invalid arg[1], i.e. invalid mode!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid mode!"); + CHECK_ARGS(ctxt, ctxt->mode <= uint32_t(SyncMode::PUSH_PULL), "invalid arg[1], i.e. invalid mode!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& kvStore = reinterpret_cast(ctxt->native)->GetNative(); Status status = kvStore->Sync(ctxt->deviceIdList, static_cast(ctxt->mode)); @@ -415,12 +416,13 @@ napi_value JsDeviceKVStore::New(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &storeId](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], storeId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid storeId!"); - ZLOGE_ON_ARGS(ctxt, !storeId.empty(), "invalid arg[0], i.e. invalid storeId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid storeId!"); + CHECK_ARGS(ctxt, !storeId.empty(), "invalid arg[0], i.e. invalid storeId!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsDeviceKVStore* kvStore = new (std::nothrow) JsDeviceKVStore(storeId); NAPI_ASSERT(env, kvStore !=nullptr, "no memory for kvStore"); @@ -428,7 +430,7 @@ napi_value JsDeviceKVStore::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("deviceKvStore finalize."); auto* kvStore = reinterpret_cast(data); - ZLOGE_RETURN_VOID(kvStore != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvStore != nullptr, "finalize null!"); delete kvStore; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr)); diff --git a/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp b/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp index 4948cfc3c..70ae140be 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_field_node.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 @@ -26,8 +26,10 @@ using json = nlohmann::json; namespace OHOS::DistributedData { static std::string FIELDNAME = "FIELDNAME"; +static std::string VALUETYPE = "VALUETYPE"; static std::string DEFAULTVALUE = "DEFAULTVALUE"; static std::string ISWITHDEFAULTVALUE = "ISWITHDEFAULTVALUE"; +static std::string ISNULLABLE = "ISNULLABLE"; static std::string CHILDREN = "CHILDREN"; JsFieldNode::JsFieldNode(const std::string& fName) @@ -35,12 +37,37 @@ JsFieldNode::JsFieldNode(const std::string& fName) { } +std::string JsFieldNode::GetFieldName() +{ + return fieldName; +} + +json JsFieldNode::GetValueForJson() +{ + if (!fields.empty()) { + /* example: + { "field_root": { + "field_child1": "LONG, NOT NULL, DEFAULT 88", + "field_child2": "LONG, NOT NULL, DEFAULT 88" } } */ + json jsFields; + for (auto fld : fields) { + jsFields[fld->fieldName] = fld->GetValueForJson(); + } + return jsFields; + } + + /* example: { "field_name": "LONG, NOT NULL, DEFAULT 88" } */ + return ValueTypeToString(valueType) + "," + (isNullable ? "NULL" : "NOT NULL"); +} + napi_value JsFieldNode::Constructor(napi_env env) { const napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("appendChild", JsFieldNode::AppendChild), DECLARE_NAPI_FUNCTION("toJson", JsFieldNode::ToJson), - DECLARE_NAPI_GETTER_SETTER("defaultValue", JsFieldNode::GetDefaultValue, JsFieldNode::SetDefaultValue) + DECLARE_NAPI_GETTER_SETTER("default", JsFieldNode::GetDefaultValue, JsFieldNode::SetDefaultValue), + DECLARE_NAPI_GETTER_SETTER("nullable", JsFieldNode::GetNullable, JsFieldNode::SetNullable), + DECLARE_NAPI_GETTER_SETTER("type", JsFieldNode::GetValueType, JsFieldNode::SetValueType) }; size_t count = sizeof(properties) / sizeof(properties[0]); return JSUtil::DefineClass(env, "FieldNode", properties, count, JsFieldNode::New); @@ -53,20 +80,21 @@ napi_value JsFieldNode::New(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &fieldName](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], fieldName); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid fieldName!"); - ZLOGE_ON_ARGS(ctxt, !fieldName.empty(), "invalid arg[0], i.e. invalid fieldName!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid fieldName!"); + CHECK_ARGS(ctxt, !fieldName.empty(), "invalid arg[0], i.e. invalid fieldName!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsFieldNode* fieldNode = new (std::nothrow) JsFieldNode(fieldName); - NAPI_ASSERT(env, fieldNode !=nullptr, "no memory for fieldNode"); + NAPI_ASSERT(env, fieldNode != nullptr, "no memory for fieldNode"); auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("fieldNode finalize."); auto* field = reinterpret_cast(data); - ZLOGE_RETURN_VOID(field != nullptr, "finalize null!"); + CHECK_RETURN_VOID(field != nullptr, "finalize null!"); delete field; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, fieldNode, finalize, nullptr, nullptr)); @@ -75,17 +103,18 @@ napi_value JsFieldNode::New(napi_env env, napi_callback_info info) napi_value JsFieldNode::AppendChild(napi_env env, napi_callback_info info) { - ZLOGD("FieldNode::New"); + ZLOGD("FieldNode::AppendChild"); JsFieldNode* child = nullptr; auto ctxt = std::make_shared(); auto input = [env, ctxt, &child](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::Unwrap(env, argv[0], (void**)(&child), JsFieldNode::Constructor(env)); - ZLOGE_ON_STATUS(ctxt, "napi_unwrap to FieldNode failed"); - ZLOGE_ON_ARGS(ctxt, child != nullptr, "invalid arg[0], i.e. invalid FieldNode!"); + CHECK_STATUS(ctxt, "napi_unwrap to FieldNode failed"); + CHECK_ARGS(ctxt, child != nullptr, "invalid arg[0], i.e. invalid FieldNode!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); fieldNode->fields.push_back(child); @@ -93,45 +122,163 @@ napi_value JsFieldNode::AppendChild(napi_env env, napi_callback_info info) napi_get_boolean(env, true, &ctxt->output); return ctxt->output; } + napi_value JsFieldNode::ToJson(napi_env env, napi_callback_info info) { - ZLOGD("FieldNode::New"); + ZLOGD("FieldNode::ToJson"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); std::string js = fieldNode->Dump(); JSUtil::SetValue(env, js, ctxt->output); return ctxt->output; } + napi_value JsFieldNode::GetDefaultValue(napi_env env, napi_callback_info info) { - ZLOGD("FieldNode::New"); + ZLOGD("FieldNode::GetDefaultValue"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); JSUtil::SetValue(env, fieldNode->defaultValue, ctxt->output); return ctxt->output; } + napi_value JsFieldNode::SetDefaultValue(napi_env env, napi_callback_info info) { - ZLOGD("FieldNode::New"); + ZLOGD("FieldNode::SetDefaultValue"); auto ctxt = std::make_shared(); JSUtil::KvStoreVariant vv; auto input = [env, ctxt, &vv](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], vv); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid defaultValue!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid defaultValue!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); fieldNode->defaultValue = vv; return nullptr; } +napi_value JsFieldNode::GetNullable(napi_env env, napi_callback_info info) +{ + ZLOGD("FieldNode::GetNullable"); + auto ctxt = std::make_shared(); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto fieldNode = reinterpret_cast(ctxt->native); + JSUtil::SetValue(env, fieldNode->isNullable, ctxt->output); + return ctxt->output; +} + +napi_value JsFieldNode::SetNullable(napi_env env, napi_callback_info info) +{ + ZLOGD("FieldNode::SetNullable"); + auto ctxt = std::make_shared(); + bool isNullable = false; + auto input = [env, ctxt, &isNullable](size_t argc, napi_value* argv) { + // required 1 arguments :: + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); + ctxt->status = JSUtil::GetValue(env, argv[0], isNullable); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid isNullable!"); + }; + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto fieldNode = reinterpret_cast(ctxt->native); + fieldNode->isNullable = isNullable; + return nullptr; +} + +napi_value JsFieldNode::GetValueType(napi_env env, napi_callback_info info) +{ + ZLOGD("FieldNode::New"); + auto ctxt = std::make_shared(); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto fieldNode = reinterpret_cast(ctxt->native); + JSUtil::SetValue(env, fieldNode->valueType, ctxt->output); + return ctxt->output; +} + +napi_value JsFieldNode::SetValueType(napi_env env, napi_callback_info info) +{ + ZLOGD("FieldNode::New"); + auto ctxt = std::make_shared(); + uint32_t type = 0; + auto input = [env, ctxt, &type](size_t argc, napi_value* argv) { + // required 1 arguments :: + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); + ctxt->status = JSUtil::GetValue(env, argv[0], type); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid valueType!"); + CHECK_ARGS(ctxt, (JSUtil::STRING <= type) && (type <= JSUtil::DOUBLE), + "invalid arg[0], i.e. invalid valueType!"); + }; + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto fieldNode = reinterpret_cast(ctxt->native); + fieldNode->valueType = type; + return nullptr; +} + +std::string JsFieldNode::ValueToString(JSUtil::KvStoreVariant value) +{ + auto strValue = std::get_if(&value); + if (strValue != nullptr) { + return (*strValue); + } + auto intValue = std::get_if(&value); + if (intValue != nullptr) { + return std::to_string(*intValue); + } + auto fltValue = std::get_if(&value); + if (fltValue != nullptr) { + return std::to_string(*fltValue); + } + auto boolValue = std::get_if(&value); + if (boolValue != nullptr) { + return std::to_string(*boolValue); + } + auto dblValue = std::get_if(&value); + if (dblValue != nullptr) { + return std::to_string(*dblValue); + } + ZLOGE("ValueType is INVALID"); + return std::string(); +} + +std::string JsFieldNode::ValueTypeToString(uint32_t type) +{ + // DistributedDB::FieldType + switch (type) { + case JSUtil::STRING: + return std::string("STRING"); + case JSUtil::INTEGER: + return std::string("INTEGER"); + case JSUtil::FLOAT: + return std::string("FLOAT"); + case JSUtil::BYTE_ARRAY: + return std::string("BYTE_ARRAY"); + case JSUtil::BOOLEAN: + return std::string("BOOLEAN"); + case JSUtil::DOUBLE: + return std::string("DOUBLE"); + default: + ZLOGE("ValueType is INVALID"); + break; + } + return std::string(); +} std::string JsFieldNode::Dump() { json jsFields; @@ -139,35 +286,12 @@ std::string JsFieldNode::Dump() jsFields.push_back(fld->Dump()); } - auto defaultValueStr = [this]() -> std::string { - auto strValue = std::get_if(&defaultValue); - if (strValue != nullptr) { - return (*strValue); - } - auto intValue = std::get_if(&defaultValue); - if (intValue != nullptr) { - return std::to_string(*intValue); - } - auto fltValue = std::get_if(&defaultValue); - if (fltValue != nullptr) { - return std::to_string(*fltValue); - } - auto boolValue = std::get_if(&defaultValue); - if (boolValue != nullptr) { - return std::to_string(*boolValue); - } - auto dblValue = std::get_if(&defaultValue); - if (dblValue != nullptr) { - return std::to_string(*dblValue); - } - ZLOGE("ValueType is INVALID"); - return std::string(); - }; - json jsNode = { { FIELDNAME, fieldName }, - { DEFAULTVALUE, defaultValueStr() }, + { VALUETYPE, ValueTypeToString(valueType) }, + { DEFAULTVALUE, ValueToString(defaultValue) }, { ISWITHDEFAULTVALUE, isWithDefaultValue }, + { ISNULLABLE, isNullable }, { CHILDREN, jsFields.dump() } }; return jsNode.dump(); diff --git a/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp b/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp index e290fab50..5073d5c1b 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_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 @@ -60,13 +60,13 @@ napi_value JsKVManager::CreateKVManager(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); std::string bundleName; ctxt->status = JSUtil::GetNamedProperty(env, argv[0], "bundleName", bundleName); - ZLOGE_ON_ARGS(ctxt, (ctxt->status == napi_ok) && !bundleName.empty(), "invalid bundleName!"); + CHECK_ARGS(ctxt, (ctxt->status == napi_ok) && !bundleName.empty(), "invalid bundleName!"); ctxt->ref = JSUtil::NewWithRef(env, argc, argv, (void**)&ctxt->kvManger, JsKVManager::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->kvManger != nullptr, "KVManager::New failed!"); + CHECK_ARGS(ctxt, ctxt->kvManger != nullptr, "KVManager::New failed!"); }; ctxt->GetCbInfo(env, info, input); @@ -74,7 +74,7 @@ napi_value JsKVManager::CreateKVManager(napi_env env, napi_callback_info info) auto output = [env, ctxt](napi_value& result) { ctxt->status = napi_get_reference_value(env, ctxt->ref, &result); napi_delete_reference(env, ctxt->ref); - ZLOGE_ON_STATUS(ctxt, "output KVManager failed"); + CHECK_STATUS(ctxt, "output KVManager failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), noExecute, output); } @@ -89,12 +89,12 @@ struct GetKVStoreContext : public ContextBase { { auto input = [env, this](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(this, argc == 2, "invalid arguments!"); + CHECK_ARGS(this, argc == 2, "invalid arguments!"); status = JSUtil::GetValue(env, argv[0], storeId); - ZLOGE_ON_ARGS(this, (status == napi_ok) && !storeId.empty(), "invalid storeId!"); + CHECK_ARGS(this, (status == napi_ok) && !storeId.empty(), "invalid storeId!"); status = JSUtil::GetValue(env, argv[1], options); - ZLOGE_ON_STATUS(this, "invalid options!"); - ZLOGE_ON_ARGS(this, IsStoreTypeSupported(options), "invalid options.KvStoreType"); + CHECK_STATUS(this, "invalid options!"); + CHECK_ARGS(this, IsStoreTypeSupported(options), "invalid options.KvStoreType"); ZLOGD("GetKVStore kvStoreType=%{public}d", options.kvStoreType); if (options.kvStoreType == KvStoreType::DEVICE_COLLABORATION) { ref = JSUtil::NewWithRef(env, argc, argv, (void**)&kvStore, JsDeviceKVStore::Constructor(env)); @@ -121,20 +121,20 @@ napi_value JsKVManager::GetKVStore(napi_env env, napi_callback_info info) auto execute = [ctxt]() { auto kvm = reinterpret_cast(ctxt->native); - ZLOGE_ON_ARGS(ctxt, kvm != nullptr, "KVManager is null, failed!"); + CHECK_ARGS(ctxt, kvm != nullptr, "KVManager is null, failed!"); AppId appId = { kvm->bundleName_ }; StoreId storeId = { ctxt->storeId }; std::shared_ptr kvStore; Status status = kvm->kvDataManager_.GetSingleKvStore(ctxt->options, appId, storeId, kvStore); ZLOGD("GetSingleKvStore return status:%{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "KVManager->GetSingleKvStore() failed!"); + CHECK_STATUS(ctxt, "KVManager->GetSingleKvStore() failed!"); ctxt->kvStore->SetNative(kvStore); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = napi_get_reference_value(env, ctxt->ref, &result); napi_delete_reference(env, ctxt->ref); - ZLOGE_ON_STATUS(ctxt, "output KvStore failed"); + CHECK_STATUS(ctxt, "output KvStore failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -155,15 +155,15 @@ napi_value JsKVManager::CloseKVStore(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 3 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 3, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 3, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->appId); - ZLOGE_ON_ARGS(ctxt, (ctxt->status == napi_ok) && !ctxt->appId.empty(), "invalid appId!"); + CHECK_ARGS(ctxt, (ctxt->status == napi_ok) && !ctxt->appId.empty(), "invalid appId!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->storeId); - ZLOGE_ON_ARGS(ctxt, (ctxt->status == napi_ok) && !ctxt->storeId.empty(), "invalid storeId!"); - ZLOGE_ON_ARGS(ctxt, argv[2] != nullptr, "kvStore is nullptr!"); + CHECK_ARGS(ctxt, (ctxt->status == napi_ok) && !ctxt->storeId.empty(), "invalid storeId!"); + CHECK_ARGS(ctxt, argv[2] != nullptr, "kvStore is nullptr!"); bool isSingle = JsKVStore::IsInstanceOf(env, argv[2], ctxt->storeId, JsSingleKVStore::Constructor(env)); bool isDevice = JsKVStore::IsInstanceOf(env, argv[2], ctxt->storeId, JsDeviceKVStore::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, isSingle || isDevice, "kvStore unmatch to storeId!"); + CHECK_ARGS(ctxt, isSingle || isDevice, "kvStore unmatch to storeId!"); }; ctxt->GetCbInfo(env, info, input); @@ -195,12 +195,12 @@ napi_value JsKVManager::DeleteKVStore(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc >= 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc >= 2, "invalid arguments!"); size_t index = 0; ctxt->status = JSUtil::GetValue(env, argv[index++], ctxt->appId); - ZLOGE_ON_ARGS(ctxt, !ctxt->appId.empty(), "invalid appId"); + CHECK_ARGS(ctxt, !ctxt->appId.empty(), "invalid appId"); ctxt->status = JSUtil::GetValue(env, argv[index++], ctxt->storeId); - ZLOGE_ON_ARGS(ctxt, !ctxt->storeId.empty(), "invalid storeId"); + CHECK_ARGS(ctxt, !ctxt->storeId.empty(), "invalid storeId"); }; ctxt->GetCbInfo(env, info, input); @@ -230,15 +230,15 @@ napi_value JsKVManager::GetAllKVStoreId(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->appId); - ZLOGE_ON_ARGS(ctxt, !ctxt->appId.empty(), "invalid appId!"); + CHECK_ARGS(ctxt, !ctxt->appId.empty(), "invalid appId!"); }; ctxt->GetCbInfo(env, info, input); auto execute = [ctxt]() { auto kvm = reinterpret_cast(ctxt->native); - ZLOGE_ON_ARGS(ctxt, kvm != nullptr, "KVManager is null, failed!"); + CHECK_ARGS(ctxt, kvm != nullptr, "KVManager is null, failed!"); AppId appId { ctxt->appId }; Status status = kvm->kvDataManager_.GetAllKvStoreId(appId, ctxt->storeIdList); ZLOGD("execute status:%{public}d", status); @@ -256,19 +256,19 @@ napi_value JsKVManager::On(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); std::string event; ctxt->status = JSUtil::GetValue(env, argv[0], event); ZLOGI("subscribe to event:%{public}s", event.c_str()); - ZLOGE_ON_ARGS(ctxt, event == "distributedDataServiceDie", "invalid arg[0], i.e. invalid event!"); + CHECK_ARGS(ctxt, event == "distributedDataServiceDie", "invalid arg[0], i.e. invalid event!"); napi_valuetype valueType = napi_undefined; ctxt->status = napi_typeof(env, argv[1], &valueType); - ZLOGE_ON_STATUS(ctxt, "napi_typeof failed!"); - ZLOGE_ON_ARGS(ctxt, valueType == napi_function, "callback is not a function"); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "callback is not a function"); JsKVManager* proxy = reinterpret_cast(ctxt->native); - ZLOGE_ON_ARGS(ctxt, proxy != nullptr, "there is no native kv manager"); + CHECK_ARGS(ctxt, proxy != nullptr, "there is no native kv manager"); std::lock_guard lck(proxy->deathMutex_); for (auto& it : proxy->deathRecipient_) { @@ -283,7 +283,8 @@ napi_value JsKVManager::On(napi_env env, napi_callback_info info) proxy->deathRecipient_.push_back(kvStoreDeathRecipient); ZLOGD("on mapsize: %{public}d", static_cast(proxy->deathRecipient_.size())); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); return nullptr; } @@ -293,18 +294,18 @@ napi_value JsKVManager::Off(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 or 2 arguments :: [callback] - ZLOGE_ON_ARGS(ctxt, (argc == 1) || (argc == 2), "invalid arguments!"); + CHECK_ARGS(ctxt, (argc == 1) || (argc == 2), "invalid arguments!"); std::string event; ctxt->status = JSUtil::GetValue(env, argv[0], event); // required 1 arguments :: ZLOGI("unsubscribe to event:%{public}s %{public}s specified", event.c_str(), (argc == 1) ? "without": "with"); - ZLOGE_ON_ARGS(ctxt, event == "distributedDataServiceDie", "invalid arg[0], i.e. invalid event!"); + CHECK_ARGS(ctxt, event == "distributedDataServiceDie", "invalid arg[0], i.e. invalid event!"); // have 2 arguments :: have the [callback] if (argc == 2) { napi_valuetype valueType = napi_undefined; ctxt->status = napi_typeof(env, argv[1], &valueType); - ZLOGE_ON_STATUS(ctxt, "napi_typeof failed!"); - ZLOGE_ON_ARGS(ctxt, valueType == napi_function, "callback is not a function"); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "callback is not a function"); } JsKVManager* proxy = reinterpret_cast(ctxt->native); std::lock_guard lck(proxy->deathMutex_); @@ -321,7 +322,8 @@ napi_value JsKVManager::Off(napi_env env, napi_callback_info info) } ZLOGD("off mapsize: %{public}d", static_cast(proxy->deathRecipient_.size())); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); ZLOGD("KVManager::Off callback is not register or already unregister!"); return nullptr; } @@ -346,12 +348,13 @@ napi_value JsKVManager::New(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &bundleName](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetNamedProperty(env, argv[0], "bundleName", bundleName); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid bundleName!"); - ZLOGE_ON_ARGS(ctxt, !bundleName.empty(), "invalid arg[0], i.e. invalid bundleName!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid bundleName!"); + CHECK_ARGS(ctxt, !bundleName.empty(), "invalid arg[0], i.e. invalid bundleName!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsKVManager* kvManager = new (std::nothrow) JsKVManager(bundleName); NAPI_ASSERT(env, kvManager !=nullptr, "no memory for kvManager"); @@ -359,7 +362,7 @@ napi_value JsKVManager::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvManager finalize."); auto* kvManager = reinterpret_cast(data); - ZLOGE_RETURN_VOID(kvManager != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvManager != nullptr, "finalize null!"); delete kvManager; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, kvManager, finalize, nullptr, nullptr)); diff --git a/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp index 8015361d6..a11488768 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_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 @@ -84,11 +84,11 @@ bool JsKVStore::IsInstanceOf(napi_env env, napi_value obj, const std::string& st { bool result = false; napi_status status = napi_instanceof(env, obj, constructor, &result); - ZLOGE_RETURN(result, "is not instance of JsKVStore!", false); + CHECK_RETURN(result, "is not instance of JsKVStore!", false); JsKVStore* kvStore = nullptr; status = napi_unwrap(env, obj, (void**)&kvStore); - ZLOGE_RETURN((status == napi_ok) && (kvStore != nullptr), "can not unwrap to JsKVStore!", false); + CHECK_RETURN((status == napi_ok) && (kvStore != nullptr), "can not unwrap to JsKVStore!", false); return kvStore->storeId_ == storeId; } @@ -111,12 +111,12 @@ napi_value JsKVStore::Put(napi_env env, napi_callback_info info) ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->key); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); JSUtil::KvStoreVariant vv; ctxt->status = JSUtil::GetValue(env, argv[1], vv); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); DistributedKv::Blob blob = JSUtil::VariantValue2Blob(vv); ctxt->value = blob.Data(); }); @@ -128,7 +128,7 @@ napi_value JsKVStore::Put(napi_env env, napi_callback_info info) Status status = kvStore->Put(key, value); ZLOGD("kvStore->Put return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Put() failed!"); + CHECK_STATUS(ctxt, "kvStore->Put() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -150,9 +150,9 @@ napi_value JsKVStore::Delete(napi_env env, napi_callback_info info) ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->key); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); }); return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), [ctxt]() { @@ -161,7 +161,7 @@ napi_value JsKVStore::Delete(napi_env env, napi_callback_info info) Status status = kvStore->Delete(key); ZLOGD("kvStore->Put return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Delete() failed!"); + CHECK_STATUS(ctxt, "kvStore->Delete() failed!"); }); } @@ -177,16 +177,17 @@ napi_value JsKVStore::OnEvent(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: [...] - ZLOGE_ON_ARGS(ctxt, argc >= 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc >= 2, "invalid arguments!"); std::string event; ctxt->status = JSUtil::GetValue(env, argv[0], event); ZLOGI("subscribe to event:%{public}s", event.c_str()); auto handle = onEventHandlers_.find(event); - ZLOGE_ON_ARGS(ctxt, handle != onEventHandlers_.end(), "invalid arg[0], i.e. unsupported event"); + CHECK_ARGS(ctxt, handle != onEventHandlers_.end(), "invalid arg[0], i.e. unsupported event"); // shift 1 argument, for JsKVStore::Exec. - handle->second(env, argc - 1, &argv[1], reinterpret_cast(ctxt->native)); + handle->second(env, argc - 1, &argv[1], ctxt); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); return nullptr; } @@ -202,16 +203,17 @@ napi_value JsKVStore::OffEvent(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: [callback] - ZLOGE_ON_ARGS(ctxt, argc >= 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc >= 1, "invalid arguments!"); std::string event; ctxt->status = JSUtil::GetValue(env, argv[0], event); ZLOGI("unsubscribe to event:%{public}s", event.c_str()); auto handle = offEventHandlers_.find(event); - ZLOGE_ON_ARGS(ctxt, handle != offEventHandlers_.end(), "invalid arg[0], i.e. unsupported event"); + CHECK_ARGS(ctxt, handle != offEventHandlers_.end(), "invalid arg[0], i.e. unsupported event"); // shift 1 argument, for JsKVStore::Exec. - handle->second(env, argc - 1, &argv[1], reinterpret_cast(ctxt->native)); + handle->second(env, argc - 1, &argv[1], ctxt); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); return nullptr; } @@ -231,9 +233,9 @@ napi_value JsKVStore::PutBatch(napi_env env, napi_callback_info info) ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->entries); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid entries!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid entries!"); }); auto execute = [ctxt]() { @@ -241,7 +243,7 @@ napi_value JsKVStore::PutBatch(napi_env env, napi_callback_info info) Status status = kvStore->PutBatch(ctxt->entries); ZLOGD("kvStore->DeleteBatch return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->PutBatch() failed!"); + CHECK_STATUS(ctxt, "kvStore->PutBatch() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -261,9 +263,9 @@ napi_value JsKVStore::DeleteBatch(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); JSUtil::GetValue(env, argv[0], ctxt->keys); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid keys!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid keys!"); }; ctxt->GetCbInfo(env, info, input); @@ -277,7 +279,7 @@ napi_value JsKVStore::DeleteBatch(napi_env env, napi_callback_info info) Status status = kvStore->DeleteBatch(keys); ZLOGD("kvStore->DeleteBatch return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->DeleteBatch failed!"); + CHECK_STATUS(ctxt, "kvStore->DeleteBatch failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -299,7 +301,7 @@ napi_value JsKVStore::StartTransaction(napi_env env, napi_callback_info info) Status status = kvStore->StartTransaction(); ZLOGD("kvStore->StartTransaction return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->StartTransaction() failed!"); + CHECK_STATUS(ctxt, "kvStore->StartTransaction() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -321,7 +323,7 @@ napi_value JsKVStore::Commit(napi_env env, napi_callback_info info) Status status = kvStore->Commit(); ZLOGD("kvStore->Commit return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Commit() failed!"); + CHECK_STATUS(ctxt, "kvStore->Commit() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -343,7 +345,7 @@ napi_value JsKVStore::Rollback(napi_env env, napi_callback_info info) Status status = kvStore->Rollback(); ZLOGD("kvStore->Commit return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Rollback() failed!"); + CHECK_STATUS(ctxt, "kvStore->Rollback() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -363,9 +365,9 @@ napi_value JsKVStore::EnableSync(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = napi_get_value_bool(env, argv[0], &ctxt->enable); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid enabled!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid enabled!"); }; ctxt->GetCbInfo(env, info, input); @@ -374,7 +376,7 @@ napi_value JsKVStore::EnableSync(napi_env env, napi_callback_info info) Status status = kvStore->SetCapabilityEnabled(ctxt->enable); ZLOGD("kvStore->SetCapabilityEnabled return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->SetCapabilityEnabled() failed!"); + CHECK_STATUS(ctxt, "kvStore->SetCapabilityEnabled() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -395,11 +397,11 @@ napi_value JsKVStore::SetSyncRange(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->localLabels); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid localLabels!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid localLabels!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->remoteSupportLabels); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid remoteSupportLabels!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid remoteSupportLabels!"); }; ctxt->GetCbInfo(env, info, input); @@ -408,7 +410,7 @@ napi_value JsKVStore::SetSyncRange(napi_env env, napi_callback_info info) Status status = kvStore->SetCapabilityRange(ctxt->localLabels, ctxt->remoteSupportLabels); ZLOGD("kvStore->SetCapabilityRange return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->SetCapabilityRange() failed!"); + CHECK_STATUS(ctxt, "kvStore->SetCapabilityRange() failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -418,33 +420,35 @@ napi_value JsKVStore::SetSyncRange(napi_env env, napi_callback_info info) * [Callback] * on(event:'dataChange', subType: SubscribeType, observer: Callback): void; */ -napi_status JsKVStore::OnDataChange(napi_env env, size_t argc, napi_value* argv, JsKVStore* proxy) +void JsKVStore::OnDataChange(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt) { // required 2 arguments :: - NAPI_ASSERT_BASE(env, argc == 2, "invalid arguments on dataChange!", napi_invalid_arg); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments on dataChange!"); int32_t type = SUBSCRIBE_COUNT; - napi_get_value_int32(env, argv[0], &type); - NAPI_ASSERT_BASE(env, ValidSubscribeType(type), "invalid arg[1], i.e. invalid subscribeType", napi_invalid_arg); + ctxt->status = napi_get_value_int32(env, argv[0], &type); + CHECK_STATUS(ctxt, "napi_get_value_int32 failed!"); + CHECK_ARGS(ctxt, ValidSubscribeType(type), "invalid arg[1], i.e. invalid subscribeType"); napi_valuetype valueType = napi_undefined; - NAPI_CALL_BASE(env, napi_typeof(env, argv[1], &valueType), napi_invalid_arg); - NAPI_ASSERT_BASE(env, valueType == napi_function, "invalid arg[2], i.e. invalid callback", napi_invalid_arg); + ctxt->status = napi_typeof(env, argv[1], &valueType); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "invalid arg[2], i.e. invalid callback"); ZLOGI("subscribe data change type %{public}d", type); + auto proxy = reinterpret_cast(ctxt->native); std::lock_guard lck(proxy->listMutex_); for (auto& it : proxy->dataObserver_[type]) { auto observer = std::static_pointer_cast(it); if (*observer == argv[1]) { ZLOGI("function is already subscribe type"); - return napi_ok; + return; } } std::shared_ptr observer = std::make_shared(env, argv[1]); - napi_status status = proxy->Subscribe(type, observer); - NAPI_ASSERT_BASE(env, status == napi_ok, "Subscribe failed!", status); - return status; + ctxt->status = proxy->Subscribe(type, observer); + CHECK_STATUS(ctxt, "Subscribe failed!"); } /* @@ -454,18 +458,20 @@ napi_status JsKVStore::OnDataChange(napi_env env, size_t argc, napi_value* argv, * [NOTES!!!] no SubscribeType while off... * off(event:'dataChange', observer: Callback): void; */ -napi_status JsKVStore::OffDataChange(napi_env env, size_t argc, napi_value* argv, JsKVStore* proxy) +void JsKVStore::OffDataChange(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt) { // required 1 arguments :: [callback] - NAPI_ASSERT_BASE(env, argc <= 1, "invalid arguments, too many arguments!", napi_invalid_arg); + CHECK_ARGS(ctxt, argc <= 1, "invalid arguments off dataChange!"); // have 1 arguments :: have the callback if (argc == 1) { napi_valuetype valueType = napi_undefined; - NAPI_CALL_BASE(env, napi_typeof(env, argv[0], &valueType), napi_invalid_arg); - NAPI_ASSERT_BASE(env, valueType == napi_function, "invalid arg[1], i.e. invalid callback", napi_invalid_arg); + ctxt->status = napi_typeof(env, argv[0], &valueType); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "invalid arg[1], i.e. invalid callback"); } ZLOGI("unsubscribe dataChange, %{public}s specified observer.", (argc == 0) ? "without": "with"); + auto proxy = reinterpret_cast(ctxt->native); bool found = false; napi_status status = napi_ok; auto traverse1Type = [argc, argv, proxy, &found, &status](uint8_t type, auto& observers) { @@ -493,10 +499,7 @@ napi_status JsKVStore::OffDataChange(napi_env env, size_t argc, napi_value* argv } } found |= (argc == 0); // no specified observer, don't care about found or not. - - NAPI_ASSERT_BASE(env, found, "not Subscribed!", status); - NAPI_ASSERT_BASE(env, status == napi_ok, "UnSubscribe failed!", status); - return status; + CHECK_ARGS(ctxt, found, "not Subscribed!"); } /* @@ -504,16 +507,19 @@ napi_status JsKVStore::OffDataChange(napi_env env, size_t argc, napi_value* argv * [Callback] * on(event:'syncComplete',syncCallback: Callback>):void; */ -napi_status JsKVStore::OnSyncComplete(napi_env env, size_t argc, napi_value* argv, JsKVStore* proxy) +void JsKVStore::OnSyncComplete(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt) { // required 1 arguments :: - NAPI_ASSERT_BASE(env, argc == 1, "invalid arguments on syncComplete!", napi_invalid_arg); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments on syncComplete!"); napi_valuetype valueType = napi_undefined; - NAPI_CALL_BASE(env, napi_typeof(env, argv[0], &valueType), napi_invalid_arg); - NAPI_ASSERT_BASE(env, valueType == napi_function, "invalid arg[1], i.e. invalid callback", napi_invalid_arg); + ctxt->status = napi_typeof(env, argv[0], &valueType); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "invalid arg[1], i.e. invalid callback"); std::shared_ptr callback = std::make_shared(env, argv[0]); - return proxy->RegisterSyncCallback(callback); + auto proxy = reinterpret_cast(ctxt->native); + ctxt->status = proxy->RegisterSyncCallback(callback); + CHECK_STATUS(ctxt, "RegisterSyncCallback failed!"); } /* @@ -521,20 +527,24 @@ napi_status JsKVStore::OnSyncComplete(napi_env env, size_t argc, napi_value* arg * [Callback] * off(event:'syncComplete',syncCallback: Callback>):void; */ -napi_status JsKVStore::OffSyncComplete(napi_env env, size_t argc, napi_value* argv, JsKVStore* proxy) +void JsKVStore::OffSyncComplete(napi_env env, size_t argc, napi_value* argv, std::shared_ptr ctxt) { // required 1 arguments :: [callback] - NAPI_ASSERT_BASE(env, argc <= 1, "invalid arguments, too many arguments!", napi_invalid_arg); - ZLOGI("unsubscribe syncComplete, %{public}s specified observer.", (argc == 0) ? "without": "with"); + CHECK_ARGS(ctxt, argc <= 1, "invalid arguments off syncComplete!"); + auto proxy = reinterpret_cast(ctxt->native); // have 1 arguments :: have the callback if (argc == 1) { napi_valuetype valueType = napi_undefined; - NAPI_CALL_BASE(env, napi_typeof(env, argv[0], &valueType), napi_invalid_arg); - NAPI_ASSERT_BASE(env, valueType == napi_function, "invalid arg[1], i.e. invalid callback", napi_invalid_arg); + ctxt->status = napi_typeof(env, argv[0], &valueType); + CHECK_STATUS(ctxt, "napi_typeof failed!"); + CHECK_ARGS(ctxt, valueType == napi_function, "invalid arg[1], i.e. invalid callback"); auto observer = std::static_pointer_cast(proxy->syncObserver_); - NAPI_ASSERT_BASE(env, *observer == argv[0], "invalid arg[1], not Subscribed", napi_invalid_arg); + CHECK_ARGS(ctxt, *observer == argv[0], "invalid arg[1], not Subscribed"); } - return proxy->UnRegisterSyncCallback(); + ZLOGI("unsubscribe syncComplete, %{public}s specified observer.", (argc == 0) ? "without": "with"); + + ctxt->status = proxy->UnRegisterSyncCallback(); + CHECK_STATUS(ctxt, "UnRegisterSyncCallback failed!"); } /* diff --git a/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp b/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp index a38ed3c9d..c3c5e76ce 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.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 @@ -56,7 +56,8 @@ napi_value JsKVStoreResultSet::New(napi_env env, napi_callback_info info) { ZLOGD("constructor JsKVStoreResultSet!"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsKVStoreResultSet* resultSet = new (std::nothrow) JsKVStoreResultSet(); NAPI_ASSERT(env, resultSet !=nullptr, "no memory for resultSet"); @@ -64,7 +65,7 @@ napi_value JsKVStoreResultSet::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvStoreResultSet finalize."); auto* resultSet = reinterpret_cast(data); - ZLOGE_RETURN_VOID(resultSet != nullptr, "finalize null!"); + CHECK_RETURN_VOID(resultSet != nullptr, "finalize null!"); delete resultSet; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, resultSet, finalize, nullptr, nullptr)); @@ -75,7 +76,8 @@ napi_value JsKVStoreResultSet::GetCount(napi_env env, napi_callback_info info) / { ZLOGD("KVStoreResultSet::GetCount()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); ZLOGD("KVStoreResultSet::GetCount(status=%{public}d)", ctxt->status); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; @@ -89,7 +91,8 @@ napi_value JsKVStoreResultSet::GetPosition(napi_env env, napi_callback_info info { ZLOGD("KVStoreResultSet::GetPosition()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; int position = resultSet->GetPosition(); @@ -102,7 +105,8 @@ napi_value JsKVStoreResultSet::MoveToFirst(napi_env env, napi_callback_info info { ZLOGD("KVStoreResultSet::MoveToFirst()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isMoved = resultSet->MoveToFirst(); @@ -115,7 +119,8 @@ napi_value JsKVStoreResultSet::MoveToLast(napi_env env, napi_callback_info info) { ZLOGD("KVStoreResultSet::MoveToFirst()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isMoved = resultSet->MoveToLast(); @@ -128,7 +133,8 @@ napi_value JsKVStoreResultSet::MoveToNext(napi_env env, napi_callback_info info) { ZLOGD("KVStoreResultSet::MoveToNext()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isMoved = resultSet->MoveToNext(); @@ -141,7 +147,8 @@ napi_value JsKVStoreResultSet::MoveToPrevious(napi_env env, napi_callback_info i { ZLOGD("KVStoreResultSet::MoveToPrevious()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isMoved = resultSet->MoveToPrevious(); @@ -157,10 +164,11 @@ napi_value JsKVStoreResultSet::Move(napi_env env, napi_callback_info info) /* bo auto ctxt = std::make_shared(); auto input = [env, ctxt, &offset](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = napi_get_value_int32(env, argv[0], (int32_t*)&offset); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isMoved = resultSet->Move(offset); @@ -175,10 +183,11 @@ napi_value JsKVStoreResultSet::MoveToPosition(napi_env env, napi_callback_info i auto ctxt = std::make_shared(); auto input = [env, ctxt, &position](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = napi_get_value_int32(env, argv[0], (int32_t*)&position); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); ZLOGD("KVStoreResultSet::MoveToPosition(%{public}d)", position); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; @@ -192,7 +201,8 @@ napi_value JsKVStoreResultSet::IsFirst(napi_env env, napi_callback_info info) /* { ZLOGD("KVStoreResultSet::IsFirst()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isFirst = resultSet->IsFirst(); @@ -205,7 +215,8 @@ napi_value JsKVStoreResultSet::IsLast(napi_env env, napi_callback_info info) /* { ZLOGD("KVStoreResultSet::IsLast()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isLast = resultSet->IsLast(); @@ -218,7 +229,8 @@ napi_value JsKVStoreResultSet::IsBeforeFirst(napi_env env, napi_callback_info in { ZLOGD("KVStoreResultSet::IsBeforeFirst()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isBeforeFirst = resultSet->IsBeforeFirst(); @@ -231,7 +243,8 @@ napi_value JsKVStoreResultSet::IsAfterLast(napi_env env, napi_callback_info info { ZLOGD("KVStoreResultSet::IsAfterLast()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; bool isAfterLast = resultSet->IsAfterLast(); @@ -244,7 +257,8 @@ napi_value JsKVStoreResultSet::GetEntry(napi_env env, napi_callback_info info) / { ZLOGD("KVStoreResultSet::GetEntry()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); DistributedKv::Entry entry; auto& resultSet = reinterpret_cast(ctxt->native)->resultSet_; diff --git a/frameworks/jskitsimpl/distributeddata/src/js_query.cpp b/frameworks/jskitsimpl/distributeddata/src/js_query.cpp index 377508616..5791a9659 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_query.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_query.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 @@ -68,7 +68,8 @@ napi_value JsQuery::Constructor(napi_env env) napi_value JsQuery::New(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsQuery* query = new (std::nothrow) JsQuery(); NAPI_ASSERT(env, query !=nullptr, "no memory for query"); @@ -76,7 +77,7 @@ napi_value JsQuery::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("query finalize."); auto* query = reinterpret_cast(data); - ZLOGE_RETURN_VOID(query != nullptr, "finalize null!"); + CHECK_RETURN_VOID(query != nullptr, "finalize null!"); delete query; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, query, finalize, nullptr, nullptr)); @@ -87,7 +88,8 @@ napi_value JsQuery::Reset(napi_env env, napi_callback_info info) { ZLOGD("Query::Reset()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.Reset(); @@ -98,17 +100,17 @@ struct ValueContext : public ContextBase { std::string field; JSUtil::QueryVariant vv; - napi_status GetValueSync(napi_env env, napi_callback_info info) + void GetValueSync(napi_env env, napi_callback_info info) { auto input = [this, env](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(this, argc == 2, "invalid arguments!"); + CHECK_ARGS(this, argc == 2, "invalid arguments!"); status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(this, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(this, "invalid arg[0], i.e. invalid field!"); status = JSUtil::GetValue(env, argv[1], vv); - ZLOGE_ON_STATUS(this, "invalid arg[1], i.e. invalid value!"); + CHECK_STATUS(this, "invalid arg[1], i.e. invalid value!"); }; - return GetCbInfoSync(env, info, input); + GetCbInfoSync(env, info, input); } }; @@ -117,7 +119,8 @@ napi_value JsQuery::EqualTo(napi_env env, napi_callback_info info) { ZLOGD("Query::EqualTo()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -141,7 +144,8 @@ napi_value JsQuery::NotEqualTo(napi_env env, napi_callback_info info) { ZLOGD("Query::NotEqualTo()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -165,7 +169,8 @@ napi_value JsQuery::GreaterThan(napi_env env, napi_callback_info info) { ZLOGD("Query::GreaterThan()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -189,7 +194,8 @@ napi_value JsQuery::LessThan(napi_env env, napi_callback_info info) { ZLOGD("Query::LessThan()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -213,7 +219,8 @@ napi_value JsQuery::GreaterThanOrEqualTo(napi_env env, napi_callback_info info) { ZLOGD("Query::GreaterThanOrEqualTo()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -237,7 +244,8 @@ napi_value JsQuery::LessThanOrEqualTo(napi_env env, napi_callback_info info) { ZLOGD("Query::LessThanOrEqualTo()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetValueSync(env, info)); + ctxt->GetValueSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; auto strValue = std::get_if(&ctxt->vv); @@ -263,11 +271,12 @@ napi_value JsQuery::IsNull(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &field](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.IsNull(field); @@ -292,13 +301,13 @@ struct NumbersContext : public ContextBase { std::vector doubleList; NumberType innerType = NumberType::NUMBER_INVALID; - napi_status GetNumberSync(napi_env env, napi_callback_info info) + void GetNumberSync(napi_env env, napi_callback_info info) { auto input = [this, env](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(this, argc == 2, "invalid arguments!"); + CHECK_ARGS(this, argc == 2, "invalid arguments!"); status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(this, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(this, "invalid arg[0], i.e. invalid field!"); bool isTypedArray = false; status = napi_is_typedarray(env, argv[1], &isTypedArray); @@ -310,7 +319,7 @@ struct NumbersContext : public ContextBase { size_t offset = 0; void* data = nullptr; status = napi_get_typedarray_info(env, argv[1], &type, &length, &data, &buffer, &offset); - ZLOGE_ON_STATUS(this, "invalid arg[1], i.e. invalid number array!"); + CHECK_STATUS(this, "invalid arg[1], i.e. invalid number array!"); if (type < napi_uint32_array) { status = JSUtil::GetValue(env, argv[1], intList); innerType = NumberType::NUMBER_INT; @@ -324,21 +333,22 @@ struct NumbersContext : public ContextBase { } else { bool isArray = false; status = napi_is_array(env, argv[1], &isArray); - ZLOGE_ON_ARGS(this, isArray, "invalid arg[1], i.e. invalid number array!"); + CHECK_ARGS(this, isArray, "invalid arg[1], i.e. invalid number array!"); ZLOGD("arg[1] %{public}s a Array, treat as array of double.", isTypedArray ? "is" : "is not"); status = JSUtil::GetValue(env, argv[1], doubleList); - ZLOGE_ON_STATUS(this, "invalid arg[1], i.e. invalid number array!"); + CHECK_STATUS(this, "invalid arg[1], i.e. invalid number array!"); innerType = NumberType::NUMBER_DOUBLE; }; }; - return GetCbInfoSync(env, info, input); + GetCbInfoSync(env, info, input); } }; napi_value JsQuery::InNumber(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetNumberSync(env, info)); + ctxt->GetNumberSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; if (ctxt->innerType == NumberType::NUMBER_INT) { @@ -360,13 +370,14 @@ napi_value JsQuery::InString(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->valueList); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid valueList!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid valueList!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.InString(ctxt->field, ctxt->valueList); @@ -376,7 +387,8 @@ napi_value JsQuery::InString(napi_env env, napi_callback_info info) napi_value JsQuery::NotInNumber(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetNumberSync(env, info)); + ctxt->GetNumberSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; if (ctxt->innerType == NumberType::NUMBER_INT) { @@ -399,13 +411,14 @@ napi_value JsQuery::NotInString(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->valueList); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid valueList!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid valueList!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.NotInString(ctxt->field, ctxt->valueList); @@ -422,13 +435,14 @@ napi_value JsQuery::Like(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->value); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.Like(ctxt->field, ctxt->value); @@ -445,13 +459,14 @@ napi_value JsQuery::Unlike(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->value); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid value!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.Unlike(ctxt->field, ctxt->value); @@ -462,7 +477,8 @@ napi_value JsQuery::And(napi_env env, napi_callback_info info) { ZLOGD("Query::And()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.And(); @@ -473,7 +489,8 @@ napi_value JsQuery::Or(napi_env env, napi_callback_info info) { ZLOGD("Query::Or()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.Or(); @@ -487,11 +504,12 @@ napi_value JsQuery::OrderByAsc(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &field](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.OrderByAsc(field); @@ -505,11 +523,12 @@ napi_value JsQuery::OrderByDesc(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &field](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.OrderByDesc(field); @@ -525,13 +544,14 @@ napi_value JsQuery::Limit(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = napi_get_value_int32(env, argv[0], &ctxt->number); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid number!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid number!"); ctxt->status = napi_get_value_int32(env, argv[1], &ctxt->offset); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid offset!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid offset!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.Limit(ctxt->number, ctxt->offset); return ctxt->self; @@ -544,11 +564,12 @@ napi_value JsQuery::IsNotNull(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &field](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], field); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid field!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.IsNotNull(field); @@ -559,7 +580,8 @@ napi_value JsQuery::BeginGroup(napi_env env, napi_callback_info info) { ZLOGD("Query::BeginGroup()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.BeginGroup(); return ctxt->self; @@ -569,7 +591,8 @@ napi_value JsQuery::EndGroup(napi_env env, napi_callback_info info) { ZLOGD("Query::EndGroup()"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.EndGroup(); @@ -582,11 +605,12 @@ napi_value JsQuery::PrefixKey(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &prefix](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], prefix); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid prefix!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid prefix!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.KeyPrefix(prefix); @@ -599,11 +623,12 @@ napi_value JsQuery::SetSuggestIndex(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &suggestIndex](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], suggestIndex); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid suggestIndex!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid suggestIndex!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.SetSuggestIndex(suggestIndex); @@ -616,11 +641,12 @@ napi_value JsQuery::DeviceId(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &deviceId](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], deviceId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; query.DeviceId(deviceId); @@ -631,7 +657,8 @@ napi_value JsQuery::DeviceId(napi_env env, napi_callback_info info) napi_value JsQuery::GetSqlLike(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; JSUtil::SetValue(env, query.ToString(), ctxt->output); diff --git a/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp b/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp index 47aa77225..2d2bf4840 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_schema.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 @@ -29,7 +29,7 @@ static std::string LABEL = "Schema"; static std::string SCHEMA_VERSION = "SCHEMA_VERSION"; static std::string SCHEMA_MODE = "SCHEMA_MODE"; static std::string SCHEMA_DEFINE = "SCHEMA_DEFINE"; -static std::string SCHEMA_INDEX = "SCHEMA_INDEX"; +static std::string SCHEMA_INDEXES = "SCHEMA_INDEXES"; static std::string SCHEMA_SKIPSIZE = "SCHEMA_SKIPSIZE"; static std::string DEFAULT_SCHEMA_VERSION = "1.0"; @@ -50,8 +50,11 @@ napi_value JsSchema::Constructor(napi_env env) { ZLOGD("Init JsSchema"); const napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("toJsonString", ToJson), - DECLARE_NAPI_GETTER_SETTER("root", GetRootNode, SetRootNode) + DECLARE_NAPI_FUNCTION("toJsonString", JsSchema::ToJson), + DECLARE_NAPI_GETTER_SETTER("root", JsSchema::GetRootNode, JsSchema::SetRootNode), + DECLARE_NAPI_GETTER_SETTER("indexes", JsSchema::GetIndexes, JsSchema::SetIndexes), + DECLARE_NAPI_GETTER_SETTER("mode", JsSchema::GetMode, JsSchema::SetMode), + DECLARE_NAPI_GETTER_SETTER("skip", JsSchema::GetSkip, JsSchema::SetSkip) }; size_t count = sizeof(properties) / sizeof(properties[0]); return JSUtil::DefineClass(env, "Schema", properties, count, JsSchema::New); @@ -61,7 +64,8 @@ napi_value JsSchema::New(napi_env env, napi_callback_info info) { ZLOGD("Schema::New"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsSchema* schema = new (std::nothrow) JsSchema(env); NAPI_ASSERT(env, schema !=nullptr, "no memory for schema"); @@ -69,7 +73,7 @@ napi_value JsSchema::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("Schema finalize."); auto* schema = reinterpret_cast(data); - ZLOGE_RETURN_VOID(schema != nullptr, "finalize null!"); + CHECK_RETURN_VOID(schema != nullptr, "finalize null!"); delete schema; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, schema, finalize, nullptr, nullptr)); @@ -80,54 +84,160 @@ napi_value JsSchema::ToJson(napi_env env, napi_callback_info info) { ZLOGD("Schema::New"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); auto json = schema->Dump(); JSUtil::SetValue(env, json, ctxt->output); return ctxt->output; } + napi_value JsSchema::GetRootNode(napi_env env, napi_callback_info info) { ZLOGD("Schema::GetRootNode"); auto ctxt = std::make_shared(); - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); + if (schema->rootNode == nullptr) { + int argc = 1; + napi_value argv[1] = { nullptr }; + std::string root(SCHEMA_DEFINE); + JSUtil::SetValue(env, root, argv[0]); + schema->ref = JSUtil::NewWithRef(env, argc, argv, + (void**)&schema->rootNode, JsFieldNode::Constructor(env)); + } NAPI_ASSERT(env, schema->ref != nullptr, "no root, please set first!"); NAPI_CALL(env, napi_get_reference_value(env, schema->ref, &ctxt->output)); return ctxt->output; } + napi_value JsSchema::SetRootNode(napi_env env, napi_callback_info info) { ZLOGD("Schema::SetRootNode"); auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); JsFieldNode* node = nullptr; ctxt->status = JSUtil::Unwrap(env, argv[0], (void**)(&node), JsFieldNode::Constructor(env)); - ZLOGE_ON_STATUS(ctxt, "napi_unwrap to FieldNode failed"); - ZLOGE_ON_ARGS(ctxt, node != nullptr, "invalid arg[0], i.e. invalid node!"); + CHECK_STATUS(ctxt, "napi_unwrap to FieldNode failed"); + CHECK_ARGS(ctxt, node != nullptr, "invalid arg[0], i.e. invalid node!"); auto schema = reinterpret_cast(ctxt->native); if (schema->ref != nullptr) { napi_delete_reference(env, schema->ref); } - NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, argv[0], 1, &schema->ref)); - schema->rootFieldNode = node; + ctxt->status = napi_create_reference(env, argv[0], 1, &schema->ref); + CHECK_STATUS(ctxt, "napi_create_reference to FieldNode failed"); + schema->rootNode = node; }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); return ctxt->self; } + +napi_value JsSchema::GetMode(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + JSUtil::SetValue(env, schema->mode, ctxt->output); + return ctxt->output; +} + +napi_value JsSchema::SetMode(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + uint32_t mode = false; + auto input = [env, ctxt, &mode](size_t argc, napi_value* argv) { + // required 1 arguments :: + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); + ctxt->status = JSUtil::GetValue(env, argv[0], mode); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid mode!"); + }; + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + schema->mode = mode; + return nullptr; +} + +napi_value JsSchema::GetSkip(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + JSUtil::SetValue(env, schema->skip, ctxt->output); + return ctxt->output; +} + +napi_value JsSchema::SetSkip(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + uint32_t skip = false; + auto input = [env, ctxt, &skip](size_t argc, napi_value* argv) { + // required 1 arguments :: + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); + ctxt->status = JSUtil::GetValue(env, argv[0], skip); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid skip size!"); + }; + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + schema->skip = skip; + return nullptr; +} + +napi_value JsSchema::GetIndexes(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + ctxt->GetCbInfoSync(env, info); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + JSUtil::SetValue(env, schema->indexes, ctxt->output); + return ctxt->output; +} + +napi_value JsSchema::SetIndexes(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + std::vector indexes; + auto input = [env, ctxt, &indexes](size_t argc, napi_value* argv) { + // required 1 arguments :: + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); + ctxt->status = JSUtil::GetValue(env, argv[0], indexes); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid indexes!"); + }; + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); + + auto schema = reinterpret_cast(ctxt->native); + schema->indexes = indexes; + return nullptr; +} + std::string JsSchema::Dump() { + json jsIndexes = nlohmann::json::array(); + for (auto idx : indexes) { + jsIndexes.push_back(idx); + } json js = { { SCHEMA_VERSION, DEFAULT_SCHEMA_VERSION }, - { SCHEMA_MODE, "SCHEMA_MODE" }, - { SCHEMA_DEFINE, "SCHEMA_DEFINE" }, - { SCHEMA_INDEX, "SCHEMA_INDEX" }, - { SCHEMA_SKIPSIZE, "SCHEMA_SKIPSIZE" } + { SCHEMA_MODE, (mode == SCHEMA_MODE_STRICT) ? "STRICT" : "COMPATIBLE" }, + { SCHEMA_DEFINE, rootNode->GetValueForJson() }, + { SCHEMA_INDEXES, jsIndexes }, + { SCHEMA_SKIPSIZE, skip }, }; return js.dump(); } diff --git a/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp index 3f1391ac9..71f2fc1f3 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_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 @@ -75,10 +75,10 @@ napi_value JsSingleKVStore::Get(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->key); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid key!"); }; ctxt->GetCbInfo(env, info, input); ZLOGD("key=%{public}.8s", ctxt->key.c_str()); @@ -91,11 +91,11 @@ napi_value JsSingleKVStore::Get(napi_env env, napi_callback_info info) ZLOGD("kvStore->Get return %{public}d", status); ctxt->value = JSUtil::Blob2VariantValue(value); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->Get() failed!"); + CHECK_STATUS(ctxt, "kvStore->Get() failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, ctxt->value, result); - ZLOGE_ON_STATUS(ctxt, "output failed"); + CHECK_STATUS(ctxt, "output failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -116,17 +116,17 @@ struct VariantArgs { static napi_status GetVariantArgs(napi_env env, size_t argc, napi_value* argv, VariantArgs& va) { // required 1 arguments :: - ZLOGE_RETURN(argc == 1, "invalid arguments!", napi_invalid_arg); + CHECK_RETURN(argc == 1, "invalid arguments!", napi_invalid_arg); napi_valuetype type = napi_undefined; napi_status status = napi_typeof(env, argv[0], &type); - ZLOGE_RETURN((type == napi_string) || (type == napi_object), "invalid arg[0], type error!", napi_invalid_arg); + CHECK_RETURN((type == napi_string) || (type == napi_object), "invalid arg[0], type error!", napi_invalid_arg); if (type == napi_string) { status = JSUtil::GetValue(env, argv[0], va.keyPrefix); - ZLOGE_RETURN(!va.keyPrefix.empty(), "invalid arg[0], i.e. invalid keyPrefix!", napi_invalid_arg); + CHECK_RETURN(!va.keyPrefix.empty(), "invalid arg[0], i.e. invalid keyPrefix!", napi_invalid_arg); va.type = ArgsType::KEYPREFIX; } else if (type == napi_object) { status = JSUtil::Unwrap(env, argv[0], (void**)(&va.query), JsQuery::Constructor(env)); - ZLOGE_RETURN(va.query != nullptr, "invalid arg[0], i.e. invalid query!", napi_invalid_arg); + CHECK_RETURN(va.query != nullptr, "invalid arg[0], i.e. invalid query!", napi_invalid_arg); va.type = ArgsType::QUERY; } return status; @@ -150,9 +150,9 @@ napi_value JsSingleKVStore::GetEntries(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = GetVariantArgs(env, argc, argv, ctxt->va); - ZLOGE_ON_STATUS(ctxt, "invalid arguments!"); + CHECK_STATUS(ctxt, "invalid arguments!"); }; ctxt->GetCbInfo(env, info, input); @@ -169,11 +169,11 @@ napi_value JsSingleKVStore::GetEntries(napi_env env, napi_callback_info info) ZLOGD("kvStore->GetEntriesWithQuery() return %{public}d", status); } ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetEntries() failed"); + CHECK_STATUS(ctxt, "kvStore->GetEntries() failed"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, ctxt->entries, result); - ZLOGE_ON_STATUS(ctxt, "output failed!"); + CHECK_STATUS(ctxt, "output failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -197,13 +197,13 @@ napi_value JsSingleKVStore::GetResultSet(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = GetVariantArgs(env, argc, argv, ctxt->va); - ZLOGE_ON_STATUS(ctxt, "invalid arguments!"); + CHECK_STATUS(ctxt, "invalid arguments!"); ctxt->ref = JSUtil::NewWithRef(env, 0, nullptr, (void**)(&ctxt->resultSet), JsKVStoreResultSet::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->resultSet != nullptr, "KVStoreResultSet::New failed!"); - ZLOGE_ON_ARGS(ctxt, ctxt->ref != nullptr, "KVStoreResultSet::New failed!"); + CHECK_ARGS(ctxt, ctxt->resultSet != nullptr, "KVStoreResultSet::New failed!"); + CHECK_ARGS(ctxt, ctxt->ref != nullptr, "KVStoreResultSet::New failed!"); }; ctxt->GetCbInfo(env, info, input); @@ -221,13 +221,13 @@ napi_value JsSingleKVStore::GetResultSet(napi_env env, napi_callback_info info) ZLOGD("kvStore->GetEntriesWithQuery() return %{public}d", status); } ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetResultSet() failed!"); + CHECK_STATUS(ctxt, "kvStore->GetResultSet() failed!"); ctxt->resultSet->SetNative(kvResultSet); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = napi_get_reference_value(env, ctxt->ref, &result); napi_delete_reference(env, ctxt->ref); - ZLOGE_ON_STATUS(ctxt, "output kvResultSet failed"); + CHECK_STATUS(ctxt, "output kvResultSet failed"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -246,13 +246,13 @@ napi_value JsSingleKVStore::CloseResultSet(napi_env env, napi_callback_info info auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); napi_valuetype type = napi_undefined; ctxt->status = napi_typeof(env, argv[0], &type); - ZLOGE_ON_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid resultSet!"); + CHECK_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid resultSet!"); ctxt->status = JSUtil::Unwrap(env, argv[0], (void**)(&ctxt->resultSet), JsKVStoreResultSet::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->resultSet != nullptr, "invalid arg[0], i.e. invalid resultSet!"); + CHECK_ARGS(ctxt, ctxt->resultSet != nullptr, "invalid arg[0], i.e. invalid resultSet!"); }; ctxt->GetCbInfo(env, info, input); @@ -261,7 +261,7 @@ napi_value JsSingleKVStore::CloseResultSet(napi_env env, napi_callback_info info Status status = kvStore->CloseResultSet(ctxt->resultSet->GetNative()); ZLOGD("kvStore->CloseResultSet return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->CloseResultSet failed!"); + CHECK_STATUS(ctxt, "kvStore->CloseResultSet failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -280,12 +280,12 @@ napi_value JsSingleKVStore::GetResultSize(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); napi_valuetype type = napi_undefined; ctxt->status = napi_typeof(env, argv[0], &type); - ZLOGE_ON_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid query!"); + CHECK_ARGS(ctxt, type == napi_object, "invalid arg[0], i.e. invalid query!"); ctxt->status = JSUtil::Unwrap(env, argv[0], (void**)(&ctxt->query), JsQuery::Constructor(env)); - ZLOGE_ON_ARGS(ctxt, ctxt->query != nullptr, "invalid arg[0], i.e. invalid query!"); + CHECK_ARGS(ctxt, ctxt->query != nullptr, "invalid arg[0], i.e. invalid query!"); }; ctxt->GetCbInfo(env, info, input); @@ -295,11 +295,11 @@ napi_value JsSingleKVStore::GetResultSize(napi_env env, napi_callback_info info) Status status = kvStore->GetCountWithQuery(query.ToString(), ctxt->resultSize); ZLOGD("kvStore->GetCountWithQuery() return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->GetCountWithQuery() failed!"); + CHECK_STATUS(ctxt, "kvStore->GetCountWithQuery() failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, static_cast(ctxt->resultSize), result); - ZLOGE_ON_STATUS(ctxt, "output resultSize failed!"); + CHECK_STATUS(ctxt, "output resultSize failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -318,9 +318,9 @@ napi_value JsSingleKVStore::RemoveDeviceData(napi_env env, napi_callback_info in auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceId!"); }; ctxt->GetCbInfo(env, info, input); @@ -329,7 +329,7 @@ napi_value JsSingleKVStore::RemoveDeviceData(napi_env env, napi_callback_info in Status status = kvStore->RemoveDeviceData(ctxt->deviceId); ZLOGD("kvStore->RemoveDeviceData return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "kvStore->RemoveDeviceData failed!"); + CHECK_STATUS(ctxt, "kvStore->RemoveDeviceData failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -348,19 +348,20 @@ napi_value JsSingleKVStore::Sync(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 3 arguments :: [allowedDelayMs] - ZLOGE_ON_ARGS(ctxt, (argc == 2) || (argc == 3), "invalid arguments!"); + CHECK_ARGS(ctxt, (argc == 2) || (argc == 3), "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceIdList); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceIdList!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid deviceIdList!"); ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->mode); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid mode!"); - ZLOGE_ON_ARGS(ctxt, ctxt->mode <= uint32_t(SyncMode::PUSH_PULL), "invalid arg[1], i.e. invalid mode!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid mode!"); + CHECK_ARGS(ctxt, ctxt->mode <= uint32_t(SyncMode::PUSH_PULL), "invalid arg[1], i.e. invalid mode!"); // have 3 arguments :: have the allowedDelayMs if (argc == 3) { ctxt->status = JSUtil::GetValue(env, argv[2], ctxt->allowedDelayMs); - ZLOGE_ON_STATUS(ctxt, "invalid arg[1], i.e. invalid allowedDelayMs!"); + CHECK_STATUS(ctxt, "invalid arg[1], i.e. invalid allowedDelayMs!"); } }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); ZLOGD("sync deviceIdList.size=%{public}d, mode:%{public}u, allowedDelayMs:%{public}u", (int)ctxt->deviceIdList.size(), ctxt->mode, ctxt->allowedDelayMs); @@ -385,9 +386,9 @@ napi_value JsSingleKVStore::SetSyncParam(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt](size_t argc, napi_value* argv) { // required 1 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 1, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 1, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->allowedDelayMs); - ZLOGE_ON_STATUS(ctxt, "get allowedDelayMs failed!"); + CHECK_STATUS(ctxt, "get allowedDelayMs failed!"); }; ctxt->GetCbInfo(env, info, input); @@ -397,7 +398,7 @@ napi_value JsSingleKVStore::SetSyncParam(napi_env env, napi_callback_info info) Status status = kvStore->SetSyncParam(syncParam); ZLOGD("kvStore->SetSyncParam return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "output failed!"); + CHECK_STATUS(ctxt, "output failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } @@ -421,11 +422,11 @@ napi_value JsSingleKVStore::GetSecurityLevel(napi_env env, napi_callback_info in Status status = kvStore->GetSecurityLevel(ctxt->securityLevel); ZLOGD("kvStore->GetSecurityLevel return %{public}d", status); ctxt->status = (status == Status::SUCCESS) ? napi_ok : napi_generic_failure; - ZLOGE_ON_STATUS(ctxt, "GetSecurityLevel failed!"); + CHECK_STATUS(ctxt, "GetSecurityLevel failed!"); }; auto output = [env, ctxt](napi_value& result) { ctxt->status = JSUtil::SetValue(env, static_cast(ctxt->securityLevel), result); - ZLOGE_ON_STATUS(ctxt, "output failed!"); + CHECK_STATUS(ctxt, "output failed!"); }; return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } @@ -437,12 +438,13 @@ napi_value JsSingleKVStore::New(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto input = [env, ctxt, &storeId](size_t argc, napi_value* argv) { // required 2 arguments :: - ZLOGE_ON_ARGS(ctxt, argc == 2, "invalid arguments!"); + CHECK_ARGS(ctxt, argc == 2, "invalid arguments!"); ctxt->status = JSUtil::GetValue(env, argv[0], storeId); - ZLOGE_ON_STATUS(ctxt, "invalid arg[0], i.e. invalid storeId!"); - ZLOGE_ON_ARGS(ctxt, !storeId.empty(), "invalid arg[0], i.e. invalid storeId!"); + CHECK_STATUS(ctxt, "invalid arg[0], i.e. invalid storeId!"); + CHECK_ARGS(ctxt, !storeId.empty(), "invalid arg[0], i.e. invalid storeId!"); }; - NAPI_CALL(env, ctxt->GetCbInfoSync(env, info, input)); + ctxt->GetCbInfoSync(env, info, input); + NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsSingleKVStore* kvStore = new (std::nothrow) JsSingleKVStore(storeId); NAPI_ASSERT(env, kvStore !=nullptr, "no memory for kvStore"); @@ -450,7 +452,7 @@ napi_value JsSingleKVStore::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("singleKVStore finalize."); auto* kvStore = reinterpret_cast(data); - ZLOGE_RETURN_VOID(kvStore != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvStore != nullptr, "finalize null!"); delete kvStore; }; NAPI_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr)); diff --git a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index f4e216502..efea296a9 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 @@ -29,75 +29,68 @@ constexpr size_t STR_TAIL_LENGTH = 1; /* napi_value <-> bool */ napi_status JSUtil::GetValue(napi_env env, napi_value in, bool& out) { - ZLOGD("napi_value <- bool"); return napi_get_value_bool(env, in, &out); } napi_status JSUtil::SetValue(napi_env env, const bool& in, napi_value& out) { - ZLOGD("napi_value -> bool"); return napi_get_boolean(env, in, &out); } /* napi_value <-> int32_t */ napi_status JSUtil::GetValue(napi_env env, napi_value in, int32_t& out) { - ZLOGD("napi_value -> uint32_t"); return napi_get_value_int32(env, in, &out); } napi_status JSUtil::SetValue(napi_env env, const int32_t& in, napi_value& out) { - ZLOGD("napi_value <- uint32_t"); return napi_create_int32(env, in, &out); } /* napi_value <-> uint32_t */ napi_status JSUtil::GetValue(napi_env env, napi_value in, uint32_t& out) { - ZLOGD("napi_value -> uint32_t"); return napi_get_value_uint32(env, in, &out); } napi_status JSUtil::SetValue(napi_env env, const uint32_t& in, napi_value& out) { - ZLOGD("napi_value <- uint32_t"); return napi_create_uint32(env, in, &out); } /* napi_value <-> int64_t */ napi_status JSUtil::GetValue(napi_env env, napi_value in, int64_t& out) { - ZLOGD("napi_value <- int64_t"); return napi_get_value_int64(env, in, &out); } napi_status JSUtil::SetValue(napi_env env, const int64_t& in, napi_value& out) { - ZLOGD("napi_value <- int64_t"); return napi_create_int64(env, in, &out); } /* napi_value <-> double */ napi_status JSUtil::GetValue(napi_env env, napi_value in, double& out) { - ZLOGD("napi_value -> double"); return napi_get_value_double(env, in, &out); } napi_status JSUtil::SetValue(napi_env env, const double& in, napi_value& out) { - ZLOGD("napi_value <- double"); return napi_create_double(env, in, &out); } /* napi_value <-> std::string */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::string& out) { + napi_valuetype type = napi_undefined; + napi_status status = napi_typeof(env, in, &type); + CHECK_RETURN((status == napi_ok) && (type == napi_string), "invalid type", napi_invalid_arg); + size_t maxLen = STR_MAX_LENGTH; - napi_status status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen); + status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen); if (maxLen <= 0) { - GET_AND_THROW_LAST_ERROR(env); return status; } ZLOGD("napi_value -> std::string get length %{public}d", (int)maxLen); @@ -105,11 +98,10 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::string& out) if (buf != nullptr) { size_t len = 0; status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR(env); + if (status == napi_ok) { + buf[len] = 0; + out = std::string(buf); } - buf[len] = 0; - out = std::string(buf); delete[] buf; } else { status = napi_generic_failure; @@ -119,7 +111,6 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::string& out) napi_status JSUtil::SetValue(napi_env env, const std::string& in, napi_value& out) { - ZLOGD("napi_value <- std::string %{public}d", (int)in.length()); return napi_create_string_utf8(env, in.c_str(), in.size(), &out); } @@ -127,20 +118,21 @@ napi_status JSUtil::SetValue(napi_env env, const std::string& in, napi_value& ou napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { ZLOGD("napi_value -> std::vector"); + out.clear(); bool isArray = false; napi_is_array(env, in, &isArray); - ZLOGE_RETURN(isArray, "not an array", napi_invalid_arg); + CHECK_RETURN(isArray, "not an array", napi_invalid_arg); uint32_t length = 0; napi_status status = napi_get_array_length(env, in, &length); - ZLOGE_RETURN((status == napi_ok) && (length > 0), "get_array failed!", napi_invalid_arg); + CHECK_RETURN((status == napi_ok) && (length > 0), "get_array failed!", napi_invalid_arg); for (uint32_t i = 0; i < length; ++i) { napi_value item = nullptr; status = napi_get_element(env, in, i, &item); - ZLOGE_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg); + CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg); std::string value; status = GetValue(env, item, value); - ZLOGE_RETURN(status == napi_ok, "not a string", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "not a string", napi_invalid_arg); out.push_back(value); } return status; @@ -150,13 +142,13 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, n { ZLOGD("napi_value <- std::vector"); napi_status status = napi_create_array_with_length(env, in.size(), &out); - ZLOGE_RETURN(status == napi_ok, "create array failed!", status); + CHECK_RETURN(status == napi_ok, "create array failed!", status); int index = 0; for (auto& item : in) { napi_value element = nullptr; SetValue(env, item, element); status = napi_set_element(env, out, index++, element); - ZLOGE_RETURN((status == napi_ok), "napi_set_element failed!", status); + CHECK_RETURN((status == napi_ok), "napi_set_element failed!", status); } return status; } @@ -165,36 +157,30 @@ JSUtil::KvStoreVariant JSUtil::Blob2VariantValue(const DistributedKv::Blob& blob { auto& data = blob.Data(); // number 2 means: valid Blob must have more than 2 bytes. - if (data.size() < 2) { + if (data.size() < 1) { ZLOGE("Blob have no data!"); return JSUtil::KvStoreVariant(); } // number 1 means: skip the first byte, byte[0] is real data type. std::vector real(data.begin() + 1, data.end()); ZLOGD("Blob::type %{public}d size=%{public}d", static_cast(data[0]), static_cast(real.size())); - if (data[0] == JSUtil::STRING) { - return JSUtil::KvStoreVariant(std::string(real.begin(), real.end())); - } if (data[0] == JSUtil::INTEGER) { uint32_t tmp4int = be32toh(*reinterpret_cast(&(real[0]))); return JSUtil::KvStoreVariant(*reinterpret_cast(&tmp4int)); - } - if (data[0] == JSUtil::FLOAT) { + } else if (data[0] == JSUtil::FLOAT) { uint32_t tmp4flt = be32toh(*reinterpret_cast(&(real[0]))); - return JSUtil::KvStoreVariant(*reinterpret_cast(&tmp4flt)); - } - if (data[0] == JSUtil::BYTE_ARRAY) { + return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp4flt))); + } else if (data[0] == JSUtil::BYTE_ARRAY) { return JSUtil::KvStoreVariant(std::vector(real.begin(), real.end())); - } - if (data[0] == JSUtil::BOOLEAN) { + } else if (data[0] == JSUtil::BOOLEAN) { return JSUtil::KvStoreVariant(static_cast(real[0])); - } - if (data[0] == JSUtil::DOUBLE) { + } else if (data[0] == JSUtil::DOUBLE) { uint64_t tmp4dbl = be64toh(*reinterpret_cast(&(real[0]))); - return JSUtil::KvStoreVariant(*reinterpret_cast(&tmp4dbl)); + return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp4dbl))); + } else { + // for schema-db, if (data[0] == JSUtil::STRING), no beginning byte! + return JSUtil::KvStoreVariant(std::string(data.begin(), data.end())); } - ZLOGE("Blob::Type is INVALID"); - return JSUtil::KvStoreVariant(); } DistributedKv::Blob JSUtil::VariantValue2Blob(const JSUtil::KvStoreVariant& value) @@ -202,8 +188,8 @@ DistributedKv::Blob JSUtil::VariantValue2Blob(const JSUtil::KvStoreVariant& valu std::vector data; auto strValue = std::get_if(&value); if (strValue != nullptr) { - data.push_back(JSUtil::STRING); - data.insert(data.end(), (*strValue).begin(), (*strValue).end()); + // for schema-db, string, no beginning byte! + data.assign((*strValue).begin(), (*strValue).end()); } auto u8ArrayValue = std::get_if>(&value); if (u8ArrayValue != nullptr) { @@ -248,7 +234,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::KvStoreVariant { napi_valuetype type = napi_undefined; napi_status status = napi_typeof(env, in, &type); - ZLOGE_RETURN((status == napi_ok), "invalid type", status); + CHECK_RETURN((status == napi_ok), "invalid type", status); switch (type) { case napi_boolean: { bool vBool = false; @@ -318,7 +304,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::QueryVariant& { napi_valuetype type = napi_undefined; napi_status status = napi_typeof(env, in, &type); - ZLOGE_RETURN((status == napi_ok), "invalid type", status); + CHECK_RETURN((status == napi_ok), "invalid type", status); ZLOGD("napi_value -> QueryVariant type=%{public}d", type); switch (type) { case napi_boolean: { @@ -343,7 +329,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::QueryVariant& status = napi_invalid_arg; break; } - ZLOGE_RETURN((status == napi_ok), "napi_value -> QueryVariant bad value!", status); + CHECK_RETURN((status == napi_ok), "napi_value -> QueryVariant bad value!", status); return status; } @@ -371,6 +357,7 @@ napi_status JSUtil::SetValue(napi_env env, const JSUtil::QueryVariant& in, napi_ /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); napi_typedarray_type type = napi_biguint64_array; size_t length = 0; @@ -379,9 +366,9 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& void* data = nullptr; napi_status status = napi_get_typedarray_info(env, in, &type, &length, &data, &buffer, &offset); ZLOGD("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset); - ZLOGE_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); - ZLOGE_RETURN(type == napi_uint8_array, "is not Uint8Array!", napi_invalid_arg); - ZLOGE_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); + CHECK_RETURN(type == napi_uint8_array, "is not Uint8Array!", napi_invalid_arg); + CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); out.assign((uint8_t*)data, ((uint8_t*)data) + length); return status; } @@ -389,18 +376,18 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_value& out) { ZLOGD("napi_value <- std::vector "); - ZLOGE_RETURN(in.size() > 0, "invalid std::vector", napi_invalid_arg); + CHECK_RETURN(in.size() > 0, "invalid std::vector", napi_invalid_arg); void* data = nullptr; napi_value buffer = nullptr; napi_status status = napi_create_arraybuffer(env, in.size(), &data, &buffer); - ZLOGE_RETURN((status == napi_ok), "create array buffer failed!", status); + CHECK_RETURN((status == napi_ok), "create array buffer failed!", status); if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) { ZLOGE("memcpy_s not EOK"); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out); - ZLOGE_RETURN((status == napi_ok), "napi_value <- std::vector invalid value", status); + CHECK_RETURN((status == napi_ok), "napi_value <- std::vector invalid value", status); return status; } @@ -448,13 +435,14 @@ void TypedArray2Vector(uint8_t* data, size_t length, napi_typedarray_type type, convert(reinterpret_cast(data), length / sizeof(uint64_t)); break; default: - ZLOGE_RETURN_VOID(false, "[FATAL] invalid napi_typedarray_type!"); + CHECK_RETURN_VOID(false, "[FATAL] invalid napi_typedarray_type!"); } } /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); napi_typedarray_type type = napi_biguint64_array; size_t length = 0; @@ -463,9 +451,9 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& uint8_t* data = nullptr; napi_status status = napi_get_typedarray_info(env, in, &type, &length, (void**)&data, &buffer, &offset); ZLOGD("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset); - ZLOGE_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); - ZLOGE_RETURN(type <= napi_int32_array, "is not int32 supported typed array!", napi_invalid_arg); - ZLOGE_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); + CHECK_RETURN(type <= napi_int32_array, "is not int32 supported typed array!", napi_invalid_arg); + CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); TypedArray2Vector(data, length, type, out); return status; } @@ -474,24 +462,25 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_ { ZLOGD("napi_value <- std::vector "); size_t bytes = in.size() * sizeof(int32_t); - ZLOGE_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); + CHECK_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); void* data = nullptr; napi_value buffer = nullptr; napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { ZLOGE("memcpy_s not EOK"); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_int32_array, in.size(), buffer, 0, &out); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); return status; } /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); napi_typedarray_type type = napi_biguint64_array; size_t length = 0; @@ -500,9 +489,9 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& uint8_t* data = nullptr; napi_status status = napi_get_typedarray_info(env, in, &type, &length, (void**)&data, &buffer, &offset); ZLOGD("napi_get_typedarray_info type=%{public}d", (int)type); - ZLOGE_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); - ZLOGE_RETURN((type <= napi_uint16_array) || (type == napi_uint32_array), "invalid type!", napi_invalid_arg); - ZLOGE_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); + CHECK_RETURN((type <= napi_uint16_array) || (type == napi_uint32_array), "invalid type!", napi_invalid_arg); + CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); TypedArray2Vector(data, length, type, out); return status; } @@ -511,24 +500,25 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi { ZLOGD("napi_value <- std::vector "); size_t bytes = in.size() * sizeof(uint32_t); - ZLOGE_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); + CHECK_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); void* data = nullptr; napi_value buffer = nullptr; napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { ZLOGE("memcpy_s not EOK"); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_uint32_array, in.size(), buffer, 0, &out); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); return status; } /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); napi_typedarray_type type = napi_biguint64_array; size_t length = 0; @@ -537,9 +527,9 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& uint8_t* data = nullptr; napi_status status = napi_get_typedarray_info(env, in, &type, &length, (void**)&data, &buffer, &offset); ZLOGD("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset); - ZLOGE_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); - ZLOGE_RETURN((type <= napi_uint32_array) || (type == napi_bigint64_array), "invalid type!", napi_invalid_arg); - ZLOGE_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); + CHECK_RETURN((type <= napi_uint32_array) || (type == napi_bigint64_array), "invalid type!", napi_invalid_arg); + CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); TypedArray2Vector(data, length, type, out); return status; } @@ -548,23 +538,24 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_ { ZLOGD("napi_value <- std::vector "); size_t bytes = in.size() * sizeof(int64_t); - ZLOGE_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); + CHECK_RETURN(bytes > 0, "invalid std::vector", napi_invalid_arg); void* data = nullptr; napi_value buffer = nullptr; napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { ZLOGE("memcpy_s not EOK"); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_bigint64_array, in.size(), buffer, 0, &out); - ZLOGE_RETURN((status == napi_ok), "invalid buffer", status); + CHECK_RETURN((status == napi_ok), "invalid buffer", status); return status; } /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); bool isTypedArray = false; napi_status status = napi_is_typedarray(env, in, &isTypedArray); ZLOGD("napi_value -> std::vector input %{public}s a TypedArray", isTypedArray ? "is" : "is not"); @@ -577,24 +568,24 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& o uint8_t* data = nullptr; status = napi_get_typedarray_info(env, in, &type, &length, (void**)&data, &buffer, &offset); ZLOGD("napi_get_typedarray_info status=%{public}d type=%{public}d", status, (int)type); - ZLOGE_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); - ZLOGE_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); + CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); TypedArray2Vector(data, length, type, out); } else { bool isArray = false; status = napi_is_array(env, in, &isArray); ZLOGD("napi_value -> std::vector input %{public}s an Array", isArray ? "is" : "is not"); - ZLOGE_RETURN((status == napi_ok) && isArray, "invalid data!", napi_invalid_arg); + CHECK_RETURN((status == napi_ok) && isArray, "invalid data!", napi_invalid_arg); uint32_t length = 0; status = napi_get_array_length(env, in, &length); - ZLOGE_RETURN((status == napi_ok) && (length > 0), "invalid data!", napi_invalid_arg); + CHECK_RETURN((status == napi_ok) && (length > 0), "invalid data!", napi_invalid_arg); for (uint32_t i = 0; i < length; ++i) { napi_value item = nullptr; status = napi_get_element(env, in, i, &item); - ZLOGE_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg); + CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg); double vi = 0.0f; status = napi_get_value_double(env, item, &vi); - ZLOGE_RETURN(status == napi_ok, "element not a double", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "element not a double", napi_invalid_arg); out.push_back(vi); } } @@ -607,7 +598,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_v (void)(env); (void)(in); (void)(out); - ZLOGE_RETURN(false, "std::vector to napi_value, unsupported!", napi_invalid_arg); + CHECK_RETURN(false, "std::vector to napi_value, unsupported!", napi_invalid_arg); return napi_invalid_arg; } @@ -618,7 +609,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::map from napi_value, unsupported!", napi_invalid_arg); + CHECK_RETURN(false, "std::map from napi_value, unsupported!", napi_invalid_arg); return napi_invalid_arg; } @@ -626,7 +617,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::map "); napi_status status = napi_create_array_with_length(env, in.size(), &out); - ZLOGE_RETURN((status == napi_ok), "invalid object", status); + CHECK_RETURN((status == napi_ok), "invalid object", status); int index = 0; for (const auto& [key, value] : in) { napi_value element = nullptr; @@ -658,29 +649,29 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DistributedKv::Entry& ZLOGD("napi_value -> DistributedKv::Entry "); napi_value propKey = nullptr; napi_status status = napi_get_named_property(env, in, "key", &propKey); - ZLOGE_RETURN((status == napi_ok), "no property key", status); + CHECK_RETURN((status == napi_ok), "no property key", status); std::string key; status = GetValue(env, propKey, key); - ZLOGE_RETURN((status == napi_ok), "no value of key", status); + CHECK_RETURN((status == napi_ok), "no value of key", status); out.key = key; napi_value propValue = nullptr; status = napi_get_named_property(env, in, "value", &propValue); - ZLOGE_RETURN((status == napi_ok), "no property value", status); + CHECK_RETURN((status == napi_ok), "no property value", status); napi_value propVType = nullptr; status = napi_get_named_property(env, propValue, "type", &propVType); - ZLOGE_RETURN((status == napi_ok), "no property value.type", status); + CHECK_RETURN((status == napi_ok), "no property value.type", status); int32_t type = 0; // int8_t status = GetValue(env, propVType, type); - ZLOGE_RETURN((status == napi_ok), "no value of value.type", status); + CHECK_RETURN((status == napi_ok), "no value of value.type", status); napi_value propVValue = nullptr; status = napi_get_named_property(env, propValue, "value", &propVValue); - ZLOGE_RETURN((status == napi_ok), "no property value.value", status); + CHECK_RETURN((status == napi_ok), "no property value.value", status); KvStoreVariant value = 0; status = GetValue(env, propVValue, value); - ZLOGE_RETURN((status == napi_ok), "no value of value.value", status); + CHECK_RETURN((status == napi_ok), "no value of value.value", status); out.value = JSUtil::VariantValue2Blob(value); if (type != out.value[0]) { @@ -693,25 +684,25 @@ napi_status JSUtil::SetValue(napi_env env, const DistributedKv::Entry& in, napi_ { ZLOGD("napi_value <- DistributedKv::Entry "); napi_status status = napi_create_object(env, &out); - ZLOGE_RETURN((status == napi_ok), "invalid entry object", status); + CHECK_RETURN((status == napi_ok), "invalid entry object", status); napi_value key = nullptr; status = SetValue(env, in.key.ToString(), key); - ZLOGE_RETURN((status == napi_ok), "invalid entry key", status); + CHECK_RETURN((status == napi_ok), "invalid entry key", status); napi_set_named_property(env, out, "key", key); - ZLOGE_RETURN((in.value.Size() > 0), "invalid entry value", status); + CHECK_RETURN((in.value.Size() > 0), "invalid entry value", status); napi_value value = nullptr; status = napi_create_object(env, &value); - ZLOGE_RETURN((status == napi_ok), "invalid value object", status); + CHECK_RETURN((status == napi_ok), "invalid value object", status); napi_value vType = nullptr; napi_create_int32(env, in.value[0], &vType); napi_set_named_property(env, value, "type", vType); napi_value vValue = nullptr; status = SetValue(env, Blob2VariantValue(in.value), vValue); // Blob - ZLOGE_RETURN((status == napi_ok), "invalid entry value", status); + CHECK_RETURN((status == napi_ok), "invalid entry value", status); napi_set_named_property(env, value, "value", vValue); napi_set_named_property(env, out, "value", value); @@ -724,15 +715,15 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::list std::list "); bool isArray = false; napi_is_array(env, in, &isArray); - ZLOGE_RETURN(isArray, "not array", napi_invalid_arg); + CHECK_RETURN(isArray, "not array", napi_invalid_arg); uint32_t length = 0; napi_status status = napi_get_array_length(env, in, &length); - ZLOGE_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); + CHECK_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); for (uint32_t i = 0; i < length; ++i) { napi_value item = nullptr; status = napi_get_element(env, in, i, &item); - ZLOGE_RETURN((status == napi_ok), "no element", status); + CHECK_RETURN((status == napi_ok), "no element", status); if ((status != napi_ok) || (item == nullptr)) { continue; } @@ -747,7 +738,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::list { ZLOGD("napi_value <- std::list %{public}u", static_cast(in.size())); napi_status status = napi_create_array_with_length(env, in.size(), &out); - ZLOGE_RETURN(status == napi_ok, "create array failed!", status); + CHECK_RETURN(status == napi_ok, "create array failed!", status); int index = 0; for (const auto& item : in) { napi_value entry = nullptr; @@ -760,18 +751,19 @@ napi_status JSUtil::SetValue(napi_env env, const std::list /* napi_value <-> std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); bool isArray = false; napi_is_array(env, in, &isArray); - ZLOGE_RETURN(isArray, "not array", napi_invalid_arg); + CHECK_RETURN(isArray, "not array", napi_invalid_arg); uint32_t length = 0; napi_status status = napi_get_array_length(env, in, &length); - ZLOGE_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); + CHECK_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); for (uint32_t i = 0; i < length; ++i) { napi_value item = nullptr; status = napi_get_element(env, in, i, &item); - ZLOGE_RETURN((status == napi_ok), "no element", status); + CHECK_RETURN((status == napi_ok), "no element", status); if ((status != napi_ok) || (item == nullptr)) { continue; } @@ -786,7 +778,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector %{public}u", static_cast(in.size())); napi_status status = napi_create_array_with_length(env, in.size(), &out); - ZLOGE_RETURN(status == napi_ok, "create array failed!", status); + CHECK_RETURN(status == napi_ok, "create array failed!", status); int index = 0; for (const auto& item : in) { napi_value entry = nullptr; @@ -799,18 +791,19 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector std::vector */ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& out) { + out.clear(); ZLOGD("napi_value -> std::vector "); bool isArray = false; napi_is_array(env, in, &isArray); - ZLOGE_RETURN(isArray, "not array", napi_invalid_arg); + CHECK_RETURN(isArray, "not array", napi_invalid_arg); uint32_t length = 0; napi_status status = napi_get_array_length(env, in, &length); - ZLOGE_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); + CHECK_RETURN((status == napi_ok) && (length > 0), "get_array failed!", status); for (uint32_t i = 0; i < length; ++i) { napi_value item = nullptr; status = napi_get_element(env, in, i, &item); - ZLOGE_RETURN((status == napi_ok), "no element", status); + CHECK_RETURN((status == napi_ok), "no element", status); if ((status != napi_ok) || (item == nullptr)) { continue; } @@ -826,7 +819,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector %{public}u", static_cast(in.size())); napi_status status = napi_create_array_with_length(env, in.size(), &out); - ZLOGE_RETURN((status == napi_ok), "create_array failed!", status); + CHECK_RETURN((status == napi_ok), "create_array failed!", status); int index = 0; for (const auto& item : in) { napi_value entry = nullptr; @@ -843,7 +836,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DistributedKv::ChangeN (void)(env); (void)(in); (void)(out); - ZLOGE_RETURN(false, "DistributedKv::ChangeNotification from napi_value, unsupported!", napi_invalid_arg); + CHECK_RETURN(false, "DistributedKv::ChangeNotification from napi_value, unsupported!", napi_invalid_arg); return napi_invalid_arg; } @@ -851,30 +844,30 @@ napi_status JSUtil::SetValue(napi_env env, const DistributedKv::ChangeNotificati { ZLOGD("napi_value <- DistributedKv::ChangeNotification "); napi_status status = napi_create_object(env, &out); - ZLOGE_RETURN((status == napi_ok), "napi_create_object for DistributedKv::ChangeNotification failed!", status); + CHECK_RETURN((status == napi_ok), "napi_create_object for DistributedKv::ChangeNotification failed!", status); napi_value deviceId = nullptr; status = SetValue(env, in.GetDeviceId(), deviceId); - ZLOGE_RETURN((status == napi_ok) || (deviceId == nullptr), "GetDeviceId failed!", status); + CHECK_RETURN((status == napi_ok) || (deviceId == nullptr), "GetDeviceId failed!", status); status = napi_set_named_property(env, out, "deviceId", deviceId); - ZLOGE_RETURN((status == napi_ok), "set_named_property deviceId failed!", status); + CHECK_RETURN((status == napi_ok), "set_named_property deviceId failed!", status); napi_value insertEntries = nullptr; status = SetValue(env, in.GetInsertEntries(), insertEntries); - ZLOGE_RETURN((status == napi_ok) || (insertEntries == nullptr), "GetInsertEntries failed!", status); + CHECK_RETURN((status == napi_ok) || (insertEntries == nullptr), "GetInsertEntries failed!", status); status = napi_set_named_property(env, out, "insertEntries", insertEntries); - ZLOGE_RETURN((status == napi_ok), "set_named_property insertEntries failed!", status); + CHECK_RETURN((status == napi_ok), "set_named_property insertEntries failed!", status); napi_value updateEntries = nullptr; status = SetValue(env, in.GetUpdateEntries(), updateEntries); - ZLOGE_RETURN((status == napi_ok) || (updateEntries == nullptr), "GetUpdateEntries failed!", status); + CHECK_RETURN((status == napi_ok) || (updateEntries == nullptr), "GetUpdateEntries failed!", status); status = napi_set_named_property(env, out, "updateEntries", updateEntries); - ZLOGE_RETURN((status == napi_ok), "set_named_property updateEntries failed!", status); + CHECK_RETURN((status == napi_ok), "set_named_property updateEntries failed!", status); napi_value deleteEntries = nullptr; status = SetValue(env, in.GetDeleteEntries(), deleteEntries); - ZLOGE_RETURN((status == napi_ok) || (deleteEntries == nullptr), "GetDeleteEntries failed!", status); + CHECK_RETURN((status == napi_ok) || (deleteEntries == nullptr), "GetDeleteEntries failed!", status); status = napi_set_named_property(env, out, "deleteEntries", deleteEntries); - ZLOGE_RETURN((status == napi_ok), "set_named_property deleteEntries failed!", status); + CHECK_RETURN((status == napi_ok), "set_named_property deleteEntries failed!", status); return status; } @@ -886,9 +879,13 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DistributedKv::Options GetNamedProperty(env, in, "encrypt", options.encrypt); GetNamedProperty(env, in, "backup", options.backup); GetNamedProperty(env, in, "autoSync", options.autoSync); + int32_t kvStoreType = 0; GetNamedProperty(env, in, "kvStoreType", kvStoreType); options.kvStoreType = static_cast(kvStoreType); + + GetNamedProperty(env, in, "schema", options.schema); + int32_t level = 0; GetNamedProperty(env, in, "securityLevel", level); options.securityLevel = level; @@ -900,7 +897,7 @@ napi_status JSUtil::SetValue(napi_env env, const DistributedKv::Options& in, nap (void)(env); (void)(in); (void)(out); - ZLOGE_RETURN(false, "DistributedKv::Options to napi_value, unsupported!", napi_invalid_arg); + CHECK_RETURN(false, "DistributedKv::Options to napi_value, unsupported!", napi_invalid_arg); return napi_invalid_arg; } @@ -948,17 +945,17 @@ napi_ref JSUtil::NewWithRef(napi_env env, size_t argc, napi_value* argv, void** { napi_value object = nullptr; napi_status status = napi_new_instance(env, constructor, argc, argv, &object); - ZLOGE_RETURN(status == napi_ok, "napi_new_instance failed", nullptr); - ZLOGE_RETURN(object != nullptr, "napi_new_instance failed", nullptr); + CHECK_RETURN(status == napi_ok, "napi_new_instance failed", nullptr); + CHECK_RETURN(object != nullptr, "napi_new_instance failed", nullptr); status = napi_unwrap(env, object, out); - ZLOGE_RETURN(status == napi_ok, "napi_unwrap failed", nullptr); - ZLOGE_RETURN(out != nullptr, "napi_unwrap failed", nullptr); + CHECK_RETURN(status == napi_ok, "napi_unwrap failed", nullptr); + CHECK_RETURN(out != nullptr, "napi_unwrap failed", nullptr); napi_ref ref = nullptr; status = napi_create_reference(env, object, 1, &ref); - ZLOGE_RETURN(status == napi_ok, "napi_create_referenc!e failed", nullptr); - ZLOGE_RETURN(ref != nullptr, "napi_create_referenc!e failed", nullptr); + CHECK_RETURN(status == napi_ok, "napi_create_referenc!e failed", nullptr); + CHECK_RETURN(ref != nullptr, "napi_create_referenc!e failed", nullptr); return ref; } diff --git a/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp b/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp index 88da26470..fa51b8906 100644 --- a/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/napi_queue.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 @@ -39,12 +39,12 @@ void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoPa size_t argc = ARGC_MAX; napi_value argv[ARGC_MAX] = { nullptr }; status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr); - ZLOGE_ON_STATUS(this, "napi_get_cb_info failed!"); - ZLOGE_ON_ARGS(this, argc <= ARGC_MAX, "too many arguments!"); - ZLOGE_ON_ARGS(this, self != nullptr, "no JavaScript this argument!"); + CHECK_STATUS(this, "napi_get_cb_info failed!"); + CHECK_ARGS(this, argc <= ARGC_MAX, "too many arguments!"); + CHECK_ARGS(this, self != nullptr, "no JavaScript this argument!"); napi_create_reference(env, self, 1, &selfRef); status = napi_unwrap(env, self, &native); - ZLOGE_ON_STATUS(this, "self unwrap failed!"); + CHECK_STATUS(this, "self unwrap failed!"); if (!sync && (argc > 0)) { // get the last arguments :: @@ -53,7 +53,7 @@ void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoPa napi_status tyst = napi_typeof(env, argv[index], &type); if ((tyst == napi_ok) && (type == napi_function)) { status = napi_create_reference(env, argv[index], 1, &callbackRef); - ZLOGE_ON_STATUS(this, "ref callback failed!"); + CHECK_STATUS(this, "ref callback failed!"); argc = index; ZLOGD("async callback, no promise"); } else { @@ -64,7 +64,7 @@ void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoPa if (parse) { parse(argc, argv); } else { - ZLOGE_ON_ARGS(this, argc == 0, "required no arguments!"); + CHECK_ARGS(this, argc == 0, "required no arguments!"); } } @@ -88,21 +88,21 @@ napi_value NapiQueue::AsyncWork(napi_env env, std::shared_ptr ctxt, napi_create_async_work( ctxt->env, nullptr, resource, [](napi_env env, void* data) { + CHECK_RETURN_VOID(data != nullptr, "napi_async_execute_callback nullptr"); auto ctxt = reinterpret_cast(data); ZLOGD("napi_async_execute_callback ctxt->status=%{public}d", ctxt->status); if (ctxt->execute && ctxt->status == napi_ok) { - ZLOGD("call ctxt->execute"); ctxt->execute(); } }, [](napi_env env, napi_status status, void* data) { + CHECK_RETURN_VOID(data != nullptr, "napi_async_complete_callback nullptr"); auto ctxt = reinterpret_cast(data); ZLOGD("napi_async_complete_callback status=%{public}d, ctxt->status=%{public}d", status, ctxt->status); if ((status != napi_ok) && (ctxt->status == napi_ok)) { ctxt->status = status; } if ((ctxt->complete) && (status == napi_ok) && (ctxt->status == napi_ok)) { - ZLOGD("call ctxt->complete"); ctxt->complete(ctxt->output); } GenerateOutput(ctxt); @@ -115,7 +115,6 @@ napi_value NapiQueue::AsyncWork(napi_env env, std::shared_ptr ctxt, void NapiQueue::GenerateOutput(ContextBase* ctxt) { - ZLOGD("in"); napi_value result[RESULT_ALL] = { nullptr }; if (ctxt->status == napi_ok) { napi_get_undefined(ctxt->env, &result[RESULT_ERROR]); @@ -145,6 +144,5 @@ void NapiQueue::GenerateOutput(ContextBase* ctxt) napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult); } ctxt->hold.reset(); // release ctxt. - ZLOGD("out"); } } // namespace OHOS::DistributedData diff --git a/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp b/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp index 198d336c6..32fe0bd8d 100644 --- a/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/uv_queue.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 3d0253088..36f5a5771 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,6 +46,7 @@ ohos_shared_library("distributeddata") { sources = [ "../../../frameworks/jskitsimpl/distributeddata/src/entry_point.cpp", + "../../../frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp", "../../../frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp", -- Gitee