1 /*
2 * Copyright (C) 2024 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 #include "ArrayPropertyProxy.h"
17
18 #include <meta/api/make_callback.h>
19 #include <meta/api/util.h>
20 #include <meta/interface/intf_metadata.h>
21 #include <meta/interface/intf_task_queue_registry.h>
22 #include <napi_api.h>
23
24 #include "BaseObjectJS.h"
25
~ArrayPropertyProxy()26 ArrayPropertyProxy::~ArrayPropertyProxy()
27 {
28 // should be a no-op.
29 }
30
ArrayPropertyProxy(META_NS::IProperty::Ptr prop,META_NS::IArrayAny::IndexType index)31 ArrayPropertyProxy::ArrayPropertyProxy(META_NS::IProperty::Ptr prop, META_NS::IArrayAny::IndexType index)
32 : prop_(prop), index_(index)
33 {
34 assert(prop_);
35 }
36
GetPropertyPtr() const37 META_NS::IProperty::Ptr ArrayPropertyProxy::GetPropertyPtr() const noexcept
38 {
39 return prop_;
40 }
41
DummyFunc(napi_env e,napi_callback_info i)42 static napi_value DummyFunc(napi_env e, napi_callback_info i)
43 {
44 NapiApi::FunctionContext<> info(e, i);
45 return info.GetUndefined();
46 };
47
PropProxGet(napi_env e,napi_callback_info i)48 static napi_value PropProxGet(napi_env e, napi_callback_info i)
49 {
50 NapiApi::FunctionContext<> info(e, i);
51 auto pc = static_cast<ArrayPropertyProxy*>(info.GetData());
52 if (pc) {
53 return pc->Value();
54 }
55 return info.GetUndefined();
56 };
57
PropProxSet(napi_env e,napi_callback_info i)58 static napi_value PropProxSet(napi_env e, napi_callback_info i)
59 {
60 NapiApi::FunctionContext<> info(e, i);
61 auto pc = static_cast<ArrayPropertyProxy*>(info.GetData());
62 if (pc) {
63 pc->SetValue(info);
64 }
65 return info.GetUndefined();
66 };
67
CreateArrayProxyDesc(const char * name,BASE_NS::shared_ptr<ArrayPropertyProxy> proxy)68 napi_property_descriptor CreateArrayProxyDesc(const char* name, BASE_NS::shared_ptr<ArrayPropertyProxy> proxy)
69 {
70 napi_property_descriptor desc { name, nullptr, nullptr, nullptr, nullptr, nullptr, napi_default_jsproperty,
71 static_cast<void*>(proxy.get()) };
72 if (proxy) {
73 desc.getter = PropProxGet;
74 desc.setter = PropProxSet;
75 } else {
76 desc.getter = desc.setter = DummyFunc;
77 }
78 return desc;
79 }