• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "geometry_definition/GeometryDefinition.h"
17 
18 #include <geometry_definition/CubeJS.h>
19 #include <geometry_definition/CustomJS.h>
20 #include <geometry_definition/PlaneJS.h>
21 #include <geometry_definition/SphereJS.h>
22 #include <napi_api.h>
23 
24 namespace GeometryDefinition {
25 
GeometryDefinition()26 GeometryDefinition::GeometryDefinition() {}
27 
RegisterEnums(NapiApi::Object exports)28 void RegisterEnums(NapiApi::Object exports)
29 {
30     napi_value v;
31     NapiApi::Object GeometryType(exports.GetEnv());
32 
33 #define DECL_ENUM(enu, x)                                      \
34     {                                                          \
35         napi_create_uint32(enu.GetEnv(), GeometryType::x, &v); \
36         enu.Set(#x, v);                                        \
37     }
38     DECL_ENUM(GeometryType, CUSTOM);
39     DECL_ENUM(GeometryType, CUBE);
40     DECL_ENUM(GeometryType, PLANE);
41     DECL_ENUM(GeometryType, SPHERE);
42 #undef DECL_ENUM
43 
44     exports.Set("GeometryType", GeometryType);
45 }
46 
FromJs(NapiApi::Object jsDefinition)47 BASE_NS::unique_ptr<GeometryDefinition> GeometryDefinition::FromJs(NapiApi::Object jsDefinition)
48 {
49     GeometryDefinition* result = nullptr;
50     if (auto value = jsDefinition.Get<uint32_t>("geometryType"); value.IsValid()) {
51         uint32_t type = value;
52         if (type == GeometryType::CUSTOM) {
53             result = CustomJS::FromJs(jsDefinition);
54         } else if (type == GeometryType::CUBE) {
55             result = CubeJS::FromJs(jsDefinition);
56         } else if (type == GeometryType::PLANE) {
57             result = PlaneJS::FromJs(jsDefinition);
58         } else if (type == GeometryType::SPHERE) {
59             result = SphereJS::FromJs(jsDefinition);
60         } else {
61             LOG_E("Can't create a native geometry definition: unsupported geometryType");
62         }
63     } else {
64         LOG_E("Can't create a native geometry definition: geometryType missing");
65     }
66     return BASE_NS::unique_ptr<GeometryDefinition> { result };
67 }
68 
DefineClass(napi_env env,napi_value exports,BASE_NS::string_view name,BASE_NS::vector<napi_property_descriptor> props,napi_callback ctor)69 void GeometryDefinition::DefineClass(napi_env env, napi_value exports, BASE_NS::string_view name,
70     BASE_NS::vector<napi_property_descriptor> props, napi_callback ctor)
71 {
72     napi_value classCtor = nullptr;
73     napi_define_class(env, name.data(), NAPI_AUTO_LENGTH, ctor, nullptr, props.size(), props.data(), &classCtor);
74     NapiApi::MyInstanceState* mis {};
75     NapiApi::MyInstanceState::GetInstance(env, (void**)&mis);
76     if (mis) {
77         mis->StoreCtor(name, classCtor);
78     }
79     NapiApi::Object { env, exports }.Set(name, classCtor);
80 }
81 
82 } // namespace GeometryDefinition
83