1 /*
2 * Copyright (C) 2025 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 "value.h"
17
18 #include "array.h"
19 #include "function.h"
20 #include "object.h"
21
22 namespace NapiApi {
23
24 template<typename T>
Init(napi_env env,Type v)25 void Value<T>::Init(napi_env env, Type v)
26 {
27 if (env == nullptr) {
28 return;
29 }
30 env_ = env;
31 if constexpr (BASE_NS::is_same_v<Type, float>) {
32 napi_create_double(env_, v, &value_);
33 }
34 if constexpr (BASE_NS::is_same_v<Type, double>) {
35 napi_create_double(env_, v, &value_);
36 }
37 if constexpr (BASE_NS::is_same_v<Type, uint32_t>) {
38 napi_create_uint32(env_, v, &value_);
39 }
40 if constexpr (BASE_NS::is_same_v<Type, int32_t>) {
41 napi_create_int32(env_, v, &value_);
42 }
43 if constexpr (BASE_NS::is_same_v<Type, int64_t>) {
44 napi_create_int64(env_, v, &value_);
45 }
46 if constexpr (BASE_NS::is_same_v<Type, uint64_t>) {
47 int64_t tmp = static_cast<int64_t>(v);
48 napi_create_int64(env_, tmp, &value_);
49 }
50 if constexpr (BASE_NS::is_same_v<Type, NapiApi::Object>) {
51 value_ = v.ToNapiValue();
52 }
53 }
54
55 template<typename type>
valueOrDefault(const type defaultValue)56 type NapiApi::Value<type>::valueOrDefault(const type defaultValue)
57 {
58 if (!value_) {
59 return defaultValue;
60 }
61 napi_status status = napi_invalid_arg;
62 type value {};
63 if constexpr (BASE_NS::is_same_v<type, BASE_NS::string>) {
64 size_t length;
65 status = napi_get_value_string_utf8(env_, value_, nullptr, 0, &length);
66 if (status != napi_ok) {
67 // return default if failed.
68 return defaultValue;
69 }
70 value.reserve(length + 1);
71 value.resize(length);
72 status = napi_get_value_string_utf8(env_, value_, value.data(), length + 1, &length);
73 }
74 if constexpr (BASE_NS::is_same_v<type, bool>) {
75 status = napi_get_value_bool(env_, value_, &value);
76 }
77 if constexpr (BASE_NS::is_same_v<type, float>) {
78 double tmp;
79 status = napi_get_value_double(env_, value_, &tmp);
80 value = tmp;
81 }
82 if constexpr (BASE_NS::is_same_v<type, double>) {
83 status = napi_get_value_double(env_, value_, &value);
84 }
85 if constexpr (BASE_NS::is_same_v<type, uint32_t>) {
86 status = napi_get_value_uint32(env_, value_, &value);
87 }
88 if constexpr (BASE_NS::is_same_v<type, int32_t>) {
89 status = napi_get_value_int32(env_, value_, &value);
90 }
91 if constexpr (BASE_NS::is_same_v<type, int64_t>) {
92 status = napi_get_value_int64(env_, value_, &value);
93 }
94 if constexpr (BASE_NS::is_same_v<type, uint64_t>) {
95 int64_t tmp;
96 status = napi_get_value_int64(env_, value_, &tmp);
97 value = static_cast<uint64_t>(tmp);
98 }
99 if constexpr (BASE_NS::is_same_v<type, NapiApi::Object>) {
100 status = napi_ok;
101 value = NapiApi::Object(env_, value_);
102 }
103 if constexpr (BASE_NS::is_same_v<type, NapiApi::Function>) {
104 status = napi_ok;
105 value = NapiApi::Function(env_, value_);
106 }
107 if constexpr (BASE_NS::is_same_v<type, NapiApi::Array>) {
108 status = napi_ok;
109 value = NapiApi::Array(env_, value_);
110 }
111 if (status != napi_ok) {
112 // return default if failed.
113 return defaultValue;
114 }
115 return value;
116 }
117
118 template class Value<BASE_NS::string>;
119 template class Value<bool>;
120 template class Value<float>;
121 template class Value<double>;
122 template class Value<uint32_t>;
123 template class Value<int32_t>;
124 template class Value<int64_t>;
125 template class Value<uint64_t>;
126 template class Value<NapiApi::Object>;
127 template class Value<NapiApi::Function>;
128 template class Value<NapiApi::Array>;
129
130 } // namespace NapiApi
131