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