• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef HIVIEWDFX_NAPI_VAL
16 #define HIVIEWDFX_NAPI_VAL
17 
18 #pragma once
19 
20 #include <memory>
21 #include <vector>
22 #include "uni_header.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class NVal final {
27 public:
28     NVal() = default;
29 
30     NVal(napi_env nEnv, napi_value nVal);
31 
32     NVal &operator=(const NVal &) = default;
33 
34     virtual ~NVal() = default;
35 
36     // NOTE! env_ and val_ is LIKELY to be null
37     napi_env env_ = nullptr;
38     napi_value val_ = nullptr;
39 
40     explicit operator bool() const;
41 
42     bool TypeIs(napi_valuetype expType) const;
43 
44     bool ValueTypeIs(napi_value value, napi_valuetype expType) const;
45 
46     /* SHOULD ONLY BE USED FOR EXPECTED TYPE */
47     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const;
48 
49     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const;
50 
51     std::tuple<bool, void *> ToPointer() const;
52 
53     std::tuple<bool, bool> ToBool() const;
54 
55     std::tuple<bool, double> ToDouble() const;
56 
57     std::tuple<bool, int32_t> ToInt32() const;
58 
59     std::tuple<bool, int64_t> ToInt64() const;
60 
61     std::tuple<bool, void *, size_t> ToArraybuffer() const;
62 
63     std::tuple<bool, napi_typedarray_type, void *, size_t> ToTypedArray() const;
64 
65     std::tuple<bool, bool> IsTypeArray() const;
66 
67     std::tuple<bool, void *, size_t> ToDataview() const;
68 
69     std::tuple<bool, bool> IsArray() const;
70 
71     std::tuple<bool, void *, size_t, size_t, napi_typedarray_type> ToTypedArrayInfo() const;
72 
73     std::tuple<bool, std::string> GetValObjectAsStr() const;
74 
75     /* Static helpers to create js objects */
76     static NVal CreateUndefined(napi_env env);
77 
78     static NVal CreateNull(napi_env env);
79 
80     static NVal CreateInt64(napi_env env, int64_t val);
81 
82     static NVal CreateObject(napi_env env);
83 
84     static NVal CreateBool(napi_env env, bool val);
85 
86     static NVal CreateUTF8String(napi_env env, std::string str);
87 
88     static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen);
89 
90     static NVal CreateDouble(napi_env env, double val);
91 
92     /* SHOULD ONLY BE USED FOR OBJECT */
93     bool HasProp(std::string propName) const;
94 
95     NVal GetProp(std::string propName) const;
96 
97     bool AddProp(std::vector<napi_property_descriptor> &&propVec) const;
98 
99     bool AddProp(std::string propName, napi_value nVal) const;
100 
101     /* Static helpers to create prop of js objects */
102     static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val);
103 
104     static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val);
105 
106     static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func);
107 
108     static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func);
109 
110     static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter);
111 
112     static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter);
113 
114     static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name,
115         napi_callback getter, napi_callback setter);
116 private:
117     std::string GetPrintString(napi_value value) const;
118 };
119 } // namespace HiviewDFX
120 } // namespace OHOS
121 
122 #endif // HIVIEWDFX_NAPI_VAL