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 "PropertyProxy.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 <scene/ext/intf_internal_scene.h>
25 #include <scene/ext/intf_ecs_object_access.h>
26
27 #include "BaseObjectJS.h"
28 #include "Vec2Proxy.h"
29 #include "Vec3Proxy.h"
30 #include "Vec4Proxy.h"
31 #include "QuatProxy.h"
32 #include "ColorProxy.h"
33
~PropertyProxy()34 PropertyProxy::~PropertyProxy()
35 {
36 // should be a no-op.
37 }
38
PropertyProxy(META_NS::IProperty::Ptr prop)39 PropertyProxy::PropertyProxy(META_NS::IProperty::Ptr prop) : prop_(prop)
40 {
41 assert(prop_);
42 }
43
GetPropertyPtr() const44 META_NS::IProperty::Ptr PropertyProxy::GetPropertyPtr() const noexcept
45 {
46 return prop_;
47 }
48
SetExtra(const BASE_NS::shared_ptr<CORE_NS::IInterface> extra)49 void PropertyProxy::SetExtra(const BASE_NS::shared_ptr<CORE_NS::IInterface> extra)
50 {
51 extra_ = extra;
52 }
53
GetExtra() const54 const BASE_NS::shared_ptr<CORE_NS::IInterface> PropertyProxy::GetExtra() const noexcept
55 {
56 return extra_.lock();
57 }
58
MemberProxy(ObjectPropertyProxy * p,BASE_NS::string m)59 ObjectPropertyProxy::MemberProxy::MemberProxy(ObjectPropertyProxy* p, BASE_NS::string m) : proxy_(p), memb_(m) {}
~MemberProxy()60 ObjectPropertyProxy::MemberProxy::~MemberProxy() {}
Name() const61 const BASE_NS::string_view ObjectPropertyProxy::MemberProxy::Name() const
62 {
63 return memb_;
64 }
65
Getter(napi_env e,napi_callback_info i)66 napi_value ObjectPropertyProxy::MemberProxy::Getter(napi_env e, napi_callback_info i)
67 {
68 NapiApi::FunctionContext<> info(e, i);
69 auto pc = static_cast<ObjectPropertyProxy::MemberProxy*>(info.GetData());
70 if ((pc) && (pc->proxy_)) {
71 return pc->proxy_->GetMemberValue(info.Env(), pc->memb_);
72 }
73 return info.GetUndefined();
74 }
Setter(napi_env e,napi_callback_info i)75 napi_value ObjectPropertyProxy::MemberProxy::Setter(napi_env e, napi_callback_info i)
76 {
77 NapiApi::FunctionContext<> info(e, i);
78 auto pc = static_cast<ObjectPropertyProxy::MemberProxy*>(info.GetData());
79 if ((pc) && (pc->proxy_)) {
80 pc->proxy_->SetMemberValue(info, pc->memb_);
81 }
82 return info.GetUndefined();
83 }
84
Create(napi_env env,const BASE_NS::string jsName)85 void ObjectPropertyProxy::Create(napi_env env, const BASE_NS::string jsName)
86 {
87 if (jsName.empty()) {
88 obj_ = NapiApi::StrongRef { NapiApi::Object(env) };
89 } else {
90 NapiApi::MyInstanceState* mis;
91 NapiApi::MyInstanceState::GetInstance(env, reinterpret_cast<void**>(&mis));
92 if (!mis) {
93 CORE_LOG_E("Could not Get Napi Instance");
94 return;
95 }
96 auto ref = NapiApi::Object(env, mis->GetRef());
97 auto cl = ref.Get(jsName.c_str());
98 if (cl) {
99 napi_value value;
100 napi_new_instance(env, cl, 0, nullptr, &value);
101 obj_ = NapiApi::StrongRef { NapiApi::Object(env, value) };
102 }
103 }
104 if (obj_.IsEmpty()) {
105 CORE_LOG_F("Could not create property object for %s", jsName.c_str());
106 }
107 }
108
Hook(const BASE_NS::string member)109 void ObjectPropertyProxy::Hook(const BASE_NS::string member)
110 {
111 if (obj_.IsEmpty()) {
112 return;
113 }
114 auto ValueObject = obj_.GetObject();
115 auto* accessor = new MemberProxy(this, member);
116 accessors_.push_back(BASE_NS::unique_ptr<MemberProxy>(accessor));
117 ValueObject.AddProperty({ accessor->Name().data(), nullptr, nullptr, MemberProxy::Getter, MemberProxy::Setter,
118 nullptr, napi_default_jsproperty, static_cast<void*>(accessor) });
119 }
120
Reset()121 void ObjectPropertyProxy::Reset()
122 {
123 napi_status status;
124 if (obj_.IsEmpty()) {
125 return;
126 }
127
128 NapiApi::Env env(obj_.GetEnv());
129 // unhook all hooked members.
130 auto ValueObject = obj_.GetObject();
131 for (; !accessors_.empty();) {
132 auto it(accessors_.begin());
133 // remove the property
134 ValueObject.DeleteProperty((*it)->Name());
135 // delete accessor
136 accessors_.erase(it);
137 }
138 bool exception = false;
139 status = napi_is_exception_pending(env, &exception);
140 if (exception) {
141 // if it's set, just clear it.
142 // interestingly even though the napi_*_property might have returned napi_pending_exception (or other error
143 // state) it seems that napi_is_exception_pending WILL return false though.
144 napi_value res;
145 status = napi_get_and_clear_last_exception(env, &res);
146 }
147 }
148
Destroy()149 void ObjectPropertyProxy::Destroy()
150 {
151 Reset();
152 // release ref.
153 obj_.Reset();
154 }
155
Value()156 napi_value ObjectPropertyProxy::Value()
157 {
158 return obj_.GetValue();
159 }
160
SetValue(NapiApi::FunctionContext<> & info)161 void ObjectPropertyProxy::SetValue(NapiApi::FunctionContext<>& info)
162 {
163 NapiApi::FunctionContext<NapiApi::Object> data { info };
164 if (data) {
165 SetValue(data.Arg<0>());
166 }
167 }
168
ObjectPropertyProxy(META_NS::IProperty::Ptr prop)169 ObjectPropertyProxy::ObjectPropertyProxy(META_NS::IProperty::Ptr prop) : PropertyProxy(prop) {}
170
~ObjectPropertyProxy()171 ObjectPropertyProxy::~ObjectPropertyProxy()
172 {
173 Reset();
174 }
175
EntityProxy(NapiApi::Object scn,NapiApi::Object obj,META_NS::Property<CORE_NS::Entity> prop)176 EntityProxy::EntityProxy(NapiApi::Object scn, NapiApi::Object obj, META_NS::Property<CORE_NS::Entity> prop)
177 : PropertyProxy(prop)
178 {
179 scene_ = scn;
180 obj_ = obj;
181 }
182
~EntityProxy()183 EntityProxy::~EntityProxy()
184 {
185 // Unhook the objects.
186 Reset();
187 }
188
Reset()189 void EntityProxy::Reset()
190 {
191 auto prop = GetPropertyPtr();
192 if (!obj_.IsEmpty() && prop) {
193 obj_.GetObject().DeleteProperty(prop->GetName());
194 }
195 obj_.Reset();
196 scene_.Reset();
197 }
198
SetValue(const CORE_NS::Entity v)199 void EntityProxy::SetValue(const CORE_NS::Entity v)
200 {
201 META_NS::SetValue(GetProperty<CORE_NS::Entity>(), v);
202 }
203
Value()204 napi_value EntityProxy::Value()
205 {
206 NapiApi::Env env(obj_.GetEnv());
207 if (auto entity = META_NS::GetValue(GetProperty<CORE_NS::Entity>()); CORE_NS::EntityUtil::IsValid(entity)) {
208 if (auto scene = scene_.GetObject().GetNative<SCENE_NS::IScene>()) {
209 if (auto internal = scene->GetInternalScene()) {
210 if (auto node = internal->FindNode(entity, {})) {
211 NapiApi::Object parms(env);
212 napi_value args[] = { scene_.GetValue(), parms.ToNapiValue() };
213 return CreateFromNativeInstance(env, node, PtrType::WEAK, args).ToNapiValue();
214 }
215 }
216 }
217 }
218 return env.GetNull();
219 }
220
SetValue(NapiApi::FunctionContext<> & info)221 void EntityProxy::SetValue(NapiApi::FunctionContext<>& info)
222 {
223 NapiApi::FunctionContext<NapiApi::Object> data { info };
224 if (data) {
225 NapiApi::Object val = data.Arg<0>();
226 CORE_NS::Entity entity;
227 if (auto ecsoa = val.GetNative<SCENE_NS::IEcsObjectAccess>()) {
228 if (auto ecso = ecsoa->GetEcsObject()) {
229 entity = ecso->GetEntity();
230 }
231 }
232 META_NS::SetValue(GetProperty<CORE_NS::Entity>(), entity);
233 }
234 }
235
Reset()236 void ImageProxy::Reset()
237 {
238 auto prop = GetPropertyPtr();
239 if (!obj_.IsEmpty() && prop) {
240 obj_.GetObject().DeleteProperty(prop->GetName());
241 }
242 obj_.Reset();
243 scene_.Reset();
244 }
245
ImageProxy(NapiApi::Object scn,NapiApi::Object obj,META_NS::Property<SCENE_NS::IImage::Ptr> prop)246 ImageProxy::ImageProxy(NapiApi::Object scn, NapiApi::Object obj, META_NS::Property<SCENE_NS::IImage::Ptr> prop)
247 : PropertyProxy(prop)
248 {
249 scene_ = scn;
250 obj_ = obj;
251 }
~ImageProxy()252 ImageProxy::~ImageProxy()
253 {
254 // Unhook the objects.
255 Reset();
256 }
SetValue(const SCENE_NS::IBitmap::Ptr & v)257 void ImageProxy::SetValue(const SCENE_NS::IBitmap::Ptr& v)
258 {
259 META_NS::SetValue(GetProperty<SCENE_NS::IBitmap::Ptr>(), v);
260 }
Value()261 napi_value ImageProxy::Value()
262 {
263 auto value = META_NS::GetValue(GetProperty<SCENE_NS::IImage::Ptr>());
264 NapiApi::Env env(obj_.GetEnv());
265 if (value) {
266 NapiApi::Object parms(env);
267 napi_value args[] = { scene_.GetValue(), parms.ToNapiValue() };
268 BASE_NS::string uri;
269 if (auto m = interface_cast<META_NS::IMetadata>(value)) {
270 if (auto p = m->GetProperty<BASE_NS::string>("Uri")) {
271 uri = p->GetValue();
272 }
273 }
274 parms.Set("uri", uri);
275 return CreateFromNativeInstance(env, value, PtrType::WEAK, args).ToNapiValue();
276 }
277 return env.GetNull();
278 }
SetValue(NapiApi::FunctionContext<> & info)279 void ImageProxy::SetValue(NapiApi::FunctionContext<>& info)
280 {
281 NapiApi::FunctionContext<NapiApi::Object> data { info };
282 if (data) {
283 NapiApi::Object val = data.Arg<0>();
284 auto bitmap = val.GetNative<SCENE_NS::IImage>();
285 META_NS::SetValue(GetProperty<SCENE_NS::IImage::Ptr>(), bitmap);
286 }
287 }
288
PropertyToProxy(NapiApi::Object scene,NapiApi::Object obj,const META_NS::IProperty::Ptr & t)289 BASE_NS::shared_ptr<PropertyProxy> PropertyToProxy(
290 NapiApi::Object scene, NapiApi::Object obj, const META_NS::IProperty::Ptr& t)
291 {
292 if (META_NS::IsCompatibleWith<float>(t)) {
293 return BASE_NS::shared_ptr { new TypeProxy<float>(obj, t) };
294 }
295 if (META_NS::IsCompatibleWith<int32_t>(t)) {
296 return BASE_NS::shared_ptr { new TypeProxy<int32_t>(obj, t) };
297 }
298 if (META_NS::IsCompatibleWith<uint32_t>(t)) {
299 return BASE_NS::shared_ptr { new TypeProxy<uint32_t>(obj, t) };
300 }
301 if (META_NS::IsCompatibleWith<int64_t>(t)) {
302 return BASE_NS::shared_ptr { new TypeProxy<int64_t>(obj, t) };
303 }
304 if (META_NS::IsCompatibleWith<uint64_t>(t)) {
305 return BASE_NS::shared_ptr { new TypeProxy<uint64_t>(obj, t) };
306 }
307 if (META_NS::IsCompatibleWith<BASE_NS::string>(t)) {
308 return BASE_NS::shared_ptr { new TypeProxy<BASE_NS::string>(obj, t) };
309 }
310 if (META_NS::IsCompatibleWith<BASE_NS::Math::Vec2>(t)) {
311 return BASE_NS::shared_ptr { new Vec2Proxy(obj.GetEnv(), t) };
312 }
313 if (META_NS::IsCompatibleWith<BASE_NS::Math::Vec3>(t)) {
314 return BASE_NS::shared_ptr { new Vec3Proxy(obj.GetEnv(), t) };
315 }
316 if (META_NS::IsCompatibleWith<BASE_NS::Math::Vec4>(t)) {
317 return BASE_NS::shared_ptr { new Vec4Proxy(obj.GetEnv(), t) };
318 }
319 if (META_NS::IsCompatibleWith<BASE_NS::Math::Quat>(t)) {
320 return BASE_NS::shared_ptr { new QuatProxy(obj.GetEnv(), t) };
321 }
322 if (META_NS::IsCompatibleWith<BASE_NS::Color>(t)) {
323 return BASE_NS::shared_ptr { new ColorProxy(obj.GetEnv(), META_NS::Property<BASE_NS::Color>(t)) };
324 }
325 if (META_NS::IsCompatibleWith<SCENE_NS::IImage::Ptr>(t)) {
326 return BASE_NS::shared_ptr { new ImageProxy(scene, obj, t) };
327 }
328 if (META_NS::IsCompatibleWith<CORE_NS::Entity>(t)) {
329 return BASE_NS::shared_ptr { new EntityProxy(scene, obj, t) };
330 }
331 if (META_NS::IsCompatibleWith<bool>(t)) {
332 return BASE_NS::shared_ptr { new TypeProxy<bool>(obj, t) };
333 }
334 auto any = META_NS::GetInternalAny(t);
335 LOG_F("Unsupported property type [%s]", any ? any->GetTypeIdString().c_str() : "<Unknown>");
336 return nullptr;
337 }
338
DummyFunc(napi_env e,napi_callback_info i)339 static napi_value DummyFunc(napi_env e, napi_callback_info i)
340 {
341 NapiApi::FunctionContext<> info(e, i);
342 return info.GetUndefined();
343 };
PropProxGet(napi_env e,napi_callback_info i)344 static napi_value PropProxGet(napi_env e, napi_callback_info i)
345 {
346 NapiApi::FunctionContext<> info(e, i);
347 auto pc = static_cast<PropertyProxy*>(info.GetData());
348 if (pc) {
349 return pc->Value();
350 }
351 return info.GetUndefined();
352 };
PropProxSet(napi_env e,napi_callback_info i)353 static napi_value PropProxSet(napi_env e, napi_callback_info i)
354 {
355 NapiApi::FunctionContext<>info (e, i);
356 auto pc = static_cast<PropertyProxy*>(info.GetData());
357 if (pc) {
358 pc->SetValue(info);
359 }
360 return info.GetUndefined();
361 };
CreateProxyDesc(const char * name,BASE_NS::shared_ptr<PropertyProxy> proxy)362 napi_property_descriptor CreateProxyDesc(const char* name, BASE_NS::shared_ptr<PropertyProxy> proxy)
363 {
364 napi_property_descriptor desc { name, nullptr, nullptr, nullptr, nullptr, nullptr, napi_default_jsproperty,
365 static_cast<void*>(proxy.get()) };
366 if (proxy) {
367 desc.getter = PropProxGet;
368 desc.setter = PropProxSet;
369 } else {
370 desc.getter = desc.setter = DummyFunc;
371 }
372 return desc;
373 }
374