1 /*
2 * Copyright (c) 2021-2022 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
18
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 #include "frameworks/bridge/declarative_frontend/engine/bindings_implementation.h"
22 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h"
23
24 namespace __detail__ {
25
26 template<typename... Types>
ToTuple(panda::JsiRuntimeCallInfo * runtimeCallInfo)27 std::tuple<Types...> ToTuple(panda::JsiRuntimeCallInfo *runtimeCallInfo)
28 {
29 int index = 0;
30 return {
31 OHOS::Ace::Framework::JsiValueConvertor::fromJsiValue<Types>(
32 runtimeCallInfo->GetVM(), runtimeCallInfo->GetCallArgRef(index++))...,
33 };
34 }
35
36 }; // namespace __detail__
37
38 namespace OHOS::Ace::Framework {
39
40 template<typename C>
41 class JsiClass {
42 public:
43 using ThisJSClass = JSClassImpl<C, JsiClass>;
44
45 JsiClass() = delete;
46
47 static void Declare(const char* name);
48
49 template<typename Base, typename R, typename... Args>
50 static void Method(const char* name, R (Base::*func)(Args...), int id);
51
52 template<typename T>
53 static void CustomMethod(const char* name, MemberFunctionCallback<T> callback, int id);
54
55 static void CustomMethod(const char* name, FunctionCallback callback);
56
57 template<typename T>
58 static void CustomMethod(const char* name, JSMemberFunctionCallback<T> callback, int id);
59
60 template<typename T>
61 static void CustomProperty(const char* name, MemberFunctionGetCallback<T> callback, int getterId, int setterId);
62
63 static void CustomProperty(const char* name, FunctionGetCallback getter, FunctionSetCallback setter);
64
65 template<typename T>
66 static void CustomProperty(const char* name, JSMemberFunctionCallback<T> callback, int getterId, int setterId);
67
68 template<typename R, typename... Args>
69 static void StaticMethod(const char* name, R (*func)(Args...), int id);
70
71 static void StaticMethod(const char* name, JSFunctionCallback callback, int id);
72
73 static void CustomStaticMethod(const char* name, FunctionCallback callback);
74
75 template<typename T>
76 static void StaticConstant(const char* name, T val);
77
78 static void Bind(BindingTarget t, FunctionCallback ctor);
79
80 static void Bind(BindingTarget t, JSFunctionCallback ctor, JSDestructorCallback<C> dtor = nullptr,
81 JSGCMarkCallback<C> gcMark = nullptr);
82
83 template<typename... Args>
84 static void Bind(BindingTarget t, JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
85
86 template<typename Base>
87 static void InheritAndBind(
88 BindingTarget t, JSFunctionCallback ctor = nullptr,
89 JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
90
91 template<typename Base>
92 static void Inherit();
93
94 static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetCustomFunctions();
95
96 static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetStaticFunctions();
97
98 static panda::Local<panda::JSValueRef> NewInstance();
99
100 private:
101 template<typename T, typename... Args>
102 static panda::Local<panda::JSValueRef> InternalMemberFunctionCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
103
104 template<typename T>
105 static panda::Local<panda::JSValueRef> InternalJSMemberFunctionCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
106
107 template<typename Class, typename R, typename... Args>
108 static panda::Local<panda::JSValueRef> MethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
109
110 template<typename Class, typename R, typename... Args>
111 static panda::Local<panda::JSValueRef> JSMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
112
113 template<typename R, typename... Args>
114 static panda::Local<panda::JSValueRef> StaticMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
115
116 static panda::Local<panda::JSValueRef> JSStaticMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
117
118 template<typename... Args>
119 static panda::Local<panda::JSValueRef> InternalConstructor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
120
121 static panda::Local<panda::JSValueRef> ConstructorInterceptor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
122
123 static void DestructorInterceptor(void* nativePtr, void* data);
124
125 static panda::Local<panda::JSValueRef> JSConstructorInterceptor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
126
127 static bool CheckIfConstructCall(panda::JsiRuntimeCallInfo *runtimeCallInfo);
128
129 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> staticFunctions_;
130 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customFunctions_;
131 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customGetFunctions_;
132 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customSetFunctions_;
133 static thread_local FunctionCallback constructor_;
134 static thread_local JSFunctionCallback jsConstructor_;
135 static thread_local JSDestructorCallback<C> jsDestructor_;
136 static thread_local JSGCMarkCallback<C> jsGcMark_;
137 static thread_local std::string className_;
138 static thread_local panda::Global<panda::FunctionRef> classFunction_;
139 static thread_local std::vector<shared_ptr<int32_t>> functionIds_;
140 };
141
142 template<typename T>
143 using JSClass = JSClassImpl<T, JsiClass>;
144
145 }; // namespace OHOS::Ace::Framework
146
147 #include "jsi_bindings.inl"
148
149 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
150