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 16 #ifndef INTERFACES_KITS_NAPI_COMMON_NAPI_N_VAL_H 17 #define INTERFACES_KITS_NAPI_COMMON_NAPI_N_VAL_H 18 19 #include <tuple> 20 #include <vector> 21 22 #include "uni_header.h" 23 24 namespace OHOS { 25 namespace DistributedFS { 26 class NVal final { 27 public: 28 NVal() = default; 29 NVal(napi_env nEnv, napi_value nVal); 30 NVal(const NVal &) = default; 31 NVal &operator = (const NVal &) = default; 32 virtual ~NVal() = default; 33 34 // NOTE! env_ and val_ is LIKELY to be null 35 napi_env env_ = nullptr; 36 napi_value val_ = nullptr; 37 38 explicit operator bool() const; 39 bool TypeIs(napi_valuetype expType) const; 40 41 /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ 42 std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const; 43 std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const; 44 std::tuple<bool, void *> ToPointer() const; 45 std::tuple<bool, bool> ToBool() const; 46 std::tuple<bool, int32_t> ToInt32() const; 47 std::tuple<bool, int64_t> ToInt64() const; 48 std::tuple<bool, void *, size_t> ToArraybuffer() const; 49 std::tuple<bool, void *, size_t> ToTypedArray() const; 50 51 /* Static helpers to create js objects */ 52 static NVal CreateUndefined(napi_env env); 53 static NVal CreateInt64(napi_env env, int64_t val); 54 static NVal CreateInt32(napi_env env, int32_t val); 55 static NVal CreateObject(napi_env env); 56 static NVal CreateBool(napi_env env, bool val); 57 static NVal CreateUTF8String(napi_env env, std::string str); 58 static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen); 59 60 /* SHOULD ONLY BE USED FOR OBJECT */ 61 bool HasProp(std::string propName) const; 62 NVal GetProp(std::string propName) const; 63 bool AddProp(std::vector<napi_property_descriptor> &&propVec) const; 64 bool AddProp(std::string propName, napi_value nVal) const; 65 66 /* Static helpers to create prop of js objects */ 67 static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val); 68 static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val); 69 static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func); 70 static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func); 71 static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter); 72 static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter); 73 static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name, 74 napi_callback getter, 75 napi_callback setter); 76 }; 77 } // namespace DistributedFS 78 } // namespace OHOS 79 #endif