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