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 JS_OBJECT_CACHE_H 17 #define JS_OBJECT_CACHE_H 18 19 #include <meta/interface/intf_object.h> 20 #include <napi_api.h> 21 22 /** 23 * @brief Fetch a JS object from the cache if it's there. 24 * @param obj The native object for which to fetch the JS wrapper. 25 * @param name An identifier to separate multiple JS objects wrapping the same native. 26 * @return The JS object wrapping the native one. If none found, a JS object with a JS null as value. 27 */ 28 NapiApi::Object FetchJsObj(const META_NS::IObject::Ptr& obj, BASE_NS::string_view name = "_JSW"); 29 30 template<typename t> 31 NapiApi::Object FetchJsObj(const t& obj, BASE_NS::string_view name = "_JSW") 32 { 33 return FetchJsObj(interface_pointer_cast<META_NS::IObject>(obj)); 34 } 35 36 /** 37 * @brief Store a reference of the JS object in the cache. 38 * @param obj The native object wrapped by jsObj. 39 * @param jsObj The JS object that wraps the native obj. 40 * @param name An identifier to separate multiple JS objects wrapping the same native. 41 * @return The stored JS object. If an error occurred, a JS object with a JS null as value. 42 */ 43 NapiApi::Object StoreJsObj( 44 const META_NS::IObject::Ptr& obj, const NapiApi::Object& jsObj, BASE_NS::string_view name = "_JSW"); 45 46 #endif 47