• 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     /* SHOULD ONLY BE USED FOR EXPECTED TYPE */
45     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const;
46 
47     std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const;
48 
49     std::tuple<bool, void *> ToPointer() const;
50 
51     std::tuple<bool, bool> ToBool() const;
52 
53     std::tuple<bool, double> ToDouble() const;
54 
55     std::tuple<bool, int32_t> ToInt32() const;
56 
57     std::tuple<bool, int64_t> ToInt64() const;
58 
59     std::tuple<bool, void *, size_t> ToArraybuffer() const;
60 
61     std::tuple<bool, napi_typedarray_type, void *, size_t> ToTypedArray() const;
62 
63     std::tuple<bool, bool> IsTypeArray() const;
64 
65     std::tuple<bool, void *, size_t> ToDataview() const;
66 
67     std::tuple<bool, bool> IsArray() const;
68 
69     std::tuple<bool, void *, size_t, size_t, napi_typedarray_type> ToTypedArrayInfo() const;
70 
71     /* Static helpers to create js objects */
72     static NVal CreateUndefined(napi_env env);
73 
74     static NVal CreateNull(napi_env env);
75 
76     static NVal CreateInt64(napi_env env, int64_t val);
77 
78     static NVal CreateObject(napi_env env);
79 
80     static NVal CreateBool(napi_env env, bool val);
81 
82     static NVal CreateUTF8String(napi_env env, std::string str);
83 
84     static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen);
85 
86     static NVal CreateDouble(napi_env env, double val);
87 
88     /* SHOULD ONLY BE USED FOR OBJECT */
89     bool HasProp(std::string propName) const;
90 
91     NVal GetProp(std::string propName) const;
92 
93     bool AddProp(std::vector<napi_property_descriptor> &&propVec) const;
94 
95     bool AddProp(std::string propName, napi_value nVal) const;
96 
97     /* Static helpers to create prop of js objects */
98     static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val);
99 
100     static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val);
101 
102     static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func);
103 
104     static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func);
105 
106     static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter);
107 
108     static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter);
109 
110     static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name,
111         napi_callback getter, napi_callback setter);
112 };
113 } // namespace HiviewDFX
114 } // namespace OHOS
115 
116 #endif // HIVIEWDFX_NAPI_VAL