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 #ifndef INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_VALUE_H
17 #define INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_VALUE_H
18 
19 #include <string_view>
20 #include <any>
21 #include <tuple>
22 
23 #include "sys/types.h"
24 #include "header.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace LIBZIP {
29 class NapiValue final {
30 public:
31     NapiValue() = default;
32     NapiValue(napi_env nEnv, napi_value NapiValue);
33     NapiValue(const NapiValue &) = default;
34     NapiValue &operator=(const NapiValue &) = default;
35     virtual ~NapiValue() = default;
36 
37     // NOTE! env_ and val_ is LIKELY to be null
38     napi_env env_ = nullptr;
39     napi_value val_ = nullptr;
40 
41     explicit operator bool() const;
42     bool TypeIs(napi_valuetype expType) const;
43     bool TypeIsError(bool checkErrno = false) const;
44 
45     /* SHOULD ONLY BE USED FOR EXPECTED TYPE */
46     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const;
47     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String(std::string_view defaultValue) const;
48     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const;
49     std::tuple<bool, void *> ToPointer() const;
50     std::tuple<bool, bool> ToBool() const;
51     std::tuple<bool, bool> ToBool(bool defaultValue) const;
52     std::tuple<bool, int32_t> ToInt32() const;
53     std::tuple<bool, int32_t> ToInt32(int32_t defaultValue) const;
54     std::tuple<bool, int64_t> ToInt64() const;
55     std::tuple<bool, int64_t> ToInt64(int64_t defaultValue) const;
56     std::tuple<bool, void *, size_t> ToArrayBuffer() const;
57     std::tuple<bool, void *, size_t> ToTypedArray() const;
58     std::tuple<bool, std::vector<std::string>, uint32_t> ToStringArray();
59     std::tuple<bool, uint64_t, bool> ToUint64() const;
60     std::tuple<bool, uint32_t> ToUint32() const;
61     std::tuple<bool, double> ToDouble() const;
62 
63     /* Static helpers to create js objects */
64     static NapiValue CreateUndefined(napi_env env);
65     static NapiValue CreateBigInt64(napi_env env, int64_t val);
66     static NapiValue CreateInt64(napi_env env, int64_t val);
67     static NapiValue CreateUint64(napi_env env, uint64_t val);
68     static NapiValue CreateInt32(napi_env env, int32_t val);
69     static NapiValue CreateUint32(napi_env env, uint32_t val);
70     static NapiValue CreateObject(napi_env env);
71     static NapiValue CreateBool(napi_env env, bool val);
72     static NapiValue CreateUTF8String(napi_env env, const std::string &str);
73     static NapiValue CreateUTF8String(napi_env env, const char *str, ssize_t len);
74     static NapiValue CreateUint8Array(napi_env env, void *buf, size_t bufLen);
75     static NapiValue CreateArrayString(napi_env env, const std::vector<std::string> &strs);
76     static std::tuple<NapiValue, void *> CreateArrayBuffer(napi_env env, size_t len);
77     static NapiValue CreateInt64Array(napi_env env, const std::vector<int64_t> &int64Array);
78     static NapiValue CreateBigUint64Array(napi_env env, const std::vector<uint64_t> &int64Array);
79     /* SHOULD ONLY BE USED FOR OBJECT */
80     bool HasProp(const std::string &propName) const;
81     NapiValue GetProp(const std::string &propName) const;
82     bool AddProp(std::vector<napi_property_descriptor> &&propVec) const;
83     bool AddProp(const std::string &propName, napi_value val) const;
84 
85     /* Static helpers to create prop of js objects */
86     static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val);
87     static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val);
88     static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func);
89     static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func);
90     static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter);
91     static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter);
92     static inline napi_property_descriptor DeclareNapiGetterSetter(
93         const char *name, napi_callback getter, napi_callback setter);
94 };
95 }  // namespace LIBZIP
96 }  // namespace AppExecFwk
97 }  // namespace OHOS
98 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_VALUE_H