• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "NodeJS.h"
17 
18 #include <scene/interface/intf_node.h>
19 #include <scene/interface/intf_scene.h>
20 
21 #include "ParamParsing.h"
22 #include "SceneJS.h"
23 
Init(napi_env env,napi_value exports)24 void NodeJS::Init(napi_env env, napi_value exports)
25 {
26     BASE_NS::vector<napi_property_descriptor> node_props;
27     NodeImpl::GetPropertyDescs(node_props);
28 
29     napi_value func;
30     auto status = napi_define_class(env, "Node", NAPI_AUTO_LENGTH, BaseObject::ctor<NodeJS>(), nullptr,
31         node_props.size(), node_props.data(), &func);
32 
33     NapiApi::MyInstanceState* mis;
34     NapiApi::MyInstanceState::GetInstance(env, (void**)&mis);
35     if (mis) {
36         mis->StoreCtor("Node", func);
37     }
38 }
39 
NodeJS(napi_env e,napi_callback_info i)40 NodeJS::NodeJS(napi_env e, napi_callback_info i) : BaseObject(e, i), NodeImpl(NodeImpl::NODE)
41 {
42     LOG_V("NodeJS ++");
43 
44     NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object> fromJs(e, i);
45     if (!fromJs) {
46         // no arguments. so internal create.
47         // expecting caller to finish initialization
48         return;
49     }
50 
51     {
52         // add the dispose hook to scene. (so that the geometry node is disposed when scene is disposed)
53         NapiApi::Object meJs(fromJs.This());
54         NapiApi::Object scene = fromJs.Arg<0>();
55         if (const auto sceneJS = scene.GetJsWrapper<SceneJS>()) {
56             sceneJS->StrongDisposeHook(reinterpret_cast<uintptr_t>(&scene_), meJs);
57         }
58     }
59 
60     // java script call.. with arguments
61     scene_ = fromJs.Arg<0>().valueOrDefault();
62     auto scn = scene_.GetObject().GetNative<SCENE_NS::IScene>();
63     if (scn == nullptr) {
64         // hmm..
65         LOG_F("Invalid scene for NodeJS!");
66         return;
67     }
68     if (!GetNativeObject()) {
69         LOG_E("Cannot finish creating a node: Native node object missing");
70         assert(false);
71         return;
72     }
73 
74     auto sceneNodeParameters = NapiApi::Object { fromJs.Arg<1>() };
75     if (const auto name = ExtractName(sceneNodeParameters); !name.empty()) {
76         fromJs.This().Set("name", name);
77     }
78 }
~NodeJS()79 NodeJS::~NodeJS()
80 {
81     LOG_V("NodeJS --");
82 }
GetInstanceImpl(uint32_t id)83 void* NodeJS::GetInstanceImpl(uint32_t id)
84 {
85     if (id == NodeJS::ID)
86         return this;
87     return NodeImpl::GetInstanceImpl(id);
88 }
89 
DisposeNative(void * sc)90 void NodeJS::DisposeNative(void* sc)
91 {
92     if (!disposed_) {
93         LOG_V("NodeJS::DisposeNative");
94         disposed_ = true;
95 
96         if (auto* sceneJS = static_cast<SceneJS*>(sc)) {
97             sceneJS->ReleaseStrongDispose(reinterpret_cast<uintptr_t>(&scene_));
98         }
99 
100         if (!IsAttached()) {
101             if (auto node = interface_pointer_cast<SCENE_NS::INode>(GetNativeObject())) {
102                 if (auto scene = node->GetScene()) {
103                     scene->RemoveNode(BASE_NS::move(node)).Wait();
104                 }
105             }
106         }
107         UnsetNativeObject();
108         scene_.Reset();
109     }
110 }
Finalize(napi_env env)111 void NodeJS::Finalize(napi_env env)
112 {
113     DisposeNative(scene_.GetObject().GetJsWrapper<SceneJS>());
114     BaseObject::Finalize(env);
115 }
116