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 #include "ObjectProxy.h"
16 
17 #include <meta/interface/intf_metadata.h>
18 #include <meta/interface/property/construct_array_property.h>
19 #include <meta/interface/property/property.h>
20 
21 #include <base/math/vector.h>
22 
23 #include "Vec2Proxy.h"
24 
ObjectProxy(META_NS::IProperty::ConstPtr & property,napi_env env)25 ObjectProxy::ObjectProxy(META_NS::IProperty::ConstPtr& property, napi_env env)
26     : PropertyProxy(META_NS::IProperty::Ptr(property, const_cast<META_NS::IProperty*>(property.get()))), env_(env)
27 {
28     napi_value value;
29     napi_create_object(env, &value);
30     obj = NapiApi::StrongRef { NapiApi::Object(env, value) };
31 
32     META_NS::ConstProperty<META_NS::IObject::Ptr> prop(GetProperty());
33 
34     if (prop) {
35         auto meta = interface_cast<META_NS::IMetadata>(prop->GetValue());
36         if (meta) {
37             for (auto& p : meta->GetAllProperties()) {
38                 AddProperty(p, p->GetName());
39             }
40         }
41     }
42 }
43 
AddProperty(BASE_NS::unique_ptr<PropertyProxy> property,BASE_NS::string_view member)44 void ObjectProxy::AddProperty(BASE_NS::unique_ptr<PropertyProxy> property, BASE_NS::string_view member)
45 {
46     duh.Lock();
47 
48     properties_.insert_or_assign(member, BASE_NS::move(property));
49 
50     Hook(BASE_NS::string(member));
51 
52     ScheduleUpdate();
53 
54     duh.Unlock();
55 }
56 
AddProperty(const META_NS::IProperty::Ptr & property,BASE_NS::string_view member)57 void ObjectProxy::AddProperty(const META_NS::IProperty::Ptr& property, BASE_NS::string_view member)
58 {
59     if (property->IsCompatible(META_NS::UidFromType<BASE_NS::Math::Vec2>())) {
60         AddProperty(BASE_NS::make_unique<Vec2Proxy>(env_, property), member);
61     }
62 }
63 
SetValue(NapiApi::FunctionContext<> & info,BASE_NS::string_view memb)64 void ObjectProxy::SetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
65 {
66     duh.Lock();
67     auto i = properties_.find(memb);
68     if (i == properties_.end()) {
69         return;
70     }
71 
72     NapiApi::FunctionContext<NapiApi::Object> ctx(info.GetEnv(), info.GetInfo());
73 
74     if (i->second->GetProperty()->IsCompatible(META_NS::UidFromType<BASE_NS::Math::Vec2>())) {
75         NapiApi::Object obj = ctx.Arg<0>();
76 
77         BASE_NS::Math::Vec2 vec(obj.Get<float>("x"), obj.Get<float>("y"));
78         static_cast<Vec2Proxy&>(*i->second).SetValue(vec);
79 
80         ScheduleUpdate();
81     }
82 
83     duh.Unlock();
84 }
85 
GetValue(NapiApi::FunctionContext<> & info,BASE_NS::string_view memb)86 napi_value ObjectProxy::GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
87 {
88     duh.Lock();
89     auto i = properties_.find(memb);
90     if (i == properties_.end()) {
91         duh.Unlock();
92         return {};
93     }
94 
95     napi_value result = i->second->Value();
96 
97     duh.Unlock();
98 
99     return result;
100 }
101 
UpdateLocalValues()102 void ObjectProxy::UpdateLocalValues() {}
103 
UpdateRemoteValues()104 void ObjectProxy::UpdateRemoteValues() {}
105