• 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 #include "BaseObjectJS.h"
16 
17 #include <scene/interface/intf_light.h>
18 
19 #include "JsObjectCache.h"
20 #include "TrueRootObject.h"
21 
GetConstructorName(const META_NS::IObject::Ptr & obj)22 BASE_NS::string GetConstructorName(const META_NS::IObject::Ptr& obj)
23 {
24     if (!obj) {
25         return "";
26     }
27     BASE_NS::string name { obj->GetClassName() };
28     // specialize/remap class names & interfaces.
29     if (name == "Bitmap") {
30         name = "Image";
31     } else if (name == "Tonemap") {
32         name = "ToneMappingSettings";
33     } else if (name == "PostProcess") {
34         name = "PostProcessSettings";
35     } else if (name == "Material") {
36         // ok
37     } else if (name == "Sampler") {
38         // ok
39     } else if (name == "Texture") {
40         name = "MaterialProperty";
41     } else if (name == "Shader") {
42         // possible specialization?
43     } else if (name == "EcsAnimation") {
44         name = "Animation";
45     } else if (name == "MeshNode") {
46         name = "Geometry";
47     } else if (name == "CameraNode") {
48         name = "Camera";
49     } else if (name == "LightNode") {
50         SCENE_NS::ILight* lgt = interface_cast<SCENE_NS::ILight>(obj);
51         if (lgt == nullptr) {
52             LOG_E("lgt is null");
53             return name;
54         }
55         auto type = lgt->Type()->GetValue();
56         if (type == Scene::LightType::DIRECTIONAL) {
57             name = "DirectionalLight";
58         } else if (type == Scene::LightType::POINT) {
59             name = "PointLight";
60         } else if (type == Scene::LightType::SPOT) {
61             name = "SpotLight";
62         } else {
63             name = "Node";
64         }
65     } else if (name.ends_with("Node")) {
66         name = "Node";
67     }
68     return name;
69 }
70 
71 
CreateFromNativeInstance(napi_env env,const META_NS::IObject::Ptr & obj,PtrType ptrType,const NapiApi::JsFuncArgs & args,BASE_NS::string_view pname)72 NapiApi::Object CreateFromNativeInstance(napi_env env, const META_NS::IObject::Ptr& obj, PtrType ptrType,
73     const NapiApi::JsFuncArgs& args, BASE_NS::string_view pname)
74 {
75     if (!obj) {
76         napi_value null;
77         napi_get_null(env, &null);
78         return { env, null };
79     }
80     auto name = GetConstructorName(obj);
81     return CreateFromNativeInstance(env, name, obj, ptrType, args, pname);
82 }
83 
CreateFromNativeInstance(napi_env env,const BASE_NS::string & name,const META_NS::IObject::Ptr & obj,PtrType ptrType,const NapiApi::JsFuncArgs & args,BASE_NS::string_view pname)84 NapiApi::Object CreateFromNativeInstance(napi_env env, const BASE_NS::string& name, const META_NS::IObject::Ptr& obj,
85     PtrType ptrType, const NapiApi::JsFuncArgs& args, BASE_NS::string_view pname)
86 {
87     napi_value null;
88     napi_get_null(env, &null);
89     if (obj == nullptr) {
90         return { env, null };
91     }
92     using namespace META_NS;
93     if (const auto cached = FetchJsObj(obj, pname)) {
94         // we have a cached js object for this native object
95         return cached;
96     }
97 
98     // Ensure we have at least one arg for injection.
99     napi_value dummyArg[] = { NapiApi::Object { env }.ToNapiValue() };
100     auto argsToUse = args.argc > 0 ? args : NapiApi::JsFuncArgs { dummyArg };
101     TrueRootObject::InjectNativeObject(env, obj, ptrType, argsToUse);
102     const auto newJsObj = NapiApi::Object { env, name, argsToUse };
103     return StoreJsObj(obj, newJsObj, pname);
104 }
105