• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "condition/condition_manager.h"
16 #include "locks/async_lock_manager.h"
17 #include "json/json_manager.h"
18 #include "tools/log.h"
19 #include "utils.h"
20 
21 namespace Commonlibrary::Concurrent {
22 const std::string IS_SENDABLE_NAME = "isSendable";
23 
InitGlobal(napi_env env,const std::string & valueName,bool isFunc,napi_value exports)24 napi_value InitGlobal(napi_env env, const std::string& valueName, bool isFunc, napi_value exports)
25 {
26     napi_value global;
27     napi_value key;
28     napi_value value;
29     NAPI_CALL(env, napi_get_global(env, &global));
30     NAPI_CALL(env, napi_create_string_utf8(env, valueName.c_str(), valueName.size(), &key));
31     NAPI_CALL(env, napi_get_property(env, global, key, &value));
32 
33     if (isFunc) {
34         bool validFunction = false;
35         napi_is_callable(env, value, &validFunction);
36         if (!validFunction) {
37             HILOG_ERROR("Get function for %{public}s failed.", valueName.c_str());
38             return exports;
39         }
40     }
41     NAPI_CALL(env, napi_set_named_property(env, exports, valueName.c_str(), value));
42 
43     return exports;
44 }
Init(napi_env env,napi_value exports)45 napi_value Utils::Init(napi_env env, napi_value exports)
46 {
47     Commonlibrary::Concurrent::LocksModule::AsyncLockManager::Init(env, exports);
48     Commonlibrary::Concurrent::JsonManager::Init(env, exports);
49     Commonlibrary::Concurrent::Condition::ConditionManager::Init(env, exports);
50     InitGlobal(env, IS_SENDABLE_NAME, true, exports);
51 
52     return exports;
53 }
54 }  // namespace Commonlibrary::Concurrent
55