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