/* * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FILEMGMT_LIBN_N_VAL_H #define FILEMGMT_LIBN_N_VAL_H #include #include #include #include #include "n_napi.h" namespace OHOS { namespace FileManagement { namespace LibN { class NVal final { public: NVal() = default; NVal(napi_env nEnv, napi_value nVal); NVal(const NVal &) = default; NVal &operator=(const NVal &) = default; virtual ~NVal() = default; /* NOTE! env_ and val_ is LIKELY to be null */ napi_env env_ = nullptr; napi_value val_ = nullptr; explicit operator bool() const; bool TypeIs(napi_valuetype expType) const; bool TypeIsError(bool checkErrno = false) const; /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ std::tuple, size_t> ToUTF8String() const; std::tuple, size_t> ToUTF8String(std::string defaultValue) const; std::tuple, size_t> ToUTF16String() const; std::tuple, size_t> ToUTF8StringPath() const; std::tuple ToPointer() const; std::tuple ToBool() const; std::tuple ToBool(bool defaultValue) const; std::tuple ToInt32() const; std::tuple ToInt32(int32_t defaultValue) const; std::tuple ToInt64() const; std::tuple ToInt64(int64_t defaultValue) const; std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); std::tuple ToUint64() const; std::tuple ToUint32() const; std::tuple ToDouble() const; /* Static helpers to create js objects */ static NVal CreateUndefined(napi_env env); static NVal CreateBigInt64(napi_env env, int64_t val); static NVal CreateInt64(napi_env env, int64_t val); static NVal CreateInt32(napi_env env, int32_t val); static NVal CreateUint32(napi_env env, int32_t val); static NVal CreateObject(napi_env env); static NVal CreateBool(napi_env env, bool val); static NVal CreateUTF8String(napi_env env, std::string str); static NVal CreateUTF8String(napi_env env, const char *str, ssize_t len); static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen); static NVal CreateArrayString(napi_env env, std::vector strs); static std::tuple CreateArrayBuffer(napi_env env, size_t len); /* SHOULD ONLY BE USED FOR OBJECT */ bool HasProp(std::string propName) const; #ifdef WIN_PLATFORM NVal GetPropValue(std::string propName) const; #else NVal GetProp(std::string propName) const; #endif bool AddProp(std::vector &&propVec) const; bool AddProp(std::string propName, napi_value nVal) const; /* Static helpers to create prop of js objects */ static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val); static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val); static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func); static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func); static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter); static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter); static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter); }; } // namespace LibN } // namespace FileManagement } // namespace OHOS #endif // FILEMGMT_LIBN_N_VAL_H