1 /* 2 * Copyright (C) 2025 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 NAPI_API_OBJECT_H 17 #define NAPI_API_OBJECT_H 18 19 #ifdef __OHOS_PLATFORM__ 20 #include "napi/native_api.h" 21 #else 22 #include <node_api.h> 23 #endif 24 25 #include <meta/interface/intf_object.h> 26 27 #include <base/containers/string.h> 28 29 #include "TrueRootObject.h" 30 #include "env.h" 31 #include "value.h" 32 33 class TrueRootObject; 34 struct JsFuncArgs; 35 36 namespace NapiApi { 37 38 class Object { 39 public: 40 Object() = default; 41 explicit Object(napi_env env); 42 Object(napi_env env, napi_value v); 43 Object(napi_env env, BASE_NS::string_view className, const JsFuncArgs& args); 44 45 napi_value ToNapiValue() const; 46 napi_env GetEnv() const; 47 48 TrueRootObject* GetRoot() const; 49 50 template<class T> GetJsWrapper()51 T* GetJsWrapper() const 52 { 53 if (const auto tro = GetRoot()) { 54 return static_cast<T*>(tro->GetInstanceImpl(T::ID)); 55 } 56 return nullptr; 57 } 58 59 META_NS::IObject::Ptr GetNative() const; 60 template<typename T> GetNative()61 typename T::Ptr GetNative() const 62 { 63 return interface_pointer_cast<T>(GetNative()); 64 } 65 66 template<typename t> Get(const BASE_NS::string_view name)67 NapiApi::Value<t> Get(const BASE_NS::string_view name) 68 { 69 return NapiApi::Value<t>(env_, Get(name)); 70 } 71 72 napi_value Get(const BASE_NS::string_view name); 73 74 template<typename type> Set(const BASE_NS::string_view name,NapiApi::Value<type> value)75 void Set(const BASE_NS::string_view name, NapiApi::Value<type> value) 76 { 77 Set(name, value.ToNapiValue()); 78 } 79 80 void Set(const BASE_NS::string_view name, napi_value value); 81 void Set(const BASE_NS::string_view name, const Object& value); 82 void Set(const BASE_NS::string_view name, const BASE_NS::string_view v); 83 84 operator bool() const; 85 bool Has(const BASE_NS::string_view name); 86 bool IsNull() const; 87 bool IsNull(const BASE_NS::string_view name); 88 bool IsDefined() const; 89 bool IsUndefined(const BASE_NS::string_view name); 90 bool IsUndefinedOrNull(const BASE_NS::string_view name); 91 92 // Note: this comparison is about NapiApi::Object instances, not about the underlying JS objects. 93 bool IsSame(const Object& other) const; 94 // This API represents the invocation of the Strict Equality algorithm as defined in Section 7.2.14 of the 95 // ECMAScript Language Specification. 96 bool StrictEqual(const Object& other) const; 97 98 void AddProperty(const napi_property_descriptor desc); 99 bool DeleteProperty(const BASE_NS::string_view name); 100 101 protected: 102 struct NameAndType { 103 bool success { false }; 104 napi_value res { nullptr }; 105 napi_valuetype jstype { napi_undefined }; 106 }; 107 NameAndType GetNamedPropertyAndType(const BASE_NS::string_view name); 108 napi_value MakeTempString(const BASE_NS::string_view v); 109 110 private: 111 template<class T> GetInterface()112 T* GetInterface() const 113 { 114 if (!env_ || !object_) { 115 return nullptr; 116 } 117 T* me = nullptr; 118 napi_unwrap(env_, object_, (void**)&me); 119 return me; 120 } 121 122 napi_valuetype jstype = napi_undefined; 123 Env env_ { nullptr }; 124 napi_value object_ { nullptr }; 125 }; 126 127 } // namespace NapiApi 128 129 #endif