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 #ifndef NAPI_API_CLASS_DEFINITION_HELPERS_H
17 #define NAPI_API_CLASS_DEFINITION_HELPERS_H
18
19 #ifdef __OHOS_PLATFORM__
20 #include "napi/native_api.h"
21 #else
22 #include <node_api.h>
23 #endif
24
25 #include "function_context.h"
26
27 namespace NapiApi {
28
29 template<typename Object, napi_value (Object::*F)(NapiApi::FunctionContext<>&)>
Getter(napi_env env,napi_callback_info info)30 static inline napi_value Getter(napi_env env, napi_callback_info info)
31 {
32 NapiApi::FunctionContext fc(env, info);
33 if (fc && fc.This()) {
34 if (auto scj = fc.This().template GetJsWrapper<Object>()) {
35 if (auto ret = (scj->*F)(fc)) {
36 return ret;
37 }
38 }
39 }
40 return fc.GetUndefined();
41 };
42
43 template<typename Type, typename Object, void (Object::*F)(NapiApi::FunctionContext<Type>&)>
Setter(napi_env env,napi_callback_info info)44 static inline napi_value Setter(napi_env env, napi_callback_info info)
45 {
46 NapiApi::FunctionContext<Type> fc(env, info);
47 if (fc && fc.This()) {
48 if (auto scj = fc.This().template GetJsWrapper<Object>()) {
49 (scj->*F)(fc);
50 }
51 }
52 return fc.GetUndefined();
53 };
54
55 template<typename FC, typename Object, napi_value (Object::*F)(FC&)>
MethodI(napi_env env,napi_callback_info info)56 static inline napi_value MethodI(napi_env env, napi_callback_info info)
57 {
58 FC fc(env, info);
59 if (fc && fc.This()) {
60 if (auto scj = fc.This().template GetJsWrapper<Object>()) {
61 return (scj->*F)(fc);
62 }
63 }
64 return fc.GetUndefined();
65 };
66
67 template<typename FC, typename Object, napi_value (Object::*F)(FC&)>
68 static inline napi_property_descriptor Method(
69 const char* const name, napi_property_attributes flags = napi_default_method)
70 {
71 return napi_property_descriptor { name, nullptr, MethodI<FC, Object, F>, nullptr, nullptr, nullptr, flags,
72 nullptr };
73 }
74
75 template<typename Type, typename Object, void (Object::*F2)(NapiApi::FunctionContext<Type>&)>
76 static inline napi_property_descriptor SetProperty(
77 const char* const name, napi_property_attributes flags = napi_default_jsproperty)
78 {
79 static_assert(F2 != nullptr);
80 return napi_property_descriptor { name, nullptr, nullptr, nullptr, Setter<Type, Object, F2>, nullptr, flags,
81 nullptr };
82 }
83
84 template<typename Type, typename Object, napi_value (Object::*F)(NapiApi::FunctionContext<>&)>
85 static inline napi_property_descriptor GetProperty(
86 const char* const name, napi_property_attributes flags = napi_default_jsproperty)
87 {
88 static_assert(F != nullptr);
89 return napi_property_descriptor { name, nullptr, nullptr, Getter<Object, F>, nullptr, nullptr, flags, nullptr };
90 }
91
92 template<typename Type, typename Object, napi_value (Object::*F)(NapiApi::FunctionContext<>&),
93 void (Object::*F2)(NapiApi::FunctionContext<Type>&)>
94 static inline napi_property_descriptor GetSetProperty(
95 const char* const name, napi_property_attributes flags = napi_default_jsproperty)
96 {
97 static_assert(F != nullptr);
98 static_assert(F2 != nullptr);
99 return napi_property_descriptor { name, nullptr, nullptr, Getter<Object, F>, Setter<Type, Object, F2>, nullptr,
100 flags, nullptr };
101 }
102
103 } // namespace NapiApi
104
105 #define NAPI_API_xs(s) NAPI_API_s(s)
106 #define NAPI_API_s(s) #s
107 #define NAPI_API_xcn(s) NAPI_API_cn(s)
108 #define NAPI_API_cn(s) s##JS
109 #define NAPI_API_JS_NAME_STRING NAPI_API_xs(NAPI_API_JS_NAME)
110 #define NAPI_API_CLASS_NAME NAPI_API_xcn(NAPI_API_JS_NAME)
111
112 // First #define NAPI_API_JS_NAME YourJsClassNameHere
113 // and then use these macros.
114 #define DeclareGet(type, name, getter) \
115 node_props.push_back(NapiApi::GetProperty<type, NAPI_API_CLASS_NAME, &NAPI_API_CLASS_NAME::getter>(name));
116 #define DeclareSet(type, name, setter) \
117 node_props.push_back(NapiApi::SetProperty<type, NAPI_API_CLASS_NAME, &NAPI_API_CLASS_NAME::setter>(name));
118 #define DeclareGetSet(type, name, getter, setter) \
119 node_props.push_back(NapiApi::GetSetProperty<type, NAPI_API_CLASS_NAME, &NAPI_API_CLASS_NAME::getter, \
120 &NAPI_API_CLASS_NAME::setter>(name));
121 #define DeclareMethod(name, function, ...) \
122 node_props.push_back( \
123 NapiApi::Method<NapiApi::FunctionContext<__VA_ARGS__>, NAPI_API_CLASS_NAME, &NAPI_API_CLASS_NAME::function>( \
124 name));
125 #define DeclareClass() { \
126 napi_value func; \
127 auto status = napi_define_class(env, NAPI_API_JS_NAME_STRING, NAPI_AUTO_LENGTH, \
128 BaseObject::ctor<NAPI_API_CLASS_NAME>(), nullptr, node_props.size(), node_props.data(), &func); \
129 NapiApi::MyInstanceState* mis; \
130 NapiApi::MyInstanceState::GetInstance(env, (void**)&mis); \
131 if (mis) { \
132 mis->StoreCtor(NAPI_API_JS_NAME_STRING, func); \
133 } \
134 }
135
136 #endif
137