/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SceneComponentJS.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "SceneJS.h"
using namespace SCENE_NS;
void SceneComponentJS::Init(napi_env env, napi_value exports)
{
using namespace NapiApi;
BASE_NS::vector props;
napi_value func;
auto status = napi_define_class(env, "SceneComponent", NAPI_AUTO_LENGTH, BaseObject::ctor(),
nullptr, props.size(), props.data(), &func);
NapiApi::MyInstanceState* mis;
NapiApi::MyInstanceState::GetInstance(env, (void**)&mis);
if (mis) {
mis->StoreCtor("SceneComponent", func);
}
}
napi_value SceneComponentJS::Dispose(NapiApi::FunctionContext<>& ctx)
{
LOG_V("SceneComponentJS::Dispose");
DisposeNative(nullptr);
return {};
}
void SceneComponentJS::DisposeNative(void*)
{
if (!disposed_) {
disposed_ = true;
LOG_V("SceneComponentJS::DisposeNative");
if (!jsProps_.IsEmpty()) {
NapiApi::Object inp = jsProps_.GetObject();
for (auto&& v : proxies_) {
inp.DeleteProperty(v.first);
}
proxies_.clear();
}
jsProps_.Reset();
UnsetNativeObject();
scene_.Reset();
}
}
void* SceneComponentJS::GetInstanceImpl(uint32_t id)
{
if (id == SceneComponentJS::ID) {
return this;
}
return nullptr;
}
void SceneComponentJS::Finalize(napi_env env)
{
DisposeNative(scene_.GetObject().GetJsWrapper());
BaseObject::Finalize(env);
}
SceneComponentJS::SceneComponentJS(napi_env e, napi_callback_info i) : BaseObject(e, i)
{
LOG_V("SceneComponentJS ++");
NapiApi::FunctionContext fromJs(e, i);
if (!fromJs) {
return;
}
scene_ = { NapiApi::Object(fromJs.Arg<0>()) };
NapiApi::Object node = fromJs.Arg<1>();
const auto native = GetNativeObject();
if (!native) {
return;
}
NapiApi::Object meJs(fromJs.This());
BASE_NS::string name = native->GetName();
meJs.Set("name", name);
AddProperties(meJs, native);
SetNativeObject(native, PtrType::WEAK);
}
SceneComponentJS::~SceneComponentJS()
{
LOG_V("SceneComponentJS --");
DisposeNative(nullptr);
}
void SceneComponentJS::AddProperties(NapiApi::Object meJs, const META_NS::IObject::Ptr& obj)
{
auto comp = interface_cast(obj);
auto meta = interface_cast(obj);
if (!comp || !meta) {
return;
}
comp->PopulateAllProperties();
NapiApi::Object jsProps(meJs.GetEnv());
BASE_NS::vector napi_descs;
for (auto&& p : meta->GetProperties()) {
if (auto proxy = PropertyToProxy(scene_.GetObject(), jsProps, p)) {
auto res = proxies_.insert_or_assign(SCENE_NS::PropertyName(p->GetName()), proxy);
napi_descs.push_back(CreateProxyDesc(res.first->first.c_str(), BASE_NS::move(proxy)));
}
}
if (!napi_descs.empty()) {
napi_define_properties(jsProps.GetEnv(), jsProps.ToNapiValue(), napi_descs.size(), napi_descs.data());
}
jsProps_ = NapiApi::StrongRef(jsProps);
meJs.Set("property", jsProps_.GetValue());
}